1 /* Copyright (c) 2016-2021. 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_utils.hpp"
9 #include "src/surf/xml/platf_private.hpp"
11 #include "xbt/parse_units.hpp"
12 #include "xbt/sysdep.h"
13 #include <boost/tokenizer.hpp>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_utils, smpi, "Logging specific to SMPI (utils)");
17 extern std::string surf_parsed_filename;
18 extern int surf_parse_lineno;
20 std::vector<s_smpi_factor_t> parse_factor(const std::string& smpi_coef_string)
22 std::vector<s_smpi_factor_t> smpi_factor;
24 /** Setup the tokenizer that parses the string **/
25 using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
26 boost::char_separator<char> sep(";");
27 boost::char_separator<char> factor_separator(":");
28 Tokenizer tokens(smpi_coef_string, sep);
31 * Iterate over patterns like A:B:C:D;E:F;G:H
32 * These will be broken down into:
37 for (Tokenizer::iterator token_iter = tokens.begin(); token_iter != tokens.end(); ++token_iter) {
38 XBT_DEBUG("token : %s", token_iter->c_str());
39 Tokenizer factor_values(*token_iter, factor_separator);
41 if (factor_values.begin() == factor_values.end()) {
42 xbt_die("Malformed radical for smpi factor: '%s'", smpi_coef_string.c_str());
44 unsigned int iteration = 0;
45 for (Tokenizer::iterator factor_iter = factor_values.begin(); factor_iter != factor_values.end(); ++factor_iter) {
48 if (factor_iter == factor_values.begin()) { /* first element */
50 fact.factor = std::stoi(*factor_iter);
51 } catch (const std::invalid_argument&) {
52 throw std::invalid_argument(std::string("Invalid factor in chunk ") + std::to_string(smpi_factor.size() + 1) +
57 fact.values.push_back(
58 xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, (*factor_iter).c_str(), "smpi factor", ""));
59 } catch (const std::invalid_argument&) {
60 throw std::invalid_argument(std::string("Invalid factor value ") + std::to_string(iteration) + " in chunk " +
61 std::to_string(smpi_factor.size() + 1) + ": " + *factor_iter);
66 smpi_factor.push_back(fact);
67 XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size(), fact.values[0]);
69 std::sort(smpi_factor.begin(), smpi_factor.end(), [](const s_smpi_factor_t &pa, const s_smpi_factor_t &pb) {
70 return (pa.factor < pb.factor);
72 for (auto const& fact : smpi_factor) {
73 XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size() ,fact.values[0]);
75 smpi_factor.shrink_to_fit();