CMS 3D CMS Logo

Debug.h
Go to the documentation of this file.
1 #ifndef RecoTracker_MkFitCore_src_Debug_h
2 
3 namespace mkfit {
4  extern bool g_debug;
5 }
6 
7 #ifdef DEBUG
8 #define RecoTracker_MkFitCore_src_Debug_h
9 
10 #ifdef dprint
11 
12 #undef dprint
13 #undef dprint_np
14 #undef dcall
15 #undef dprintf
16 #undef dprintf_np
17 
18 #endif
19 /*
20  Usage: DEBUG must be defined before this header file is included, typically
21 
22  #define DEBUG
23  #include "Debug.h"
24 
25  This defines macros dprint(), dcall() and dprintf();
26  dprint(x) is equivalent to std::cout << x << std::endl;
27  example: dprint("Hits in layer=" << ilayer);
28 
29  dcall(x) simply calls x
30  example: dcall(pre_prop_print(ilay, mkfp));
31 
32  dprintf(x) is equivalent to printf(x)
33  example: dprintf("Bad label for simtrack %d -- %d\n", itrack, track.label());
34 
35  All printouts are also controlled by a bool variable "debug"
36  bool debug = true; is declared as a file global in an anonymous
37  namespace, and thus can be overridden within any interior scope
38  as needed, so one could change the global to false and only set
39  a local to true within certain scopes.
40 
41  All are protected by a file scope mutex to avoid mixed printouts.
42  This mutex can also be acquired within a block via dmutex_guard:
43 
44  if (debug && g_debug) {
45  dmutex_guard;
46  [do complicated stuff]
47  }
48 
49  The mutex is not reentrant, so avoid using dprint et al. within a scope
50  where the mutex has already been acquired, as doing so will deadlock.
51  */
52 #include <mutex>
53 
54 #define dmutex_guard std::lock_guard<std::mutex> dlock(debug_mutex)
55 #define dprint(x) \
56  if (debug && g_debug) { \
57  dmutex_guard; \
58  std::cout << x << std::endl; \
59  }
60 #define dprint_np(n, x) \
61  if (debug && g_debug && n < N_proc) { \
62  dmutex_guard; \
63  std::cout << n << ": " << x << std::endl; \
64  }
65 #define dcall(x) \
66  if (debug && g_debug) { \
67  dmutex_guard; \
68  x; \
69  }
70 #define dprintf(...) \
71  if (debug && g_debug) { \
72  dmutex_guard; \
73  printf(__VA_ARGS__); \
74  }
75 #define dprintf_np(n, ...) \
76  if (debug && g_debug && n < N_proc) { \
77  dmutex_guard; \
78  std::cout << n << ": "; \
79  printf(__VA_ARGS__); \
80  }
81 
82 namespace {
83  bool debug = false; // default, can be overridden locally
84  std::mutex debug_mutex;
85 
86  struct debug_guard {
87  bool m_prev_debug;
88  debug_guard(bool state = true) : m_prev_debug(debug) { debug = state; }
89  ~debug_guard() { debug = m_prev_debug; }
90  };
91 } // namespace
92 
93 #else
94 
95 #define dprint(x) (void(0))
96 #define dprint_np(n, x) (void(0))
97 #define dcall(x) (void(0))
98 #define dprintf(...) (void(0))
99 #define dprintf_np(n, ...) (void(0))
100 
101 #endif
102 
103 // Convert TBB execution to simple loops for debugging, perfomance measeurements.
104 
105 #ifdef TBB_DISABLE
106 #include "oneapi/tbb/blocked_range.h"
107 #include "oneapi/tbb/partitioner.h"
108 
109 #define TBB_PARALLEL_FOR mkfit_tbb::parallel_for
110 #define TBB_PARALLEL_FOR_EACH mkfit_tbb::parallel_for_each
111 
112 namespace mkfit_tbb {
113 
114  template <typename Range, typename Body>
115  void parallel_for(const Range& range, const Body& body) {
116  typename Range::const_iterator step = range.grainsize();
117  for (auto i = range.begin(); i < range.end(); i += step) {
118  step = std::min(step, range.end() - i);
119  body(Range(i, i + step, 1));
120  }
121  }
122 
123  template <typename Range, typename Body>
124  void parallel_for(const Range& range, const Body& body, const tbb::simple_partitioner& partitioner) {
125  typename Range::const_iterator step = range.grainsize();
126  for (auto i = range.begin(); i < range.end(); i += step) {
127  step = std::min(step, range.end() - i);
128  body(Range(i, i + step, 1));
129  }
130  }
131 
132  template <typename InputIterator, typename Function>
133  void parallel_for_each(InputIterator first, InputIterator last, const Function& f) {
134  for (auto& i = first; i != last; ++i) {
135  f(*i);
136  }
137  }
138 
139 } // namespace mkfit_tbb
140 
141 #else
142 
143 #define TBB_PARALLEL_FOR tbb::parallel_for
144 #define TBB_PARALLEL_FOR_EACH tbb::parallel_for_each
145 
146 #endif
147 
148 #endif
PixelRecoRange< float > Range
static std::mutex mutex
Definition: Proxy.cc:8
bool g_debug
Definition: Debug.cc:2
double f[11][100]
body
create merge config
Definition: mps_merge.py:62
#define debug
Definition: HDRShower.cc:19
step
Definition: StallMonitor.cc:83