1 /* Copyright (c) 2007-2010, 2013-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 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
13 static char * build_channel_name(char *buffer, const char *sender, const char* receiver)
15 strcpy(buffer, sender);
17 strcat(buffer, receiver);
21 /* forward definitions */
22 static int master(int argc, char* argv[]);
23 static int worker(int argc, char* argv[]);
25 static int master(int argc, char *argv[])
27 int workers_count = 0;
28 msg_host_t *workers = NULL;
29 msg_task_t *todo = NULL;
30 msg_host_t host_self = MSG_host_self();
31 char *master_name = (char *) MSG_host_get_name(host_self);
34 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s"); /** - Number of tasks */
35 double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
36 double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
39 char sprintf_buffer[64];
41 todo = xbt_new0(msg_task_t, number_of_tasks);
43 for (int i = 0; i < number_of_tasks; i++) {
44 sprintf(sprintf_buffer, "Task_%d", i);
45 todo[i] = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
49 { /* Process organization */
50 workers_count = MSG_get_host_number();
51 workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
53 for (int i = 0; i < workers_count; i++)
54 if(host_self == workers[i]) {
55 workers[i] = workers[workers_count-1];
60 for (int i = 0; i < workers_count; i++)
61 MSG_process_create("worker", worker, master_name, workers[i]);
64 XBT_INFO("Got %d workers and %Ld tasks to process", workers_count, number_of_tasks);
66 for (int i = 0; i < number_of_tasks; i++) {
67 build_channel_name(channel, master_name, MSG_host_get_name(workers[i % workers_count]));
69 XBT_INFO("Sending \"%s\" to channel \"%s\"", todo[i]->name, channel);
71 MSG_task_send(todo[i], channel);
75 XBT_INFO ("All tasks have been dispatched. Let's tell everybody the computation is over.");
76 for (int i = 0; i < workers_count; i++) {
77 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
78 MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])));
81 XBT_INFO("Goodbye now!");
87 /** Receiver function */
88 static int worker(int argc, char *argv[])
90 msg_task_t task = NULL;
93 build_channel_name(channel, MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
95 XBT_INFO("Receiving on channel \"%s\"", channel);
98 int res = MSG_task_receive(&(task), channel);
99 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
101 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
102 if (!strcmp(MSG_task_get_name(task), "finalize")) {
103 MSG_task_destroy(task);
107 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
108 MSG_task_execute(task);
109 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
110 MSG_task_destroy(task);
113 XBT_INFO("I'm done. See you!");
115 } /* end_of_worker */
118 int main(int argc, char *argv[])
121 MSG_init(&argc, argv);
122 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
123 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
124 { /* Simulation setting */
125 MSG_create_environment(argv[1]);
127 { /* Application deployment */
128 MSG_function_register("master", master);
129 MSG_function_register("worker", worker);
130 MSG_launch_application(argv[2]);
132 msg_error_t res = MSG_main();
134 XBT_INFO("Simulation time %g", MSG_get_clock());
135 return (res != MSG_OK);