Logo AND Algorithmique Numérique Distribuée

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