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::System 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));
46 var->set_concurrency_share(concurrency_share);
48 std::vector<int> used(nb_cnst, 0);
49 for (int j = 0; j < nb_elem; j++) {
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));
60 fprintf(stderr, "Starting to solve(%i)\n", simgrid::xbt::random::uniform_int(0, 999));
61 double date = xbt_os_time();
63 date = (xbt_os_time() - date) * 1e6;
66 fprintf(stderr,"Max concurrency:\n");
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);
74 fprintf(stderr,"(%i):%i/%i ",i,j,k);
75 constraints[i]->reset_concurrency_maximum();
76 xbt_assert(not constraints[i]->get_concurrency_maximum());
80 fprintf(stderr,"\nTotal maximum concurrency is %i\n",l);
85 for (auto const& var : variables)
86 Sys.variable_free(var);
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
99 int main(int argc, char **argv)
101 simgrid::s4u::Engine e(&argc, argv);
103 double rate_no_limit = 0.2;
104 double acc_date = 0.0;
105 double acc_date2 = 0.0;
109 fprintf(stderr, "Syntax: <small|medium|big|huge> <count> [test|debug|perf]\n");
114 if (not strcmp(argv[1], "small"))
116 else if (not strcmp(argv[1], "medium"))
118 else if (not strcmp(argv[1], "big"))
120 else if (not strcmp(argv[1], "huge"))
123 fprintf(stderr, "Unknown class \"%s\", aborting!\n",argv[1]);
128 int testcount=atoi(argv[2]);
130 //Show me everything (debug or performance)!
132 if(argc>=4 && strcmp(argv[3],"test")==0)
134 if(argc>=4 && strcmp(argv[3],"debug")==0)
136 if(argc>=4 && strcmp(argv[3],"perf")==0)
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\' ");
144 xbt_log_control_set("surf/maxmin.threshold:DEBUG surf.threshold:DEBUG");
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)
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):
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);
162 acc_date2+=date*date;
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);
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),
173 fprintf(stderr, "Execution time: %g +- %g microseconds \n",mean_date, stdev_date);