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 ! :)");
89 MSG_task_put(todo[i], slaves[i % slaves_count],
91 INFO0("Send completed");
94 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
95 for (i = 0; i < slaves_count; i++)
96 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
99 INFO0("Goodbye now!");
103 } /* end_of_master */
105 /** Receiver function */
106 int slave(int argc, char *argv[])
109 m_task_t task = NULL;
111 a = MSG_task_get(&(task), PORT_22);
113 INFO1("Received \"%s\" ", MSG_task_get_name(task));
114 if(MSG_task_get_data(task)==FINALIZE) {
115 MSG_task_destroy(task);
118 INFO1("Processing \"%s\" ", MSG_task_get_name(task));
119 MSG_task_execute(task);
120 INFO1("\"%s\" done ", MSG_task_get_name(task));
121 MSG_task_destroy(task);
123 INFO0("Hey ?! What's up ? ");
124 xbt_assert0(0,"Unexpected behavior");
127 INFO0("I'm done. See you!");
131 /** Forwarder function */
132 int forwarder(int argc, char *argv[])
138 { /* Process organisation */
139 slaves_count = argc - 1;
140 slaves = calloc(slaves_count, sizeof(m_host_t));
142 for (i = 1; i < argc; i++) {
143 slaves[i-1] = MSG_get_host_by_name(argv[i]);
144 if(slaves[i-1]==NULL) {
145 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
153 m_task_t task = NULL;
155 a = MSG_task_get(&(task), PORT_22);
157 INFO1("Received \"%s\" ", MSG_task_get_name(task));
158 if(MSG_task_get_data(task)==FINALIZE) {
159 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
160 for (i = 0; i < slaves_count; i++)
161 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
163 MSG_task_destroy(task);
166 INFO2("Sending \"%s\" to \"%s\"",
167 MSG_task_get_name(task),
168 slaves[i% slaves_count]->name);
169 MSG_task_put(task, slaves[i % slaves_count],
173 INFO0("Hey ?! What's up ? ");
174 xbt_assert0(0,"Unexpected behavior");
178 INFO0("I'm done. See you!");
180 } /* 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;