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,"Messages specific for this msg example");
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19 int forwarder(int argc, char *argv[]);
20 MSG_error_t test_all(const char *platform_file, const char *application_file);
27 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
29 /** Emitter function */
30 int master(int argc, char *argv[])
33 m_host_t *slaves = NULL;
34 m_task_t *todo = NULL;
35 int number_of_tasks = 0;
36 double task_comp_size = 0;
37 double task_comm_size = 0;
42 xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
43 "Invalid argument %s\n",argv[1]);
44 xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
45 "Invalid argument %s\n",argv[2]);
46 xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
47 "Invalid argument %s\n",argv[3]);
50 char sprintf_buffer[64];
52 todo = calloc(number_of_tasks, sizeof(m_task_t));
54 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);
60 { /* Process organisation */
61 slaves_count = argc - 4;
62 slaves = calloc(slaves_count, sizeof(m_host_t));
64 for (i = 4; i < argc; i++) {
65 slaves[i-4] = MSG_get_host_by_name(argv[i]);
66 if(slaves[i-4]==NULL) {
67 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
73 INFO1("Got %d slave(s) :", slaves_count);
74 for (i = 0; i < slaves_count; i++)
75 INFO1("\t %s", slaves[i]->name);
77 INFO1("Got %d task to process :", number_of_tasks);
79 for (i = 0; i < number_of_tasks; i++)
80 INFO1("\t\"%s\"", todo[i]->name);
82 for (i = 0; i < number_of_tasks; i++) {
83 INFO2("Sending \"%s\" to \"%s\"",
85 slaves[i % slaves_count]->name);
86 if(MSG_host_self()==slaves[i % slaves_count]) {
87 INFO0("Hey ! It's me ! :)");
90 MSG_task_put(todo[i], slaves[i % slaves_count],
92 INFO0("Send completed");
95 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
96 for (i = 0; i < slaves_count; i++)
97 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
100 INFO0("Goodbye now!");
104 } /* end_of_master */
106 /** Receiver function */
107 int slave(int argc, char *argv[])
110 m_task_t task = NULL;
112 a = MSG_task_get(&(task), PORT_22);
114 INFO1("Received \"%s\" ", MSG_task_get_name(task));
115 if(MSG_task_get_data(task)==FINALIZE) {
116 MSG_task_destroy(task);
119 INFO1("Processing \"%s\" ", MSG_task_get_name(task));
120 MSG_task_execute(task);
121 INFO1("\"%s\" done ", MSG_task_get_name(task));
122 MSG_task_destroy(task);
124 INFO0("Hey ?! What's up ? ");
125 xbt_assert0(0,"Unexpected behavior");
128 INFO0("I'm done. See you!");
132 /** Forwarder function */
133 int forwarder(int argc, char *argv[])
139 { /* Process organisation */
140 slaves_count = argc - 1;
141 slaves = calloc(slaves_count, sizeof(m_host_t));
143 for (i = 1; i < argc; i++) {
144 slaves[i-1] = MSG_get_host_by_name(argv[i]);
145 if(slaves[i-1]==NULL) {
146 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
154 m_task_t task = NULL;
156 a = MSG_task_get(&(task), PORT_22);
158 INFO1("Received \"%s\" ", MSG_task_get_name(task));
159 if(MSG_task_get_data(task)==FINALIZE) {
160 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
161 for (i = 0; i < slaves_count; i++)
162 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
164 MSG_task_destroy(task);
167 INFO2("Sending \"%s\" to \"%s\"",
168 MSG_task_get_name(task),
169 slaves[i% slaves_count]->name);
170 MSG_task_put(task, slaves[i % slaves_count],
174 INFO0("Hey ?! What's up ? ");
175 xbt_assert0(0,"Unexpected behavior");
179 INFO0("I'm done. See you!");
181 } /* end_of_forwarder */
184 MSG_error_t test_all(const char *platform_file,
185 const char *application_file)
187 MSG_error_t res = MSG_OK;
189 /* MSG_config("surf_workstation_model","KCCFLN05"); */
190 { /* Simulation setting */
191 MSG_set_channel_number(MAX_CHANNEL);
192 MSG_paje_output("msg_test.trace");
193 MSG_create_environment(platform_file);
195 { /* Application deployment */
196 MSG_function_register("master", master);
197 MSG_function_register("slave", slave);
198 MSG_function_register("forwarder", forwarder);
199 MSG_launch_application(application_file);
203 INFO1("Simulation time %g",MSG_get_clock());
205 } /* end_of_test_all */
209 int main(int argc, char *argv[])
211 MSG_error_t res = MSG_OK;
213 MSG_global_init(&argc,argv);
215 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
216 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
219 res = test_all(argv[1],argv[2]);