Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge if statements.
[simgrid.git] / teshsuite / msg / actions-comm / actions-comm.c
1 /* Copyright (c) 2009-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <simgrid/msg.h>
7 #include <xbt/replay.hpp>
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
10 int communicator_size = 0;
11
12 static void action_Isend(const char* const* action);
13
14 typedef struct {
15   int last_Irecv_sender_id;
16   int bcast_counter;
17   xbt_dynar_t isends; /* of msg_comm_t */
18   /* Used to implement irecv+wait */
19   xbt_dynar_t irecvs; /* of msg_comm_t */
20   xbt_dynar_t tasks;  /* of msg_task_t */
21 } s_process_globals_t;
22
23 typedef s_process_globals_t* process_globals_t;
24
25 /* Helper function */
26 static double parse_double(const char* string)
27 {
28   double value;
29   char* endptr;
30
31   value = strtod(string, &endptr);
32   if (*endptr != '\0')
33     THROWF(unknown_error, 0, "%s is not a double", string);
34   return value;
35 }
36
37 #define ACT_DEBUG(...)                                                                                                 \
38   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {                                                          \
39     char* NAME = xbt_str_join_array(action, " ");                                                                      \
40     XBT_DEBUG(__VA_ARGS__);                                                                                            \
41     xbt_free(NAME);                                                                                                    \
42   } else                                                                                                               \
43   ((void)0)
44
45 static void log_action(const char* const* action, double date)
46 {
47   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
48     char* name = xbt_str_join_array(action, " ");
49     XBT_VERB("%s %f", name, date);
50     xbt_free(name);
51   }
52 }
53
54 static void asynchronous_cleanup(void)
55 {
56   process_globals_t globals = (process_globals_t)MSG_process_get_data(MSG_process_self());
57
58   /* Destroy any isend which correspond to completed communications */
59   msg_comm_t comm;
60   while (1 /*true*/) {
61     int pos_found = MSG_comm_testany(globals->isends);
62     if (pos_found == -1) /* none remaining */
63       break;
64     xbt_dynar_remove_at(globals->isends, pos_found, &comm);
65     MSG_comm_destroy(comm);
66   }
67 }
68
69 /* My actions */
70 static void action_send(const char* const* action)
71 {
72   char to[250];
73   const char* size_str = action[3];
74   double size          = parse_double(size_str);
75   double clock         = MSG_get_clock();
76
77   snprintf(to, 249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
78
79   ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
80   if (size < 65536) {
81     action_Isend(action);
82   } else {
83     MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
84   }
85
86   log_action(action, MSG_get_clock() - clock);
87   asynchronous_cleanup();
88 }
89
90 static void action_Isend(const char* const* action)
91 {
92   char to[250];
93   const char* size          = action[3];
94   double clock              = MSG_get_clock();
95   process_globals_t globals = (process_globals_t)MSG_process_get_data(MSG_process_self());
96
97   snprintf(to, 249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
98   msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
99   xbt_dynar_push(globals->isends, &comm);
100
101   XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
102   log_action(action, MSG_get_clock() - clock);
103   asynchronous_cleanup();
104 }
105
106 static void action_recv(const char* const* action)
107 {
108   char mailbox_name[250];
109   msg_task_t task = NULL;
110   double clock    = MSG_get_clock();
111
112   snprintf(mailbox_name, 249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
113
114   ACT_DEBUG("Receiving: %s", NAME);
115   msg_error_t res = MSG_task_receive(&task, mailbox_name);
116   log_action(action, MSG_get_clock() - clock);
117
118   if (res == MSG_OK) {
119     MSG_task_destroy(task);
120   }
121   asynchronous_cleanup();
122 }
123
124 static void action_Irecv(const char* const* action)
125 {
126   char mailbox[250];
127   double clock              = MSG_get_clock();
128   process_globals_t globals = (process_globals_t)MSG_process_get_data(MSG_process_self());
129
130   XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
131
132   snprintf(mailbox, 249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
133   msg_task_t t = NULL;
134   xbt_dynar_push(globals->tasks, &t);
135   msg_comm_t c = MSG_task_irecv(xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
136   xbt_dynar_push(globals->irecvs, &c);
137
138   log_action(action, MSG_get_clock() - clock);
139   asynchronous_cleanup();
140 }
141
142 static void action_wait(const char* const* action)
143 {
144   msg_task_t task = NULL;
145   msg_comm_t comm;
146   double clock              = MSG_get_clock();
147   process_globals_t globals = (process_globals_t)MSG_process_get_data(MSG_process_self());
148
149   xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
150              xbt_str_join_array(action, " "));
151
152   ACT_DEBUG("Entering %s", NAME);
153   comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
154   MSG_comm_wait(comm, -1);
155   task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
156   MSG_comm_destroy(comm);
157   MSG_task_destroy(task);
158
159   log_action(action, MSG_get_clock() - clock);
160 }
161
162 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
163 static void action_barrier(const char* const* action)
164 {
165   static msg_bar_t barrier           = NULL;
166   static int processes_arrived_sofar = 0;
167
168   if (barrier == NULL) {                                    // first arriving on the barrier
169     msg_bar_t newbar = MSG_barrier_init(communicator_size); // This is a simcall, unscheduling the current process
170     if (barrier == NULL)                                    // Still null, commit our new value
171       barrier = newbar;
172     else // some other process commited a new barrier before me, so dismiss mine
173       MSG_barrier_destroy(newbar);
174   }
175
176   processes_arrived_sofar++;
177   MSG_barrier_wait(barrier);
178
179   ACT_DEBUG("Exiting barrier: %s", NAME);
180
181   processes_arrived_sofar--;
182   if (processes_arrived_sofar <= 0) {
183     MSG_barrier_destroy(barrier);
184     barrier = NULL;
185   }
186 }
187
188 static void action_bcast(const char* const* action)
189 {
190   char mailbox[80];
191   double comm_size = parse_double(action[2]);
192   msg_task_t task  = NULL;
193   double clock     = MSG_get_clock();
194
195   process_globals_t counters = (process_globals_t)MSG_process_get_data(MSG_process_self());
196
197   xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
198
199   const char* process_name = MSG_process_get_name(MSG_process_self());
200
201   char* bcast_identifier = bprintf("bcast_%d", counters->bcast_counter);
202   counters->bcast_counter++;
203
204   if (!strcmp(process_name, "p0")) {
205     XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
206
207     msg_comm_t* comms = xbt_new0(msg_comm_t, communicator_size - 1);
208
209     for (int i = 1; i < communicator_size; i++) {
210       snprintf(mailbox, 79, "%s_p0_p%d", bcast_identifier, i);
211       comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
212     }
213     MSG_comm_waitall(comms, communicator_size - 1, -1);
214     for (int i = 1; i < communicator_size; i++)
215       MSG_comm_destroy(comms[i - 1]);
216     xbt_free(comms);
217
218     XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
219   } else {
220     snprintf(mailbox, 79, "%s_p0_%s", bcast_identifier, process_name);
221     MSG_task_receive(&task, mailbox);
222     MSG_task_destroy(task);
223     XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
224   }
225
226   log_action(action, MSG_get_clock() - clock);
227   xbt_free(bcast_identifier);
228 }
229
230 static void action_comm_size(const char* const* action)
231 {
232   const char* size = action[2];
233   double clock     = MSG_get_clock();
234
235   communicator_size = parse_double(size);
236   log_action(action, MSG_get_clock() - clock);
237 }
238
239 static void action_compute(const char* const* action)
240 {
241   const char* amount = action[2];
242   msg_task_t task    = MSG_task_create("task", parse_double(amount), 0, NULL);
243   double clock       = MSG_get_clock();
244
245   ACT_DEBUG("Entering %s", NAME);
246   MSG_task_execute(task);
247   MSG_task_destroy(task);
248   log_action(action, MSG_get_clock() - clock);
249 }
250
251 static void action_init(const char* const* action)
252 {
253   XBT_DEBUG("Initialize the counters");
254   process_globals_t globals = (process_globals_t)calloc(1, sizeof(s_process_globals_t));
255   globals->isends           = xbt_dynar_new(sizeof(msg_comm_t), NULL);
256   globals->irecvs           = xbt_dynar_new(sizeof(msg_comm_t), NULL);
257   globals->tasks            = xbt_dynar_new(sizeof(msg_task_t), NULL);
258   MSG_process_set_data(MSG_process_self(), globals);
259 }
260
261 static void action_finalize(const char* const* action)
262 {
263   process_globals_t globals = (process_globals_t)MSG_process_get_data(MSG_process_self());
264   if (globals) {
265     asynchronous_cleanup();
266     xbt_dynar_free_container(&(globals->isends));
267     xbt_dynar_free_container(&(globals->irecvs));
268     xbt_dynar_free_container(&(globals->tasks));
269     xbt_free(globals);
270   }
271 }
272
273 int main(int argc, char* argv[])
274 {
275   /* Check the given arguments */
276   MSG_init(&argc, argv);
277   /* Explicit initialization of the action module is required now*/
278   MSG_action_init();
279
280   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file [action_files]\n"
281                        "\t# if all actions are in the same file\n"
282                        "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
283                        "\t# if actions are in separate files, specified in deployment\n"
284                        "\tExample: %s msg_platform.xml msg_deployment.xml ",
285              argv[0], argv[0], argv[0]);
286
287   MSG_create_environment(argv[1]);
288   MSG_launch_application(argv[2]);
289
290   /*   Action registration */
291   xbt_replay_action_register("init", action_init);
292   xbt_replay_action_register("finalize", action_finalize);
293   xbt_replay_action_register("comm_size", action_comm_size);
294   xbt_replay_action_register("send", action_send);
295   xbt_replay_action_register("Isend", action_Isend);
296   xbt_replay_action_register("recv", action_recv);
297   xbt_replay_action_register("Irecv", action_Irecv);
298   xbt_replay_action_register("wait", action_wait);
299   xbt_replay_action_register("barrier", action_barrier);
300   xbt_replay_action_register("bcast", action_bcast);
301   xbt_replay_action_register("compute", action_compute);
302
303   /* Actually do the simulation using MSG_action_trace_run */
304   msg_error_t res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
305
306   XBT_INFO("Simulation time %g", MSG_get_clock());
307
308   MSG_action_exit(); /* Explicit finalization of the action module */
309
310   return res != MSG_OK;
311 }