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. */
9 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
10 #include "xbt/sysdep.h" /* calloc, printf */
12 /* Create a log channel to have nice outputs. */
14 #include "xbt/asserts.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
16 "Messages specific for this msg example");
18 int master(int argc, char *argv[]);
19 int slave(int argc, char *argv[]);
20 int forwarder(int argc, char *argv[]);
21 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 m_host_t *slaves = 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;
40 TRACE_host_variable_set ("is_master", 1);
44 xbt_assert1(sscanf(argv[1], "%d", &number_of_tasks),
45 "Invalid argument %s\n", argv[1]);
46 xbt_assert1(sscanf(argv[2], "%lg", &task_comp_size),
47 "Invalid argument %s\n", argv[2]);
48 xbt_assert1(sscanf(argv[3], "%lg", &task_comm_size),
49 "Invalid argument %s\n", argv[3]);
52 char sprintf_buffer[64];
54 todo = xbt_new0(m_task_t, number_of_tasks);
56 for (i = 0; i < number_of_tasks; i++) {
57 sprintf(sprintf_buffer, "Task_%d", i);
59 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
60 TRACE_host_variable_set ("task_creation", i);
61 TRACE_msg_set_task_category (todo[i], "compute");
65 { /* Process organisation */
66 slaves_count = argc - 4;
67 slaves = xbt_new0(m_host_t, slaves_count);
69 for (i = 4; i < argc; i++) {
70 slaves[i - 4] = MSG_get_host_by_name(argv[i]);
71 xbt_assert1(slaves[i - 4] != NULL, "Unknown host %s. Stopping Now! ",
76 INFO2("Got %d slaves and %d tasks to process", slaves_count,
78 for (i = 0; i < slaves_count; i++)
79 DEBUG1("%s", slaves[i]->name);
81 for (i = 0; i < number_of_tasks; i++) {
82 INFO2("Sending \"%s\" to \"%s\"",
83 todo[i]->name, slaves[i % slaves_count]->name);
84 if (MSG_host_self() == slaves[i % slaves_count]) {
85 INFO0("Hey ! It's me ! :)");
88 MSG_task_put(todo[i], slaves[i % slaves_count], PORT_22);
93 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
94 for (i = 0; i < slaves_count; i++){
95 m_task_t finalize=MSG_task_create("finalize", 0, 0, FINALIZE);
96 TRACE_msg_set_task_category(finalize,"finalize");
97 MSG_task_put(finalize, slaves[i], PORT_22);
100 INFO0("Goodbye now!");
104 } /* end_of_master */
106 /** Receiver function */
107 int slave(int argc, char *argv[])
109 m_task_t task = NULL;
110 TRACE_host_variable_set ("is_slave", 1);
113 res = MSG_task_get(&(task), PORT_22);
114 xbt_assert0(res == MSG_OK, "MSG_task_get failed");
116 INFO1("Received \"%s\"", MSG_task_get_name(task));
117 if (!strcmp(MSG_task_get_name(task), "finalize")) {
118 MSG_task_destroy(task);
122 INFO1("Processing \"%s\"", MSG_task_get_name(task));
123 TRACE_host_variable_add ("task_computation", MSG_task_get_compute_duration(task));
124 MSG_task_execute(task);
125 INFO1("\"%s\" done", MSG_task_get_name(task));
126 MSG_task_destroy(task);
129 INFO0("I'm done. See you!");
133 /** Forwarder function */
134 int forwarder(int argc, char *argv[])
140 { /* Process organisation */
141 slaves_count = argc - 1;
142 slaves = xbt_new0(m_host_t, slaves_count);
144 for (i = 1; i < argc; i++) {
145 slaves[i - 1] = MSG_get_host_by_name(argv[i]);
146 if (slaves[i - 1] == NULL) {
147 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
155 m_task_t task = NULL;
157 a = MSG_task_get(&(task), PORT_22);
159 INFO1("Received \"%s\"", MSG_task_get_name(task));
160 if (MSG_task_get_data(task) == FINALIZE) {
162 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
163 for (i = 0; i < slaves_count; i++)
164 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
166 MSG_task_destroy(task);
169 INFO2("Sending \"%s\" to \"%s\"",
170 MSG_task_get_name(task), slaves[i % slaves_count]->name);
171 MSG_task_put(task, slaves[i % slaves_count], PORT_22);
174 INFO0("Hey ?! What's up ? ");
175 xbt_assert0(0, "Unexpected behavior");
180 INFO0("I'm done. See you!");
182 } /* end_of_forwarder */
185 MSG_error_t test_all(const char *platform_file, const char *application_file)
187 MSG_error_t res = MSG_OK;
189 /* MSG_config("workstation/model","KCCFLN05"); */
190 { /* Simulation setting */
191 MSG_set_channel_number(MAX_CHANNEL);
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;
214 for (i = 0; i < argc; i++){
215 if (!strcmp (argv[i], "--trace")){
221 //if TRACE_start is not called, all other tracing
222 //functions will be disabled
223 TRACE_start ("simulation.trace");
225 TRACE_host_variable_declare ("is_slave");
226 TRACE_host_variable_declare ("is_master");
227 TRACE_host_variable_declare ("task_creation");
228 TRACE_host_variable_declare ("task_computation");
229 TRACE_category ("compute");
230 TRACE_category ("finalize");
232 MSG_global_init(&argc, argv);
234 printf("Usage: %s platform_file deployment_file\n", argv[0]);
235 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
238 res = test_all(argv[1], argv[2]);