ARTEMIS
MsgLogger.H
Go to the documentation of this file.
1 /* Copyright 2021 Luca Fedeli
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 
8 #ifndef ABLASTR_MSG_LOGGER_H_
9 #define ABLASTR_MSG_LOGGER_H_
10 
11 #include <AMReX.H>
12 
13 #include <cstdint>
14 #include <map>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
20 {
24  enum class Priority
25  {
27  low,
29  medium,
31  high
32  };
33 
41  std::string PriorityToString(const Priority& priority);
42 
50  Priority StringToPriority(const std::string& priority_string);
51 
57  struct Msg
58  {
59  std::string topic ;
60  std::string text ;
62 
68  std::vector<char> serialize() const;
69 
76  static Msg deserialize(std::vector<char>::const_iterator& it);
77 
85  static Msg deserialize(std::vector<char>::const_iterator&& it);
86  };
87 
95  {
96  Msg msg ;
97  std::int64_t counter ;
98 
104  std::vector<char> serialize() const;
105 
112  static MsgWithCounter deserialize(std::vector<char>::const_iterator& it);
113 
121  static MsgWithCounter deserialize(std::vector<char>::const_iterator&& it);
122  };
123 
133  {
135  bool all_ranks ;
136  std::vector<int> ranks ;
137 
143  std::vector<char> serialize() const;
144 
151  static MsgWithCounterAndRanks deserialize(std::vector<char>::const_iterator& it);
152 
160  static MsgWithCounterAndRanks deserialize(std::vector<char>::const_iterator&& it);
161  };
162 
173  constexpr bool operator<(const Msg& l, const Msg& r)
174  {
175  return
176  (l.priority > r.priority) ||
177  ((l.priority == r.priority) && (l.topic < r.topic)) ||
178  ((l.priority == r.priority) && (l.topic == r.topic) && (l.text < r.text));
179  }
180 
185  class Logger
186  {
187  public:
188 
192  Logger();
193 
199  void record_msg(Msg msg);
200 
206  std::vector<Msg> get_msgs() const;
207 
214  std::vector<MsgWithCounter> get_msgs_with_counter() const;
215 
223  std::vector<MsgWithCounterAndRanks>
225 
226  private:
227 
234  std::vector<MsgWithCounterAndRanks>
236 
237 #ifdef AMREX_USE_MPI
246  std::pair<int, int> find_gather_rank_and_its_msgs(
247  int how_many_msgs) const;
248 
259  std::vector<MsgWithCounterAndRanks>
261  const std::map<Msg,std::int64_t>& my_msg_map,
262  const std::vector<char>& all_data,
263  const std::vector<int>& displacements,
264  const int gather_rank
265  ) const;
266 
267 
275  void
277  std::vector<MsgWithCounterAndRanks>& msgs_with_counter_and_ranks,
278  int gather_rank) const;
279 
280 #endif
281 
282  const int m_rank ;
283  const int m_num_procs ;
284  const int m_io_rank ;
285 
286  std::map<Msg, std::int64_t> m_messages ;
287  };
288 }
289 
290 #endif //ABLASTR_MSG_LOGGER_H_
Definition: MsgLogger.H:186
const int m_io_rank
Definition: MsgLogger.H:284
std::map< Msg, std::int64_t > m_messages
Definition: MsgLogger.H:286
std::pair< int, int > find_gather_rank_and_its_msgs(int how_many_msgs) const
This collective function finds the rank having the most messages and how many messages this rank has....
Definition: MsgLogger.cpp:322
const int m_num_procs
Definition: MsgLogger.H:283
std::vector< MsgWithCounter > get_msgs_with_counter() const
This function returns a vector containing the recorded messages with the corresponding counters.
Definition: MsgLogger.cpp:235
void swap_with_io_rank(std::vector< MsgWithCounterAndRanks > &msgs_with_counter_and_ranks, int gather_rank) const
If the gather_rank is not the I/O rank, this function sends msgs_with_counter_and_ranks to the I/O ra...
Definition: MsgLogger.cpp:459
std::vector< MsgWithCounterAndRanks > one_rank_gather_msgs_with_counter_and_ranks() const
This function implements the trivial special case of collective_gather_msgs_with_counter_and_ranks wh...
Definition: MsgLogger.cpp:306
std::vector< MsgWithCounterAndRanks > collective_gather_msgs_with_counter_and_ranks() const
This collective function generates a vector containing the messages with counters and emitting ranks ...
Definition: MsgLogger.cpp:246
Logger()
The constructor.
Definition: MsgLogger.cpp:214
void record_msg(Msg msg)
This function records a message.
Definition: MsgLogger.cpp:220
const int m_rank
Definition: MsgLogger.H:282
std::vector< Msg > get_msgs() const
This function returns a vector containing the recorded messages.
Definition: MsgLogger.cpp:225
std::vector< MsgWithCounterAndRanks > compute_msgs_with_counter_and_ranks(const std::map< Msg, std::int64_t > &my_msg_map, const std::vector< char > &all_data, const std::vector< int > &displacements, const int gather_rank) const
This function uses data gathered on the "gather rank" to generate a vector of messages with global co...
Definition: MsgLogger.cpp:347
Definition: MsgLogger.H:20
Priority
Definition: MsgLogger.H:25
std::string PriorityToString(const Priority &priority)
This function converts a Priority into the corresponding string (e.g, Priority::low --> "low")
Definition: MsgLogger.cpp:102
constexpr bool operator<(const Msg &l, const Msg &r)
This implements the < operator for Msg. Warning messages are first ordered by priority (warning: high...
Definition: MsgLogger.H:173
Priority StringToPriority(const std::string &priority_string)
This function converts a string into the corresponding priority (e.g, "low" --> Priority::low)
Definition: MsgLogger.cpp:112
Definition: MsgLogger.H:58
static Msg deserialize(std::vector< char >::const_iterator &it)
This function generates a Msg struct from a byte vector.
Definition: MsgLogger.cpp:140
Priority priority
Definition: MsgLogger.H:61
std::string text
Definition: MsgLogger.H:60
std::string topic
Definition: MsgLogger.H:59
std::vector< char > serialize() const
This function returns a byte representation of the struct.
Definition: MsgLogger.cpp:128
bool all_ranks
Definition: MsgLogger.H:135
std::vector< char > serialize() const
This function returns a byte representation of the struct.
Definition: MsgLogger.cpp:183
std::vector< int > ranks
Definition: MsgLogger.H:136
MsgWithCounter msg_with_counter
Definition: MsgLogger.H:134
static MsgWithCounterAndRanks deserialize(std::vector< char >::const_iterator &it)
This function generates a MsgWithCounterAndRanks struct from a byte vector.
Definition: MsgLogger.cpp:195
Msg msg
Definition: MsgLogger.H:96
static MsgWithCounter deserialize(std::vector< char >::const_iterator &it)
This function generates a MsgWithCounter struct from a byte vector.
Definition: MsgLogger.cpp:166
std::vector< char > serialize() const
This function returns a byte representation of the struct.
Definition: MsgLogger.cpp:156
std::int64_t counter
Definition: MsgLogger.H:97