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.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 ;
61  Priority priority ;
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  {
134  MsgWithCounter msg_with_counter ;
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>
224  collective_gather_msgs_with_counter_and_ranks() const;
225 
226  private:
227 
234  std::vector<MsgWithCounterAndRanks>
235  one_rank_gather_msgs_with_counter_and_ranks() const;
236 
237 #ifdef AMREX_USE_MPI
238 
246  std::pair<int, int> find_gather_rank_and_its_msgs(
247  int how_many_msgs) const;
248 
259  std::vector<MsgWithCounterAndRanks>
260  compute_msgs_with_counter_and_ranks(
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
276  swap_with_io_rank(
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_
Priority priority
Definition: MsgLogger.H:61
std::string PriorityToString(const Priority &priority)
This function converts a Priority into the corresponding string (e.g, Priority::low –> "low") ...
Definition: MsgLogger.cpp:102
std::string text
Definition: MsgLogger.H:60
Priority
Definition: MsgLogger.H:24
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
std::string topic
Definition: MsgLogger.H:59
Definition: MsgLogger.H:57
Definition: MsgLogger.H:185
Definition: MsgLogger.H:19