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 */
10 /* Create a log channel to have nice outputs. */
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
14 int master(int argc, char *argv[]);
15 int slave(int argc, char *argv[]);
16 int forwarder(int argc, char *argv[]);
17 MSG_error_t test_all(const char *platform_file, const char *application_file);
24 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
26 /** Emitter function */
27 int master(int argc, char *argv[])
30 m_host_t *slaves = NULL;
31 m_task_t *todo = NULL;
32 int number_of_tasks = 0;
33 double task_comp_size = 0;
34 double task_comm_size = 0;
39 xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
40 "Invalid argument %s\n",argv[1]);
41 xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
42 "Invalid argument %s\n",argv[2]);
43 xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
44 "Invalid argument %s\n",argv[3]);
47 char sprintf_buffer[64];
49 todo = calloc(number_of_tasks, sizeof(m_task_t));
51 for (i = 0; i < number_of_tasks; i++) {
52 sprintf(sprintf_buffer, "Task_%d", i);
53 todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
57 { /* Process organisation */
58 slaves_count = argc - 4;
59 slaves = calloc(slaves_count, sizeof(m_host_t));
61 for (i = 4; i < argc; i++) {
62 slaves[i-4] = MSG_get_host_by_name(argv[i]);
63 if(slaves[i-4]==NULL) {
64 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
70 INFO1("Got %d slave(s) :", slaves_count);
71 for (i = 0; i < slaves_count; i++)
72 INFO1("\t %s", slaves[i]->name);
74 INFO1("Got %d task to process :", number_of_tasks);
76 for (i = 0; i < number_of_tasks; i++)
77 INFO1("\t\"%s\"", todo[i]->name);
79 for (i = 0; i < number_of_tasks; i++) {
80 INFO2("Sending \"%s\" to \"%s\"",
82 slaves[i % slaves_count]->name);
83 if(MSG_host_self()==slaves[i % slaves_count]) {
84 INFO0("Hey ! It's me ! :)");
86 MSG_task_put(todo[i], slaves[i % slaves_count],
88 INFO0("Send completed");
91 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
92 for (i = 0; i < slaves_count; i++)
93 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
96 INFO0("Goodbye now!");
100 } /* end_of_master */
102 /** Receiver function */
103 int slave(int argc, char *argv[])
106 m_task_t task = NULL;
108 a = MSG_task_get(&(task), PORT_22);
110 INFO1("Received \"%s\" ", MSG_task_get_name(task));
111 if(MSG_task_get_data(task)==FINALIZE) {
112 MSG_task_destroy(task);
115 INFO1("Processing \"%s\" ", MSG_task_get_name(task));
116 MSG_task_execute(task);
117 INFO1("\"%s\" done ", MSG_task_get_name(task));
118 MSG_task_destroy(task);
120 INFO0("Hey ?! What's up ? ");
121 xbt_assert0(0,"Unexpected behavior");
124 INFO0("I'm done. See you!");
128 /** Receiver function */
129 int forwarder(int argc, char *argv[])
132 int slaves_count = argc - 1;
133 m_host_t *slaves = calloc(slaves_count, sizeof(m_host_t));
135 { /* Process organisation */
136 slaves_count = argc - 1;
137 slaves = calloc(slaves_count, sizeof(m_host_t));
139 for (i = 1; i < argc; i++) {
140 slaves[i-1] = MSG_get_host_by_name(argv[i]);
141 if(slaves[i-1]==NULL) {
142 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
150 m_task_t task = NULL;
152 a = MSG_task_get(&(task), PORT_22);
154 INFO1("Received \"%s\" ", MSG_task_get_name(task));
155 if(MSG_task_get_data(task)==FINALIZE) {
156 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
157 for (i = 0; i < slaves_count; i++)
158 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
160 MSG_task_destroy(task);
163 INFO2("Sending \"%s\" to \"%s\"",
164 MSG_task_get_name(task),
165 slaves[i% slaves_count]->name);
166 MSG_task_put(task, slaves[i % slaves_count],
169 INFO0("Hey ?! What's up ? ");
170 xbt_assert0(0,"Unexpected behavior");
174 INFO0("I'm done. See you!");
176 } /* end_of_forwarder */
180 MSG_error_t test_all(const char *platform_file,
181 const char *application_file)
183 MSG_error_t res = MSG_OK;
185 /* MSG_config("surf_workstation_model","KCCFLN05"); */
186 { /* Simulation setting */
187 MSG_set_channel_number(MAX_CHANNEL);
188 MSG_paje_output("msg_test.trace");
189 MSG_create_environment(platform_file);
191 { /* Application deployment */
192 MSG_function_register("master", master);
193 MSG_function_register("slave", slave);
194 MSG_function_register("forwarder", forwarder);
195 MSG_launch_application(application_file);
199 INFO1("Simulation time %g",MSG_get_clock());
201 } /* end_of_test_all */
205 int main(int argc, char *argv[])
207 MSG_error_t res = MSG_OK;
209 MSG_global_init(&argc,argv);
211 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
212 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
215 res = test_all(argv[1],argv[2]);
218 if(res==MSG_OK) return 0;