1 /* Copyright (c) 2007-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 "simgrid/msg.h" /* Yeah! If you want to use msg, you need to include simgrid/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 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 /** Emitter function */
25 int master(int argc, char *argv[])
28 msg_host_t *slaves = NULL;
29 int number_of_tasks = 0;
30 double task_comp_size = 0;
31 double task_comm_size = 0;
33 _XBT_GNUC_UNUSED int read;
35 read = sscanf(argv[1], "%d", &number_of_tasks);
36 xbt_assert(read, "Invalid argument %s\n", argv[1]);
37 read = sscanf(argv[2], "%lg", &task_comp_size);
38 xbt_assert(read, "Invalid argument %s\n", argv[2]);
39 read = sscanf(argv[3], "%lg", &task_comm_size);
40 xbt_assert(read, "Invalid argument %s\n", argv[3]);
42 { /* Process organisation */
43 slaves_count = argc - 4;
44 slaves = xbt_new0(msg_host_t, slaves_count);
46 for (i = 4; i < argc; i++) {
47 slaves[i - 4] = MSG_get_host_by_name(argv[i]);
48 if (slaves[i - 4] == NULL) {
49 XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
55 XBT_INFO("Got %d slave(s) :", slaves_count);
56 for (i = 0; i < slaves_count; i++)
57 XBT_INFO("%s", MSG_host_get_name(slaves[i]));
59 XBT_INFO("Got %d task to process :", number_of_tasks);
61 for (i = 0; i < number_of_tasks; i++) {
62 msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size,
65 *((double *) task->data) = MSG_get_clock();
67 a = MSG_task_send_with_timeout(task,MSG_host_get_name(slaves[i % slaves_count]),10.0);
70 XBT_INFO("Send completed");
71 } else if (a == MSG_HOST_FAILURE) {
73 ("Gloups. The cpu on which I'm running just turned off!. See you!");
75 MSG_task_destroy(task);
78 } else if (a == MSG_TRANSFER_FAILURE) {
80 ("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!",
81 MSG_host_get_name(slaves[i % slaves_count]));
83 MSG_task_destroy(task);
84 } else if (a == MSG_TIMEOUT) {
86 ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
87 MSG_host_get_name(slaves[i % slaves_count]));
89 MSG_task_destroy(task);
91 XBT_INFO("Hey ?! What's up ? ");
92 xbt_die( "Unexpected behavior");
97 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
98 for (i = 0; i < slaves_count; i++) {
99 msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
100 int a = MSG_task_send_with_timeout(task,MSG_host_get_name(slaves[i]),1.0);
103 if (a == MSG_HOST_FAILURE) {
105 ("Gloups. The cpu on which I'm running just turned off!. See you!");
106 MSG_task_destroy(task);
109 } else if (a == MSG_TRANSFER_FAILURE) {
110 XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!",
111 MSG_host_get_name(slaves[i]));
112 MSG_task_destroy(task);
113 } else if (a == MSG_TIMEOUT) {
115 ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
116 MSG_host_get_name(slaves[i % slaves_count]));
117 MSG_task_destroy(task);
119 XBT_INFO("Hey ?! What's up ? ");
120 xbt_die("Unexpected behavior with '%s': %d", MSG_host_get_name(slaves[i]), a);
124 XBT_INFO("Goodbye now!");
127 } /* end_of_master */
129 /** Receiver function */
130 int slave(int argc, char *argv[])
133 msg_task_t task = NULL;
137 time1 = MSG_get_clock();
138 a = MSG_task_receive( &(task), MSG_host_get_name(MSG_host_self()) );
139 time2 = MSG_get_clock();
141 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
142 if (MSG_task_get_data(task) == FINALIZE) {
143 MSG_task_destroy(task);
146 if (time1 < *((double *) task->data))
147 time1 = *((double *) task->data);
148 XBT_INFO("Communication time : \"%f\"", time2 - time1);
149 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
150 a = MSG_task_execute(task);
152 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
154 MSG_task_destroy(task);
155 } else if (a == MSG_HOST_FAILURE) {
157 ("Gloups. The cpu on which I'm running just turned off!. See you!");
159 MSG_task_destroy(task);
162 XBT_INFO("Hey ?! What's up ? ");
163 xbt_die("Unexpected behavior");
165 } else if (a == MSG_HOST_FAILURE) {
167 ("Gloups. The cpu on which I'm running just turned off!. See you!");
169 } else if (a == MSG_TRANSFER_FAILURE) {
170 XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
172 XBT_INFO("Hey ?! What's up ? ");
173 xbt_die("Unexpected behavior");
176 XBT_INFO("I'm done. See you!");
181 msg_error_t test_all(const char *platform_file,
182 const char *application_file)
184 msg_error_t res = MSG_OK;
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]);