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 /** Worker function */
22 static int master(int argc, char *argv[])
24 int workers_count = 0;
25 XBT_ATTRIB_UNUSED int res;
26 msg_host_t *workers = NULL;
27 msg_task_t *todo = NULL;
28 msg_host_t host_self = MSG_host_self();
29 char *master_name = (char *) MSG_host_get_name(host_self);
34 TRACE_category(master_name);
36 double timeout = xbt_str_parse_double(argv[1], "Invalid timeout: %s"); /** - timeout */
37 double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
38 double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
40 { /* Process organization */
41 workers_count = MSG_get_host_number();
42 workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
44 for (i = 0; i < workers_count; i++)
45 if(host_self == workers[i]) {
46 workers[i] = workers[workers_count-1];
51 for (i = 0; i < workers_count; i++)
52 MSG_process_create("worker", worker, master_name, workers[i]);
55 XBT_INFO("Got %d workers and will send tasks for %g seconds!", workers_count, timeout);
56 xbt_dynar_t idle_hosts = xbt_dynar_new(sizeof(msg_host_t), NULL);
57 msg_host_t request_host = NULL;
60 char sprintf_buffer[64];
61 msg_task_t task = NULL;
63 msg_task_t request = NULL;
64 while(MSG_task_listen(master_name)) {
65 res = MSG_task_receive(&(request),master_name);
66 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
67 request_host = MSG_task_get_data(request);
68 xbt_dynar_push(idle_hosts, &request_host);
69 MSG_task_destroy(request);
73 if(MSG_get_clock()>timeout) {
74 if(xbt_dynar_length(idle_hosts) == workers_count) break;
76 MSG_process_sleep(.1);
81 if(xbt_dynar_length(idle_hosts)<=0) {
82 /* No request. Let's wait... */
83 MSG_process_sleep(.1);
87 sprintf(sprintf_buffer, "Task_%d", i);
88 task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
89 MSG_task_set_category(task, master_name);
91 xbt_dynar_shift(idle_hosts, &request_host);
93 build_channel_name(channel,master_name, MSG_host_get_name(request_host));
95 XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
96 MSG_task_send(task, channel);
103 XBT_DEBUG ("All tasks have been dispatched. Let's tell everybody the computation is over.");
104 for (i = 0; i < workers_count; i++) {
105 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
106 MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])));
109 XBT_INFO("Sent %d tasks in total!", task_num);
113 } /* end_of_master */
115 /** Worker function */
116 static int worker(int argc, char *argv[])
118 msg_task_t task = NULL;
119 XBT_ATTRIB_UNUSED int res;
122 const char *my_master = MSG_process_get_data(MSG_process_self());
123 build_channel_name(channel, my_master, MSG_host_get_name(MSG_host_self()));
125 XBT_DEBUG("Receiving on channel \"%s\"", channel);
129 msg_task_t request = MSG_task_create("request", 0, 0, MSG_host_self());
130 MSG_task_send(request, my_master);
132 res = MSG_task_receive(&(task),channel);
133 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
135 XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
136 if (!strcmp(MSG_task_get_name(task), "finalize")) {
137 MSG_task_destroy(task);
141 XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
142 MSG_task_execute(task);
143 XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
144 MSG_task_destroy(task);
147 XBT_DEBUG("I'm done. See you!");
149 } /* end_of_worker */
152 int main(int argc, char *argv[])
154 msg_error_t res = MSG_OK;
156 MSG_init(&argc, argv);
157 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
158 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
159 { /* Simulation setting */
160 MSG_create_environment(argv[1]);
162 { /* Application deployment */
163 MSG_function_register("master", master);
164 MSG_function_register("worker", worker);
165 MSG_launch_application(argv[2]);
169 XBT_INFO("Simulation time %g", MSG_get_clock());
170 return (res != MSG_OK);