1 /* Copyright (c) 2007-2010, 2013-2014. 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. */
8 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
17 int master(int argc, char *argv[]);
18 int worker(int argc, char *argv[]);
19 msg_error_t test_all(const char *platform_file,
20 const char *application_file);
22 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
24 static char * build_channel_name(char *buffer, const char *sender, const char* receiver)
26 strcpy(buffer, sender);
28 strcat(buffer, receiver);
32 /** Emitter function */
33 int master(int argc, char *argv[])
35 int workers_count = 0;
36 msg_host_t *workers = NULL;
37 msg_task_t *todo = NULL;
38 msg_host_t host_self = MSG_host_self();
39 char *master_name = (char *) MSG_host_get_name(host_self);
40 double task_comp_size = 0;
41 double task_comm_size = 0;
47 TRACE_category(master_name);
49 _XBT_GNUC_UNUSED int res = sscanf(argv[1], "%lg", &timeout);
50 xbt_assert(res,"Invalid argument %s\n", argv[1]);
51 res = sscanf(argv[2], "%lg", &task_comp_size);
52 xbt_assert(res, "Invalid argument %s\n", argv[2]);
53 res = sscanf(argv[3], "%lg", &task_comm_size);
54 xbt_assert(res, "Invalid argument %s\n", argv[3]);
56 { /* Process organisation */
57 workers_count = MSG_get_host_number();
58 workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
60 for (i = 0; i < workers_count; i++)
61 if(host_self == workers[i]) {
62 workers[i] = workers[workers_count-1];
67 for (i = 0; i < workers_count; i++)
68 MSG_process_create("worker", worker, master_name, workers[i]);
71 XBT_INFO("Got %d workers and will send tasks for %g seconds!",
72 workers_count, timeout);
73 xbt_dynar_t idle_hosts = xbt_dynar_new(sizeof(msg_host_t), NULL);
74 msg_host_t request_host = NULL;
77 char sprintf_buffer[64];
78 msg_task_t task = NULL;
80 msg_task_t request = NULL;
81 while(MSG_task_listen(master_name)) {
82 res = MSG_task_receive(&(request),master_name);
83 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
84 request_host = MSG_task_get_data(request);
85 xbt_dynar_push(idle_hosts, &request_host);
86 MSG_task_destroy(request);
90 if(MSG_get_clock()>timeout) {
91 if(xbt_dynar_length(idle_hosts) == workers_count) break;
93 MSG_process_sleep(.1);
98 if(xbt_dynar_length(idle_hosts)<=0) {
99 /* No request. Let's wait... */
100 MSG_process_sleep(.1);
104 sprintf(sprintf_buffer, "Task_%d", i);
105 task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
107 MSG_task_set_category(task, master_name);
109 xbt_dynar_shift(idle_hosts, &request_host);
111 build_channel_name(channel,master_name, MSG_host_get_name(request_host));
113 XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
114 MSG_task_send(task, channel);
122 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
123 for (i = 0; i < workers_count; i++) {
124 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
125 MSG_task_send(finalize, build_channel_name(channel,master_name,
126 MSG_host_get_name(workers[i % workers_count])));
129 XBT_INFO("Sent %d tasks in total!", task_num);
133 } /* end_of_master */
135 /** Receiver function */
136 int worker(int argc, char *argv[])
138 msg_task_t task = NULL;
139 _XBT_GNUC_UNUSED int res;
142 const char *my_master = MSG_process_get_data(MSG_process_self());
143 build_channel_name(channel, my_master, MSG_host_get_name(MSG_host_self()));
145 XBT_DEBUG("Receiving on channel \"%s\"", channel);
149 msg_task_t request = MSG_task_create("request", 0, 0, MSG_host_self());
150 MSG_task_send(request, my_master);
152 res = MSG_task_receive(&(task),channel);
153 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
155 XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
156 if (!strcmp(MSG_task_get_name(task), "finalize")) {
157 MSG_task_destroy(task);
161 XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
162 MSG_task_execute(task);
163 XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
164 MSG_task_destroy(task);
167 XBT_DEBUG("I'm done. See you!");
169 } /* end_of_worker */
172 msg_error_t test_all(const char *platform_file,
173 const char *application_file)
175 msg_error_t res = MSG_OK;
177 { /* Simulation setting */
178 MSG_create_environment(platform_file);
180 { /* Application deployment */
181 MSG_function_register("master", master);
182 MSG_function_register("worker", worker);
183 MSG_launch_application(application_file);
187 XBT_INFO("Simulation time %g", MSG_get_clock());
189 } /* end_of_test_all */
193 int main(int argc, char *argv[])
195 msg_error_t res = MSG_OK;
197 MSG_init(&argc, argv);
199 printf("Usage: %s platform_file deployment_file\n", argv[0]);
200 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
203 res = test_all(argv[1], argv[2]);