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 worker(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 static char * build_channel_name(char *buffer, const char *sender, const char* receiver)
26 strcpy(buffer, sender);
28 strcat(buffer, receiver);
32 /** Emitter function */
33 int master(int argc, char *argv[])
35 int workers_count = 0;
36 msg_host_t *workers = NULL;
37 msg_task_t *todo = NULL;
38 msg_host_t host_self = MSG_host_self();
39 char *master_name = (char *) MSG_host_get_name(host_self);
40 int number_of_tasks = 0;
41 double task_comp_size = 0;
42 double task_comm_size = 0;
47 _XBT_GNUC_UNUSED int res = sscanf(argv[1], "%d", &number_of_tasks);
48 xbt_assert(res,"Invalid argument %s\n", argv[1]);
49 res = sscanf(argv[2], "%lg", &task_comp_size);
50 xbt_assert(res, "Invalid argument %s\n", argv[2]);
51 res = sscanf(argv[3], "%lg", &task_comm_size);
52 xbt_assert(res, "Invalid argument %s\n", argv[3]);
55 char sprintf_buffer[64];
57 todo = xbt_new0(msg_task_t, number_of_tasks);
59 for (i = 0; i < number_of_tasks; i++) {
60 sprintf(sprintf_buffer, "Task_%d", i);
62 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
67 { /* Process organisation */
68 workers_count = MSG_get_host_number();
69 workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
71 for (i = 0; i < workers_count; i++)
72 if(host_self == workers[i]) {
73 workers[i] = workers[workers_count-1];
78 for (i = 0; i < workers_count; i++)
79 MSG_process_create("worker", worker, master_name, workers[i]);
82 XBT_INFO("Got %d workers and %d tasks to process", workers_count,
85 for (i = 0; i < number_of_tasks; i++) {
86 build_channel_name(channel,master_name,
87 MSG_host_get_name(workers[i % workers_count]));
89 XBT_INFO("Sending \"%s\" to channel \"%s\"", todo[i]->name, channel);
91 MSG_task_send(todo[i], channel);
96 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
97 for (i = 0; i < workers_count; i++) {
98 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
99 MSG_task_send(finalize, build_channel_name(channel,master_name,
100 MSG_host_get_name(workers[i % workers_count])));
103 XBT_INFO("Goodbye now!");
107 } /* end_of_master */
109 /** Receiver function */
110 int worker(int argc, char *argv[])
112 msg_task_t task = NULL;
113 _XBT_GNUC_UNUSED int res;
116 build_channel_name(channel,MSG_process_get_data(MSG_process_self()),
117 MSG_host_get_name(MSG_host_self()));
119 XBT_INFO("Receiving on channel \"%s\"", channel);
122 res = MSG_task_receive(&(task),channel);
123 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
125 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
126 if (!strcmp(MSG_task_get_name(task), "finalize")) {
127 MSG_task_destroy(task);
131 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
132 MSG_task_execute(task);
133 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
134 MSG_task_destroy(task);
137 XBT_INFO("I'm done. See you!");
139 } /* end_of_worker */
142 msg_error_t test_all(const char *platform_file,
143 const char *application_file)
145 msg_error_t res = MSG_OK;
147 { /* Simulation setting */
148 MSG_create_environment(platform_file);
150 { /* Application deployment */
151 MSG_function_register("master", master);
152 MSG_function_register("worker", worker);
153 MSG_launch_application(application_file);
157 XBT_INFO("Simulation time %g", MSG_get_clock());
159 } /* end_of_test_all */
163 int main(int argc, char *argv[])
165 msg_error_t res = MSG_OK;
167 MSG_init(&argc, argv);
169 printf("Usage: %s platform_file deployment_file\n", argv[0]);
170 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
173 res = test_all(argv[1], argv[2]);