Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a2cc1ff76c191553955158cff936486d9c234120
[simgrid.git] / teshsuite / surf / maxmin_bench / maxmin_bench.cpp
1 /* A few crash tests for the maxmin library                                 */
2
3 /* Copyright (c) 2004-2022. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "src/kernel/lmm/maxmin.hpp"
9 #include "simgrid/s4u/Engine.hpp"
10 #include "xbt/module.h"
11 #include "xbt/random.hpp"
12 #include "xbt/sysdep.h" /* time manipulation for benchmarking */
13 #include "xbt/xbt_os_time.h"
14
15 #include <array>
16 #include <cstdint>
17 #include <cstdio>
18 #include <cstdlib>
19
20 static double test(int nb_cnst, int nb_var, int nb_elem, unsigned int pw_base_limit, unsigned int pw_max_limit,
21                    double rate_no_limit, int max_share, int mode)
22 {
23   std::vector<simgrid::kernel::lmm::Constraint*> constraints(nb_cnst);
24   std::vector<simgrid::kernel::lmm::Variable*> variables(nb_var);
25
26   /* We cannot activate the selective update as we pass nullptr as an Action when creating the variables */
27   simgrid::kernel::lmm::System Sys(false);
28
29   for (auto& cnst : constraints) {
30     cnst = Sys.constraint_new(nullptr, simgrid::xbt::random::uniform_real(0.0, 10.0));
31     int l;
32     if (rate_no_limit > simgrid::xbt::random::uniform_real(0.0, 1.0)) {
33       // Look at what happens when there is no concurrency limit
34       l = -1;
35     } else {
36       // Badly logarithmically random concurrency limit in [2^pw_base_limit+1,2^pw_base_limit+2^pw_max_limit]
37       l = (1 << pw_base_limit) + (1 << simgrid::xbt::random::uniform_int(0, pw_max_limit - 1));
38     }
39     cnst->set_concurrency_limit(l);
40   }
41
42   for (auto& var : variables) {
43     var = Sys.variable_new(nullptr, 1.0, -1.0, nb_elem);
44     //Have a few variables with a concurrency share of two (e.g. cross-traffic in some cases)
45     short concurrency_share = 1 + static_cast<short>(simgrid::xbt::random::uniform_int(0, max_share - 1));
46     var->set_concurrency_share(concurrency_share);
47
48     std::vector<int> used(nb_cnst, 0);
49     for (int j = 0; j < nb_elem; j++) {
50       int k;
51       do {
52         k = simgrid::xbt::random::uniform_int(0, nb_cnst - 1);
53       } while (used[k] >= concurrency_share);
54       Sys.expand(constraints[k], var, simgrid::xbt::random::uniform_real(0.0, 1.5));
55       Sys.expand_add(constraints[k], var, simgrid::xbt::random::uniform_real(0.0, 1.5));
56       used[k]++;
57     }
58   }
59
60   fprintf(stderr, "Starting to solve(%i)\n", simgrid::xbt::random::uniform_int(0, 999));
61   double date = xbt_os_time();
62   Sys.solve();
63   date = (xbt_os_time() - date) * 1e6;
64
65   if(mode==2){
66     fprintf(stderr,"Max concurrency:\n");
67     int l=0;
68     for (int i = 0; i < nb_cnst; i++) {
69       int j = constraints[i]->get_concurrency_maximum();
70       int k = constraints[i]->get_concurrency_limit();
71       xbt_assert(k<0 || j<=k);
72       if(j>l)
73         l=j;
74       fprintf(stderr,"(%i):%i/%i ",i,j,k);
75       constraints[i]->reset_concurrency_maximum();
76       xbt_assert(not constraints[i]->get_concurrency_maximum());
77       if(i%10==9)
78         fprintf(stderr,"\n");
79     }
80     fprintf(stderr,"\nTotal maximum concurrency is %i\n",l);
81
82     Sys.print();
83   }
84
85   for (auto const& var : variables)
86     Sys.variable_free(var);
87
88   return date;
89 }
90
91 constexpr std::array<std::array<unsigned int, 4>, 4> TestClasses{{
92     // Nbcnst Nbvar Baselimit Maxlimit
93     {{10, 10, 1, 2}},       // small
94     {{100, 100, 3, 6}},     // medium
95     {{2000, 2000, 5, 8}},   // big
96     {{20000, 20000, 7, 10}} // huge
97 }};
98
99 int main(int argc, char **argv)
100 {
101   simgrid::s4u::Engine e(&argc, argv);
102
103   double rate_no_limit = 0.2;
104   double acc_date      = 0.0;
105   double acc_date2     = 0.0;
106   int testclass;
107
108   if(argc<3) {
109     fprintf(stderr, "Syntax: <small|medium|big|huge> <count> [test|debug|perf]\n");
110     return -1;
111   }
112
113   //what class?
114   if (not strcmp(argv[1], "small"))
115     testclass = 0;
116   else if (not strcmp(argv[1], "medium"))
117     testclass = 1;
118   else if (not strcmp(argv[1], "big"))
119     testclass = 2;
120   else if (not strcmp(argv[1], "huge"))
121     testclass = 3;
122   else {
123     fprintf(stderr, "Unknown class \"%s\", aborting!\n",argv[1]);
124     return -2;
125   }
126
127   //How many times?
128   int testcount=atoi(argv[2]);
129
130   //Show me everything (debug or performance)!
131   int mode=0;
132   if(argc>=4 && strcmp(argv[3],"test")==0)
133     mode=1;
134   if(argc>=4 && strcmp(argv[3],"debug")==0)
135     mode=2;
136   if(argc>=4 && strcmp(argv[3],"perf")==0)
137     mode=3;
138
139   if(mode==1)
140     xbt_log_control_set("surf/maxmin.threshold:DEBUG surf/maxmin.fmt:\'[%r]: [%c/%p] %m%n\' "
141                         "surf.threshold:DEBUG surf.fmt:\'[%r]: [%c/%p] %m%n\' ");
142
143   if(mode==2)
144     xbt_log_control_set("surf/maxmin.threshold:DEBUG surf.threshold:DEBUG");
145
146   unsigned int nb_cnst= TestClasses[testclass][0];
147   unsigned int nb_var= TestClasses[testclass][1];
148   unsigned int pw_base_limit= TestClasses[testclass][2];
149   unsigned int pw_max_limit= TestClasses[testclass][3];
150   unsigned int max_share    = 2; // 1<<(pw_base_limit/2+1)
151
152   //If you want to test concurrency, you need nb_elem >> 2^pw_base_limit:
153   unsigned int nb_elem= (1<<pw_base_limit)+(1<<(8*pw_max_limit/10));
154   //Otherwise, just set it to a constant value (and set rate_no_limit to 1.0):
155   //nb_elem=200
156
157   for(int i=0;i<testcount;i++){
158     simgrid::xbt::random::set_mersenne_seed(i + 1);
159     fprintf(stderr, "Starting %i: (%i)\n", i, simgrid::xbt::random::uniform_int(0, 999));
160     double date = test(nb_cnst, nb_var, nb_elem, pw_base_limit, pw_max_limit, rate_no_limit, max_share, mode);
161     acc_date+=date;
162     acc_date2+=date*date;
163   }
164
165   double mean_date  = acc_date / static_cast<double>(testcount);
166   double stdev_date = sqrt(acc_date2 / static_cast<double>(testcount) - mean_date * mean_date);
167
168   fprintf(stderr, "%ix One shot execution time for a total of %u constraints, "
169                   "%u variables with %u active constraint each, concurrency in [%i,%i] and max concurrency share %u\n",
170           testcount, nb_cnst, nb_var, nb_elem, (1 << pw_base_limit), (1 << pw_base_limit) + (1 << pw_max_limit),
171           max_share);
172   if(mode==3)
173     fprintf(stderr, "Execution time: %g +- %g  microseconds \n",mean_date, stdev_date);
174
175   return 0;
176 }