1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/msg.h"
8 #include <stdio.h> /* sprintf */
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
12 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
14 static char* build_channel_name(char* buffer, const char* sender, const char* receiver)
16 strcpy(buffer, sender);
18 strcat(buffer, receiver);
22 /* forward definitions */
23 static int master(int argc, char* argv[]);
24 static int worker(int argc, char* argv[]);
26 static int master(int argc, char* argv[])
28 msg_host_t host_self = MSG_host_self();
29 char* master_name = (char*)MSG_host_get_name(host_self);
32 TRACE_category(master_name);
34 double timeout = xbt_str_parse_double(argv[1], "Invalid timeout: %s"); /** - timeout */
35 double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
36 double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
38 /* Get the info about the worker processes */
39 int workers_count = MSG_get_host_number();
40 msg_host_t* workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
42 for (int i = 0; i < workers_count; i++) // Remove my host from the list
43 if (host_self == workers[i]) {
44 workers[i] = workers[workers_count - 1];
49 for (int i = 0; i < workers_count; i++)
50 MSG_process_create("worker", worker, (void*)master_name, workers[i]);
51 XBT_INFO("Got %d workers and will send tasks for %g seconds", workers_count, timeout);
53 /* Dispatch the tasks */
56 if (MSG_get_clock() > timeout)
59 char sprintf_buffer[64];
60 sprintf(sprintf_buffer, "Task_%d", task_num);
62 msg_task_t task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
63 MSG_task_set_category(task, master_name);
65 build_channel_name(channel, master_name, MSG_host_get_name(workers[task_num % workers_count]));
67 XBT_DEBUG("Sending '%s' to channel '%s'", task->name, channel);
68 MSG_task_send(task, channel);
73 XBT_DEBUG("All tasks have been dispatched. Let's tell everybody the computation is over.");
74 for (int i = 0; i < workers_count; i++) {
75 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
76 MSG_task_send(finalize, build_channel_name(channel, master_name, MSG_host_get_name(workers[i % workers_count])));
79 XBT_INFO("Sent %d tasks in total!", task_num);
84 /** Worker function */
85 static int worker(int argc, char* argv[])
88 build_channel_name(channel, MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
90 XBT_DEBUG("Receiving on channel '%s'", channel);
93 msg_task_t task = NULL;
94 int res = MSG_task_receive(&(task), channel);
95 xbt_assert(res == MSG_OK, "MSG_task_get failed");
97 XBT_DEBUG("Received '%s'", MSG_task_get_name(task));
98 if (!strcmp(MSG_task_get_name(task), "finalize")) {
99 MSG_task_destroy(task);
103 XBT_DEBUG("Processing '%s'", MSG_task_get_name(task));
104 MSG_task_execute(task);
105 XBT_DEBUG("'%s' done", MSG_task_get_name(task));
106 MSG_task_destroy(task);
108 XBT_DEBUG("I'm done. See you!");
113 int main(int argc, char* argv[])
115 MSG_init(&argc, argv);
117 "Usage: %s platform_file deployment_file\n"
118 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
121 /* Create a simulated platform */
122 MSG_create_environment(argv[1]);
124 /* Application deployment */
125 MSG_function_register("master", master);
126 MSG_function_register("worker", worker);
127 MSG_launch_application(argv[2]);
129 /* Run the simulation */
130 msg_error_t res = MSG_main();
132 XBT_INFO("Simulation time %g", MSG_get_clock());
133 return (res != MSG_OK);