Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'file' into 'master'
[simgrid.git] / src / smpi / internals / smpi_utils.cpp
1 /* Copyright (c) 2016-2019. 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 #include "src/surf/xml/platf_private.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 std::string& 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   Tokenizer tokens(smpi_coef_string, sep);
24
25   /**
26    * Iterate over patterns like A:B:C:D;E:F;G:H
27    * These will be broken down into:
28    * A --> B, C, D
29    * E --> F
30    * G --> H
31    */
32   for (Tokenizer::iterator token_iter = tokens.begin(); token_iter != tokens.end(); ++token_iter) {
33     XBT_DEBUG("token : %s", token_iter->c_str());
34     Tokenizer factor_values(*token_iter, factor_separator);
35     s_smpi_factor_t fact;
36     if (factor_values.begin() == factor_values.end()) {
37       xbt_die("Malformed radical for smpi factor: '%s'", smpi_coef_string.c_str());
38     }
39     unsigned int iteration = 0;
40     for (Tokenizer::iterator factor_iter = factor_values.begin(); factor_iter != factor_values.end(); ++factor_iter) {
41       iteration++;
42
43       if (factor_iter == factor_values.begin()) { /* first element */
44         try {
45           fact.factor = std::stoi(*factor_iter);
46         } catch (std::invalid_argument& ia) {
47           throw std::invalid_argument(std::string("Invalid factor in chunk ") + std::to_string(smpi_factor.size() + 1) +
48                                       ": " + *factor_iter);
49         }
50       } else {
51         try {
52           fact.values.push_back(surf_parse_get_time((*factor_iter).c_str(), "smpi factor", std::to_string(smpi_factor.size() + 1) + ": " + *factor_iter));
53         } catch (std::invalid_argument& ia) {
54           throw std::invalid_argument(std::string("Invalid factor value ") + std::to_string(iteration) + " in chunk " +
55                                       std::to_string(smpi_factor.size() + 1) + ": " + *factor_iter);
56         }
57       }
58     }
59
60     smpi_factor.push_back(fact);
61     XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size(), fact.values[0]);
62   }
63   std::sort(smpi_factor.begin(), smpi_factor.end(), [](const s_smpi_factor_t &pa, const s_smpi_factor_t &pb) {
64     return (pa.factor < pb.factor);
65   });
66   for (auto const& fact : smpi_factor) {
67     XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size() ,fact.values[0]);
68   }
69   smpi_factor.shrink_to_fit();
70
71   return smpi_factor;
72 }