3 /* Copyright (c) 2009. The SimGrid team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
11 #include "xbt.h" /* calloc, printf */
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
16 int communicator_size=0;
18 typedef struct coll_ctr_t{
21 int allReduce_counter;
25 static double parse_double(const char *string) {
29 value=strtod(string, &endptr);
31 THROW1(unknown_error, 0, "%s is not a double", string);
37 static void send(xbt_dynar_t action)
39 char *name = xbt_str_join(action, " ");
40 char *to = xbt_dynar_get_as(action, 2, char *);
41 char *size = xbt_dynar_get_as(action, 3, char *);
42 INFO2("Send: %s (size: %lg)", name, parse_double(size));
43 MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
44 INFO1("Sent %s", name);
49 static int spawned_send(int argc, char *argv[])
51 INFO3("%s: Sending %s on %s", MSG_process_self()->name,
53 MSG_task_send(MSG_task_create(argv[0], 0, parse_double(argv[1]), NULL),
58 static void Isend(xbt_dynar_t action)
61 char *to = xbt_dynar_get_as(action, 2, char *);
62 char *size = xbt_dynar_get_as(action, 3, char *);
64 m_process_t comm_helper;
66 INFO1("Isend on %s: spawn process ",
67 MSG_process_get_name(MSG_process_self()));
69 myargv = (char**) calloc (3, sizeof (char*));
71 myargv[0] = xbt_strdup(to);
72 myargv[1] = xbt_strdup(size);
75 sprintf(spawn_name,"%s_wait",MSG_process_get_name(MSG_process_self()));
77 MSG_process_create_with_arguments(spawn_name, spawned_send,
78 NULL, MSG_host_self(), 2, myargv);
82 static void recv(xbt_dynar_t action)
84 char *name = xbt_str_join(action, " ");
86 INFO1("Receiving: %s", name);
87 //FIXME: argument of action ignored so far; semantic not clear
88 //char *from=xbt_dynar_get_as(action,2,char*);
89 MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
90 INFO1("Received %s", MSG_task_get_name(task));
91 MSG_task_destroy(task);
95 static int spawned_recv(int argc, char *argv[])
98 char* name = (char *) MSG_process_get_data(MSG_process_self());
99 INFO1("Receiving on %s", name);
100 MSG_task_receive(&task, name);
101 INFO1("Received %s", MSG_task_get_name(task));
102 INFO1("waiter on %s", MSG_process_get_name(MSG_process_self()));
103 MSG_task_send(MSG_task_create("waiter",0,0,NULL),MSG_process_get_name(MSG_process_self()));
105 MSG_task_destroy(task);
110 static void Irecv(xbt_dynar_t action)
112 char *name = xbt_str_join(action, " ");
113 m_process_t comm_helper;
115 INFO1("Irecv on %s: spawn process ",
116 MSG_process_get_name(MSG_process_self()));
118 sprintf(name,"%s_wait",MSG_process_get_name(MSG_process_self()));
119 comm_helper = MSG_process_create(name,spawned_recv,
120 (void *) MSG_process_get_name(MSG_process_self()),
128 static void wait(xbt_dynar_t action)
130 char *name = xbt_str_join(action, " ");
132 m_task_t task = NULL;
134 INFO1("wait: %s", name);
135 sprintf(task_name,"%s_wait",MSG_process_get_name(MSG_process_self()));
136 MSG_task_receive(&task,task_name);
137 MSG_task_destroy(task);
138 INFO1("waited: %s", name);
142 static void barrier (xbt_dynar_t action)
144 char *name = xbt_str_join(action, " ");
145 INFO1("barrier: %s", name);
152 static void reduce(xbt_dynar_t action)
158 char *comm_size = xbt_dynar_get_as(action, 2, char *);
159 char *comp_size = xbt_dynar_get_as(action, 3, char *);
160 m_process_t comm_helper=NULL;
161 m_task_t task=NULL, comp_task=NULL;
162 const char* process_name;
164 coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
166 xbt_assert0(communicator_size, "Size of Communicator is not defined"
167 ", can't use collective operations");
169 process_name = MSG_process_get_name(MSG_process_self());
172 DEBUG0("Initialize the counters");
173 counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
176 name = bprintf("reduce_%d", counters->reduce_counter++);
178 if (!strcmp(process_name, "process0")){
179 INFO2("%s: %s is the Root",name, process_name);
180 for(i=1;i<communicator_size;i++){
181 sprintf(spawn_name,"%s_process%d", name, i);
182 sprintf(task_name,"%s_wait", spawn_name);
184 MSG_process_create(task_name, spawned_recv,
185 (void *) xbt_strdup(spawn_name),
189 for(i=1;i<communicator_size;i++){
190 sprintf(task_name,"%s_process%d_wait", name, i);
191 MSG_task_receive(&task,task_name);
192 MSG_task_destroy(task);
197 MSG_task_create("reduce_comp", parse_double(comp_size), 0, NULL);
198 INFO1("%s: computing 'reduce_comp'", name);
199 MSG_task_execute(comp_task);
200 MSG_task_destroy(comp_task);
201 INFO1("%s: computed", name);
203 INFO2("%s: %s sends", name, process_name);
204 sprintf(task_name,"%s_%s", name, process_name);
205 INFO1("put on %s", task_name);
206 MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
210 MSG_process_set_data(MSG_process_self(), (void*)counters);
214 static void bcast (xbt_dynar_t action)
218 const char* process_name;
222 m_process_t comm_helper=NULL;
224 char *size = xbt_dynar_get_as(action, 2, char *);
225 coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
227 xbt_assert0(communicator_size, "Size of Communicator is not defined"
228 ", can't use collective operations");
231 process_name = MSG_process_get_name(MSG_process_self());
233 DEBUG0("Initialize the counters");
234 counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
237 name = bprintf("bcast_%d", counters->bcast_counter++);
238 if (!strcmp(process_name, "process0")){
239 INFO2("%s: %s is the Root",name, process_name);
241 for(i=1;i<communicator_size;i++){
242 myargv = (char**) calloc (3, sizeof (char*));
243 myargv[0] = xbt_strdup(name);
244 myargv[1] = xbt_strdup(size);
247 sprintf(spawn_name,"%s_%d", myargv[0], i);
249 MSG_process_create_with_arguments(spawn_name, spawned_send,
250 NULL, MSG_host_self(), 2, myargv);
253 for(i=1;i<communicator_size;i++){
254 sprintf(task_name,"process%d_wait", i);
255 DEBUG1("get on %s", task_name);
256 MSG_task_receive(&task,task_name);
257 MSG_task_destroy(task);
260 INFO2("%s: all messages sent by %s have been received",
263 INFO2("%s: %s receives", name, process_name);
264 MSG_task_receive(&task, name);
265 MSG_task_destroy(task);
266 INFO2("%s: %s has received", name,process_name);
267 sprintf(task_name,"%s_wait", process_name);
268 DEBUG1("put on %s", task_name);
269 MSG_task_send(MSG_task_create("waiter",0,0,NULL),task_name);
272 MSG_process_set_data(MSG_process_self(), (void*)counters);
277 static void sleep(xbt_dynar_t action)
279 char *name = xbt_str_join(action, " ");
280 char *duration = xbt_dynar_get_as(action, 2, char *);
281 INFO1("sleep: %s", name);
282 MSG_process_sleep(parse_double(duration));
283 INFO1("sleept: %s", name);
287 static void allReduce(xbt_dynar_t action)
294 char *comm_size = xbt_dynar_get_as(action, 2, char *);
295 char *comp_size = xbt_dynar_get_as(action, 3, char *);
296 m_process_t comm_helper=NULL;
297 m_task_t task=NULL, comp_task=NULL;
298 const char* process_name;
300 coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
302 xbt_assert0(communicator_size, "Size of Communicator is not defined"
303 ", can't use collective operations");
305 process_name = MSG_process_get_name(MSG_process_self());
308 DEBUG0("Initialize the counters");
309 counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
312 name = bprintf("allReduce_%d", counters->allReduce_counter++);
314 if (!strcmp(process_name, "process0")){
315 INFO2("%s: %s is the Root",name, process_name);
316 for(i=1;i<communicator_size;i++){
317 sprintf(spawn_name,"%s_process%d", name, i);
318 sprintf(task_name,"%s_wait", spawn_name);
320 MSG_process_create(task_name, spawned_recv,
321 (void *) xbt_strdup(spawn_name),
325 for(i=1;i<communicator_size;i++){
326 sprintf(task_name,"%s_process%d_wait", name, i);
327 MSG_task_receive(&task,task_name);
328 MSG_task_destroy(task);
333 MSG_task_create("allReduce_comp", parse_double(comp_size), 0, NULL);
334 INFO1("%s: computing 'reduce_comp'", name);
335 MSG_task_execute(comp_task);
336 MSG_task_destroy(comp_task);
337 INFO1("%s: computed", name);
339 for(i=1;i<communicator_size;i++){
340 myargv = (char**) calloc (3, sizeof (char*));
341 myargv[0] = xbt_strdup(name);
342 myargv[1] = xbt_strdup(comm_size);
345 sprintf(spawn_name,"%s_%d", myargv[0], i);
347 MSG_process_create_with_arguments(spawn_name, spawned_send,
348 NULL, MSG_host_self(), 2, myargv);
351 for(i=1;i<communicator_size;i++){
352 sprintf(task_name,"process%d_wait", i);
353 DEBUG1("get on %s", task_name);
354 MSG_task_receive(&task,task_name);
355 MSG_task_destroy(task);
358 INFO2("%s: all messages sent by %s have been received",
362 INFO2("%s: %s sends", name, process_name);
363 sprintf(task_name,"%s_%s", name, process_name);
364 INFO1("put on %s", task_name);
365 MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
368 MSG_task_receive(&task, name);
369 MSG_task_destroy(task);
370 INFO2("%s: %s has received", name,process_name);
371 sprintf(task_name,"%s_wait", process_name);
372 DEBUG1("put on %s", task_name);
373 MSG_task_send(MSG_task_create("waiter",0,0,NULL),task_name);
377 MSG_process_set_data(MSG_process_self(), (void*)counters);
382 static void comm_size(xbt_dynar_t action)
384 char *size = xbt_dynar_get_as(action, 2, char *);
385 communicator_size = parse_double(size);
388 static void compute(xbt_dynar_t action)
390 char *name = xbt_str_join(action, " ");
391 char *amout = xbt_dynar_get_as(action, 2, char *);
392 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
393 INFO1("computing: %s", name);
394 MSG_task_execute(task);
395 MSG_task_destroy(task);
396 INFO1("computed: %s", name);
401 int main(int argc, char *argv[])
403 MSG_error_t res = MSG_OK;
405 /* Check the given arguments */
406 MSG_global_init(&argc, argv);
408 printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
409 printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
414 /* Simulation setting */
415 MSG_create_environment(argv[1]);
417 /* No need to register functions as in classical MSG programs: the actions get started anyway */
418 MSG_launch_application(argv[2]);
420 /* Action registration */
421 MSG_action_register("comm_size", comm_size);
422 MSG_action_register("send", send);
423 MSG_action_register("Isend", Isend);
424 MSG_action_register("recv", recv);
425 MSG_action_register("Irecv", Irecv);
426 MSG_action_register("wait", wait);
427 MSG_action_register("barrier", barrier);
428 MSG_action_register("bcast", bcast);
429 MSG_action_register("reduce", reduce);
430 MSG_action_register("allReduce", allReduce);
431 MSG_action_register("sleep", sleep);
432 MSG_action_register("compute", compute);
435 /* Actually do the simulation using MSG_action_trace_run */
436 res = MSG_action_trace_run(argv[3]);
438 INFO1("Simulation time %g", MSG_get_clock());