Logo AND Algorithmique Numérique Distribuée

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