WarpX
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_Config.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  [[nodiscard]] 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  [[nodiscard]] 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  [[nodiscard]] 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(const Msg& msg);
200 
206  [[nodiscard]] std::vector<Msg> get_msgs() const;
207 
214  [[nodiscard]] std::vector<MsgWithCounter>
215  get_msgs_with_counter() const;
216 
224  [[nodiscard]] std::vector<MsgWithCounterAndRanks>
226 
227  private:
228 
235  [[nodiscard]] std::vector<MsgWithCounterAndRanks>
237 
238 #ifdef AMREX_USE_MPI
247  [[nodiscard]] std::pair<int, int>
248  find_gather_rank_and_its_msgs(int how_many_msgs) const;
249 
260  [[nodiscard]] std::vector<MsgWithCounterAndRanks>
262  const std::map<Msg,std::int64_t>& my_msg_map,
263  const std::vector<char>& all_data,
264  const std::vector<int>& displacements,
265  int gather_rank
266  ) const;
267 
268 
276  void
278  std::vector<MsgWithCounterAndRanks>& msgs_with_counter_and_ranks,
279  int gather_rank) const;
280 
281 #endif
282 
283  const int m_rank ;
284  const int m_num_procs ;
285  const int m_io_rank ;
286 
287  std::map<Msg, std::int64_t> m_messages ;
288  };
289 }
290 
291 #endif //ABLASTR_MSG_LOGGER_H_
Definition: MsgLogger.H:186
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, 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:353
const int m_io_rank
Definition: MsgLogger.H:285
std::map< Msg, std::int64_t > m_messages
Definition: MsgLogger.H:287
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:328
const int m_num_procs
Definition: MsgLogger.H:284
std::vector< MsgWithCounter > get_msgs_with_counter() const
This function returns a vector containing the recorded messages with the corresponding counters.
Definition: MsgLogger.cpp:236
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:467
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:311
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:249
Logger()
The constructor.
Definition: MsgLogger.cpp:213
const int m_rank
Definition: MsgLogger.H:283
void record_msg(const Msg &msg)
This function records a message.
Definition: MsgLogger.cpp:219
std::vector< Msg > get_msgs() const
This function returns a vector containing the recorded messages.
Definition: MsgLogger.cpp:224
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:99
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:110
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:139
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:127
bool all_ranks
Definition: MsgLogger.H:135
std::vector< char > serialize() const
This function returns a byte representation of the struct.
Definition: MsgLogger.cpp:182
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:194
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:165
std::vector< char > serialize() const
This function returns a byte representation of the struct.
Definition: MsgLogger.cpp:155
std::int64_t counter
Definition: MsgLogger.H:97