1 /* Copyright (c) 2007-2015. 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. */
7 #include "simgrid/msg.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11 /** @addtogroup MSG_examples
13 * - <b>masterslave/masterslave_forwarder.c: Master/slaves example</b>. This good old example is also very simple. Its
14 * basic version is fully commented on this page: \ref MSG_ex_master_slave, but several variants can be found in the
18 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
20 /** Emitter function */
21 static int master(int argc, char *argv[])
24 msg_host_t *slaves = NULL;
25 msg_task_t *todo = NULL;
26 int number_of_tasks = 0;
27 double task_comp_size = 0;
28 double task_comm_size = 0;
32 XBT_ATTRIB_UNUSED int res = sscanf(argv[1], "%d", &number_of_tasks);
33 xbt_assert(res,"Invalid argument %s\n", argv[1]);
34 res = sscanf(argv[2], "%lg", &task_comp_size);
35 xbt_assert(res, "Invalid argument %s\n", argv[2]);
36 res = sscanf(argv[3], "%lg", &task_comm_size);
37 xbt_assert(res, "Invalid argument %s\n", argv[3]);
40 char sprintf_buffer[64];
42 todo = xbt_new0(msg_task_t, number_of_tasks);
44 for (i = 0; i < number_of_tasks; i++) {
45 sprintf(sprintf_buffer, "Task_%d", i);
46 todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
50 { /* Process organization */
51 slaves_count = argc - 4;
52 slaves = xbt_new0(msg_host_t, slaves_count);
54 for (i = 4; i < argc; i++) {
55 slaves[i - 4] = MSG_host_by_name(argv[i]);
56 xbt_assert(slaves[i - 4] != NULL, "Unknown host %s. Stopping Now! ", argv[i]);
60 XBT_INFO("Got %d slaves and %d tasks to process", slaves_count, number_of_tasks);
61 for (i = 0; i < slaves_count; i++)
62 XBT_DEBUG("%s", MSG_host_get_name(slaves[i]));
64 for (i = 0; i < number_of_tasks; i++) {
65 XBT_INFO("Sending \"%s\" to \"%s\"", todo[i]->name, MSG_host_get_name(slaves[i % slaves_count]));
66 if (MSG_host_self() == slaves[i % slaves_count]) {
67 XBT_INFO("Hey ! It's me ! :)");
70 MSG_task_send(todo[i], MSG_host_get_name(slaves[i % slaves_count]));
74 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
75 for (i = 0; i < slaves_count; i++) {
76 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
77 MSG_task_send(finalize, MSG_host_get_name(slaves[i]));
80 XBT_INFO("Goodbye now!");
86 static int slave(int argc, char *argv[])
88 msg_task_t task = NULL;
89 XBT_ATTRIB_UNUSED int res;
91 res = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
92 xbt_assert(res == MSG_OK, "MSG_task_get failed");
94 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
95 if (!strcmp(MSG_task_get_name(task), "finalize")) {
96 MSG_task_destroy(task);
100 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
101 MSG_task_execute(task);
102 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
103 MSG_task_destroy(task);
106 XBT_INFO("I'm done. See you!");
110 static int forwarder(int argc, char *argv[])
116 { /* Process organization */
117 slaves_count = argc - 1;
118 slaves = xbt_new0(msg_host_t, slaves_count);
120 for (i = 1; i < argc; i++) {
121 slaves[i - 1] = MSG_host_by_name(argv[i]);
122 if (slaves[i - 1] == NULL) {
123 XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
131 msg_task_t task = NULL;
133 a = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
135 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
136 if (MSG_task_get_data(task) == FINALIZE) {
137 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
138 for (i = 0; i < slaves_count; i++)
139 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE), MSG_host_get_name(slaves[i]));
140 MSG_task_destroy(task);
143 XBT_INFO("Sending \"%s\" to \"%s\"", MSG_task_get_name(task), MSG_host_get_name(slaves[i % slaves_count]));
144 MSG_task_send(task, MSG_host_get_name(slaves[i % slaves_count]));
147 XBT_INFO("Hey ?! What's up ? ");
148 xbt_die("Unexpected behavior");
153 XBT_INFO("I'm done. See you!");
157 int main(int argc, char *argv[])
159 msg_error_t res = MSG_OK;
161 MSG_init(&argc, argv);
162 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
163 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
165 MSG_create_environment(argv[1]);
167 MSG_function_register("master", master);
168 MSG_function_register("slave", slave);
169 MSG_function_register("forwarder", forwarder);
170 MSG_launch_application(argv[2]);
174 XBT_INFO("Simulation time %g", MSG_get_clock());
176 return res != MSG_OK;