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");
19 static double parse_double(const char *string) {
23 value=strtod(string, &endptr);
25 THROW1(unknown_error, 0, "%s is not a double", string);
31 static void send(xbt_dynar_t action)
33 char *name = xbt_str_join(action, " ");
34 char *to = xbt_dynar_get_as(action, 2, char *);
35 char *size = xbt_dynar_get_as(action, 3, char *);
36 INFO2("Send: %s (size: %lg)", name, parse_double(size));
37 MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
38 INFO1("Sent %s", name);
42 static int spawned_send(int argc, char *argv[])
44 xbt_dynar_t action= (xbt_dynar_t) MSG_process_get_data(MSG_process_self());
45 char *name = xbt_str_join(action, " ");
46 char *to = xbt_dynar_get_as(action, 2, char *);
47 char *size = xbt_dynar_get_as(action, 3, char *);
49 INFO1("Sending on %s", name);
50 MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
51 INFO1("Sent %s", name);
56 static void Isend(xbt_dynar_t action)
58 char *name = xbt_str_join(action, " ");
59 m_process_t comm_helper;
61 INFO1("Isend on %s: spawn process ",
62 MSG_process_get_name(MSG_process_self()));
64 sprintf(name,"%s_wait",MSG_process_self()->name);
65 comm_helper = MSG_process_create(name,spawned_send,
72 static void recv(xbt_dynar_t action)
74 char *name = xbt_str_join(action, " ");
76 INFO1("Receiving: %s", name);
77 //FIXME: argument of action ignored so far; semantic not clear
78 //char *from=xbt_dynar_get_as(action,2,char*);
79 MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
80 INFO1("Received %s", MSG_task_get_name(task));
81 MSG_task_destroy(task);
85 static int spawned_recv(int argc, char *argv[])
88 char* name = (char *) MSG_process_get_data(MSG_process_self());
89 INFO1("Receiving on %s", name);
90 MSG_task_receive(&task, name);
91 INFO1("Received %s", MSG_task_get_name(task));
92 MSG_task_send(MSG_task_create("waiter",0,0,NULL),MSG_process_self()->name);
94 MSG_task_destroy(task);
99 static void Irecv(xbt_dynar_t action)
101 char *name = xbt_str_join(action, " ");
102 m_process_t comm_helper;
104 INFO1("Irecv on %s: spawn process ",
105 MSG_process_get_name(MSG_process_self()));
107 sprintf(name,"%s_wait",MSG_process_self()->name);
108 comm_helper = MSG_process_create(name,spawned_recv,
109 (void *) MSG_process_get_name(MSG_process_self()),
117 static void wait(xbt_dynar_t action)
119 char *name = xbt_str_join(action, " ");
121 m_task_t task = NULL;
123 INFO1("wait: %s", name);
124 sprintf(task_name,"%s_wait",MSG_process_self()->name);
125 MSG_task_receive(&task,task_name);
126 INFO1("waited: %s", name);
130 static void barrier (xbt_dynar_t action)
132 char *name = xbt_str_join(action, " ");
133 INFO1("barrier: %s", name);
139 static void sleep(xbt_dynar_t action)
141 char *name = xbt_str_join(action, " ");
142 char *duration = xbt_dynar_get_as(action, 2, char *);
143 INFO1("sleep: %s", name);
144 MSG_process_sleep(parse_double(duration));
145 INFO1("sleept: %s", name);
149 static void compute(xbt_dynar_t action)
151 char *name = xbt_str_join(action, " ");
152 char *amout = xbt_dynar_get_as(action, 2, char *);
153 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
154 INFO1("computing: %s", name);
155 MSG_task_execute(task);
156 MSG_task_destroy(task);
157 INFO1("computed: %s", name);
162 int main(int argc, char *argv[])
164 MSG_error_t res = MSG_OK;
166 /* Check the given arguments */
167 MSG_global_init(&argc, argv);
169 printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
170 printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
175 /* Simulation setting */
176 MSG_create_environment(argv[1]);
178 /* No need to register functions as in classical MSG programs: the actions get started anyway */
179 MSG_launch_application(argv[2]);
181 /* Action registration */
182 MSG_action_register("send", send);
183 MSG_action_register("Isend", Isend);
184 MSG_action_register("recv", recv);
185 MSG_action_register("Irecv", Irecv);
186 MSG_action_register("wait", wait);
187 MSG_action_register("barrier", barrier);
188 MSG_action_register("sleep", sleep);
189 MSG_action_register("compute", compute);
192 /* Actually do the simulation using MSG_action_trace_run */
193 res = MSG_action_trace_run(argv[3]);
195 INFO1("Simulation time %g", MSG_get_clock());
196 xbt_dynar_free_container(&comm_world);