1 /* $Id: masterslave_forwarder.c 5108 2007-12-04 16:29:05Z mquinson $ */
\r
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved. */
\r
5 /* This program is free software; you can redistribute it and/or modify it
\r
6 * under the terms of the license (GNU LGPL) which comes with this package. */
\r
9 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
\r
10 #include "xbt/sysdep.h" /* calloc, printf */
\r
12 /* Create a log channel to have nice outputs. */
\r
13 #include "xbt/log.h"
\r
14 #include "xbt/asserts.h"
\r
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
\r
23 int master(int argc, char *argv[]);
\r
24 int slave(int argc, char *argv[]);
\r
25 int forwarder(int argc, char *argv[]);
\r
26 MSG_error_t test_all(const char *platform_file, const char *application_file);
\r
29 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
\r
31 /** Emitter function */
\r
32 int master(int argc, char *argv[])
\r
34 int alias_count = 0;
\r
35 char** aliases = NULL;
\r
36 m_task_t *todo = NULL;
\r
37 int number_of_tasks = 0;
\r
38 double task_comp_size = 0;
\r
39 double task_comm_size = 0;
\r
44 xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),"Invalid argument %s\n",argv[1]);
\r
45 xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),"Invalid argument %s\n",argv[2]);
\r
46 xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),"Invalid argument %s\n",argv[3]);
\r
50 char sprintf_buffer[64];
\r
52 todo = calloc(number_of_tasks, sizeof(m_task_t));
\r
54 for (i = 0; i < number_of_tasks; i++)
\r
56 sprintf(sprintf_buffer, "Task_%d", i);
\r
57 todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
\r
62 /* Process organisation */
\r
63 alias_count = argc - 4;
\r
64 aliases = (char**) calloc(alias_count, sizeof(char*));
\r
66 for(i = 4; i < argc; i++)
\r
68 aliases[i-4] = strdup(argv[i]);
\r
72 INFO2("Got %d aliases and %d tasks to process", alias_count,number_of_tasks);
\r
74 for (i = 0; i < alias_count; i++)
\r
75 DEBUG1("%s", aliases[i]);
\r
77 for (i = 0; i < number_of_tasks; i++)
\r
79 INFO2("Sending \"%s\" to \"%s\"",todo[i]->name,aliases[i % alias_count]);
\r
81 if(!strcmp(MSG_host_get_name(MSG_host_self()), aliases[i % alias_count]))
\r
83 INFO0("Hey ! It's me ! :)");
\r
86 MSG_task_send(todo[i], aliases[i % alias_count]);
\r
90 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
\r
92 for (i = 0; i < alias_count; i++)
\r
93 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),aliases[i]);
\r
95 INFO0("Goodbye now!");
\r
97 for(i = 0; i < alias_count; i++)
\r
103 } /* end_of_master */
\r
105 /** Receiver function */
\r
106 int slave(int argc, char *argv[])
\r
108 m_task_t task = NULL;
\r
113 res = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
\r
114 xbt_assert0(res == MSG_OK, "MSG_task_receive failed");
\r
116 INFO1("Received \"%s\"", MSG_task_get_name(task));
\r
118 if (!strcmp(MSG_task_get_name(task),"finalize"))
\r
120 MSG_task_destroy(task);
\r
124 INFO1("Processing \"%s\"", MSG_task_get_name(task));
\r
125 MSG_task_execute(task);
\r
126 INFO1("\"%s\" done", MSG_task_get_name(task));
\r
127 MSG_task_destroy(task);
\r
131 INFO0("I'm done. See you!");
\r
133 } /* end_of_slave */
\r
135 /** Forwarder function */
\r
136 int forwarder(int argc, char *argv[])
\r
142 { /* Process organisation */
\r
143 alias_count = argc - 1;
\r
144 aliases = calloc(alias_count, sizeof(char*));
\r
146 for (i = 1; i < argc; i++)
\r
147 aliases[i-1] =strdup(argv[i]);
\r
154 m_task_t task = NULL;
\r
157 a = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
\r
161 INFO1("Received \"%s\"", MSG_task_get_name(task));
\r
163 if(MSG_task_get_data(task)==FINALIZE)
\r
165 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
\r
167 for (i = 0; i < alias_count; i++)
\r
168 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),aliases[i]);
\r
170 MSG_task_destroy(task);
\r
174 INFO2("Sending \"%s\" to \"%s\"",MSG_task_get_name(task),aliases[i% alias_count]);
\r
175 MSG_task_send(task, aliases[i % alias_count]);
\r
180 INFO0("Hey ?! What's up ? ");
\r
181 xbt_assert0(0,"Unexpected behavior");
\r
185 for(i = 0; i < alias_count; i++)
\r
188 INFO0("I'm done. See you!");
\r
191 } /* end_of_forwarder */
\r
193 /** Test function */
\r
194 MSG_error_t test_all(const char *platform_file,const char *application_file)
\r
196 MSG_error_t res = MSG_OK;
\r
198 { /* Simulation setting */
\r
199 MSG_set_channel_number(MAX_CHANNEL);
\r
200 MSG_paje_output("msg_test.trace");
\r
201 MSG_create_environment(platform_file);
\r
205 /* Application deployment */
\r
206 MSG_function_register("master", master);
\r
207 MSG_function_register("slave", slave);
\r
208 MSG_function_register("forwarder", forwarder);
\r
209 MSG_launch_application(application_file);
\r
214 INFO1("Simulation time %g",MSG_get_clock());
\r
216 } /* end_of_test_all */
\r
219 /** Main function */
\r
220 int main(int argc, char *argv[])
\r
222 MSG_error_t res = MSG_OK;
\r
224 MSG_global_init(&argc,argv);
\r
228 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
\r
229 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
\r
233 res = test_all(argv[1],argv[2]);
\r
240 } /* end_of_main */
\r