Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge conflicts
[simgrid.git] / examples / msg / icomms / peer.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int sender(int argc, char *argv[]);
18 int receiver(int argc, char *argv[]);
19
20 MSG_error_t test_all(const char *platform_file,
21                      const char *application_file);
22
23 /** Sender function  */
24 int sender(int argc, char *argv[])
25 {
26   long number_of_tasks = atol(argv[1]);
27   double task_comp_size = atof(argv[2]);
28   double task_comm_size = atof(argv[3]);
29   long receivers_count = atol(argv[4]);
30   double sleep_start_time = atof(argv[5]);
31   double sleep_test_time = atof(argv[6]);
32
33   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time,
34         sleep_test_time);
35
36   msg_comm_t comm = NULL;
37   int i;
38   m_task_t task = NULL;
39   MSG_process_sleep(sleep_start_time);
40   for (i = 0; i < number_of_tasks; i++) {
41     char mailbox[256];
42     char sprintf_buffer[256];
43
44     sprintf(mailbox, "receiver-%ld", i % receivers_count);
45     sprintf(sprintf_buffer, "Task_%d", i);
46
47     task =
48         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
49                         NULL);
50     comm = MSG_task_isend(task, mailbox);
51     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
52
53     if (sleep_test_time == 0) {
54       MSG_comm_wait(comm, -1);
55     } else {
56       while (MSG_comm_test(comm) == 0) {
57         MSG_process_sleep(sleep_test_time);
58       };
59       MSG_comm_destroy(comm);
60     }
61
62   }
63
64   for (i = 0; i < receivers_count; i++) {
65     char mailbox[80];
66     sprintf(mailbox, "receiver-%ld", i % receivers_count);
67     task = MSG_task_create("finalize", 0, 0, 0);
68     comm = MSG_task_isend(task, mailbox);
69     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
70     if (sleep_test_time == 0) {
71       MSG_comm_wait(comm, -1);
72     } else {
73       while (MSG_comm_test(comm) == 0) {
74         MSG_process_sleep(sleep_test_time);
75       };
76       MSG_comm_destroy(comm);
77     }
78
79   }
80
81   XBT_INFO("Goodbye now!");
82   return 0;
83 }                               /* end_of_sender */
84
85 /** Receiver function  */
86 int receiver(int argc, char *argv[])
87 {
88   m_task_t task = NULL;
89   _XBT_GNUC_UNUSED MSG_error_t res;
90   int id = -1;
91   char mailbox[80];
92   msg_comm_t res_irecv;
93   double sleep_start_time = atof(argv[2]);
94   double sleep_test_time = atof(argv[3]);
95   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time,
96         sleep_test_time);
97
98   _XBT_GNUC_UNUSED int read;
99   read = sscanf(argv[1], "%d", &id);
100   xbt_assert(read,
101               "Invalid argument %s\n", argv[1]);
102
103   MSG_process_sleep(sleep_start_time);
104
105   sprintf(mailbox, "receiver-%d", id);
106   while (1) {
107     res_irecv = MSG_task_irecv(&(task), mailbox);
108     XBT_INFO("Wait to receive a task");
109
110     if (sleep_test_time == 0) {
111       res = MSG_comm_wait(res_irecv, -1);
112       xbt_assert(res == MSG_OK, "MSG_task_get failed");
113     } else {
114       while (MSG_comm_test(res_irecv) == 0) {
115         MSG_process_sleep(sleep_test_time);
116       };
117       MSG_comm_destroy(res_irecv);
118     }
119
120     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
121     if (!strcmp(MSG_task_get_name(task), "finalize")) {
122       MSG_task_destroy(task);
123       break;
124     }
125
126     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
127     MSG_task_execute(task);
128     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
129     MSG_task_destroy(task);
130     task = NULL;
131   }
132   XBT_INFO("I'm done. See you!");
133   return 0;
134 }                               /* end_of_receiver */
135
136 /** Test function */
137 MSG_error_t test_all(const char *platform_file,
138                      const char *application_file)
139 {
140   MSG_error_t res = MSG_OK;
141
142   /* MSG_config("workstation/model","KCCFLN05"); */
143   {                             /*  Simulation setting */
144     MSG_set_channel_number(0);
145     MSG_create_environment(platform_file);
146   }
147   {                             /*   Application deployment */
148     MSG_function_register("sender", sender);
149     MSG_function_register("receiver", receiver);
150     MSG_launch_application(application_file);
151   }
152   res = MSG_main();
153
154   XBT_INFO("Simulation time %g", MSG_get_clock());
155   return res;
156 }                               /* end_of_test_all */
157
158
159 /** Main function */
160 int main(int argc, char *argv[])
161 {
162   MSG_error_t res = MSG_OK;
163
164   MSG_global_init(&argc, argv);
165   if (argc < 3) {
166     printf("Usage: %s platform_file deployment_file\n", argv[0]);
167     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
168     exit(1);
169   }
170   res = test_all(argv[1], argv[2]);
171   MSG_clean();
172
173   if (res == MSG_OK)
174     return 0;
175   else
176     return 1;
177 }                               /* end_of_main */