Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not use deprecated and redirected functions in examples
[simgrid.git] / examples / 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 ((void)0)
43
44 static void log_action(const char *const *action, double date)
45 {
46   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
47     char *name = xbt_str_join_array(action, " ");
48     XBT_VERB("%s %f", name, date);
49     xbt_free(name);
50   }
51 }
52
53 static void asynchronous_cleanup(void)
54 {
55   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
56
57   /* Destroy any isend which correspond to completed communications */
58   msg_comm_t comm;
59   while (1/*true*/) {
60     int pos_found = MSG_comm_testany(globals->isends);
61     if (pos_found == -1) /* none remaining */
62       break;
63     xbt_dynar_remove_at(globals->isends, pos_found, &comm);
64     MSG_comm_destroy(comm);
65   }
66 }
67
68 /* My actions */
69 static void action_send(const char *const *action)
70 {
71   char to[250];
72   const char *size_str = action[3];
73   double size = parse_double(size_str);
74   double clock = MSG_get_clock();
75
76   snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
77
78   ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
79   if (size < 65536) {
80     action_Isend(action);
81   } else {
82     MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
83   }
84
85   log_action(action, MSG_get_clock() - clock);
86   asynchronous_cleanup();
87 }
88
89 static void action_Isend(const char *const *action)
90 {
91   char to[250];
92   const char *size = action[3];
93   double clock = MSG_get_clock();
94   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
95
96   snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
97   msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
98   xbt_dynar_push(globals->isends, &comm);
99
100   XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
101   log_action(action, MSG_get_clock() - clock);
102   asynchronous_cleanup();
103 }
104
105 static void action_recv(const char *const *action)
106 {
107   char mailbox_name[250];
108   msg_task_t task = NULL;
109   double clock = MSG_get_clock();
110
111   snprintf(mailbox_name,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
112
113   ACT_DEBUG("Receiving: %s", NAME);
114   msg_error_t res = MSG_task_receive(&task, mailbox_name);
115   log_action(action, MSG_get_clock() - clock);
116
117   if (res == MSG_OK) {
118     MSG_task_destroy(task);
119   }
120   asynchronous_cleanup();
121 }
122
123 static void action_Irecv(const char *const *action)
124 {
125   char mailbox[250];
126   double clock = MSG_get_clock();
127   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
128
129   XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
130
131   snprintf(mailbox,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
132   msg_task_t t = NULL;
133   xbt_dynar_push(globals->tasks, &t);
134   msg_comm_t c = MSG_task_irecv(xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
135   xbt_dynar_push(globals->irecvs, &c);
136
137   log_action(action, MSG_get_clock() - clock);
138   asynchronous_cleanup();
139 }
140
141 static void action_wait(const char *const *action)
142 {
143   msg_task_t task = NULL;
144   msg_comm_t comm;
145   double clock = MSG_get_clock();
146   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
147
148   xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
149              xbt_str_join_array(action, " "));
150
151   ACT_DEBUG("Entering %s", NAME);
152   comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
153   MSG_comm_wait(comm, -1);
154   task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
155   MSG_comm_destroy(comm);
156   MSG_task_destroy(task);
157
158   log_action(action, MSG_get_clock() - clock);
159 }
160
161 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
162 static void action_barrier(const char *const *action)
163 {
164   static msg_bar_t barrier           = NULL;
165   static int processes_arrived_sofar = 0;
166
167   if (barrier == NULL) {                                    // first arriving on the barrier
168     msg_bar_t newbar = MSG_barrier_init(communicator_size); // This is a simcall, unscheduling the current process
169     if (barrier == NULL)                                    // Still null, commit our new value
170       barrier = newbar;
171     else // some other process commited a new barrier before me, so dismiss mine
172       MSG_barrier_destroy(newbar);
173   }
174
175   processes_arrived_sofar++;
176   MSG_barrier_wait(barrier);
177
178   ACT_DEBUG("Exiting barrier: %s", NAME);
179
180   processes_arrived_sofar--;
181   if (processes_arrived_sofar<=0) {
182     MSG_barrier_destroy(barrier);
183     barrier = NULL;
184   }
185 }
186
187 static void action_bcast(const char *const *action)
188 {
189   char mailbox[80];
190   double comm_size = parse_double(action[2]);
191   msg_task_t task = NULL;
192   double clock = MSG_get_clock();
193
194   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
195
196   xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
197
198   const char * process_name = MSG_process_get_name(MSG_process_self());
199
200   char *bcast_identifier = bprintf("bcast_%d", counters->bcast_counter);
201   counters->bcast_counter++;
202
203   if (!strcmp(process_name, "p0")) {
204     XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
205
206     msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
207
208     for (int i = 1; i < communicator_size; i++) {
209       snprintf(mailbox,79, "%s_p0_p%d", bcast_identifier, i);
210       comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
211     }
212     MSG_comm_waitall(comms, communicator_size - 1, -1);
213     for (int i = 1; i < communicator_size; i++)
214       MSG_comm_destroy(comms[i - 1]);
215     xbt_free(comms);
216
217     XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
218   } else {
219     snprintf(mailbox,79, "%s_p0_%s", bcast_identifier, process_name);
220     MSG_task_receive(&task, mailbox);
221     MSG_task_destroy(task);
222     XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
223   }
224
225   log_action(action, MSG_get_clock() - clock);
226   xbt_free(bcast_identifier);
227 }
228
229 static void action_comm_size(const char *const *action)
230 {
231   const char *size = action[2];
232   double clock = MSG_get_clock();
233
234   communicator_size = parse_double(size);
235   log_action(action, MSG_get_clock() - clock);
236 }
237
238 static void action_compute(const char *const *action)
239 {
240   const char *amount = action[2];
241   msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
242   double clock = MSG_get_clock();
243
244   ACT_DEBUG("Entering %s", NAME);
245   MSG_task_execute(task);
246   MSG_task_destroy(task);
247   log_action(action, MSG_get_clock() - clock);
248 }
249
250 static void action_init(const char *const *action)
251 {
252   XBT_DEBUG("Initialize the counters");
253   process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
254   globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
255   globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
256   globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
257   MSG_process_set_data(MSG_process_self(), globals);
258 }
259
260 static void action_finalize(const char *const *action)
261 {
262   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
263   if (globals) {
264     asynchronous_cleanup();
265     xbt_dynar_free_container(&(globals->isends));
266     xbt_dynar_free_container(&(globals->irecvs));
267     xbt_dynar_free_container(&(globals->tasks));
268     xbt_free(globals);
269   }
270 }
271
272 int main(int argc, char *argv[])
273 {
274   /* Check the given arguments */
275   MSG_init(&argc, argv);
276   /* Explicit initialization of the action module is required now*/
277   MSG_action_init();
278
279   xbt_assert(argc > 2,
280        "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 }