Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
public headers should include simgrid in a system-wide way, not a project-wide one
[simgrid.git] / doc / tuto-msg / masterworker-sol1.c
1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "simgrid/msg.h"
7
8 #include <stdio.h> /* sprintf */
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11
12 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
13
14 static char* build_channel_name(char* buffer, const char* sender, const char* receiver)
15 {
16   strcpy(buffer, sender);
17   strcat(buffer, ":");
18   strcat(buffer, receiver);
19   return buffer;
20 }
21
22 /* forward definitions */
23 static int master(int argc, char* argv[]);
24 static int worker(int argc, char* argv[]);
25
26 static int master(int argc, char* argv[])
27 {
28   msg_host_t host_self    = MSG_host_self();
29   const char* master_name = MSG_host_get_name(host_self);
30   char channel[1024];
31
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 */
35
36   /* Create the tasks in advance */
37   msg_task_t* todo = xbt_new0(msg_task_t, number_of_tasks);
38
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);
43   }
44
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());
48
49   for (int i = 0; i < workers_count; i++)
50     if (host_self == workers[i]) {
51       workers[i] = workers[workers_count - 1];
52       workers_count--;
53       break;
54     }
55
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);
59
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]));
63
64     XBT_INFO("Sending '%s' to channel '%s'", todo[i]->name, channel);
65
66     MSG_task_send(todo[i], channel);
67     XBT_INFO("Sent");
68   }
69
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])));
74   }
75
76   XBT_INFO("Goodbye now!");
77   free(workers);
78   free(todo);
79   return 0;
80 } /* end_of_master */
81
82 /** Receiver function  */
83 static int worker(int argc, char* argv[])
84 {
85   char channel[1024];
86
87   build_channel_name(channel, MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
88
89   XBT_INFO("Receiving on channel '%s'", channel);
90
91   while (1) {
92     msg_task_t task = NULL;
93     int res         = MSG_task_receive(&(task), channel);
94     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
95
96     XBT_INFO("Received '%s'", MSG_task_get_name(task));
97     if (!strcmp(MSG_task_get_name(task), "finalize")) {
98       MSG_task_destroy(task);
99       break;
100     }
101
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);
106   }
107   XBT_INFO("I'm done. See you!");
108   return 0;
109 } /* end_of_worker */
110
111 /** Main function */
112 int main(int argc, char* argv[])
113 {
114   MSG_init(&argc, argv);
115   xbt_assert(argc > 2,
116              "Usage: %s platform_file deployment_file\n"
117              "\tExample: %s msg_platform.xml msg_deployment.xml\n",
118              argv[0], argv[0]);
119
120   /*  Create a simulated platform */
121   MSG_create_environment(argv[1]);
122
123   /*   Application deployment */
124   MSG_function_register("master", master);
125   MSG_function_register("worker", worker);
126   MSG_launch_application(argv[2]);
127
128   /* Run the simulation */
129   msg_error_t res = MSG_main();
130
131   XBT_INFO("Simulation time %g", MSG_get_clock());
132   return (res != MSG_OK);
133 }