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 const char* master_name = MSG_host_get_name(host_self);
32 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s"); /** - Number of tasks */
33 double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
34 double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
36 /* Create the tasks in advance */
37 msg_task_t* todo = xbt_new0(msg_task_t, number_of_tasks);
39 for (int i = 0; i < number_of_tasks; i++) {
40 char sprintf_buffer[64];
41 sprintf(sprintf_buffer, "Task_%d", i);
42 todo[i] = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
45 /* Get the info about the worker processes (directly from SimGrid) */
46 int workers_count = MSG_get_host_number();
47 msg_host_t* workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
49 for (int i = 0; i < workers_count; i++)
50 if (host_self == workers[i]) {
51 workers[i] = workers[workers_count - 1];
56 for (int i = 0; i < workers_count; i++)
57 MSG_process_create("worker", worker, (void*)master_name, workers[i]);
58 XBT_INFO("Got %d workers and %ld tasks to process", workers_count, number_of_tasks);
60 /* Dispatch the tasks */
61 for (int i = 0; i < number_of_tasks; i++) {
62 build_channel_name(channel, master_name, MSG_host_get_name(workers[i % workers_count]));
64 XBT_INFO("Sending '%s' to channel '%s'", todo[i]->name, channel);
66 MSG_task_send(todo[i], channel);
70 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
71 for (int i = 0; i < workers_count; i++) {
72 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
73 MSG_task_send(finalize, build_channel_name(channel, master_name, MSG_host_get_name(workers[i % workers_count])));
76 XBT_INFO("Goodbye now!");
82 /** Receiver function */
83 static int worker(int argc, char* argv[])
87 build_channel_name(channel, MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
89 XBT_INFO("Receiving on channel '%s'", channel);
92 msg_task_t task = NULL;
93 int res = MSG_task_receive(&(task), channel);
94 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
96 XBT_INFO("Received '%s'", MSG_task_get_name(task));
97 if (!strcmp(MSG_task_get_name(task), "finalize")) {
98 MSG_task_destroy(task);
102 XBT_INFO("Processing '%s'", MSG_task_get_name(task));
103 MSG_task_execute(task);
104 XBT_INFO("'%s' done", MSG_task_get_name(task));
105 MSG_task_destroy(task);
107 XBT_INFO("I'm done. See you!");
109 } /* end_of_worker */
112 int main(int argc, char* argv[])
114 MSG_init(&argc, argv);
116 "Usage: %s platform_file deployment_file\n"
117 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
120 /* Create a simulated platform */
121 MSG_create_environment(argv[1]);
123 /* Application deployment */
124 MSG_function_register("master", master);
125 MSG_function_register("worker", worker);
126 MSG_launch_application(argv[2]);
128 /* Run the simulation */
129 msg_error_t res = MSG_main();
131 XBT_INFO("Simulation time %g", MSG_get_clock());
132 return (res != MSG_OK);