Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge 'master' into mc
[simgrid.git] / examples / msg / masterslave / masterslave_platfgen.c
1 /* Copyright (c) 2012-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "simgrid/platf_generator.h"
8 #include "xbt.h"
9 #include "msg/msg.h"
10
11 #include "xbt/log.h"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(test_generation,
13                              "Messages specific for this msg example");
14
15
16 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
17
18 void promoter_1(context_node_t node);
19 void labeler_1(context_edge_t edge);
20 int master(int argc, char *argv[]);
21 int slave(int argc, char *argv[]);
22
23 /** Promoter function
24  * Just promote each node into a host, with fixed power
25  */
26 void promoter_1(context_node_t node) {
27
28   s_sg_platf_host_cbarg_t host_parameters;
29
30   host_parameters.id = NULL;
31   host_parameters.power_peak = xbt_dynar_new(sizeof(double), NULL);
32   xbt_dynar_push_as(host_parameters.power_peak, double, 1000000.0);
33   host_parameters.core_amount = 1;
34   host_parameters.power_scale = 1;
35   host_parameters.power_trace = NULL;
36   host_parameters.initial_state = SURF_RESOURCE_ON;
37   host_parameters.state_trace = NULL;
38   host_parameters.coord = NULL;
39   host_parameters.properties = NULL;
40
41   platf_graph_promote_to_host(node, &host_parameters);
42
43 }
44
45 /** Labeler function
46  * Set all links with the same bandwidth and latency
47  */
48 void labeler_1(context_edge_t edge) {
49   s_sg_platf_link_cbarg_t link_parameters;
50   link_parameters.id = NULL;
51   link_parameters.bandwidth = 1000000;
52   link_parameters.bandwidth_trace = NULL;
53   link_parameters.latency = 0.01;
54   link_parameters.latency_trace = NULL;
55   link_parameters.state = SURF_RESOURCE_ON;
56   link_parameters.state_trace = NULL;
57   link_parameters.policy = SURF_LINK_SHARED;
58   link_parameters.properties = NULL;
59
60   platf_graph_link_label(edge, &link_parameters);
61 }
62
63 /** Emitter function  */
64 int master(int argc, char *argv[])
65 {
66   int slaves_count = 0;
67   msg_host_t *slaves = NULL;
68   int number_of_tasks = 0;
69   double task_comp_size = 0;
70   double task_comm_size = 0;
71   int i;
72
73   number_of_tasks = 3*argc;
74   task_comp_size = 2400000*argc;
75   task_comm_size = 1000000;
76
77   {                             /* Process organisation */
78     slaves_count = argc;
79     slaves = xbt_new0(msg_host_t, slaves_count);
80
81     for (i = 0; i < argc; i++) {
82       slaves[i] = MSG_get_host_by_name(argv[i]);
83       if (slaves[i] == NULL) {
84         XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
85         abort();
86       }
87     }
88   }
89
90   XBT_INFO("Got %d slave(s) :", slaves_count);
91   for (i = 0; i < slaves_count; i++)
92     XBT_INFO("%s", MSG_host_get_name(slaves[i]));
93
94   XBT_INFO("Got %d task to process :", number_of_tasks);
95
96   for (i = 0; i < number_of_tasks; i++) {
97     msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size,
98                                     xbt_new0(double, 1));
99     int a;
100     *((double *) task->data) = MSG_get_clock();
101
102     a = MSG_task_send(task,MSG_host_get_name(slaves[i % slaves_count]));
103
104     if (a == MSG_OK) {
105       XBT_INFO("Send completed");
106     } else {
107       XBT_INFO("Hey ?! What's up ? ");
108       xbt_die( "Unexpected behavior");
109     }
110   }
111
112   XBT_INFO
113       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
114   for (i = 0; i < slaves_count; i++) {
115     msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
116     int a = MSG_task_send(task,MSG_host_get_name(slaves[i]));
117     if (a != MSG_OK) {
118       XBT_INFO("Hey ?! What's up ? ");
119       xbt_die("Unexpected behavior with '%s': %d", MSG_host_get_name(slaves[i]), a);
120     }
121   }
122
123   XBT_INFO("Goodbye now!");
124   free(slaves);
125   return 0;
126 }                               /* end_of_master */
127
128 /** Receiver function  */
129 int slave(int argc, char *argv[])
130 {
131   while (1) {
132     msg_task_t task = NULL;
133     int a;
134     double time1, time2;
135
136     time1 = MSG_get_clock();
137     a = MSG_task_receive( &(task), MSG_host_get_name(MSG_host_self()) );
138     time2 = MSG_get_clock();
139     if (a == MSG_OK) {
140       XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
141       if (MSG_task_get_data(task) == FINALIZE) {
142         MSG_task_destroy(task);
143         break;
144       }
145       if (time1 < *((double *) task->data))
146         time1 = *((double *) task->data);
147       XBT_INFO("Communication time : \"%f\"", time2 - time1);
148       XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
149       a = MSG_task_execute(task);
150       if (a == MSG_OK) {
151         XBT_INFO("\"%s\" done", MSG_task_get_name(task));
152         free(task->data);
153         MSG_task_destroy(task);
154       } else {
155         XBT_INFO("Hey ?! What's up ? ");
156         xbt_die("Unexpected behavior");
157       }
158     } else {
159       XBT_INFO("Hey ?! What's up ? error %d", a);
160       xbt_die("Unexpected behavior");
161     }
162   }
163   XBT_INFO("I'm done. See you!");
164   return 0;
165 }
166
167 /**
168  * Main function
169  * Create the platform, list the available hosts and give them some work
170  */
171 int main(int argc, char **argv) {
172
173   unsigned long seed[] = {134, 233445, 865, 2634, 424242, 876543};
174   int connected;
175   int max_tries = 10;
176
177   //MSG initialisation
178   MSG_init(&argc, argv);
179
180   //Set up the seed for the platform generation
181   platf_random_seed(seed);
182
183   XBT_INFO("creating nodes...");
184   platf_graph_uniform(50);
185   do {
186     max_tries--;
187     XBT_INFO("creating links...");
188     platf_graph_clear_links();
189     platf_graph_interconnect_uniform(0.07); //Unrealistic, but simple
190     XBT_INFO("done. Check connectedness...");
191     connected = platf_graph_is_connected();
192     XBT_INFO("Is it connected : %s", connected ? "yes" : (max_tries ? "no, retrying" : "no"));
193   } while(!connected && max_tries);
194
195   if(!connected && !max_tries) {
196     xbt_die("Impossible to connect the graph, aborting.");
197   }
198
199   XBT_INFO("registering callbacks...");
200   platf_graph_promoter(promoter_1);
201   platf_graph_labeler(labeler_1);
202
203   XBT_INFO("protmoting...");
204   platf_do_promote();
205
206   XBT_INFO("labeling...");
207   platf_do_label();
208
209   XBT_INFO("Putting it in surf...");
210   platf_generate();
211
212   XBT_INFO("Let's get the available hosts and dispatch work:");
213
214   unsigned int i;
215   msg_host_t host = NULL;
216   msg_host_t host_master = NULL;
217   xbt_dynar_t host_dynar = MSG_hosts_as_dynar();
218   char** hostname_list = xbt_malloc(sizeof(char*) * xbt_dynar_length(host_dynar));
219
220   xbt_dynar_foreach(host_dynar, i, host) {
221     MSG_process_create("slave", slave, NULL, host);
222     if(i==0) {
223       //The first node will also be the master
224       XBT_INFO("%s will be the master", MSG_host_get_name(host));
225       host_master = host;
226     }
227     hostname_list[i] = (char*) MSG_host_get_name(host);
228   }
229
230   MSG_process_create_with_arguments("master", master, NULL, host_master, xbt_dynar_length(host_dynar), hostname_list);
231
232   XBT_INFO("Let's rock!");
233   MSG_main();
234   XBT_INFO("Simulation time %g", MSG_get_clock());
235   return 0;
236 }