3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. 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. */
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");
16 int master(int argc, char *argv[]);
17 int slave(int argc, char *argv[]);
18 int forwarder(int argc, char *argv[]);
19 MSG_error_t test_all(const char *platform_file, const char *application_file);
26 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
28 /** Emitter function */
29 int master(int argc, char *argv[])
32 m_host_t *slaves = NULL;
33 m_task_t *todo = NULL;
34 int number_of_tasks = 0;
35 double task_comp_size = 0;
36 double task_comm_size = 0;
41 xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
42 "Invalid argument %s\n",argv[1]);
43 xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
44 "Invalid argument %s\n",argv[2]);
45 xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
46 "Invalid argument %s\n",argv[3]);
49 char sprintf_buffer[64];
51 todo = calloc(number_of_tasks, sizeof(m_task_t));
53 for (i = 0; i < number_of_tasks; i++) {
54 sprintf(sprintf_buffer, "Task_%d", i);
55 todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
59 { /* Process organisation */
60 slaves_count = argc - 4;
61 slaves = calloc(slaves_count, sizeof(m_host_t));
63 for (i = 4; i < argc; i++) {
64 slaves[i-4] = MSG_get_host_by_name(argv[i]);
65 if(slaves[i-4]==NULL) {
66 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
72 INFO1("Got %d slave(s) :", slaves_count);
73 for (i = 0; i < slaves_count; i++)
74 INFO1("\t %s", slaves[i]->name);
76 INFO1("Got %d task to process :", number_of_tasks);
78 for (i = 0; i < number_of_tasks; i++)
79 INFO1("\t\"%s\"", todo[i]->name);
81 for (i = 0; i < number_of_tasks; i++) {
82 INFO2("Sending \"%s\" to \"%s\"",
84 slaves[i % slaves_count]->name);
85 if(MSG_host_self()==slaves[i % slaves_count]) {
86 INFO0("Hey ! It's me ! :)");
88 MSG_task_put(todo[i], slaves[i % slaves_count],
90 INFO0("Send completed");
93 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
94 for (i = 0; i < slaves_count; i++)
95 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
98 INFO0("Goodbye now!");
102 } /* end_of_master */
104 /** Receiver function */
105 int slave(int argc, char *argv[])
108 m_task_t task = NULL;
110 a = MSG_task_get(&(task), PORT_22);
112 INFO1("Received \"%s\" ", MSG_task_get_name(task));
113 if(MSG_task_get_data(task)==FINALIZE) {
114 MSG_task_destroy(task);
117 INFO1("Processing \"%s\" ", MSG_task_get_name(task));
118 MSG_task_execute(task);
119 INFO1("\"%s\" done ", MSG_task_get_name(task));
120 MSG_task_destroy(task);
122 INFO0("Hey ?! What's up ? ");
123 xbt_assert0(0,"Unexpected behavior");
126 INFO0("I'm done. See you!");
130 /** Receiver function */
131 int forwarder(int argc, char *argv[])
137 { /* Process organisation */
138 slaves_count = argc - 1;
139 slaves = calloc(slaves_count, sizeof(m_host_t));
141 for (i = 1; i < argc; i++) {
142 slaves[i-1] = MSG_get_host_by_name(argv[i]);
143 if(slaves[i-1]==NULL) {
144 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
152 m_task_t task = NULL;
154 a = MSG_task_get(&(task), PORT_22);
156 INFO1("Received \"%s\" ", MSG_task_get_name(task));
157 if(MSG_task_get_data(task)==FINALIZE) {
158 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
159 for (i = 0; i < slaves_count; i++)
160 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
162 MSG_task_destroy(task);
165 INFO2("Sending \"%s\" to \"%s\"",
166 MSG_task_get_name(task),
167 slaves[i% slaves_count]->name);
168 MSG_task_put(task, slaves[i % slaves_count],
172 INFO0("Hey ?! What's up ? ");
173 xbt_assert0(0,"Unexpected behavior");
177 INFO0("I'm done. See you!");
179 } /* end_of_forwarder */
183 MSG_error_t test_all(const char *platform_file,
184 const char *application_file)
186 MSG_error_t res = MSG_OK;
188 /* MSG_config("surf_workstation_model","KCCFLN05"); */
189 { /* Simulation setting */
190 MSG_set_channel_number(MAX_CHANNEL);
191 MSG_paje_output("msg_test.trace");
192 MSG_create_environment(platform_file);
194 { /* Application deployment */
195 MSG_function_register("master", master);
196 MSG_function_register("slave", slave);
197 MSG_function_register("forwarder", forwarder);
198 MSG_launch_application(application_file);
202 INFO1("Simulation time %g",MSG_get_clock());
204 } /* end_of_test_all */
208 int main(int argc, char *argv[])
210 MSG_error_t res = MSG_OK;
212 MSG_global_init(&argc,argv);
214 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
215 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
218 res = test_all(argv[1],argv[2]);
221 if(res==MSG_OK) return 0;