1 /* A few crash tests for the maxmin library */
3 /* Copyright (c) 2004-2023. The SimGrid Team. All rights reserved. */
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. */
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"
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)
22 std::vector<simgrid::kernel::lmm::Constraint*> constraints(nb_cnst);
23 std::vector<simgrid::kernel::lmm::Variable*> variables(nb_var);
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);
28 for (auto& cnst : constraints) {
29 cnst = Sys.constraint_new(nullptr, simgrid::xbt::random::uniform_real(0.0, 10.0));
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
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));
38 cnst->set_concurrency_limit(l);
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));
46 std::vector<int> used(nb_cnst, 0);
47 for (int j = 0; j < nb_elem; j++) {
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));
58 fprintf(stderr, "Starting to solve(%i)\n", simgrid::xbt::random::uniform_int(0, 999));
59 double date = xbt_os_time();
61 date = (xbt_os_time() - date) * 1e6;
64 fprintf(stderr, "Max concurrency:\n");
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);
72 fprintf(stderr, "(%i):%i/%i ", i, j, k);
73 constraints[i]->reset_concurrency_maximum();
74 xbt_assert(not constraints[i]->get_concurrency_maximum());
76 fprintf(stderr, "\n");
78 fprintf(stderr, "\nTotal maximum concurrency is %i\n", l);
83 for (auto const& var : variables)
84 Sys.variable_free(var);
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
97 int main(int argc, char** argv)
99 simgrid::s4u::Engine e(&argc, argv);
101 double rate_no_limit = 0.2;
102 double acc_date = 0.0;
103 double acc_date2 = 0.0;
107 fprintf(stderr, "Syntax: <small|medium|big|huge> <count> [test|debug|perf]\n");
112 if (not strcmp(argv[1], "small"))
114 else if (not strcmp(argv[1], "medium"))
116 else if (not strcmp(argv[1], "big"))
118 else if (not strcmp(argv[1], "huge"))
121 fprintf(stderr, "Unknown class \"%s\", aborting!\n", argv[1]);
126 int testcount = atoi(argv[2]);
128 // Show me everything (debug or performance)!
130 if (argc >= 4 && strcmp(argv[3], "test") == 0)
132 if (argc >= 4 && strcmp(argv[3], "debug") == 0)
134 if (argc >= 4 && strcmp(argv[3], "perf") == 0)
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' ");
142 xbt_log_control_set("ker_lmm.threshold:DEBUG kernel.threshold:DEBUG");
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)
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):
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);
160 acc_date2 += date * date;
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);
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),
172 fprintf(stderr, "Execution time: %g +- %g microseconds \n", mean_date, stdev_date);