muda
viewer_base.h
1 #pragma once
2 #include <cuda.h>
3 #include <cuda_runtime.h>
4 #include <cuda_runtime_api.h>
5 
6 #include <muda/muda_def.h>
7 #include <muda/tools/debug_log.h>
8 #include <muda/muda_config.h>
9 #include <muda/assert.h>
10 #include <muda/tools/launch_info_cache.h>
11 #include <muda/tools/fuzzy.h>
12 #include <muda/type_traits/type_modifier.h>
13 
14 namespace muda
15 {
16 namespace details
17 {
18  class ViewerBaseAccessor;
19 }
20 template <bool IsConst_ = false>
22 {
23  public:
24  constexpr static bool IsConst = IsConst_;
25  constexpr static bool IsNonConst = !IsConst_;
26 
27  protected:
28  template <typename T>
29  using auto_const_t = std::conditional_t<IsConst, const T, T>;
30  template <typename T>
31  using non_const_enable_t = std::enable_if_t<IsNonConst, T>;
32 
33  private:
34  // friend class details::ViewerBaseAccessor;
35 
36 #if MUDA_CHECK_ON
37  details::StringPointer m_viewer_name;
38  details::StringPointer m_kernel_name;
39 #else
40  char m_dummy = 0; // a dummy member to avoid empty class
41 #endif
42  public:
43  MUDA_GENERIC ViewerBase()
44  {
45 #if MUDA_CHECK_ON
46 #ifndef __CUDA_ARCH__
47  m_kernel_name = details::LaunchInfoCache::current_kernel_name();
48 #endif
49 #endif
50  }
51 
52  MUDA_GENERIC const char* name() const MUDA_NOEXCEPT
53  {
54 #if MUDA_CHECK_ON
55  auto n = m_viewer_name.auto_select();
56  if(n && *n != '\0')
57  return n;
58 #endif
59  return "~";
60  }
61 
62  MUDA_GENERIC const char* kernel_name() const MUDA_NOEXCEPT
63  {
64 #if MUDA_CHECK_ON
65  auto n = m_kernel_name.auto_select();
66  if(n && *n != '\0')
67  return n;
68 #endif
69  return "~";
70  }
71 
72  // default copy / move
73  ViewerBase(const ViewerBase&) = default;
74  ViewerBase(ViewerBase&&) = default;
75  ViewerBase& operator=(const ViewerBase&) = default;
76  ViewerBase& operator=(ViewerBase&&) = default;
77 
78  protected:
79  MUDA_INLINE MUDA_HOST void name(const char* n) MUDA_NOEXCEPT
80  {
81 #if MUDA_CHECK_ON
82  m_viewer_name = details::LaunchInfoCache::view_name(n);
83 #endif
84  }
85 
86  MUDA_INLINE MUDA_GENERIC void name(details::StringPointer pointer) MUDA_NOEXCEPT
87  {
88 #if MUDA_CHECK_ON
89  m_viewer_name = pointer;
90 #endif
91  }
92 
93  MUDA_INLINE MUDA_GENERIC void copy_name(const ViewerBase& other) MUDA_NOEXCEPT
94  {
95 #if MUDA_CHECK_ON
96  m_kernel_name = other.m_kernel_name;
97  m_viewer_name = other.m_viewer_name;
98 #endif
99  }
100 };
101 
102 #define MUDA_VIEWER_COMMON_NAME(viewer_name) \
103  public: \
104  using this_type = viewer_name; \
105  \
106  MUDA_INLINE MUDA_HOST this_type& name(const char* n) noexcept \
107  { \
108  ::muda::ViewerBase<viewer_name::IsConst>::name(n); \
109  return *this; \
110  } \
111  \
112  MUDA_INLINE MUDA_GENERIC const char* name() const noexcept \
113  { \
114  return ::muda::ViewerBase<viewer_name::IsConst>::name(); \
115  } \
116  \
117  private:
118 } // namespace muda
Definition: assert.h:13
Definition: viewer_base.h:21
Definition: string_pointer.h:6