Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / src / smpi / internals / smpi_utils.cpp
1 /* Copyright (c) 2016-2021. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "smpi_utils.hpp"
8
9 #include "src/surf/xml/platf_private.hpp"
10 #include "xbt/log.h"
11 #include "xbt/parse_units.hpp"
12 #include "xbt/sysdep.h"
13 #include <boost/tokenizer.hpp>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_utils, smpi, "Logging specific to SMPI (utils)");
16
17 extern std::string surf_parsed_filename;
18 extern int surf_parse_lineno;
19
20 std::vector<s_smpi_factor_t> parse_factor(const std::string& smpi_coef_string)
21 {
22   std::vector<s_smpi_factor_t> smpi_factor;
23
24   /** Setup the tokenizer that parses the string **/
25   using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
26   boost::char_separator<char> sep(";");
27   boost::char_separator<char> factor_separator(":");
28   Tokenizer tokens(smpi_coef_string, sep);
29
30   /**
31    * Iterate over patterns like A:B:C:D;E:F;G:H
32    * These will be broken down into:
33    * A --> B, C, D
34    * E --> F
35    * G --> H
36    */
37   for (Tokenizer::iterator token_iter = tokens.begin(); token_iter != tokens.end(); ++token_iter) {
38     XBT_DEBUG("token : %s", token_iter->c_str());
39     Tokenizer factor_values(*token_iter, factor_separator);
40     s_smpi_factor_t fact;
41     if (factor_values.begin() == factor_values.end()) {
42       xbt_die("Malformed radical for smpi factor: '%s'", smpi_coef_string.c_str());
43     }
44     unsigned int iteration = 0;
45     for (Tokenizer::iterator factor_iter = factor_values.begin(); factor_iter != factor_values.end(); ++factor_iter) {
46       iteration++;
47
48       if (factor_iter == factor_values.begin()) { /* first element */
49         try {
50           fact.factor = std::stoi(*factor_iter);
51         } catch (const std::invalid_argument&) {
52           throw std::invalid_argument(std::string("Invalid factor in chunk ") + std::to_string(smpi_factor.size() + 1) +
53                                       ": " + *factor_iter);
54         }
55       } else {
56         try {
57           fact.values.push_back(
58               xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, (*factor_iter).c_str(), "smpi factor", ""));
59         } catch (const std::invalid_argument&) {
60           throw std::invalid_argument(std::string("Invalid factor value ") + std::to_string(iteration) + " in chunk " +
61                                       std::to_string(smpi_factor.size() + 1) + ": " + *factor_iter);
62         }
63       }
64     }
65
66     smpi_factor.push_back(fact);
67     XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size(), fact.values[0]);
68   }
69   std::sort(smpi_factor.begin(), smpi_factor.end(), [](const s_smpi_factor_t &pa, const s_smpi_factor_t &pb) {
70     return (pa.factor < pb.factor);
71   });
72   for (auto const& fact : smpi_factor) {
73     XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size() ,fact.values[0]);
74   }
75   smpi_factor.shrink_to_fit();
76
77   return smpi_factor;
78 }