Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DistStats.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
27 #ifndef GALOIS_RUNTIME_DIST_STATS_H
28 #define GALOIS_RUNTIME_DIST_STATS_H
29 
31 #ifndef MORE_DIST_STATS
32 #define MORE_DIST_STATS 0
33 #endif
34 #ifndef GALOIS_COMM_STATS
36 #define GALOIS_COMM_STATS 0
37 #endif
38 #ifndef GALOIS_PER_ROUND_STATS
41 #define GALOIS_PER_ROUND_STATS 0
42 #endif
43 
45 #include "galois/runtime/Network.h"
46 
47 #include <string>
48 
49 namespace galois {
50 namespace runtime {
51 
55 class StatRecvHelper;
56 
65  using Str = galois::gstl::Str;
66  using Base::SEP;
67 
68  static constexpr const char* const HSTAT_SEP = Base::TSTAT_SEP;
69  static constexpr const char* const HSTAT_NAME = "HostValues";
70  static constexpr const char* const HSTAT_ENV_VAR = "PRINT_PER_HOST_STATS";
71 
72  static bool printingHostVals(void);
73 
74  template <typename _UNUSED = void>
75  struct HostTotalTypesImpl {
76  struct DummyStat {
78 
79  explicit DummyStat(StatTotal::Type total) : m_totalTy(total) {}
80 
81  template <typename _U>
82  void add(const _U&) const {}
83 
84  const StatTotal::Type& totalTy(void) const { return m_totalTy; }
85  };
86 
87  using TMap = internal::BasicStatMap<DummyStat>;
88 
89  bool merged = false;
91 
92  void addToStat(const Str& region, const Str& category,
93  const StatTotal::Type& hTotalTy) {
94  perThrdMap.getLocal()->addToStat(region, category, 0, hTotalTy);
95  }
96 
97  void mergeStats(void) {
98  if (merged) {
99  return;
100  }
101  GALOIS_ASSERT(perThrdMap.getLocal() == perThrdMap.getRemote(0),
102  "Must call from Thread 0");
103 
104  auto* t0Map = perThrdMap.getRemote(0);
105 
106  for (unsigned t = 1; t < perThrdMap.size(); ++t) {
107  const auto* manager = perThrdMap.getRemote(t);
108 
109  for (auto i = manager->cbegin(), end_i = manager->cend(); i != end_i;
110  ++i) {
111  t0Map->addToStat(manager->region(i), manager->category(i), 0,
112  manager->stat(i).totalTy());
113  }
114  }
115 
116  merged = true;
117  }
118 
119  const TMap& mergedMap(void) const {
120  assert(merged && "Must merge first");
121  return *perThrdMap.getRemote(0);
122  }
123  };
124 
125  using HostTotalTypes = HostTotalTypesImpl<>;
126 
127  template <typename T>
128  using ThrdVals = galois::gstl::Vector<T>;
129 
130  template <typename T>
131  using HostStatVal =
132  std::tuple<unsigned, T, StatTotal::Type, const ThrdVals<T>&>;
133 
134  template <typename T>
135  struct HostStat : public internal::VecStat<T> {
136  using Base = internal::VecStat<T>;
137  using ThrdStats = internal::VecStat<T>;
138  using PerHostThrdStats = galois::gstl::Map<unsigned, ThrdStats>;
139 
140  PerHostThrdStats perHostThrdStats;
141 
142  explicit HostStat(const StatTotal::Type& hTotalTy) : Base(hTotalTy) {}
143 
144  void add(const HostStatVal<T>& val) {
145  const auto& hostID = std::get<0>(val);
146  const auto& thrdTotal = std::get<1>(val);
147  const auto& thrdTotalTy = std::get<2>(val);
148  const auto& thrdVals = std::get<3>(val);
149 
150  Base::add(thrdTotal);
151 
152  auto p = perHostThrdStats.emplace(hostID, ThrdStats(thrdTotalTy));
153  auto& tstat = p.first->second;
154 
155  for (const auto& i : thrdVals) {
156  tstat.add(i);
157  }
158  }
159 
160  void printHostVals(std::ostream& out, const Str& region,
161  const Str& category) const {
162  out << StatManager::statKind<T>() << SEP << galois::runtime::getHostID()
163  << SEP;
164  out << region << SEP << category << SEP;
165  out << HSTAT_NAME << SEP;
166 
167  const char* sep = "";
168 
169  for (const auto& v : Base::values()) {
170  out << sep << v;
171  sep = HSTAT_SEP;
172  }
173 
174  out << std::endl;
175  }
176 
177  void printThreadVals(std::ostream& out, const Str& region,
178  const Str& category) const {
179  for (const auto& p : perHostThrdStats) {
180  out << StatManager::statKind<T>() << SEP << p.first << SEP;
181  out << region << SEP << category << SEP;
182  out << StatTotal::str(p.second.totalTy()) << SEP << p.second.total();
183  out << std::endl;
184 
185  out << StatManager::statKind<T>() << SEP << p.first << SEP;
186  out << region << SEP << category << SEP;
187  out << StatManager::TSTAT_NAME << SEP;
188 
189  const char* sep = "";
190  for (const auto& v : p.second.values()) {
191  out << sep << v;
193  }
194 
195  out << std::endl;
196  }
197  }
198  };
199 
200  template <typename T>
201  struct DistStatCombiner : public internal::BasicStatMap<HostStat<T>> {
202  using Base = internal::BasicStatMap<HostStat<T>>;
203 
204 #if __GNUC__ < 5
205  static const char* htotalName(const StatTotal::Type& type){
206 #else
207  static constexpr const char* htotalName(const StatTotal::Type& type) {
208 #endif
209  switch (type) {
210  case StatTotal::SINGLE : return "HOST_0";
211  case StatTotal::TSUM:
212  return "HSUM";
213  case StatTotal::TAVG:
214  return "HAVG";
215  case StatTotal::TMIN:
216  return "HMIN";
217  case StatTotal::TMAX:
218  return "HMAX";
219  default:
220  std::abort();
221  return nullptr;
222  }
223 }
224 
225  void print(std::ostream& out) const {
226  for (auto i = Base::cbegin(), end_i = Base::cend(); i != end_i; ++i) {
227  out << StatManager::statKind<T>() << SEP << galois::runtime::getHostID()
228  << SEP;
229  out << Base::region(i) << SEP << Base::category(i) << SEP;
230 
231  const HostStat<T>& hs = Base::stat(i);
232 
233  out << htotalName(hs.totalTy()) << SEP << hs.total();
234  out << std::endl;
235 
236  if (DistStatManager::printingHostVals()) {
237  hs.printHostVals(out, Base::region(i), Base::category(i));
238  }
239 
241  hs.printThreadVals(out, Base::region(i), Base::category(i));
242  }
243  }
244 }
245 }; // namespace runtime
246 
247 DistStatCombiner<int64_t> intDistStats;
248 DistStatCombiner<double> fpDistStats;
249 DistStatCombiner<Str> strDistStats;
250 HostTotalTypes hostTotalTypes;
251 
252 protected:
258 void mergeStats(void);
259 
265 void printHeader(std::ostream& out) const;
266 
270 virtual void printStats(std::ostream& out);
271 
272 public:
274 DistStatManager(const std::string& outfile = "");
276 
288 template <typename T>
289 void addToStat(const Str& region, const Str& category, const T& val,
290  const StatTotal::Type& thrdTotalTy,
291  const StatTotal::Type& hTotalTy) {
292  Base::addToStat(region, category, val, thrdTotalTy);
293  hostTotalTypes.addToStat(region, category, hTotalTy);
294 }
295 
296 private:
297 void combineAtHost_0_helper(void);
298 void combineAtHost_0_helper2(void);
299 void receiveAtHost_0_helper(void);
300 void receiveAtHost_0_helper2(void);
301 void combineAtHost_0(void);
302 StatTotal::Type findHostTotalTy(const Str& region, const Str& category,
303  const StatTotal::Type& thrdTotalTy) const;
304 void addRecvdHostTotalTy(const Str& region, const Str& category,
305  const StatTotal::Type& totalTy);
306 void addRecvdStat(unsigned hostID, const Str& region, const Str& category,
307  int64_t thrdTotal, const StatTotal::Type& thrdTotalTy,
308  const ThrdVals<int64_t>& thrdVals);
309 void addRecvdStat(unsigned hostID, const Str& region, const Str& category,
310  double thrdTotal, const StatTotal::Type& thrdTotalTy,
311  const ThrdVals<double>& thrdVals);
312 void addRecvdParam(unsigned hostID, const Str& region, const Str& category,
313  const Str& thrdTotal, const StatTotal::Type& thrdTotalTy,
314  const ThrdVals<Str>& thrdVals);
315 }; // namespace galois
316 
317 namespace internal {
323 DistStatManager* distSysStatManager(void);
324 } // namespace internal
325 
338 template <typename S1, typename S2, typename T>
339 inline void reportDistStat(const S1& region, const S2& category, const T& value,
340  const StatTotal::Type& thrdTotalTy,
341  const StatTotal::Type& hTotalTy) {
342  internal::distSysStatManager()->addToStat(gstl::makeStr(region),
343  gstl::makeStr(category), value,
344  thrdTotalTy, hTotalTy);
345 }
346 
347 } // end namespace runtime
348 } // end namespace galois
349 
350 #endif // GALOIS_RUNTIME_DIST_STATS_H
Definition: DistStats.cpp:53
void printHeader(std::ostream &out) const
Print the header of the stats file output.
Definition: DistStats.cpp:376
uint32_t getHostID()
Gets this host&#39;s ID.
Definition: Network.cpp:41
DummyStat(StatTotal::Type total)
Definition: DistStats.h:79
static constexpr const char *const TSTAT_NAME
Definition: Statistics.h:342
Definition: Statistics.h:157
Definition: Statistics.h:157
std::map< K, V, C, FixedSizeAlloc< std::pair< const K, V >>> Map
Definition: gstl.h:65
static constexpr const char *const SEP
Definition: Statistics.h:340
Type
Definition: Statistics.h:157
void add(const _U &) const
Definition: DistStats.h:82
Contains the network interface class which is the base class for all network layer implementations...
void addToStat(const Str &region, const Str &category, const T &val, const StatTotal::Type &thrdTotalTy, const StatTotal::Type &hTotalTy)
Adds a statistic to the statistics manager.
Definition: DistStats.h:289
#define GALOIS_ASSERT(cond,...)
Like assert but unconditionally executed.
Definition: gIO.h:102
void print(void)
Definition: Statistics.cpp:68
std::vector< T, Pow2Alloc< T >> Vector
[STL vector using Pow_2_VarSizeAlloc]
Definition: gstl.h:52
Definition: Statistics.h:335
static bool printingThreadVals(void)
Definition: Statistics.cpp:64
~DistStatManager()
Definition: DistStats.cpp:49
Str makeStr(const T &x)
Definition: gstl.h:102
DistStatManager(const std::string &outfile="")
Dist stat manager constructor.
Definition: DistStats.cpp:47
virtual void printStats(std::ostream &out)
Merge all stats.
Definition: DistStats.cpp:385
static const char * str(const Type &t)
Definition: Statistics.h:159
void reportDistStat(const S1 &region, const S2 &category, const T &value, const StatTotal::Type &thrdTotalTy, const StatTotal::Type &hTotalTy)
Adds a statistic to the statistics manager.
Definition: DistStats.h:339
const Ty add(std::atomic< Ty > &a, const Ty &b)
Definition: AtomicHelpers.h:98
Class responsible for tracking all statistics of a running distributed Galois program and reporting t...
Definition: DistStats.h:61
void addToStat(const S1 &region, const S2 &category, const T &val, const StatTotal::Type &type)
Definition: Statistics.h:508
const StatTotal::Type & totalTy(void) const
Definition: DistStats.h:84
Definition: Statistics.h:157
void mergeStats(void)
Merge all stats from each individual thread as well as each individual host as prescribed the the red...
Definition: DistStats.cpp:90
Definition: Statistics.h:157
static constexpr const char *const TSTAT_SEP
Definition: Statistics.h:341
StatTotal::Type m_totalTy
Definition: DistStats.h:77
Definition: Statistics.h:157
std::basic_string< char, std::char_traits< char >, Pow2Alloc< char >> Str
Definition: gstl.h:75