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;
43 xbt_assert1(sscanf(argv[1], "%d", &number_of_tasks),
44 "Invalid argument %s\n", argv[1]);
45 xbt_assert1(sscanf(argv[2], "%lg", &task_comp_size),
46 "Invalid argument %s\n", argv[2]);
47 xbt_assert1(sscanf(argv[3], "%lg", &task_comm_size),
48 "Invalid argument %s\n", argv[3]);
51 char sprintf_buffer[64];
53 todo = xbt_new0(m_task_t, number_of_tasks);
55 for (i = 0; i < number_of_tasks; i++) {
56 sprintf(sprintf_buffer, "Task_%d", i);
58 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
62 { /* Process organisation */
63 slaves_count = argc - 4;
64 slaves = xbt_new0(m_host_t, slaves_count);
66 for (i = 4; i < argc; i++) {
67 slaves[i - 4] = MSG_get_host_by_name(argv[i]);
68 xbt_assert1(slaves[i - 4] != NULL, "Unknown host %s. Stopping Now! ",
73 INFO2("Got %d slaves and %d tasks to process", slaves_count,
75 for (i = 0; i < slaves_count; i++)
76 DEBUG1("%s", slaves[i]->name);
78 for (i = 0; i < number_of_tasks; i++) {
79 INFO2("Sending \"%s\" to \"%s\"",
80 todo[i]->name, slaves[i % slaves_count]->name);
81 if (MSG_host_self() == slaves[i % slaves_count]) {
82 INFO0("Hey ! It's me ! :)");
85 MSG_task_put(todo[i], slaves[i % slaves_count], PORT_22);
90 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
91 for (i = 0; i < slaves_count; i++)
92 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
95 INFO0("Goodbye now!");
101 /** Receiver function */
102 int slave(int argc, char *argv[])
104 m_task_t task = NULL;
107 res = MSG_task_get(&(task), PORT_22);
108 xbt_assert0(res == MSG_OK, "MSG_task_get failed");
110 INFO1("Received \"%s\"", MSG_task_get_name(task));
111 if (!strcmp(MSG_task_get_name(task), "finalize")) {
112 MSG_task_destroy(task);
116 INFO1("Processing \"%s\"", MSG_task_get_name(task));
117 MSG_task_execute(task);
118 INFO1("\"%s\" done", MSG_task_get_name(task));
119 MSG_task_destroy(task);
122 INFO0("I'm done. See you!");
126 /** Forwarder function */
127 int forwarder(int argc, char *argv[])
133 { /* Process organisation */
134 slaves_count = argc - 1;
135 slaves = xbt_new0(m_host_t, slaves_count);
137 for (i = 1; i < argc; i++) {
138 slaves[i - 1] = MSG_get_host_by_name(argv[i]);
139 if (slaves[i - 1] == NULL) {
140 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
148 m_task_t task = NULL;
150 a = MSG_task_get(&(task), PORT_22);
152 INFO1("Received \"%s\"", MSG_task_get_name(task));
153 if (MSG_task_get_data(task) == FINALIZE) {
155 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
156 for (i = 0; i < slaves_count; i++)
157 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
159 MSG_task_destroy(task);
162 INFO2("Sending \"%s\" to \"%s\"",
163 MSG_task_get_name(task), slaves[i % slaves_count]->name);
164 MSG_task_put(task, slaves[i % slaves_count], PORT_22);
167 INFO0("Hey ?! What's up ? ");
168 xbt_assert0(0, "Unexpected behavior");
173 INFO0("I'm done. See you!");
175 } /* end_of_forwarder */
178 MSG_error_t test_all(const char *platform_file, const char *application_file)
180 MSG_error_t res = MSG_OK;
182 /* MSG_config("workstation/model","KCCFLN05"); */
183 { /* Simulation setting */
184 MSG_set_channel_number(MAX_CHANNEL);
185 MSG_create_environment(platform_file);
187 { /* Application deployment */
188 MSG_function_register("master", master);
189 MSG_function_register("slave", slave);
190 MSG_function_register("forwarder", forwarder);
191 MSG_launch_application(application_file);
195 INFO1("Simulation time %g", MSG_get_clock());
197 } /* end_of_test_all */
201 int main(int argc, char *argv[])
203 MSG_error_t res = MSG_OK;
205 MSG_global_init(&argc, argv);
207 printf("Usage: %s platform_file deployment_file\n", argv[0]);
208 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
211 res = test_all(argv[1], argv[2]);