Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move the surf tesh tests with the other models' ones
[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/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::MaxMin 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
47     std::vector<int> used(nb_cnst, 0);
48     for (int j = 0; j < nb_elem; j++) {
49       int k;
50       do {
51         k = simgrid::xbt::random::uniform_int(0, nb_cnst - 1);
52       } while (used[k] >= concurrency_share);
53       Sys.expand(constraints[k], var, simgrid::xbt::random::uniform_real(0.0, 1.5));
54       Sys.expand(constraints[k], var, simgrid::xbt::random::uniform_real(0.0, 1.5));
55       used[k]++;
56     }
57   }
58
59   fprintf(stderr, "Starting to solve(%i)\n", simgrid::xbt::random::uniform_int(0, 999));
60   double date = xbt_os_time();
61   Sys.solve();
62   date = (xbt_os_time() - date) * 1e6;
63
64   if (mode == 2) {
65     fprintf(stderr, "Max concurrency:\n");
66     int l = 0;
67     for (int i = 0; i < nb_cnst; i++) {
68       int j = constraints[i]->get_concurrency_maximum();
69       int k = constraints[i]->get_concurrency_limit();
70       xbt_assert(k < 0 || j <= k);
71       if (j > l)
72         l = j;
73       fprintf(stderr, "(%i):%i/%i ", i, j, k);
74       constraints[i]->reset_concurrency_maximum();
75       xbt_assert(not constraints[i]->get_concurrency_maximum());
76       if (i % 10 == 9)
77         fprintf(stderr, "\n");
78     }
79     fprintf(stderr, "\nTotal maximum concurrency is %i\n", l);
80
81     Sys.print();
82   }
83
84   for (auto const& var : variables)
85     Sys.variable_free(var);
86
87   return date;
88 }
89
90 constexpr std::array<std::array<unsigned int, 4>, 4> TestClasses{{
91     // Nbcnst Nbvar Baselimit Maxlimit
92     {{10, 10, 1, 2}},       // small
93     {{100, 100, 3, 6}},     // medium
94     {{2000, 2000, 5, 8}},   // big
95     {{20000, 20000, 7, 10}} // huge
96 }};
97
98 int main(int argc, char** argv)
99 {
100   simgrid::s4u::Engine e(&argc, argv);
101
102   double rate_no_limit = 0.2;
103   double acc_date      = 0.0;
104   double acc_date2     = 0.0;
105   int testclass;
106
107   if (argc < 3) {
108     fprintf(stderr, "Syntax: <small|medium|big|huge> <count> [test|debug|perf]\n");
109     return -1;
110   }
111
112   // what class?
113   if (not strcmp(argv[1], "small"))
114     testclass = 0;
115   else if (not strcmp(argv[1], "medium"))
116     testclass = 1;
117   else if (not strcmp(argv[1], "big"))
118     testclass = 2;
119   else if (not strcmp(argv[1], "huge"))
120     testclass = 3;
121   else {
122     fprintf(stderr, "Unknown class \"%s\", aborting!\n", argv[1]);
123     return -2;
124   }
125
126   // How many times?
127   int testcount = atoi(argv[2]);
128
129   // Show me everything (debug or performance)!
130   int mode = 0;
131   if (argc >= 4 && strcmp(argv[3], "test") == 0)
132     mode = 1;
133   if (argc >= 4 && strcmp(argv[3], "debug") == 0)
134     mode = 2;
135   if (argc >= 4 && strcmp(argv[3], "perf") == 0)
136     mode = 3;
137
138   if (mode == 1)
139     xbt_log_control_set("ker_lmm.threshold:DEBUG ker_lmm.fmt:'[%r]: [%c/%p] %m%n' "
140                         "kernel.threshold:DEBUG kernel.fmt:'[%r]: [%c/%p] %m%n' ");
141
142   if (mode == 2)
143     xbt_log_control_set("ker_lmm.threshold:DEBUG kernel.threshold:DEBUG");
144
145   unsigned int nb_cnst       = TestClasses[testclass][0];
146   unsigned int nb_var        = TestClasses[testclass][1];
147   unsigned int pw_base_limit = TestClasses[testclass][2];
148   unsigned int pw_max_limit  = TestClasses[testclass][3];
149   unsigned int max_share     = 2; // 1<<(pw_base_limit/2+1)
150
151   // If you want to test concurrency, you need nb_elem >> 2^pw_base_limit:
152   unsigned int nb_elem = (1 << pw_base_limit) + (1 << (8 * pw_max_limit / 10));
153   // Otherwise, just set it to a constant value (and set rate_no_limit to 1.0):
154   // nb_elem=200
155
156   for (int i = 0; i < testcount; i++) {
157     simgrid::xbt::random::set_mersenne_seed(i + 1);
158     fprintf(stderr, "Starting %i: (%i)\n", i, simgrid::xbt::random::uniform_int(0, 999));
159     double date = test(nb_cnst, nb_var, nb_elem, pw_base_limit, pw_max_limit, rate_no_limit, max_share, mode);
160     acc_date += date;
161     acc_date2 += date * date;
162   }
163
164   double mean_date  = acc_date / static_cast<double>(testcount);
165   double stdev_date = sqrt(acc_date2 / static_cast<double>(testcount) - mean_date * mean_date);
166
167   fprintf(stderr,
168           "%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 }