Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4f8942b203a8caca92c7231ec6ad94982b58eab1
[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
17 #include "xbt/log.h"
18 #include "xbt/module.h"
19
20
21 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test,"Messages specific for surf example");
22
23 #define PRINT_VAR(var) DEBUG1(#var " = %g",lmm_variable_getvalue(var));
24
25 /*                               */
26 /*        ______                 */
27 /*  ==l1==  L2  ==L3==           */
28 /*        ------                 */
29 /*                               */
30
31 typedef enum {
32   MAXMIN,
33   SDP,
34   LAGRANGE
35 } method_t;
36
37 void test1(method_t method);
38 void test1(method_t method)
39 {
40   lmm_system_t Sys = NULL ;
41   lmm_constraint_t L1 = NULL;
42   lmm_constraint_t L2 = NULL;
43   lmm_constraint_t L3 = NULL;
44
45   lmm_variable_t R_1_2_3 = NULL;
46   lmm_variable_t R_1 = NULL;
47   lmm_variable_t R_2 = NULL;
48   lmm_variable_t R_3 = NULL;
49
50   Sys = lmm_system_new();
51   L1 = lmm_constraint_new(Sys, (void *) "L1", 1.0);
52   L2 = lmm_constraint_new(Sys, (void *) "L2", 10.0);
53   L3 = lmm_constraint_new(Sys, (void *) "L3", 1.0);
54
55   R_1_2_3 = lmm_variable_new(Sys, (void *) "R 1->2->3", 1.0 , -1.0 , 3);
56   R_1 = lmm_variable_new(Sys, (void *) "R 1", 1.0 , -1.0 , 1);
57   R_2 = lmm_variable_new(Sys, (void *) "R 2", 1.0 , -1.0 , 1);
58   R_3 = lmm_variable_new(Sys, (void *) "R 3", 1.0 , -1.0 , 1);
59
60   lmm_expand(Sys, L1, R_1_2_3, 1.0);
61   lmm_expand(Sys, L2, R_1_2_3, 1.0);
62   lmm_expand(Sys, L3, R_1_2_3, 1.0);
63
64   lmm_expand(Sys, L1, R_1, 1.0);
65
66   lmm_expand(Sys, L2, R_2, 1.0);
67
68   lmm_expand(Sys, L3, R_3, 1.0);
69
70   PRINT_VAR(R_1_2_3);
71   PRINT_VAR(R_1);
72   PRINT_VAR(R_2);
73   PRINT_VAR(R_3);
74
75   DEBUG0("\n");
76   if(method==MAXMIN)
77     lmm_solve(Sys);
78 #ifdef HAVE_SDP
79   else if(method==SDP)
80     sdp_solve(Sys);    
81 #endif
82   else if(method==LAGRANGE)
83     lagrange_solve(Sys);  
84   else 
85     xbt_assert0(0,"Invalid method");
86
87   PRINT_VAR(R_1_2_3);
88   PRINT_VAR(R_1);
89   PRINT_VAR(R_2);
90   PRINT_VAR(R_3);
91 /*   DEBUG0("\n"); */
92
93 /*   lmm_update_variable_weight(Sys,R_1_2_3,.5); */
94 /*   lmm_solve(Sys); */
95
96 /*   PRINT_VAR(R_1_2_3); */
97 /*   PRINT_VAR(R_1); */
98 /*   PRINT_VAR(R_2); */
99 /*   PRINT_VAR(R_3); */
100
101   lmm_system_free(Sys);
102
103
104 void test2(method_t method);
105 void test2(method_t method)
106 {
107   lmm_system_t Sys = NULL ;
108   lmm_constraint_t CPU1 = NULL;
109   lmm_constraint_t CPU2 = NULL;
110
111   lmm_variable_t T1 = NULL;
112   lmm_variable_t T2 = NULL;
113
114   Sys = lmm_system_new();
115   CPU1 = lmm_constraint_new(Sys, (void *) "CPU1", 200.0);
116   CPU2 = lmm_constraint_new(Sys, (void *) "CPU2", 100.0);
117
118   T1 = lmm_variable_new(Sys, (void *) "T1", 1.0 , -1.0 , 1);
119   T2 = lmm_variable_new(Sys, (void *) "T2", 1.0 , -1.0 , 1);
120
121   lmm_expand(Sys, CPU1, T1, 1.0);
122   lmm_expand(Sys, CPU2, T2, 1.0);
123
124   PRINT_VAR(T1);
125   PRINT_VAR(T2);
126
127   DEBUG0("\n");
128   if(method==MAXMIN)
129     lmm_solve(Sys);
130 #ifdef HAVE_SDP
131   else if(method==SDP)
132     sdp_solve(Sys);    
133 #endif
134   else if(method==LAGRANGE)
135     lagrange_solve(Sys);  
136   else 
137     xbt_assert0(0,"Invalid method");
138
139   PRINT_VAR(T1);
140   PRINT_VAR(T2);
141
142   DEBUG0("\n");
143
144   lmm_system_free(Sys);
145 }
146
147 void test3(method_t method);
148 void test3(method_t method)
149 {
150   int flows=11;  //flows and conexions are synonnims ?
151   int links=10;  //topology info
152   
153   //just to be carefull
154   int i = 0;
155   int j = 0;
156
157   double **A;
158
159   lmm_system_t Sys = NULL ;
160   lmm_constraint_t *tmp_cnst = NULL;
161   lmm_variable_t   *tmp_var  = NULL;
162   char tmp_name[13];
163
164
165   /*array to add the the constraints of fictiv variables */
166   double B[15] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
167                 1, 1, 1, 1, 1};
168
169   A = (double **)calloc(links+5, sizeof(double));
170  
171   for(i=0 ; i< links+5; i++){
172     A[i] = (double *)calloc(flows+5, sizeof(double));
173
174     for(j=0 ; j< flows+5; j++){
175       A[i][j] = 0.0;
176
177       if(i >= links || j >= flows){
178           A[i][j] = 0.0;
179       }
180     }
181   }
182
183   /*matrix that store the constraints/topollogy*/
184   /*double A[15][16]=
185     {{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,    0, 0, 0, 0, 0},
186      {0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0,    0, 0, 0, 0, 0},
187      {0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0,    0, 0, 0, 0, 0},
188      {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,    0, 0, 0, 0, 0},
189      {1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0,    0, 0, 0, 0, 0},
190      {1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0,    0, 0, 0, 0, 0},
191      {1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1,    0, 0, 0, 0, 0},
192      {0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1,    0, 0, 0, 0, 0},
193      {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,    0, 0, 0, 0, 0},
194      {0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0,    0, 0, 0, 0, 0},
195      
196      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    1, 0, 0, 0, 0},
197      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 1, 0, 0, 0},
198      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 1, 0, 0},
199      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 1, 0},
200      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 1}
201      }; */
202
203   A[0][1]  = 1.0;
204   A[0][7]  = 1.0;
205   
206   A[1][1]  = 1.0;
207   A[1][7]  = 1.0;
208   A[1][8]  = 1.0;
209
210   A[2][1]  = 1.0;
211   A[2][8]  = 1.0;
212
213   A[2][1]  = 1.0;
214   A[2][8]  = 1.0;
215
216   A[3][8]  = 1.0;
217
218   A[4][0]  = 1.0;
219   A[4][3]  = 1.0;
220   A[4][9]  = 1.0;
221
222   A[5][0]  = 1.0;
223   A[5][3]  = 1.0;
224   A[5][4]  = 1.0;
225   A[5][9]  = 1.0;
226
227   A[6][0]  = 1.0;
228   A[6][4]  = 1.0;
229   A[6][9]  = 1.0;
230   A[6][10] = 1.0;
231
232   A[7][2]  = 1.0;
233   A[7][4]  = 1.0;
234   A[7][6]  = 1.0;
235   A[7][9]  = 1.0;
236   A[7][10] = 1.0;
237
238   A[8][2]  = 1.0;
239   A[8][10] = 1.0;
240
241   A[9][5]  = 1.0;
242   A[9][6]  = 1.0;
243   A[9][9]  = 1.0;
244
245   
246   A[10][11] = 1.0;
247   A[11][12] = 1.0;
248   A[12][13] = 1.0;
249   A[13][14] = 1.0;
250   A[14][15] = 1.0;
251
252
253   Sys = lmm_system_new();
254   
255   
256   /*
257    * Creates the constraints
258    */
259   tmp_cnst = calloc(15, sizeof(lmm_constraint_t));
260   for(i=0; i<15; i++){
261     sprintf(tmp_name, "C_%03d", i); 
262     tmp_cnst[i] = lmm_constraint_new(Sys, (void *) tmp_name, B[i]);
263   }
264
265
266   /*
267    * Creates the variables
268    */
269   tmp_var = calloc(16, sizeof(lmm_variable_t));
270   for(j=0; j<16; j++){
271     sprintf(tmp_name, "X_%03d", j); 
272     tmp_var[j] = lmm_variable_new(Sys, (void *) tmp_name, 1.0, -1.0 , 15);
273   }
274
275   /*
276    * Link constraints and variables
277    */
278   for(i=0;i<15;i++) { 
279     for(j=0; j<16; j++){
280       if(A[i][j]){
281         lmm_expand(Sys, tmp_cnst[i], tmp_var[j], 1.0); 
282       }
283     }
284   }
285
286   for(j=0; j<16; j++){
287     PRINT_VAR(tmp_var[j]);
288   }
289
290   if(method==MAXMIN)
291     lmm_solve(Sys);
292 #ifdef HAVE_SDP
293   else if(method==SDP)
294     sdp_solve(Sys);    
295 #endif
296   else if(method==LAGRANGE)
297     lagrange_solve(Sys);  
298   else 
299     xbt_assert0(0,"Invalid method");
300
301   for(j=0; j<16; j++){
302     PRINT_VAR(tmp_var[j]);
303   }
304
305   free(tmp_var);
306   free(tmp_cnst);
307   lmm_system_free(Sys);
308 }
309
310 #ifdef __BORLANDC__
311 #pragma argsused
312 #endif
313
314 int main(int argc, char **argv)
315 {
316   xbt_init(&argc,argv);
317
318 /*   DEBUG0("***** Test 1 (Max-Min) ***** \n"); */
319 /*   test1(MAXMIN); */
320 /* #ifdef HAVE_SDP */
321 /*   DEBUG0("***** Test 1 (SDP) ***** \n"); */
322 /*   test1(SDP); */
323 /* #endif */
324 /*   DEBUG0("***** Test 1 (Lagrange - dicotomi) ***** \n"); */
325 /*   test1(LAGRANGE); */
326
327
328 /*   DEBUG0("***** Test 2 (Max-Min) ***** \n"); */
329 /*   test2(MAXMIN); */
330 /* #ifdef HAVE_SDP */
331 /*   DEBUG0("***** Test 2 (SDP) ***** \n"); */
332 /*   test2(SDP); */
333 /* #endif */
334 /*   DEBUG0("***** Test 2 (Lagrange) ***** \n"); */
335 /*   test2(LAGRANGE); */
336
337
338
339 /*   DEBUG0("***** Test 3 (Max-Min) ***** \n"); */
340 /*   test3(MAXMIN); */
341 #ifdef HAVE_SDP
342   DEBUG0("***** Test 3 (SDP) ***** \n");
343   test3(SDP);
344 #endif
345   DEBUG0("***** Test 3 (Lagrange) ***** \n");
346   test3(LAGRANGE);
347
348
349   return 0;
350 }