1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * 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 */
9 #include "xbt/sysdep.h" /* calloc, printf */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "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;
41 xbt_assert1(sscanf(argv[1], "%d", &number_of_tasks),
42 "Invalid argument %s\n", argv[1]);
43 xbt_assert1(sscanf(argv[2], "%lg", &task_comp_size),
44 "Invalid argument %s\n", argv[2]);
45 xbt_assert1(sscanf(argv[3], "%lg", &task_comm_size),
46 "Invalid argument %s\n", argv[3]);
49 char sprintf_buffer[64];
51 todo = xbt_new0(m_task_t, number_of_tasks);
53 for (i = 0; i < number_of_tasks; i++) {
54 sprintf(sprintf_buffer, "Task_%d", i);
56 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! ",
71 INFO2("Got %d slaves and %d tasks to process", slaves_count,
73 for (i = 0; i < slaves_count; i++)
74 DEBUG1("%s", slaves[i]->name);
76 for (i = 0; i < number_of_tasks; i++) {
77 INFO2("Sending \"%s\" to \"%s\"",
78 todo[i]->name, slaves[i % slaves_count]->name);
79 if (MSG_host_self() == slaves[i % slaves_count]) {
80 INFO0("Hey ! It's me ! :)");
83 MSG_task_put(todo[i], slaves[i % slaves_count], PORT_22);
88 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
89 for (i = 0; i < slaves_count; i++){
90 m_task_t finalize=MSG_task_create("finalize", 0, 0, FINALIZE);
91 MSG_task_put(finalize, slaves[i], PORT_22);
94 INFO0("Goodbye now!");
100 /** Receiver function */
101 int slave(int argc, char *argv[])
103 m_task_t task = NULL;
106 res = MSG_task_get(&(task), PORT_22);
107 xbt_assert0(res == MSG_OK, "MSG_task_get failed");
109 INFO1("Received \"%s\"", MSG_task_get_name(task));
110 if (!strcmp(MSG_task_get_name(task), "finalize")) {
111 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);
121 INFO0("I'm done. See you!");
125 /** Forwarder function */
126 int forwarder(int argc, char *argv[])
132 { /* Process organisation */
133 slaves_count = argc - 1;
134 slaves = xbt_new0(m_host_t, slaves_count);
136 for (i = 1; i < argc; i++) {
137 slaves[i - 1] = MSG_get_host_by_name(argv[i]);
138 if (slaves[i - 1] == NULL) {
139 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
147 m_task_t task = NULL;
149 a = MSG_task_get(&(task), PORT_22);
151 INFO1("Received \"%s\"", MSG_task_get_name(task));
152 if (MSG_task_get_data(task) == FINALIZE) {
154 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
155 for (i = 0; i < slaves_count; i++)
156 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
158 MSG_task_destroy(task);
161 INFO2("Sending \"%s\" to \"%s\"",
162 MSG_task_get_name(task), slaves[i % slaves_count]->name);
163 MSG_task_put(task, slaves[i % slaves_count], PORT_22);
166 INFO0("Hey ?! What's up ? ");
167 xbt_assert0(0, "Unexpected behavior");
172 INFO0("I'm done. See you!");
174 } /* end_of_forwarder */
177 MSG_error_t test_all(const char *platform_file, const char *application_file)
179 MSG_error_t res = MSG_OK;
181 /* MSG_config("workstation/model","KCCFLN05"); */
182 { /* Simulation setting */
183 MSG_set_channel_number(MAX_CHANNEL);
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]);