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);
28 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
30 /** Emitter function */
31 int master(int argc, char *argv[])
34 m_host_t *slaves = NULL;
35 int number_of_tasks = 0;
36 double task_comp_size = 0;
37 double task_comm_size = 0;
42 xbt_assert1(sscanf(argv[1], "%d", &number_of_tasks),
43 "Invalid argument %s\n", argv[1]);
44 xbt_assert1(sscanf(argv[2], "%lg", &task_comp_size),
45 "Invalid argument %s\n", argv[2]);
46 xbt_assert1(sscanf(argv[3], "%lg", &task_comm_size),
47 "Invalid argument %s\n", argv[3]);
49 { /* Process organisation */
50 slaves_count = argc - 4;
51 slaves = xbt_new0(m_host_t, slaves_count);
53 for (i = 4; i < argc; i++) {
54 slaves[i - 4] = MSG_get_host_by_name(argv[i]);
55 if (slaves[i - 4] == NULL) {
56 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
62 INFO1("Got %d slave(s) :", slaves_count);
63 for (i = 0; i < slaves_count; i++)
64 INFO1("%s", slaves[i]->name);
66 INFO1("Got %d task to process :", number_of_tasks);
68 for (i = 0; i < number_of_tasks; i++) {
69 m_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size,
72 *((double *) task->data) = MSG_get_clock();
74 a = MSG_task_put_with_timeout(task, slaves[i % slaves_count], PORT_22,
77 INFO0("Send completed");
78 } else if (a == MSG_HOST_FAILURE) {
80 ("Gloups. The cpu on which I'm running just turned off!. See you!");
83 } else if (a == MSG_TRANSFER_FAILURE) {
85 ("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!",
86 slaves[i % slaves_count]->name);
87 MSG_task_destroy(task);
88 } else if (a == MSG_TIMEOUT) {
90 ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
91 slaves[i % slaves_count]->name);
92 MSG_task_destroy(task);
94 INFO0("Hey ?! What's up ? ");
95 xbt_assert0(0, "Unexpected behavior");
100 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
101 for (i = 0; i < slaves_count; i++) {
102 m_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
103 int a = MSG_task_put_with_timeout(task, slaves[i], PORT_22, 1.0);
106 if (a == MSG_HOST_FAILURE) {
108 ("Gloups. The cpu on which I'm running just turned off!. See you!");
110 } else if (a == MSG_TRANSFER_FAILURE) {
111 INFO1("Mmh. Can't reach '%s'! Nevermind. Let's keep going!",
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 slaves[i % slaves_count]->name);
118 MSG_task_destroy(task);
120 INFO0("Hey ?! What's up ? ");
121 xbt_assert2(0, "Unexpected behavior with '%s': %d", slaves[i]->name,
126 INFO0("Goodbye now!");
129 } /* end_of_master */
131 /** Receiver function */
132 int slave(int argc, char *argv[])
135 m_task_t task = NULL;
139 time1 = MSG_get_clock();
140 a = MSG_task_get(&(task), PORT_22);
141 time2 = MSG_get_clock();
143 INFO1("Received \"%s\"", MSG_task_get_name(task));
144 if (MSG_task_get_data(task) == FINALIZE) {
145 MSG_task_destroy(task);
148 if (time1 < *((double *) task->data))
149 time1 = *((double *) task->data);
150 INFO1("Communication time : \"%f\"", time2 - time1);
151 INFO1("Processing \"%s\"", MSG_task_get_name(task));
152 a = MSG_task_execute(task);
154 INFO1("\"%s\" done", MSG_task_get_name(task));
156 MSG_task_destroy(task);
157 } else if (a == MSG_HOST_FAILURE) {
159 ("Gloups. The cpu on which I'm running just turned off!. See you!");
162 INFO0("Hey ?! What's up ? ");
163 xbt_assert0(0, "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 INFO0("Mmh. Something went wrong. Nevermind. Let's keep going!");
172 INFO0("Hey ?! What's up ? ");
173 xbt_assert0(0, "Unexpected behavior");
176 INFO0("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 /* MSG_config("workstation/model","KCCFLN05"); */
187 { /* Simulation setting */
188 MSG_set_channel_number(MAX_CHANNEL);
189 MSG_create_environment(platform_file);
191 { /* Application deployment */
192 MSG_function_register("master", master);
193 MSG_function_register("slave", slave);
194 MSG_launch_application(application_file);
198 INFO1("Simulation time %g", MSG_get_clock());
200 } /* end_of_test_all */
204 int main(int argc, char *argv[])
206 MSG_error_t res = MSG_OK;
208 MSG_global_init(&argc, argv);
210 printf("Usage: %s platform_file deployment_file\n", argv[0]);
211 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
214 res = test_all(argv[1], argv[2]);