1 /* Copyright (c) 2007-2017. 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 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
12 static char * build_channel_name(char *buffer, const char *sender, const char* receiver)
14 strcpy(buffer, sender);
16 strcat(buffer, receiver);
20 /* forward definitions */
21 static int master(int argc, char* argv[]);
22 static int worker(int argc, char* argv[]);
24 static int master(int argc, char *argv[])
26 msg_host_t host_self = MSG_host_self();
27 const char* master_name = MSG_host_get_name(host_self);
30 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s"); /** - Number of tasks */
31 double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
32 double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
34 /* Create the tasks in advance */
35 msg_task_t* todo = xbt_new0(msg_task_t, number_of_tasks);
37 for (int i = 0; i < number_of_tasks; i++) {
38 char sprintf_buffer[64];
39 sprintf(sprintf_buffer, "Task_%d", i);
40 todo[i] = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
43 /* Get the info about the worker processes (directly from SimGrid) */
44 int workers_count = argc - 4;
45 msg_host_t* workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
47 for (int i = 0; i < workers_count; i++)
48 if (host_self == workers[i]) {
49 workers[i] = workers[workers_count - 1];
54 for (int i = 0; i < workers_count; i++)
55 MSG_process_create("worker", worker, (void*)master_name, workers[i]);
56 XBT_INFO("Got %d workers and %ld tasks to process", workers_count, number_of_tasks);
58 /* Dispatch the tasks */
59 for (int i = 0; i < number_of_tasks; i++) {
60 build_channel_name(channel, master_name, MSG_host_get_name(workers[i % workers_count]));
62 XBT_INFO("Sending '%s' to channel '%s'", todo[i]->name, channel);
64 MSG_task_send(todo[i], channel);
68 XBT_INFO ("All tasks have been dispatched. Let's tell everybody the computation is over.");
69 for (int i = 0; i < workers_count; i++) {
70 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
71 MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])));
74 XBT_INFO("Goodbye now!");
80 /** Receiver function */
81 static int worker(int argc, char *argv[])
85 build_channel_name(channel, MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
87 XBT_INFO("Receiving on channel '%s'", channel);
90 msg_task_t task = NULL;
91 int res = MSG_task_receive(&(task), channel);
92 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
94 XBT_INFO("Received '%s'", MSG_task_get_name(task));
95 if (!strcmp(MSG_task_get_name(task), "finalize")) {
96 MSG_task_destroy(task);
100 XBT_INFO("Processing '%s'", MSG_task_get_name(task));
101 MSG_task_execute(task);
102 XBT_INFO("'%s' done", MSG_task_get_name(task));
103 MSG_task_destroy(task);
105 XBT_INFO("I'm done. See you!");
107 } /* end_of_worker */
110 int main(int argc, char *argv[])
112 MSG_init(&argc, argv);
113 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
114 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
116 /* Create a simulated platform */
117 MSG_create_environment(argv[1]);
119 /* Application deployment */
120 MSG_function_register("master", master);
121 MSG_function_register("worker", worker);
122 MSG_launch_application(argv[2]);
124 /* Run the simulation */
125 msg_error_t res = MSG_main();
127 XBT_INFO("Simulation time %g", MSG_get_clock());
128 return (res != MSG_OK);