1 /* Copyright (c) 2016-2017. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "smpi/smpi_utils.hpp"
8 #include "xbt/sysdep.h"
11 #include <boost/tokenizer.hpp>
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_utils, smpi, "Logging specific to SMPI (utils)");
15 std::vector<s_smpi_factor_t> parse_factor(const char *smpi_coef_string)
17 std::vector<s_smpi_factor_t> smpi_factor;
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);
27 * Iterate over patterns like A:B:C:D;E:F;G:H
28 * These will be broken down into:
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);
37 if (factor_values.begin() == factor_values.end()) {
38 xbt_die("Malformed radical for smpi factor: '%s'", smpi_coef_string);
40 unsigned int iteration = 0;
41 for (Tokenizer::iterator factor_iter = factor_values.begin(); factor_iter != factor_values.end(); ++factor_iter) {
44 if (factor_iter == factor_values.begin()) { /* first element */
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) +
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);
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]);
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);
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]);
70 smpi_factor.shrink_to_fit();