1 /* Copyright (c) 2007-2010, 2013-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 char * build_channel_name(char *buffer, const char *sender, const char* receiver)
15 strcpy(buffer, sender);
17 strcat(buffer, receiver);
21 /** Emitter function */
22 static int master(int argc, char *argv[])
24 int workers_count = 0;
25 msg_host_t *workers = NULL;
26 msg_task_t *todo = NULL;
27 msg_host_t host_self = MSG_host_self();
28 char *master_name = (char *) MSG_host_get_name(host_self);
33 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s"); /** - Number of tasks */
34 double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
35 double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
38 char sprintf_buffer[64];
40 todo = xbt_new0(msg_task_t, number_of_tasks);
42 for (i = 0; i < number_of_tasks; i++) {
43 sprintf(sprintf_buffer, "Task_%d", i);
44 todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
48 { /* Process organization */
49 workers_count = MSG_get_host_number();
50 workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
52 for (i = 0; i < workers_count; i++)
53 if(host_self == workers[i]) {
54 workers[i] = workers[workers_count-1];
59 for (i = 0; i < workers_count; i++)
60 MSG_process_create("worker", worker, master_name, workers[i]);
63 XBT_INFO("Got %d workers and %Ld tasks to process", workers_count, number_of_tasks);
65 for (i = 0; i < number_of_tasks; i++) {
66 build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count]));
68 XBT_INFO("Sending \"%s\" to channel \"%s\"", todo[i]->name, channel);
70 MSG_task_send(todo[i], channel);
74 XBT_INFO ("All tasks have been dispatched. Let's tell everybody the computation is over.");
75 for (i = 0; i < workers_count; i++) {
76 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
77 MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])));
80 XBT_INFO("Goodbye now!");
86 /** Receiver function */
87 static int worker(int argc, char *argv[])
89 msg_task_t task = NULL;
90 XBT_ATTRIB_UNUSED int res;
93 build_channel_name(channel,MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
95 XBT_INFO("Receiving on channel \"%s\"", channel);
98 res = MSG_task_receive(&(task),channel);
99 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
101 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
102 if (!strcmp(MSG_task_get_name(task), "finalize")) {
103 MSG_task_destroy(task);
107 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
108 MSG_task_execute(task);
109 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
110 MSG_task_destroy(task);
113 XBT_INFO("I'm done. See you!");
115 } /* end_of_worker */
118 int main(int argc, char *argv[])
120 msg_error_t res = MSG_OK;
122 MSG_init(&argc, argv);
123 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
124 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
125 { /* Simulation setting */
126 MSG_create_environment(argv[1]);
128 { /* Application deployment */
129 MSG_function_register("master", master);
130 MSG_function_register("worker", worker);
131 MSG_launch_application(argv[2]);
135 XBT_INFO("Simulation time %g", MSG_get_clock());
136 return (res != MSG_OK);