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 _XBT_GNUC_UNUSED int res = sscanf(argv[1], "%lg", &timeout);
48 xbt_assert(res,"Invalid argument %s\n", argv[1]);
49 res = sscanf(argv[2], "%lg", &task_comp_size);
50 xbt_assert(res, "Invalid argument %s\n", argv[2]);
51 res = sscanf(argv[3], "%lg", &task_comm_size);
52 xbt_assert(res, "Invalid argument %s\n", argv[3]);
54 { /* Process organisation */
55 workers_count = MSG_get_host_number();
56 workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
58 for (i = 0; i < workers_count; i++)
59 if(host_self == workers[i]) {
60 workers[i] = workers[workers_count-1];
65 for (i = 0; i < workers_count; i++)
66 MSG_process_create("worker", worker, master_name, workers[i]);
69 XBT_INFO("Got %d workers and will send tasks for %g seconds!",
70 workers_count, timeout);
73 char sprintf_buffer[64];
74 msg_task_t task = NULL;
76 if(MSG_get_clock()>timeout) break;
78 sprintf(sprintf_buffer, "Task_%d", i);
79 task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
82 build_channel_name(channel,master_name,
83 MSG_host_get_name(workers[i % workers_count]));
85 XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
86 MSG_task_send(task, channel);
93 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
94 for (i = 0; i < workers_count; i++) {
95 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
96 MSG_task_send(finalize, build_channel_name(channel,master_name,
97 MSG_host_get_name(workers[i % workers_count])));
100 XBT_INFO("Sent %d tasks in total!", task_num);
104 } /* end_of_master */
106 /** Receiver function */
107 int worker(int argc, char *argv[])
109 msg_task_t task = NULL;
110 _XBT_GNUC_UNUSED int res;
113 build_channel_name(channel,MSG_process_get_data(MSG_process_self()),
114 MSG_host_get_name(MSG_host_self()));
116 XBT_DEBUG("Receiving on channel \"%s\"", channel);
119 res = MSG_task_receive(&(task),channel);
120 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
122 XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
123 if (!strcmp(MSG_task_get_name(task), "finalize")) {
124 MSG_task_destroy(task);
128 XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
129 MSG_task_execute(task);
130 XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
131 MSG_task_destroy(task);
134 XBT_DEBUG("I'm done. See you!");
136 } /* end_of_worker */
139 msg_error_t test_all(const char *platform_file,
140 const char *application_file)
142 msg_error_t res = MSG_OK;
144 { /* Simulation setting */
145 MSG_create_environment(platform_file);
147 { /* Application deployment */
148 MSG_function_register("master", master);
149 MSG_function_register("worker", worker);
150 MSG_launch_application(application_file);
154 XBT_INFO("Simulation time %g", MSG_get_clock());
156 } /* end_of_test_all */
160 int main(int argc, char *argv[])
162 msg_error_t res = MSG_OK;
164 MSG_init(&argc, argv);
166 printf("Usage: %s platform_file deployment_file\n", argv[0]);
167 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
170 res = test_all(argv[1], argv[2]);