Logo AND Algorithmique Numérique Distribuée

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