Logo AND Algorithmique Numérique Distribuée

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