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{
24 static double parse_double(const char *string) {
28 value=strtod(string, &endptr);
30 THROW1(unknown_error, 0, "%s is not a double", string);
36 static void send(xbt_dynar_t action)
38 char *name = xbt_str_join(action, " ");
39 char *to = xbt_dynar_get_as(action, 2, char *);
40 char *size = xbt_dynar_get_as(action, 3, char *);
41 INFO2("Send: %s (size: %lg)", name, parse_double(size));
42 MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
43 INFO1("Sent %s", name);
48 static int spawned_send(int argc, char *argv[])
50 INFO3("%s: Sending %s on %s", MSG_process_self()->name,
52 MSG_task_send(MSG_task_create(argv[0], 0, parse_double(argv[1]), NULL),
57 static void Isend(xbt_dynar_t action)
60 char *to = xbt_dynar_get_as(action, 2, char *);
61 char *size = xbt_dynar_get_as(action, 3, char *);
63 m_process_t comm_helper;
65 INFO1("Isend on %s: spawn process ",
66 MSG_process_self()->name);
68 myargv = (char**) calloc (3, sizeof (char*));
70 myargv[0] = xbt_strdup(to);
71 myargv[1] = xbt_strdup(size);
74 sprintf(spawn_name,"%s_wait",MSG_process_self()->name);
76 MSG_process_create_with_arguments(spawn_name, spawned_send,
77 NULL, MSG_host_self(), 2, myargv);
81 static void recv(xbt_dynar_t action)
83 char *name = xbt_str_join(action, " ");
85 INFO1("Receiving: %s", name);
86 //FIXME: argument of action ignored so far; semantic not clear
87 //char *from=xbt_dynar_get_as(action,2,char*);
88 MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
89 INFO1("Received %s", MSG_task_get_name(task));
90 MSG_task_destroy(task);
94 static int spawned_recv(int argc, char *argv[])
97 char* name = (char *) MSG_process_self()->data;
98 INFO1("Receiving on %s", name);
99 MSG_task_receive(&task, name);
100 INFO1("Received %s", MSG_task_get_name(task));
101 INFO1("waiter on %s", MSG_process_self()->name);
102 MSG_task_send(MSG_task_create("waiter",0,0,NULL),MSG_process_self()->name);
104 MSG_task_destroy(task);
109 static void Irecv(xbt_dynar_t action)
111 char *name = xbt_str_join(action, " ");
112 m_process_t comm_helper;
114 INFO1("Irecv on %s: spawn process ",
115 MSG_process_get_name(MSG_process_self()));
117 sprintf(name,"%s_wait",MSG_process_self()->name);
118 comm_helper = MSG_process_create(name,spawned_recv,
119 (void *) MSG_process_self()->name,
127 static void wait(xbt_dynar_t action)
129 char *name = xbt_str_join(action, " ");
131 m_task_t task = NULL;
133 INFO1("wait: %s", name);
134 sprintf(task_name,"%s_wait",MSG_process_self()->name);
135 MSG_task_receive(&task,task_name);
136 MSG_task_destroy(task);
137 INFO1("waited: %s", name);
141 static void barrier (xbt_dynar_t action)
143 char *name = xbt_str_join(action, " ");
144 INFO1("barrier: %s", name);
151 static void reduce(xbt_dynar_t action)
157 char *comm_size = xbt_dynar_get_as(action, 2, char *);
158 char *comp_size = xbt_dynar_get_as(action, 3, char *);
159 m_process_t comm_helper=NULL;
160 m_task_t task=NULL, comp_task=NULL;
161 const char* process_name;
163 coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
165 xbt_assert0(communicator_size, "Size of Communicator is not defined"
166 ", can't use collective operations");
168 MSG_process_self()->data=NULL;
170 process_name = MSG_process_self()->name;
173 DEBUG0("Initialize the counters");
174 counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
177 name = bprintf("reduce_%d", counters->reduce_counter++);
179 if (!strcmp(process_name, "process0")){
180 INFO2("%s: %s is the Root",name, process_name);
181 for(i=1;i<communicator_size;i++){
182 sprintf(spawn_name,"%s_process%d", name, i);
183 sprintf(task_name,"%s_wait", spawn_name);
185 MSG_process_create(task_name, spawned_recv,
186 (void *) xbt_strdup(spawn_name),
190 for(i=1;i<communicator_size;i++){
191 sprintf(task_name,"%s_process%d_wait", name, i);
192 MSG_task_receive(&task,task_name);
193 MSG_task_destroy(task);
198 MSG_task_create("reduce_comp", parse_double(comp_size), 0, NULL);
199 INFO1("%s: computing 'reduce_comp'", name);
200 MSG_task_execute(comp_task);
201 MSG_task_destroy(comp_task);
202 INFO1("%s: computed", name);
204 INFO2("%s: %s sends", name, process_name);
205 sprintf(task_name,"%s_%s", name, process_name);
206 INFO1("put on %s", task_name);
207 MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
211 MSG_process_set_data(MSG_process_self(), (void*)counters);
215 static void bcast (xbt_dynar_t action)
219 const char* process_name;
223 m_process_t comm_helper=NULL;
225 char *size = xbt_dynar_get_as(action, 2, char *);
226 coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
228 xbt_assert0(communicator_size, "Size of Communicator is not defined"
229 ", can't use collective operations");
231 MSG_process_self()->data=NULL;
233 process_name = MSG_process_self()->name;
235 DEBUG0("Initialize the counters");
236 counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
239 name = bprintf("bcast_%d", counters->bcast_counter++);
240 if (!strcmp(process_name, "process0")){
241 INFO2("%s: %s is the Root",name, process_name);
243 for(i=1;i<communicator_size;i++){
244 myargv = (char**) calloc (3, sizeof (char*));
245 myargv[0] = xbt_strdup(name);
246 myargv[1] = xbt_strdup(size);
249 sprintf(spawn_name,"%s_%d", myargv[0], i);
251 MSG_process_create_with_arguments(spawn_name, spawned_send,
252 NULL, MSG_host_self(), 2, myargv);
255 for(i=1;i<communicator_size;i++){
256 sprintf(task_name,"process%d_wait", i);
257 DEBUG1("get on %s", task_name);
258 MSG_task_receive(&task,task_name);
259 MSG_task_destroy(task);
262 INFO2("%s: all messages sent by %s have been received",
265 INFO2("%s: %s receives", name, process_name);
266 MSG_task_receive(&task, name);
267 MSG_task_destroy(task);
268 INFO2("%s: %s has received", name,process_name);
269 sprintf(task_name,"%s_wait", process_name);
270 DEBUG1("put on %s", task_name);
271 MSG_task_send(MSG_task_create("waiter",0,0,NULL),task_name);
274 MSG_process_set_data(MSG_process_self(), (void*)counters);
279 static void sleep(xbt_dynar_t action)
281 char *name = xbt_str_join(action, " ");
282 char *duration = xbt_dynar_get_as(action, 2, char *);
283 INFO1("sleep: %s", name);
284 MSG_process_sleep(parse_double(duration));
285 INFO1("sleept: %s", name);
289 static void comm_size(xbt_dynar_t action)
291 char *size = xbt_dynar_get_as(action, 2, char *);
292 communicator_size = parse_double(size);
295 static void compute(xbt_dynar_t action)
297 char *name = xbt_str_join(action, " ");
298 char *amout = xbt_dynar_get_as(action, 2, char *);
299 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
300 INFO1("computing: %s", name);
301 MSG_task_execute(task);
302 MSG_task_destroy(task);
303 INFO1("computed: %s", name);
308 int main(int argc, char *argv[])
310 MSG_error_t res = MSG_OK;
312 /* Check the given arguments */
313 MSG_global_init(&argc, argv);
315 printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
316 printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
321 /* Simulation setting */
322 MSG_create_environment(argv[1]);
324 /* No need to register functions as in classical MSG programs: the actions get started anyway */
325 MSG_launch_application(argv[2]);
327 /* Action registration */
328 MSG_action_register("comm_size", comm_size);
329 MSG_action_register("send", send);
330 MSG_action_register("Isend", Isend);
331 MSG_action_register("recv", recv);
332 MSG_action_register("Irecv", Irecv);
333 MSG_action_register("wait", wait);
334 MSG_action_register("barrier", barrier);
335 MSG_action_register("bcast", bcast);
336 MSG_action_register("reduce", reduce);
337 MSG_action_register("sleep", sleep);
338 MSG_action_register("compute", compute);
341 /* Actually do the simulation using MSG_action_trace_run */
342 res = MSG_action_trace_run(argv[3]);
344 INFO1("Simulation time %g", MSG_get_clock());