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 XBT_ATTRIB_UNUSED int res;
29 msg_host_t *workers = NULL;
30 msg_task_t *todo = NULL;
31 msg_host_t host_self = MSG_host_self();
32 char *master_name = (char *) MSG_host_get_name(host_self);
37 TRACE_category(master_name);
39 double timeout = xbt_str_parse_double(argv[1], "Invalid timeout: %s"); /** - timeout */
40 double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
41 double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
43 { /* Process organization */
44 workers_count = MSG_get_host_number();
45 workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
47 for (i = 0; i < workers_count; i++)
48 if(host_self == workers[i]) {
49 workers[i] = workers[workers_count-1];
54 for (i = 0; i < workers_count; i++)
55 MSG_process_create("worker", worker, master_name, workers[i]);
58 XBT_INFO("Got %d workers and will send tasks for %g seconds!", workers_count, timeout);
59 xbt_dynar_t idle_hosts = xbt_dynar_new(sizeof(msg_host_t), NULL);
60 msg_host_t request_host = NULL;
63 char sprintf_buffer[64];
64 msg_task_t task = NULL;
66 msg_task_t request = NULL;
67 while(MSG_task_listen(master_name)) {
68 res = MSG_task_receive(&(request),master_name);
69 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
70 request_host = MSG_task_get_data(request);
71 xbt_dynar_push(idle_hosts, &request_host);
72 MSG_task_destroy(request);
76 if(MSG_get_clock()>timeout) {
77 if(xbt_dynar_length(idle_hosts) == workers_count) break;
79 MSG_process_sleep(.1);
84 if(xbt_dynar_length(idle_hosts)<=0) {
85 /* No request. Let's wait... */
86 MSG_process_sleep(.1);
90 sprintf(sprintf_buffer, "Task_%d", i);
91 task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
92 MSG_task_set_category(task, master_name);
94 xbt_dynar_shift(idle_hosts, &request_host);
96 build_channel_name(channel,master_name, MSG_host_get_name(request_host));
98 XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
99 MSG_task_send(task, channel);
106 XBT_DEBUG ("All tasks have been dispatched. Let's tell everybody the computation is over.");
107 for (i = 0; i < workers_count; i++) {
108 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
109 MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])));
112 XBT_INFO("Sent %d tasks in total!", task_num);
116 } /* end_of_master */
118 /** Worker function */
119 static int worker(int argc, char *argv[])
121 msg_task_t task = NULL;
122 XBT_ATTRIB_UNUSED int res;
125 const char *my_master = MSG_process_get_data(MSG_process_self());
126 build_channel_name(channel, my_master, MSG_host_get_name(MSG_host_self()));
128 XBT_DEBUG("Receiving on channel \"%s\"", channel);
132 msg_task_t request = MSG_task_create("request", 0, 0, MSG_host_self());
133 MSG_task_send(request, my_master);
135 res = MSG_task_receive(&(task),channel);
136 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
138 XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
139 if (!strcmp(MSG_task_get_name(task), "finalize")) {
140 MSG_task_destroy(task);
144 XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
145 MSG_task_execute(task);
146 XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
147 MSG_task_destroy(task);
150 XBT_DEBUG("I'm done. See you!");
152 } /* end_of_worker */
155 int main(int argc, char *argv[])
157 msg_error_t res = MSG_OK;
159 MSG_init(&argc, argv);
160 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
161 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
162 { /* Simulation setting */
163 MSG_create_environment(argv[1]);
165 { /* Application deployment */
166 MSG_function_register("master", master);
167 MSG_function_register("worker", worker);
168 MSG_launch_application(argv[2]);
172 XBT_INFO("Simulation time %g", MSG_get_clock());
173 return (res != MSG_OK);