1 /* Copyright (c) 2007, 2008, 2009, 2010. 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 slave(int argc, char *argv[]);
19 int forwarder(int argc, char *argv[]);
20 msg_error_t test_all(const char *platform_file,
21 const char *application_file);
23 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
25 /** Emitter function */
26 int master(int argc, char *argv[])
29 msg_host_t *slaves = NULL;
30 int number_of_tasks = 0;
31 double task_comp_size = 0;
32 double task_comm_size = 0;
34 _XBT_GNUC_UNUSED int read;
36 read = sscanf(argv[1], "%d", &number_of_tasks);
37 xbt_assert(read, "Invalid argument %s\n", argv[1]);
38 read = sscanf(argv[2], "%lg", &task_comp_size);
39 xbt_assert(read, "Invalid argument %s\n", argv[2]);
40 read = sscanf(argv[3], "%lg", &task_comm_size);
41 xbt_assert(read, "Invalid argument %s\n", argv[3]);
43 { /* Process organisation */
44 slaves_count = argc - 4;
45 slaves = xbt_new0(msg_host_t, slaves_count);
47 for (i = 4; i < argc; i++) {
48 slaves[i - 4] = MSG_get_host_by_name(argv[i]);
49 if (slaves[i - 4] == NULL) {
50 XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
56 XBT_INFO("Got %d slave(s) :", slaves_count);
57 for (i = 0; i < slaves_count; i++)
58 XBT_INFO("%s", MSG_host_get_name(slaves[i]));
60 XBT_INFO("Got %d task to process :", number_of_tasks);
62 for (i = 0; i < number_of_tasks; i++) {
63 msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size,
66 *((double *) task->data) = MSG_get_clock();
68 a = MSG_task_send_with_timeout(task,MSG_host_get_name(slaves[i % slaves_count]),10.0);
71 XBT_INFO("Send completed");
72 } else if (a == MSG_HOST_FAILURE) {
74 ("Gloups. The cpu on which I'm running just turned off!. See you!");
76 MSG_task_destroy(task);
79 } else if (a == MSG_TRANSFER_FAILURE) {
81 ("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!",
82 MSG_host_get_name(slaves[i % slaves_count]));
84 MSG_task_destroy(task);
85 } else if (a == MSG_TIMEOUT) {
87 ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
88 MSG_host_get_name(slaves[i % slaves_count]));
90 MSG_task_destroy(task);
92 XBT_INFO("Hey ?! What's up ? ");
93 xbt_die( "Unexpected behavior");
98 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
99 for (i = 0; i < slaves_count; i++) {
100 msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
101 int a = MSG_task_send_with_timeout(task,MSG_host_get_name(slaves[i]),1.0);
104 if (a == MSG_HOST_FAILURE) {
106 ("Gloups. The cpu on which I'm running just turned off!. See you!");
107 MSG_task_destroy(task);
110 } else if (a == MSG_TRANSFER_FAILURE) {
111 XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!",
112 MSG_host_get_name(slaves[i]));
113 MSG_task_destroy(task);
114 } else if (a == MSG_TIMEOUT) {
116 ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
117 MSG_host_get_name(slaves[i % slaves_count]));
118 MSG_task_destroy(task);
120 XBT_INFO("Hey ?! What's up ? ");
121 xbt_die("Unexpected behavior with '%s': %d", MSG_host_get_name(slaves[i]), a);
125 XBT_INFO("Goodbye now!");
128 } /* end_of_master */
130 /** Receiver function */
131 int slave(int argc, char *argv[])
134 msg_task_t task = NULL;
138 time1 = MSG_get_clock();
139 a = MSG_task_receive( &(task), MSG_host_get_name(MSG_host_self()) );
140 time2 = MSG_get_clock();
142 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
143 if (MSG_task_get_data(task) == FINALIZE) {
144 MSG_task_destroy(task);
147 if (time1 < *((double *) task->data))
148 time1 = *((double *) task->data);
149 XBT_INFO("Communication time : \"%f\"", time2 - time1);
150 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
151 a = MSG_task_execute(task);
153 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
155 MSG_task_destroy(task);
156 } else if (a == MSG_HOST_FAILURE) {
158 ("Gloups. The cpu on which I'm running just turned off!. See you!");
161 XBT_INFO("Hey ?! What's up ? ");
162 xbt_die("Unexpected behavior");
164 } else if (a == MSG_HOST_FAILURE) {
166 ("Gloups. The cpu on which I'm running just turned off!. See you!");
168 } else if (a == MSG_TRANSFER_FAILURE) {
169 XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
171 XBT_INFO("Hey ?! What's up ? ");
172 xbt_die("Unexpected behavior");
175 XBT_INFO("I'm done. See you!");
180 msg_error_t test_all(const char *platform_file,
181 const char *application_file)
183 msg_error_t res = MSG_OK;
185 /* MSG_config("workstation/model","KCCFLN05"); */
186 { /* Simulation setting */
187 MSG_create_environment(platform_file);
189 { /* Application deployment */
190 MSG_function_register("master", master);
191 MSG_function_register("slave", slave);
192 MSG_launch_application(application_file);
196 XBT_INFO("Simulation time %g", MSG_get_clock());
198 } /* end_of_test_all */
202 int main(int argc, char *argv[])
204 msg_error_t res = MSG_OK;
206 MSG_init(&argc, argv);
208 printf("Usage: %s platform_file deployment_file\n", argv[0]);
209 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
212 res = test_all(argv[1], argv[2]);