Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Tasks must be initialized.
[simgrid.git] / examples / amok / alnem / alnem.c
1 /* ALNeM itself                                                             */
2
3 /* Copyright (c) 2005, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <signal.h>
13 #include <time.h>
14
15 #include <gras.h>
16
17 #include <tbx_graph.h>          /* alvin's graph toolbox (+ reconstruction algorithm) */
18
19 /* **********************************************************************
20  * Sensor code
21  * **********************************************************************/
22
23 /* Global private data */
24 typedef struct {
25   gras_sock_t *sock;
26 } sensor_data_t;
27
28 /* Function prototypes */
29 int sensor(int argc, char *argv[]);
30
31 int sensor(int argc, char *argv[])
32 {
33   xbt_error_t errcode;
34   sensor_data_t *g = gras_userdata_new(sensor_data_t);
35
36   if ((errcode = gras_sock_server_open(4000, 4000, &(g->sock)))) {
37     fprintf(stderr,
38             "Sensor: Error %s encountered while opening the server socket\n",
39             xbt_error_name(errcode));
40     return 1;
41   }
42
43   if (grasbw_register_messages()) {
44     gras_sock_close(g->sock);
45     return 1;
46   }
47
48   while (1) {
49     if ((errcode = gras_msg_handle(3600.0)) && errcode != timeout_error) {
50       fprintf(stderr, "Sensor: Error '%s' while handling message\n",
51               xbt_error_name(errcode));
52     }
53   }
54
55   gras_sleep(5, 0);
56   return gras_sock_close(g->sock);
57 }
58
59 /* **********************************************************************
60  * Maestro code
61  * **********************************************************************/
62
63 /* Global private data */
64 typedef struct {
65   gras_sock_t *sock;
66 } maestro_data_t;
67
68 /* Function prototypes */
69 int maestro(int argc, char *argv[]);
70
71 #define MAXHOSTS 100
72
73 #define INTERF(graph,table,a,u,b,v) INTERFERENCE(table,\
74                                                 TBX_Graph_nodeSearch(graph,a),\
75                                                 TBX_Graph_nodeSearch(graph,u),\
76                                                 TBX_Graph_nodeSearch(graph,b),\
77                                                 TBX_Graph_nodeSearch(graph,v))
78
79 int maestro(int argc, char *argv[])
80 {
81   int bufSize = 32 * 1024;
82   int expSize = 1024 * 1024;
83   int msgSize = expSize;
84   int satSize = msgSize * 100;
85   double dummy, beginSim;
86   xbt_error_t errcode;
87   maestro_data_t *g = gras_userdata_new(maestro_data_t);
88
89   double bw[MAXHOSTS][MAXHOSTS];
90   double bw_sat[MAXHOSTS][MAXHOSTS];
91
92   int a, b, c, d, begin;
93
94   TBX_Graph_t graph = TBX_Graph_newGraph("Essai", 0, NULL);     /* a dummy graph containing all hosts */
95   TBX_FIFO_t host_fifo = TBX_FIFO_newFIFO();
96   TBX_InterfTable_t interf = NULL;      /* the measured interferences */
97   TBX_Graph_t builded_graph = NULL;     /* the graph builded from the interferences */
98
99   /* basics setups */
100   if (argc > MAXHOSTS) {
101     fprintf(stderr,
102             "You gave more than %d sensors for this experiment. Increase the MAX HOSTS constant in alnem code to be bigger than this number.\n",
103             argc);
104     return 1;
105   }
106
107   if ((errcode = gras_sock_server_open(4000, 5000, &(g->sock)))) {
108     fprintf(stderr,
109             "MAESTRO: Error %s encountered while opening the server socket\n",
110             xbt_error_name(errcode));
111     return 1;
112   }
113
114   if (grasbw_register_messages()) {
115     gras_sock_close(g->sock);
116     return 1;
117   }
118
119   for (a = 1; a < argc; a++) {
120     TBX_FIFO_insert(host_fifo, TBX_Graph_newNode(graph, argv[a], NULL));
121   }
122   TBX_Graph_fixNodeNumbering(graph);
123   interf = TBX_Graph_newInterfTable(host_fifo);
124   TBX_Graph_graphInterfTableInit(graph, interf);
125
126   /* measure the bandwidths */
127   begin = time(NULL);
128   beginSim = gras_time();
129   for (a = 1; a < argc; a++) {
130     for (b = 1; b < argc; b++) {
131       int test = 0;
132
133       if (a == b)
134         continue;
135       fprintf(stderr, "BW XP(%s %s)=", argv[a], argv[b]);
136       while ((errcode =
137               grasbw_request(argv[a], 4000, argv[b], 4000, bufSize,
138                              expSize, msgSize, &dummy, &(bw[a][b])))
139              && (errcode == timeout_error)) {
140         test++;
141         if (test == 10) {
142           fprintf(stderr, "MAESTRO: 10 Timeouts; giving up\n");
143           return 1;
144         }
145       }
146       if (errcode) {
147         fprintf(stderr,
148                 "MAESTRO: Error %s encountered while doing the test\n",
149                 xbt_error_name(errcode));
150         return 1;
151       }
152       fprintf(stderr, "%f Mb/s in %f sec\n", bw[a][b], dummy);
153     }
154   }
155   fprintf(stderr, "Did all BW tests in %ld sec (%.2f simulated sec)\n",
156           time(NULL) - begin, gras_time() - beginSim);
157
158   /* saturation tests */
159   for (a = 1; a < argc; a++) {
160     for (b = 1; b < argc; b++) {
161       for (c = 1; c < argc; c++) {
162         INTERF(graph, interf, argv[a], argv[c], argv[b], argv[c]) = 1;
163       }
164     }
165   }
166
167   for (a = 1; a < argc; a++) {
168     for (b = 1; b < argc; b++) {
169       if (a == b)
170         continue;
171
172       if ((errcode =
173            grasbw_saturate_start(argv[a], 4000, argv[b], 4000, satSize,
174                                  360000000))) {
175         fprintf(stderr,
176                 "MAESTRO: Error %s encountered while starting saturation\n",
177                 xbt_error_name(errcode));
178         return -1;
179       }
180       gras_sleep(1, 0);
181
182       begin = time(NULL);
183       beginSim = gras_time();
184       for (c = 1; c < argc; c++) {
185         if (a == c || b == c)
186           continue;
187
188         for (d = 1; d < argc; d++) {
189           if (a == d || b == d || c == d)
190             continue;
191
192           if ((errcode =
193                grasbw_request(argv[c], 4000, argv[d], 4000, bufSize,
194                               expSize, msgSize, &dummy,
195                               &(bw_sat[c][d])))) {
196             fprintf(stderr, "MAESTRO: Error %s encountered in test\n",
197                     xbt_error_name(errcode));
198             return 1;
199           }
200           fprintf(stderr,
201                   "MAESTRO[%.2f sec]: SATURATED BW XP(%s %s // %s %s) => %f (%f vs %f)%s\n",
202                   gras_time(), argv[c], argv[d], argv[a], argv[b],
203                   bw_sat[c][d] / bw[c][d], bw[c][d], bw_sat[c][d],
204                   (bw_sat[c][d] / bw[c][d] <
205                    0.75) ? " THERE IS SOME INTERFERENCE !!!" : "");
206           INTERF(graph, interf, argv[c], argv[d], argv[a], argv[b]) =
207               (bw_sat[c][d] / bw[c][d] < 0.75) ? 1 : 0;
208         }
209       }
210
211       if ((errcode = grasbw_saturate_stop(argv[a], 4000, argv[b], 4000))) {
212         fprintf(stderr,
213                 "MAESTRO: Error %s encountered while stopping saturation\n",
214                 xbt_error_name(errcode));
215         return -1;
216       }
217       fprintf(stderr,
218               "Did an iteration on saturation pair in %ld sec (%.2f simulated sec)\n",
219               time(NULL) - begin, gras_time() - beginSim);
220     }
221   }
222
223   /* reconstruct the graph */
224   TBX_Graph_interferenceTableDump(interf);
225   TBX_Graph_interferenceTableSave(interf, "interference.dat");
226   begin = time(NULL);
227   fprintf(stderr, "MAESTRO: Reconstruct the graph... ");
228   builded_graph = TBX_Graph_exploreInterference(interf);
229   TBX_Graph_exportToGraphViz(builded_graph, "toto.dot");
230   fprintf(stderr, "done (took %d sec)", (int) time(NULL));
231
232   /* end */
233   TBX_Graph_freeGraph(graph, NULL, NULL, NULL);
234   TBX_Graph_freeGraph(builded_graph, NULL, NULL, NULL);
235   TBX_Graph_freeInterfTable(interf);
236
237   gras_sleep(5, 0);
238   exit(0);                      /* FIXME: There is a bug in MSG preventing me from terminating this server properly */
239   return gras_sock_close(g->sock);
240 }