1 /* Copyright (c) 2007-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 int master(int argc, char *argv[])
16 msg_host_t *slaves = NULL;
17 int number_of_tasks = 0;
18 double task_comp_size = 0;
19 double task_comm_size = 0;
21 XBT_ATTRIB_UNUSED int read;
23 read = sscanf(argv[1], "%d", &number_of_tasks);
24 xbt_assert(read, "Invalid argument %s\n", argv[1]);
25 read = sscanf(argv[2], "%lg", &task_comp_size);
26 xbt_assert(read, "Invalid argument %s\n", argv[2]);
27 read = sscanf(argv[3], "%lg", &task_comm_size);
28 xbt_assert(read, "Invalid argument %s\n", argv[3]);
30 { /* Process organization */
31 slaves_count = argc - 4;
32 slaves = xbt_new0(msg_host_t, slaves_count);
34 for (i = 4; i < argc; i++) {
35 slaves[i - 4] = MSG_host_by_name(argv[i]);
36 if (slaves[i - 4] == NULL) {
37 XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
43 XBT_INFO("Got %d slave(s) :", slaves_count);
44 for (i = 0; i < slaves_count; i++)
45 XBT_INFO("%s", MSG_host_get_name(slaves[i]));
47 XBT_INFO("Got %d task to process :", number_of_tasks);
49 for (i = 0; i < number_of_tasks; i++) {
50 msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size, xbt_new0(double, 1));
51 *((double *) task->data) = MSG_get_clock();
53 msg_error_t a = MSG_task_send_with_timeout(task,MSG_host_get_name(slaves[i % slaves_count]),10.0);
56 XBT_INFO("Send completed");
57 } else if (a == MSG_HOST_FAILURE) {
58 XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
60 MSG_task_destroy(task);
63 } else if (a == MSG_TRANSFER_FAILURE) {
64 XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!",
65 MSG_host_get_name(slaves[i % slaves_count]));
67 MSG_task_destroy(task);
68 } else if (a == MSG_TIMEOUT) {
69 XBT_INFO ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
70 MSG_host_get_name(slaves[i % slaves_count]));
72 MSG_task_destroy(task);
74 XBT_INFO("Hey ?! What's up ? ");
75 xbt_die( "Unexpected behavior");
79 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
80 for (i = 0; i < slaves_count; i++) {
81 msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
82 int a = MSG_task_send_with_timeout(task,MSG_host_get_name(slaves[i]),1.0);
85 if (a == MSG_HOST_FAILURE) {
86 XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
87 MSG_task_destroy(task);
90 } else if (a == MSG_TRANSFER_FAILURE) {
91 XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!", MSG_host_get_name(slaves[i]));
92 MSG_task_destroy(task);
93 } else if (a == MSG_TIMEOUT) {
94 XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
95 MSG_host_get_name(slaves[i % slaves_count]));
96 MSG_task_destroy(task);
98 XBT_INFO("Hey ?! What's up ? ");
99 xbt_die("Unexpected behavior with '%s': %d", MSG_host_get_name(slaves[i]), a);
103 XBT_INFO("Goodbye now!");
108 static int slave(int argc, char *argv[])
111 msg_task_t task = NULL;
115 time1 = MSG_get_clock();
116 a = MSG_task_receive( &(task), MSG_host_get_name(MSG_host_self()) );
117 time2 = MSG_get_clock();
119 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
120 if (MSG_task_get_data(task) == FINALIZE) {
121 MSG_task_destroy(task);
124 if (time1 < *((double *) task->data))
125 time1 = *((double *) task->data);
126 XBT_INFO("Communication time : \"%f\"", time2 - time1);
127 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
128 a = MSG_task_execute(task);
130 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
132 MSG_task_destroy(task);
133 } else if (a == MSG_HOST_FAILURE) {
134 XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
136 MSG_task_destroy(task);
139 XBT_INFO("Hey ?! What's up ? ");
140 xbt_die("Unexpected behavior");
142 } else if (a == MSG_HOST_FAILURE) {
143 XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
145 } else if (a == MSG_TRANSFER_FAILURE) {
146 XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
148 XBT_INFO("Hey ?! What's up ? ");
149 xbt_die("Unexpected behavior");
152 XBT_INFO("I'm done. See you!");
156 int main(int argc, char *argv[])
158 msg_error_t res = MSG_OK;
160 MSG_init(&argc, argv);
161 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
162 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
164 MSG_create_environment(argv[1]);
166 MSG_function_register("master", master);
167 MSG_function_register("slave", slave);
168 MSG_launch_application(argv[2]);
172 XBT_INFO("Simulation time %g", MSG_get_clock());
174 return res != MSG_OK;