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 m_task_t *todo = NULL;
36 int number_of_tasks = 0;
37 double task_comp_size = 0;
38 double task_comm_size = 0;
42 _XBT_GNUC_UNUSED int res = sscanf(argv[1], "%d", &number_of_tasks);
43 xbt_assert(res,"Invalid argument %s\n", argv[1]);
44 res = sscanf(argv[2], "%lg", &task_comp_size);
45 xbt_assert(res, "Invalid argument %s\n", argv[2]);
46 res = sscanf(argv[3], "%lg", &task_comm_size);
47 xbt_assert(res, "Invalid argument %s\n", argv[3]);
50 char sprintf_buffer[64];
52 todo = xbt_new0(m_task_t, number_of_tasks);
54 for (i = 0; i < number_of_tasks; i++) {
55 sprintf(sprintf_buffer, "Task_%d", i);
57 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
62 { /* Process organisation */
63 slaves_count = argc - 4;
64 slaves = xbt_new0(m_host_t, slaves_count);
66 for (i = 4; i < argc; i++) {
67 slaves[i - 4] = MSG_get_host_by_name(argv[i]);
68 xbt_assert(slaves[i - 4] != NULL, "Unknown host %s. Stopping Now! ",
73 XBT_INFO("Got %d slaves and %d tasks to process", slaves_count,
75 for (i = 0; i < slaves_count; i++)
76 XBT_DEBUG("%s", slaves[i]->name);
78 for (i = 0; i < number_of_tasks; i++) {
79 XBT_INFO("Sending \"%s\" to \"%s\"",
80 todo[i]->name, slaves[i % slaves_count]->name);
81 if (MSG_host_self() == slaves[i % slaves_count]) {
82 XBT_INFO("Hey ! It's me ! :)");
85 MSG_task_put(todo[i], slaves[i % slaves_count], PORT_22);
90 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
91 for (i = 0; i < slaves_count; i++) {
92 m_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
93 MSG_task_put(finalize, slaves[i], PORT_22);
96 XBT_INFO("Goodbye now!");
100 } /* end_of_master */
102 /** Receiver function */
103 int slave(int argc, char *argv[])
105 m_task_t task = NULL;
106 _XBT_GNUC_UNUSED int res;
108 res = MSG_task_get(&(task), PORT_22);
109 xbt_assert(res == MSG_OK, "MSG_task_get failed");
111 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
112 if (!strcmp(MSG_task_get_name(task), "finalize")) {
113 MSG_task_destroy(task);
117 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
118 MSG_task_execute(task);
119 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
120 MSG_task_destroy(task);
123 XBT_INFO("I'm done. See you!");
127 /** Forwarder function */
128 int forwarder(int argc, char *argv[])
134 { /* Process organisation */
135 slaves_count = argc - 1;
136 slaves = xbt_new0(m_host_t, slaves_count);
138 for (i = 1; i < argc; i++) {
139 slaves[i - 1] = MSG_get_host_by_name(argv[i]);
140 if (slaves[i - 1] == NULL) {
141 XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
149 m_task_t task = NULL;
151 a = MSG_task_get(&(task), PORT_22);
153 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
154 if (MSG_task_get_data(task) == FINALIZE) {
156 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
157 for (i = 0; i < slaves_count; i++)
158 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
160 MSG_task_destroy(task);
163 XBT_INFO("Sending \"%s\" to \"%s\"",
164 MSG_task_get_name(task), slaves[i % slaves_count]->name);
165 MSG_task_put(task, slaves[i % slaves_count], PORT_22);
168 XBT_INFO("Hey ?! What's up ? ");
169 xbt_die("Unexpected behavior");
174 XBT_INFO("I'm done. See you!");
176 } /* end_of_forwarder */
179 MSG_error_t test_all(const char *platform_file,
180 const char *application_file)
182 MSG_error_t res = MSG_OK;
184 /* MSG_config("workstation/model","KCCFLN05"); */
185 { /* Simulation setting */
186 MSG_set_channel_number(MAX_CHANNEL);
187 MSG_create_environment(platform_file);
189 { /* Application deployment */
190 MSG_function_register("master", master);
191 MSG_function_register("slave", slave);
192 MSG_function_register("forwarder", forwarder);
193 MSG_launch_application(application_file);
197 XBT_INFO("Simulation time %g", MSG_get_clock());
199 } /* end_of_test_all */
203 int main(int argc, char *argv[])
205 MSG_error_t res = MSG_OK;
207 MSG_global_init(&argc, argv);
209 printf("Usage: %s platform_file deployment_file\n", argv[0]);
210 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
213 res = test_all(argv[1], argv[2]);