1 /* Copyright (c) 2008, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
22 int master(int argc, char *argv[]);
23 int slave(int argc, char *argv[]);
24 int forwarder(int argc, char *argv[]);
25 MSG_error_t test_all(const char *platform_file, const char *application_file);
28 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
30 /** Emitter function */
31 int master(int argc, char *argv[])
34 char** aliases = NULL;
35 m_task_t *todo = NULL;
36 int number_of_tasks = 0;
37 double task_comp_size = 0;
38 double task_comm_size = 0;
43 xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),"Invalid argument %s\n",argv[1]);
44 xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),"Invalid argument %s\n",argv[2]);
45 xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),"Invalid argument %s\n",argv[3]);
49 char sprintf_buffer[64];
51 todo = xbt_new0(m_task_t,number_of_tasks);
53 for (i = 0; i < number_of_tasks; i++)
55 sprintf(sprintf_buffer, "Task_%d", i);
56 todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
61 /* Process organisation */
62 alias_count = argc - 4;
63 aliases = xbt_new0(char*,alias_count);
65 for(i = 4; i < argc; i++)
67 aliases[i-4] = strdup(argv[i]);
71 INFO2("Got %d aliases and %d tasks to process", alias_count,number_of_tasks);
73 for (i = 0; i < alias_count; i++)
74 DEBUG1("%s", aliases[i]);
76 for (i = 0; i < number_of_tasks; i++)
78 INFO2("Sending \"%s\" to \"%s\"",todo[i]->name,aliases[i % alias_count]);
80 if(!strcmp(MSG_host_get_name(MSG_host_self()), aliases[i % alias_count]))
82 INFO0("Hey ! It's me ! :)");
85 MSG_task_send(todo[i], aliases[i % alias_count]);
89 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
91 for (i = 0; i < alias_count; i++)
92 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),aliases[i]);
94 INFO0("Goodbye now!");
96 for(i = 0; i < alias_count; i++)
102 } /* end_of_master */
104 /** Receiver function */
105 int slave(int argc, char *argv[])
107 m_task_t task = NULL;
112 res = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
113 xbt_assert0(res == MSG_OK, "MSG_task_receive failed");
115 INFO1("Received \"%s\"", MSG_task_get_name(task));
117 if (!strcmp(MSG_task_get_name(task),"finalize"))
119 MSG_task_destroy(task);
123 INFO1("Processing \"%s\"", MSG_task_get_name(task));
124 MSG_task_execute(task);
125 INFO1("\"%s\" done", MSG_task_get_name(task));
126 MSG_task_destroy(task);
130 INFO0("I'm done. See you!");
134 /** Forwarder function */
135 int forwarder(int argc, char *argv[])
141 { /* Process organisation */
142 alias_count = argc - 1;
143 aliases = xbt_new0(char*,alias_count);
145 for (i = 1; i < argc; i++)
146 aliases[i-1] =strdup(argv[i]);
153 m_task_t task = NULL;
156 a = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
160 INFO1("Received \"%s\"", MSG_task_get_name(task));
162 if(MSG_task_get_data(task)==FINALIZE)
164 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
166 for (i = 0; i < alias_count; i++)
167 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),aliases[i]);
169 MSG_task_destroy(task);
173 INFO2("Sending \"%s\" to \"%s\"",MSG_task_get_name(task),aliases[i% alias_count]);
174 MSG_task_send(task, aliases[i % alias_count]);
179 INFO0("Hey ?! What's up ? ");
180 xbt_assert0(0,"Unexpected behavior");
184 for(i = 0; i < alias_count; i++)
187 INFO0("I'm done. See you!");
190 } /* end_of_forwarder */
193 MSG_error_t test_all(const char *platform_file,const char *application_file)
195 MSG_error_t res = MSG_OK;
197 { /* Simulation setting */
198 MSG_set_channel_number(MAX_CHANNEL);
199 MSG_paje_output("msg_test.trace");
200 MSG_create_environment(platform_file);
204 /* Application deployment */
205 MSG_function_register("master", master);
206 MSG_function_register("slave", slave);
207 MSG_function_register("forwarder", forwarder);
208 MSG_launch_application(application_file);
213 INFO1("Simulation time %g",MSG_get_clock());
215 } /* end_of_test_all */
219 int main(int argc, char *argv[])
221 MSG_error_t res = MSG_OK;
223 MSG_global_init(&argc,argv);
227 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
228 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
232 res = test_all(argv[1],argv[2]);