Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[simgrid.git] / src / smpi / smpi_utils.cpp
1 /* Copyright (c) 2016. 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();
42          factor_iter != factor_values.end(); factor_iter++, iteration++) {
43       char *errmsg;
44
45       if (factor_iter == factor_values.begin()) { /* first element */
46         errmsg = bprintf("Invalid factor in chunk #%zu: %%s", smpi_factor.size()+1);
47         fact.factor = xbt_str_parse_int(factor_iter->c_str(), errmsg);
48       }
49       else {
50         errmsg = bprintf("Invalid factor value %d in chunk #%zu: %%s", iteration, smpi_factor.size()+1);
51         fact.values.push_back(xbt_str_parse_double(factor_iter->c_str(), errmsg));
52       }
53       xbt_free(errmsg);
54     }
55
56     smpi_factor.push_back(fact);
57     XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size(), fact.values[0]);
58   }
59   std::sort(smpi_factor.begin(), smpi_factor.end(), [](const s_smpi_factor_t &pa, const s_smpi_factor_t &pb) {
60     return (pa.factor < pb.factor);
61   });
62   for (auto& fact : smpi_factor) {
63     XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size() ,fact.values[0]);
64   }
65   smpi_factor.shrink_to_fit();
66
67   return smpi_factor;
68 }