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{
23 static double parse_double(const char *string) {
27 value=strtod(string, &endptr);
29 THROW1(unknown_error, 0, "%s is not a double", string);
35 static void send(xbt_dynar_t action)
37 char *name = xbt_str_join(action, " ");
38 char *to = xbt_dynar_get_as(action, 2, char *);
39 char *size = xbt_dynar_get_as(action, 3, char *);
40 INFO2("Send: %s (size: %lg)", name, parse_double(size));
41 MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
42 INFO1("Sent %s", name);
46 static int spawned_send(int argc, char *argv[])
48 xbt_dynar_t action= (xbt_dynar_t) MSG_process_get_data(MSG_process_self());
49 char *name = xbt_str_join(action, " ");
50 char *to = xbt_dynar_get_as(action, 2, char *);
51 char *size = xbt_dynar_get_as(action, 3, char *);
52 INFO3("name is %s, to is %s, sizeis %s", name, to, size);
53 INFO1("Sending on %s\n", name);
54 MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
55 INFO1("Sent %s", name);
60 static void Isend(xbt_dynar_t action)
62 char *name = xbt_str_join(action, " ");
63 m_process_t comm_helper;
65 INFO1("Isend on %s: spawn process ",
66 MSG_process_get_name(MSG_process_self()));
68 sprintf(name,"%s_wait",MSG_process_self()->name);
69 comm_helper = MSG_process_create(name,spawned_send,
76 static void recv(xbt_dynar_t action)
78 char *name = xbt_str_join(action, " ");
80 INFO1("Receiving: %s", name);
81 //FIXME: argument of action ignored so far; semantic not clear
82 //char *from=xbt_dynar_get_as(action,2,char*);
83 MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
84 INFO1("Received %s", MSG_task_get_name(task));
85 MSG_task_destroy(task);
89 static int spawned_recv(int argc, char *argv[])
92 char* name = (char *) MSG_process_get_data(MSG_process_self());
93 INFO1("Receiving on %s", name);
94 MSG_task_receive(&task, name);
95 INFO1("Received %s", MSG_task_get_name(task));
96 MSG_task_send(MSG_task_create("waiter",0,0,NULL),MSG_process_self()->name);
98 MSG_task_destroy(task);
103 static void Irecv(xbt_dynar_t action)
105 char *name = xbt_str_join(action, " ");
106 m_process_t comm_helper;
108 INFO1("Irecv on %s: spawn process ",
109 MSG_process_get_name(MSG_process_self()));
111 sprintf(name,"%s_wait",MSG_process_self()->name);
112 comm_helper = MSG_process_create(name,spawned_recv,
113 (void *) MSG_process_get_name(MSG_process_self()),
121 static void wait(xbt_dynar_t action)
123 char *name = xbt_str_join(action, " ");
125 m_task_t task = NULL;
127 INFO1("wait: %s", name);
128 sprintf(task_name,"%s_wait",MSG_process_self()->name);
129 MSG_task_receive(&task,task_name);
130 INFO1("waited: %s", name);
134 static void barrier (xbt_dynar_t action)
136 char *name = xbt_str_join(action, " ");
137 INFO1("barrier: %s", name);
144 static int bcast_spawned_send(int argc, char *argv[])
147 INFO3("%s: Sending %s on %s", MSG_process_self()->name,
149 MSG_task_send(MSG_task_create(argv[0], 0, parse_double(argv[1]), NULL),
152 sprintf(name,"%s_wait",argv[0]);
156 static void bcast (xbt_dynar_t action)
160 const char* process_name;
164 m_process_t comm_helper=NULL;
166 char *size = xbt_dynar_get_as(action, 2, char *);
167 coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
169 xbt_assert0(communicator_size, "Size of Communicator is not defined"
170 ", can't use collective operations");
172 MSG_process_self()->data=NULL;
174 process_name = MSG_process_self()->name;
176 DEBUG0("Initialize the counters");
177 counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
180 name = bprintf("bcast_%d", counters->bcast_counter++);
181 if (!strcmp(process_name, "process0")){
182 INFO2("%s: %s is the Root",name, process_name);
184 for(i=1;i<communicator_size;i++){
185 myargv = (char**) calloc (3, sizeof (char*));
186 myargv[0] = xbt_strdup(name);
187 myargv[1] = xbt_strdup(size);
190 sprintf(spawn_name,"%s_%d", myargv[0], i);
192 MSG_process_create_with_arguments(spawn_name, bcast_spawned_send,
193 NULL, MSG_host_self(), 2, myargv);
196 for(i=1;i<communicator_size;i++){
197 sprintf(task_name,"process%d_wait", i);
198 DEBUG1("get on %s", task_name);
199 MSG_task_receive(&task,task_name);
200 MSG_task_destroy(task);
203 INFO2("%s: all messages sent by %s have been received",
206 INFO2("%s: %s receives", name, process_name);
207 MSG_task_receive(&task, name);
208 MSG_task_destroy(task);
209 INFO2("%s: %s has received", name,process_name);
210 sprintf(task_name,"%s_wait", process_name);
211 DEBUG1("put on %s", task_name);
212 MSG_task_send(MSG_task_create("waiter",0,0,NULL),task_name);
215 MSG_process_set_data(MSG_process_self(), (void*)counters);
220 static void sleep(xbt_dynar_t action)
222 char *name = xbt_str_join(action, " ");
223 char *duration = xbt_dynar_get_as(action, 2, char *);
224 INFO1("sleep: %s", name);
225 MSG_process_sleep(parse_double(duration));
226 INFO1("sleept: %s", name);
230 static void comm_size(xbt_dynar_t action)
232 char *size = xbt_dynar_get_as(action, 2, char *);
233 communicator_size = parse_double(size);
236 static void compute(xbt_dynar_t action)
238 char *name = xbt_str_join(action, " ");
239 char *amout = xbt_dynar_get_as(action, 2, char *);
240 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
241 INFO1("computing: %s", name);
242 MSG_task_execute(task);
243 MSG_task_destroy(task);
244 INFO1("computed: %s", name);
249 int main(int argc, char *argv[])
251 MSG_error_t res = MSG_OK;
253 /* Check the given arguments */
254 MSG_global_init(&argc, argv);
256 printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
257 printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
262 /* Simulation setting */
263 MSG_create_environment(argv[1]);
265 /* No need to register functions as in classical MSG programs: the actions get started anyway */
266 MSG_launch_application(argv[2]);
268 /* Action registration */
269 MSG_action_register("comm_size", comm_size);
270 MSG_action_register("send", send);
271 MSG_action_register("Isend", Isend);
272 MSG_action_register("recv", recv);
273 MSG_action_register("Irecv", Irecv);
274 MSG_action_register("wait", wait);
275 MSG_action_register("barrier", barrier);
276 MSG_action_register("bcast", bcast);
277 MSG_action_register("sleep", sleep);
278 MSG_action_register("compute", compute);
281 /* Actually do the simulation using MSG_action_trace_run */
282 res = MSG_action_trace_run(argv[3]);
284 INFO1("Simulation time %g", MSG_get_clock());