1 /* A few crash tests for the maxmin library */
3 /* Copyright (c) 2004-2022. 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 "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"
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)
23 std::vector<simgrid::kernel::lmm::Constraint*> constraints(nb_cnst);
24 std::vector<simgrid::kernel::lmm::Variable*> variables(nb_var);
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);
29 for (auto& cnst : constraints) {
30 cnst = Sys.constraint_new(nullptr, simgrid::xbt::random::uniform_real(0.0, 10.0));
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
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));
39 cnst->set_concurrency_limit(l);
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));
47 std::vector<int> used(nb_cnst, 0);
48 for (int j = 0; j < nb_elem; j++) {
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));
59 fprintf(stderr, "Starting to solve(%i)\n", simgrid::xbt::random::uniform_int(0, 999));
60 double date = xbt_os_time();
62 date = (xbt_os_time() - date) * 1e6;
65 fprintf(stderr,"Max concurrency:\n");
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);
73 fprintf(stderr,"(%i):%i/%i ",i,j,k);
74 constraints[i]->reset_concurrency_maximum();
75 xbt_assert(not constraints[i]->get_concurrency_maximum());
79 fprintf(stderr,"\nTotal maximum concurrency is %i\n",l);
84 for (auto const& var : variables)
85 Sys.variable_free(var);
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
98 int main(int argc, char **argv)
100 simgrid::s4u::Engine e(&argc, argv);
102 double rate_no_limit = 0.2;
103 double acc_date = 0.0;
104 double acc_date2 = 0.0;
108 fprintf(stderr, "Syntax: <small|medium|big|huge> <count> [test|debug|perf]\n");
113 if (not strcmp(argv[1], "small"))
115 else if (not strcmp(argv[1], "medium"))
117 else if (not strcmp(argv[1], "big"))
119 else if (not strcmp(argv[1], "huge"))
122 fprintf(stderr, "Unknown class \"%s\", aborting!\n",argv[1]);
127 int testcount=atoi(argv[2]);
129 //Show me everything (debug or performance)!
131 if(argc>=4 && strcmp(argv[3],"test")==0)
133 if(argc>=4 && strcmp(argv[3],"debug")==0)
135 if(argc>=4 && strcmp(argv[3],"perf")==0)
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\' ");
143 xbt_log_control_set("ker_lmm.threshold:DEBUG kernel.threshold:DEBUG");
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)
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):
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);
161 acc_date2+=date*date;
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);
167 fprintf(stderr, "%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);