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>masterworker/masterworker.c: Master/workers example</b>. This good old example is also very simple. Its
14 * basic version is fully commented on this page: \ref MSG_ex_master_worker, but several variants can be found in the
18 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
20 static int master(int argc, char *argv[])
22 msg_host_t *workers = NULL;
23 msg_task_t *todo = NULL;
24 long workers_count = 0;
27 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
28 double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
29 double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
32 char sprintf_buffer[64];
34 todo = xbt_new0(msg_task_t, number_of_tasks);
36 for (i = 0; i < number_of_tasks; i++) {
37 sprintf(sprintf_buffer, "Task_%d", i);
38 todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
42 { /* Process organization */
43 workers_count = argc - 4;
44 workers = xbt_new0(msg_host_t, workers_count);
46 for (i = 4; i < argc; i++) {
47 workers[i - 4] = MSG_host_by_name(argv[i]);
48 xbt_assert(workers[i - 4] != NULL, "Unknown host %s. Stopping Now! ", argv[i]);
52 XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
53 for (i = 0; i < workers_count; i++)
54 XBT_DEBUG("%s", MSG_host_get_name(workers[i]));
56 for (i = 0; i < number_of_tasks; i++) {
57 XBT_INFO("Sending \"%s\" to \"%s\"", todo[i]->name, MSG_host_get_name(workers[i % workers_count]));
58 if (MSG_host_self() == workers[i % workers_count]) {
59 XBT_INFO("Hey ! It's me ! :)");
62 MSG_task_send(todo[i], MSG_host_get_name(workers[i % workers_count]));
66 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
67 for (i = 0; i < workers_count; i++) {
68 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
69 MSG_task_send(finalize, MSG_host_get_name(workers[i]));
72 XBT_INFO("Goodbye now!");
78 static int worker(int argc, char *argv[])
80 msg_task_t task = NULL;
81 XBT_ATTRIB_UNUSED int res;
83 res = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
84 xbt_assert(res == MSG_OK, "MSG_task_get failed");
86 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
87 if (!strcmp(MSG_task_get_name(task), "finalize")) {
88 MSG_task_destroy(task);
92 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
93 MSG_task_execute(task);
94 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
95 MSG_task_destroy(task);
98 XBT_INFO("I'm done. See you!");
102 int main(int argc, char *argv[])
104 msg_error_t res = MSG_OK;
106 MSG_init(&argc, argv);
107 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
108 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
110 MSG_create_environment(argv[1]);
112 MSG_function_register("master", master);
113 MSG_function_register("worker", worker);
114 MSG_launch_application(argv[2]);
118 XBT_INFO("Simulation time %g", MSG_get_clock());
120 return res != MSG_OK;