Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9ac16734e7fddf9f1d7e6ce6694d60ca70b3f730
[simgrid.git] / testsuite / surf / simeng_usage.c
1 /*      $Id$     */
2
3 /* A few tests for the maxmin library                                       */
4
5 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #ifdef __BORLANDC__
11 #pragma hdrstop
12 #endif
13
14 #include "xbt/sysdep.h"
15 #include "surf/maxmin.h"
16 #include "surf/maxmin_private.h"
17
18 #include "xbt/log.h"
19 #include "xbt/module.h"
20
21
22 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test,"Messages specific for surf example");
23
24 #define PRINT_VAR(var) DEBUG1(#var " = %g",lmm_variable_getvalue(var));
25
26 /*                               */
27 /*        ______                 */
28 /*  ==l1==  L2  ==L3==           */
29 /*        ------                 */
30 /*                               */
31
32 typedef enum {
33   MAXMIN,
34   SDP,
35   LAGRANGE_RENO,
36   LAGRANGE_VEGAS,
37 } method_t;
38
39 void test1(method_t method);
40 void test1(method_t method)
41 {
42   lmm_system_t Sys = NULL ;
43   lmm_constraint_t L1 = NULL;
44   lmm_constraint_t L2 = NULL;
45   lmm_constraint_t L3 = NULL;
46
47   lmm_variable_t R_1_2_3 = NULL;
48   lmm_variable_t R_1 = NULL;
49   lmm_variable_t R_2 = NULL;
50   lmm_variable_t R_3 = NULL;
51
52   if(method==LAGRANGE_VEGAS){
53     //set default functions for TCP Vegas
54     lmm_set_default_protocol_function(func_vegas_fpi);
55   }else if(method==LAGRANGE_RENO){
56     //set default functions for TCP Reno
57     lmm_set_default_protocol_function(func_reno_fpi);
58   }
59
60
61   Sys = lmm_system_new();
62   L1 = lmm_constraint_new(Sys, (void *) "L1", 1.0);
63   L2 = lmm_constraint_new(Sys, (void *) "L2", 10.0);
64   L3 = lmm_constraint_new(Sys, (void *) "L3", 1.0);
65
66   R_1_2_3 = lmm_variable_new(Sys, (void *) "R 1->2->3", 1.0 , -1.0 , 3);
67   R_1 = lmm_variable_new(Sys, (void *) "R 1", 1.0 , -1.0 , 1);
68   R_2 = lmm_variable_new(Sys, (void *) "R 2", 1.0 , -1.0 , 1);
69   R_3 = lmm_variable_new(Sys, (void *) "R 3", 1.0 , -1.0 , 1);
70
71   lmm_update_variable_latency(Sys, R_1_2_3, 1.0);
72   lmm_update_variable_latency(Sys,R_1, 1.0);
73   lmm_update_variable_latency(Sys,R_2, 1.0); 
74   lmm_update_variable_latency(Sys,R_3, 1.0);
75
76
77   lmm_expand(Sys, L1, R_1_2_3, 1.0);
78   lmm_expand(Sys, L2, R_1_2_3, 1.0);
79   lmm_expand(Sys, L3, R_1_2_3, 1.0);
80
81   lmm_expand(Sys, L1, R_1, 1.0);
82
83   lmm_expand(Sys, L2, R_2, 1.0);
84
85   lmm_expand(Sys, L3, R_3, 1.0);
86
87
88   if(method==MAXMIN){
89     lmm_solve(Sys);
90 #ifdef HAVE_SDP
91   }else if(method==SDP){
92     sdp_solve(Sys);    
93 #endif
94   }else if(method==LAGRANGE_VEGAS){
95     //set default functions for TCP Vegas
96     lagrange_solve(Sys);  
97   }else if(method==LAGRANGE_RENO){
98     //set default functions for TCP Reno
99     lagrange_solve(Sys);  
100   }else{ 
101     xbt_assert0(0,"Invalid method");
102   }
103
104   PRINT_VAR(R_1_2_3);
105   PRINT_VAR(R_1);
106   PRINT_VAR(R_2);
107   PRINT_VAR(R_3);
108
109   lmm_system_free(Sys);
110
111
112 void test2(method_t method);
113 void test2(method_t method)
114 {
115   lmm_system_t Sys = NULL ;
116   lmm_constraint_t CPU1 = NULL;
117   lmm_constraint_t CPU2 = NULL;
118
119   lmm_variable_t T1 = NULL;
120   lmm_variable_t T2 = NULL;
121
122
123   if(method==LAGRANGE_VEGAS){
124     //set default functions for TCP Vegas
125     lmm_set_default_protocol_function(func_vegas_fpi);
126   }else if(method==LAGRANGE_RENO){
127     //set default functions for TCP Reno
128     lmm_set_default_protocol_function(func_reno_fpi);
129   }
130
131   Sys = lmm_system_new();
132   CPU1 = lmm_constraint_new(Sys, (void *) "CPU1", 200.0);
133   CPU2 = lmm_constraint_new(Sys, (void *) "CPU2", 100.0);
134
135   T1 = lmm_variable_new(Sys, (void *) "T1", 1.0 , -1.0 , 1);
136   T2 = lmm_variable_new(Sys, (void *) "T2", 1.0 , -1.0 , 1);
137
138   lmm_update_variable_latency(Sys, T1, 1.0);
139   lmm_update_variable_latency(Sys, T2, 1.0);
140
141
142   lmm_expand(Sys, CPU1, T1, 1.0);
143   lmm_expand(Sys, CPU2, T2, 1.0);
144
145
146
147   if(method==MAXMIN){
148     lmm_solve(Sys);
149 #ifdef HAVE_SDP
150   }else if(method==SDP){
151     sdp_solve(Sys);    
152 #endif
153   }else if(method==LAGRANGE_VEGAS){
154     //set default functions for TCP Vegas
155     lagrange_solve(Sys);  
156   }else if(method==LAGRANGE_RENO){
157     //set default functions for TCP Reno
158     lagrange_solve(Sys);  
159   }else{ 
160     xbt_assert0(0,"Invalid method");
161   }
162
163   PRINT_VAR(T1);
164   PRINT_VAR(T2);
165
166   DEBUG0("\n");
167
168   lmm_system_free(Sys);
169 }
170
171 void test3(method_t method);
172 void test3(method_t method)
173 {
174   int flows=11;  //flows and conexions are synonnims ?
175   int links=10;  //topology info
176   
177   //just to be carefull
178   int i = 0;
179   int j = 0;
180
181   double **A;
182
183   lmm_system_t Sys = NULL ;
184   lmm_constraint_t *tmp_cnst = NULL;
185   lmm_variable_t   *tmp_var  = NULL;
186   char **tmp_name;
187
188
189   /*array to add the the constraints of fictiv variables */
190   double B[15] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
191
192                 1, 1, 1, 1, 1};
193
194   A = (double **)calloc(links+5, sizeof(double));
195  
196   for(i=0 ; i< links+5; i++){
197     A[i] = (double *)calloc(flows+5, sizeof(double));
198
199     for(j=0 ; j< flows+5; j++){
200       A[i][j] = 0.0;
201
202       if(i >= links || j >= flows){
203           A[i][j] = 0.0;
204       }
205     }
206   }
207
208   /*matrix that store the constraints/topollogy*/
209   /*double A[15][16]=
210     {{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,    0, 0, 0, 0, 0},
211      {0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0,    0, 0, 0, 0, 0},
212      {0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0,    0, 0, 0, 0, 0},
213      {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,    0, 0, 0, 0, 0},
214      {1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0,    0, 0, 0, 0, 0},
215      {1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0,    0, 0, 0, 0, 0},
216      {1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1,    0, 0, 0, 0, 0},
217      {0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1,    0, 0, 0, 0, 0},
218      {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,    0, 0, 0, 0, 0},
219      {0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0,    0, 0, 0, 0, 0},
220      
221      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    1, 0, 0, 0, 0},
222      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 1, 0, 0, 0},
223      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 1, 0, 0},
224      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 1, 0},
225      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 1}
226      }; */
227
228   A[0][1]  = 1.0;
229   A[0][7]  = 1.0;
230   
231   A[1][1]  = 1.0;
232   A[1][7]  = 1.0;
233   A[1][8]  = 1.0;
234
235   A[2][1]  = 1.0;
236   A[2][8]  = 1.0;
237
238   A[2][1]  = 1.0;
239   A[2][8]  = 1.0;
240
241   A[3][8]  = 1.0;
242
243   A[4][0]  = 1.0;
244   A[4][3]  = 1.0;
245   A[4][9]  = 1.0;
246
247   A[5][0]  = 1.0;
248   A[5][3]  = 1.0;
249   A[5][4]  = 1.0;
250   A[5][9]  = 1.0;
251
252   A[6][0]  = 1.0;
253   A[6][4]  = 1.0;
254   A[6][9]  = 1.0;
255   A[6][10] = 1.0;
256
257   A[7][2]  = 1.0;
258   A[7][4]  = 1.0;
259   A[7][6]  = 1.0;
260   A[7][9]  = 1.0;
261   A[7][10] = 1.0;
262
263   A[8][2]  = 1.0;
264   A[8][10] = 1.0;
265
266   A[9][5]  = 1.0;
267   A[9][6]  = 1.0;
268   A[9][9]  = 1.0;
269
270   
271   A[10][11] = 1.0;
272   A[11][12] = 1.0;
273   A[12][13] = 1.0;
274   A[13][14] = 1.0;
275   A[14][15] = 1.0;
276
277
278   if(method==LAGRANGE_VEGAS){
279     //set default functions for TCP Vegas
280     lmm_set_default_protocol_function(func_vegas_fpi);
281   }else if(method==LAGRANGE_RENO){
282     //set default functions for TCP Reno
283     lmm_set_default_protocol_function(func_reno_fpi);
284   }
285
286   Sys = lmm_system_new();
287   
288
289
290   tmp_name = (char **) calloc(31, sizeof(char *));
291   for(i=0; i<31; i++){
292     tmp_name[i] = (char *) calloc(10, sizeof(char));
293   }
294   
295   /*
296    * Creates the constraints
297    */
298   tmp_cnst = calloc(15, sizeof(lmm_constraint_t));
299   for(i=0; i<15; i++){
300     sprintf(tmp_name[i], "C_%03d", i); 
301     tmp_cnst[i] = lmm_constraint_new(Sys, (void *) tmp_name[i], B[i]);
302   }
303
304
305   /*
306    * Creates the variables
307    */
308   tmp_var = calloc(16, sizeof(lmm_variable_t));
309   for(j=0; j<16; j++){
310     sprintf(tmp_name[i+j], "X_%03d", j); 
311     tmp_var[j] = lmm_variable_new(Sys, (void *) tmp_name[i+j], 1.0, -1.0 , 15);
312     lmm_update_variable_latency(Sys, tmp_var[j], 1.0);
313   }
314
315   /*
316    * Link constraints and variables
317    */
318   for(i=0;i<15;i++) { 
319     for(j=0; j<16; j++){
320       if(A[i][j]){
321         lmm_expand(Sys, tmp_cnst[i], tmp_var[j], 1.0); 
322       }
323     }
324   }
325
326
327
328   if(method==MAXMIN){
329     lmm_solve(Sys);
330 #ifdef HAVE_SDP
331   }else if(method==SDP){
332     sdp_solve(Sys);    
333 #endif
334   }else if(method==LAGRANGE_VEGAS){
335     //set default functions for TCP Vegas
336     lagrange_solve(Sys);  
337   }else if(method==LAGRANGE_RENO){
338     //set default functions for TCP Reno
339     lagrange_solve(Sys);  
340   }else{ 
341     xbt_assert0(0,"Invalid method");
342   }
343
344   for(j=0; j<16; j++){
345     PRINT_VAR(tmp_var[j]);
346   }
347
348   free(tmp_var);
349   free(tmp_cnst);
350   free(tmp_name);
351   lmm_system_free(Sys);
352 }
353
354 #ifdef __BORLANDC__
355 #pragma argsused
356 #endif
357
358 int main(int argc, char **argv)
359 {
360   xbt_init(&argc,argv);
361
362
363   DEBUG0("**************************** \n");
364   DEBUG0("*****                  ***** \n");
365   DEBUG0("*****      TEST 1      ***** \n");
366   DEBUG0("*****                  ***** \n");
367   DEBUG0("**************************** \n");
368
369
370   DEBUG0("***** Test 1 (Max-Min) ***** \n");
371   test1(MAXMIN);
372 #ifdef HAVE_SDP
373   DEBUG0("***** Test 1 (SDP) ***** \n");
374   test1(SDP);
375 #endif
376   DEBUG0("***** Test 1 (Lagrange - Vegas) ***** \n");
377   test1(LAGRANGE_VEGAS);
378   DEBUG0("***** Test 1 (Lagrange - Reno) ***** \n");
379   test1(LAGRANGE_RENO);
380
381
382   DEBUG0("**************************** \n");
383   DEBUG0("*****                  ***** \n");
384   DEBUG0("*****      TEST 2      ***** \n");
385   DEBUG0("*****                  ***** \n");
386   DEBUG0("**************************** \n");
387
388
389   DEBUG0("***** Test 2 (Max-Min) ***** \n");
390   test2(MAXMIN);
391 #ifdef HAVE_SDP
392   DEBUG0("***** Test 2 (SDP) ***** \n");
393   test2(SDP);
394 #endif
395   DEBUG0("***** Test 2 (Lagrange - Vegas) ***** \n");
396   test2(LAGRANGE_VEGAS);
397   DEBUG0("***** Test 2 (Lagrange - Reno) ***** \n");
398   test2(LAGRANGE_RENO);
399
400
401
402   DEBUG0("**************************** \n");
403   DEBUG0("*****                  ***** \n");
404   DEBUG0("*****      TEST 3      ***** \n");
405   DEBUG0("*****                  ***** \n");
406   DEBUG0("**************************** \n");
407
408
409   DEBUG0("***** Test 3 (Max-Min) ***** \n");
410   test3(MAXMIN);
411 #ifdef HAVE_SDP
412   DEBUG0("***** Test 3 (SDP) ***** \n");
413   test3(SDP);
414 #endif
415   DEBUG0("***** Test 3 (Lagrange - Vegas) ***** \n");
416   test3(LAGRANGE_VEGAS);
417   DEBUG0("***** Test 3 (Lagrange - Reno) ***** \n");
418   test3(LAGRANGE_RENO);
419
420   return 0;
421 }