Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Parameter is not used.
[simgrid.git] / src / smpi / internals / smpi_utils.cpp
1 /* Copyright (c) 2016-2018. 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 #include "xbt/log.h"
9 #include "xbt/sysdep.h"
10 #include <boost/tokenizer.hpp>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_utils, smpi, "Logging specific to SMPI (utils)");
13
14 std::vector<s_smpi_factor_t> parse_factor(std::string smpi_coef_string)
15 {
16   std::vector<s_smpi_factor_t> smpi_factor;
17
18   /** Setup the tokenizer that parses the string **/
19   typedef boost::tokenizer<boost::char_separator<char>> Tokenizer;
20   boost::char_separator<char> sep(";");
21   boost::char_separator<char> factor_separator(":");
22   Tokenizer tokens(smpi_coef_string, sep);
23
24   /**
25    * Iterate over patterns like A:B:C:D;E:F;G:H
26    * These will be broken down into:
27    * A --> B, C, D
28    * E --> F
29    * G --> H
30    */
31   for (Tokenizer::iterator token_iter = tokens.begin(); token_iter != tokens.end(); ++token_iter) {
32     XBT_DEBUG("token : %s", token_iter->c_str());
33     Tokenizer factor_values(*token_iter, factor_separator);
34     s_smpi_factor_t fact;
35     if (factor_values.begin() == factor_values.end()) {
36       xbt_die("Malformed radical for smpi factor: '%s'", smpi_coef_string.c_str());
37     }
38     unsigned int iteration = 0;
39     for (Tokenizer::iterator factor_iter = factor_values.begin(); factor_iter != factor_values.end(); ++factor_iter) {
40       iteration++;
41
42       if (factor_iter == factor_values.begin()) { /* first element */
43         try {
44           fact.factor = std::stoi(*factor_iter);
45         } catch (std::invalid_argument& ia) {
46           throw std::invalid_argument(std::string("Invalid factor in chunk ") + std::to_string(smpi_factor.size() + 1) +
47                                       ": " + *factor_iter);
48         }
49       } else {
50         try {
51           fact.values.push_back(std::stod(*factor_iter));
52         } catch (std::invalid_argument& ia) {
53           throw std::invalid_argument(std::string("Invalid factor value ") + std::to_string(iteration) + " in chunk " +
54                                       std::to_string(smpi_factor.size() + 1) + ": " + *factor_iter);
55         }
56       }
57     }
58
59     smpi_factor.push_back(fact);
60     XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size(), fact.values[0]);
61   }
62   std::sort(smpi_factor.begin(), smpi_factor.end(), [](const s_smpi_factor_t &pa, const s_smpi_factor_t &pb) {
63     return (pa.factor < pb.factor);
64   });
65   for (auto const& fact : smpi_factor) {
66     XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size() ,fact.values[0]);
67   }
68   smpi_factor.shrink_to_fit();
69
70   return smpi_factor;
71 }