Logo AND Algorithmique Numérique Distribuée

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