Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adding test for SURF concurrency feature
[simgrid.git] / teshsuite / surf / maxmin_bench / maxmin_bench.c
1 /* A crash few tests for the maxmin library                                 */
2
3 /* Copyright (c) 2004-2015. 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 #include "surf/maxmin.h"
10 #include "xbt/module.h"
11 #include "xbt/xbt_os_time.h"
12 #include "xbt/sysdep.h"         /* time manipulation for benchmarking */
13
14 #include <stdlib.h>
15 #include <stdio.h>
16
17 double date;
18
19 double float_random(double max);
20 double float_random(double max)
21 {
22   return ((max * rand()) / (RAND_MAX + 1.0));
23 }
24
25 int int_random(int max);
26 int int_random(int max)
27 {
28   return (int) (((max * 1.0) * rand()) / (RAND_MAX + 1.0));
29 }
30
31 void test(int nb_cnst, int nb_var, int nb_elem, int pw_base_limit, int pw_max_limit, float rate_no_limit, int max_share);
32 void test(int nb_cnst, int nb_var, int nb_elem, int pw_base_limit, int pw_max_limit, float rate_no_limit, int max_share)
33 {
34   lmm_system_t Sys = NULL;
35   lmm_constraint_t *cnst = xbt_new0(lmm_constraint_t, nb_cnst);
36   lmm_variable_t *var = xbt_new0(lmm_variable_t, nb_var);
37   int *used = xbt_new0(int, nb_cnst);
38   int i, j, k,l;
39   
40   Sys = lmm_system_new(1);
41
42   for (i = 0; i < nb_cnst; i++) {
43     cnst[i] = lmm_constraint_new(Sys, NULL, float_random(10.0));
44     if(rate_no_limit>float_random(1.0))
45       //Look at what happens when there is no concurrency limit 
46       l=-1;
47     else
48       //Badly logarithmically random concurrency limit in [2^pw_base_limit+1,2^pw_base_limit+2^pw_max_limit]
49       l=(1<<pw_base_limit)+(1<<int_random(pw_max_limit));
50  
51     lmm_constraint_concurrency_limit_set(cnst[i],l );
52   }
53   
54   for (i = 0; i < nb_var; i++) {
55     var[i] = lmm_variable_new(Sys, NULL, 1.0, -1.0, nb_elem);
56     //Have a few variables with a concurrency share of two (e.g. cross-traffic in some cases)
57     lmm_variable_concurrency_share_set(var[i],1+int_random(max_share));
58       
59     for (j = 0; j < nb_cnst; j++)
60       used[j] = 0;
61     for (j = 0; j < nb_elem; j++) {
62       k = int_random(nb_cnst);
63       if (used[k]) {
64         j--;
65         continue;
66       }
67       lmm_expand(Sys, cnst[k], var[i], float_random(1.0));
68       used[k] = 1;
69     }
70   }
71
72   printf("Starting to solve\n");
73   date = xbt_os_time() * 1000000;
74   lmm_solve(Sys);
75   date = xbt_os_time() * 1000000 - date;
76
77   printf("Max concurrency:\n");
78   l=0;
79   for (i = 0; i < nb_cnst; i++) {
80     j=lmm_constraint_concurrency_maximum_get(cnst[i]);
81     k=lmm_constraint_concurrency_limit_get(cnst[i]);
82     xbt_assert(k<0 || j<=k);
83     if(j>l)
84       l=j;
85     printf("(%i):%i/%i ",i,j,k);
86     lmm_constraint_concurrency_maximum_reset(cnst[i]);
87     xbt_assert(!lmm_constraint_concurrency_maximum_get(cnst[i]));
88     if(i%10==9)
89        printf("\n");    
90   }
91   printf("\nTotal maximum concurrency is %i\n",l);
92
93   lmm_print(Sys);
94   
95   for (i = 0; i < nb_var; i++)
96     lmm_variable_free(Sys, var[i]);
97   lmm_system_free(Sys);
98   free(cnst);
99   free(var);
100   free(used);
101   
102 }
103
104 int main(int argc, char **argv)
105 {
106   int nb_cnst = 2000;
107   int nb_var = 2000;
108   int nb_elem; 
109   int pw_base_limit=5; 
110   int pw_max_limit=8;
111   float rate_no_limit=0.2;
112   int max_share=1<<(pw_base_limit/2+1);
113   
114   //If you want to test concurrency, you need nb_elem >> 2^pw_base_limit:
115   nb_elem= (1<<pw_base_limit)+(1<<(8*pw_max_limit/10));
116   //Otherwise, just set it to a constant value (and set rate_no_limit to 1.0):
117   //nb_elem=200
118   
119   xbt_init(&argc, argv);
120   date = xbt_os_time() * 1000000;
121   test(nb_cnst, nb_var, nb_elem, pw_base_limit, pw_max_limit, rate_no_limit,max_share);    
122   printf("One shot execution time for a total of %d constraints, "
123          "%d variables with %d active constraint each : %g microseconds \n",
124          nb_cnst, nb_var, nb_elem, date);
125   return 0;
126 }