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 = xbt_new0(m_task_t,number_of_tasks);
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 = xbt_new0(m_host_t,slaves_count);
64 for (i = 4; i < argc; i++) {
65 slaves[i-4] = MSG_get_host_by_name(argv[i]);
66 xbt_assert1(slaves[i-4]!=NULL, "Unknown host %s. Stopping Now! ", argv[i]);
70 INFO2("Got %d slaves and %d tasks to process", slaves_count,number_of_tasks);
71 for (i = 0; i < slaves_count; i++)
72 DEBUG1("%s", slaves[i]->name);
74 for (i = 0; i < number_of_tasks; i++) {
75 INFO2("Sending \"%s\" to \"%s\"",
77 slaves[i % slaves_count]->name);
78 if(MSG_host_self()==slaves[i % slaves_count]) {
79 INFO0("Hey ! It's me ! :)");
82 MSG_task_put(todo[i], slaves[i % slaves_count],
87 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
88 for (i = 0; i < slaves_count; i++)
89 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
92 INFO0("Goodbye now!");
98 /** Receiver function */
99 int slave(int argc, char *argv[])
101 m_task_t task = NULL;
104 res = MSG_task_get(&(task), PORT_22);
105 xbt_assert0(res == MSG_OK, "MSG_task_get failed");
107 INFO1("Received \"%s\"", MSG_task_get_name(task));
108 if (!strcmp(MSG_task_get_name(task),"finalize")) {
109 MSG_task_destroy(task);
113 INFO1("Processing \"%s\"", MSG_task_get_name(task));
114 MSG_task_execute(task);
115 INFO1("\"%s\" done", MSG_task_get_name(task));
116 MSG_task_destroy(task);
119 INFO0("I'm done. See you!");
123 /** Forwarder function */
124 int forwarder(int argc, char *argv[])
130 { /* Process organisation */
131 slaves_count = argc - 1;
132 slaves = xbt_new0(m_host_t,slaves_count);
134 for (i = 1; i < argc; i++) {
135 slaves[i-1] = MSG_get_host_by_name(argv[i]);
136 if(slaves[i-1]==NULL) {
137 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
145 m_task_t task = NULL;
147 a = MSG_task_get(&(task), PORT_22);
149 INFO1("Received \"%s\"", MSG_task_get_name(task));
150 if(MSG_task_get_data(task)==FINALIZE) {
151 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
152 for (i = 0; i < slaves_count; i++)
153 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
155 MSG_task_destroy(task);
158 INFO2("Sending \"%s\" to \"%s\"",
159 MSG_task_get_name(task),
160 slaves[i% slaves_count]->name);
161 MSG_task_put(task, slaves[i % slaves_count],
165 INFO0("Hey ?! What's up ? ");
166 xbt_assert0(0,"Unexpected behavior");
170 INFO0("I'm done. See you!");
172 } /* end_of_forwarder */
175 MSG_error_t test_all(const char *platform_file,
176 const char *application_file)
178 MSG_error_t res = MSG_OK;
180 /* MSG_config("surf_workstation_model","KCCFLN05"); */
181 { /* Simulation setting */
182 MSG_set_channel_number(MAX_CHANNEL);
183 MSG_paje_output("msg_test.trace");
184 MSG_create_environment(platform_file);
186 { /* Application deployment */
187 MSG_function_register("master", master);
188 MSG_function_register("slave", slave);
189 MSG_function_register("forwarder", forwarder);
190 MSG_launch_application(application_file);
194 INFO1("Simulation time %g",MSG_get_clock());
196 } /* end_of_test_all */
200 int main(int argc, char *argv[])
202 MSG_error_t res = MSG_OK;
204 MSG_global_init(&argc,argv);
206 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
207 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
210 res = test_all(argv[1],argv[2]);