Logo AND Algorithmique Numérique Distribuée

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