MUDA
Loading...
Searching...
No Matches
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
14namespace muda
15{
16template <bool IsConst_ = false>
18{
19 public:
20 constexpr static bool IsConst = IsConst_;
21 constexpr static bool IsNonConst = !IsConst_;
22
23 protected:
24 template <typename T>
25 using auto_const_t = std::conditional_t<IsConst, const T, T>;
26 template <typename T>
27 using non_const_enable_t = std::enable_if_t<IsNonConst, T>;
28
29 private:
30 // friend class details::ViewerBaseAccessor;
31
32#if MUDA_CHECK_ON
33 details::StringPointer m_viewer_name;
34 details::StringPointer m_kernel_name;
35 details::StringPointer m_kernel_file;
36 int m_kernel_line = -1;
37#else
38 int m_dummy = 0; // a dummy member to avoid empty class
39#endif
40 public:
41 MUDA_GENERIC ViewerBase() MUDA_NOEXCEPT
42 {
43 if constexpr(muda::RUNTIME_CHECK_ON)
44 {
45#ifndef __CUDA_ARCH__
46 m_kernel_name = details::LaunchInfoCache::current_kernel_name();
47 m_kernel_file = details::LaunchInfoCache::current_kernel_file();
48 m_kernel_line = details::LaunchInfoCache::current_kernel_line();
49#endif
50 }
51 }
52
53 MUDA_GENERIC const char* name() const MUDA_NOEXCEPT
54 {
55 if constexpr(muda::RUNTIME_CHECK_ON)
56 {
57 auto n = m_viewer_name.auto_select();
58 if(n && *n != '\0')
59 return n;
60 }
61 return "~";
62 }
63
64 MUDA_GENERIC const char* kernel_name() const MUDA_NOEXCEPT
65 {
66 if constexpr(muda::RUNTIME_CHECK_ON)
67 {
68 auto n = m_kernel_name.auto_select();
69 if(n && *n != '\0')
70 return n;
71 }
72 return "~";
73 }
74
75 MUDA_GENERIC const char* kernel_file() const MUDA_NOEXCEPT
76 {
77 if constexpr(muda::RUNTIME_CHECK_ON)
78 {
79 auto n = m_kernel_file.auto_select();
80 if(n && *n != '\0')
81 return n;
82 }
83 return "~";
84 }
85
86 MUDA_GENERIC int kernel_line() const MUDA_NOEXCEPT
87 {
88 if constexpr(muda::RUNTIME_CHECK_ON)
89 {
90 return m_kernel_line;
91 }
92 return -1;
93 }
94
95 MUDA_INLINE MUDA_GENERIC void copy_label(const ViewerBase& other) MUDA_NOEXCEPT
96 {
97 if constexpr(muda::RUNTIME_CHECK_ON)
98 {
99 m_viewer_name = other.m_viewer_name;
100 m_kernel_name = other.m_kernel_name;
101 m_kernel_file = other.m_kernel_file;
102 m_kernel_line = other.m_kernel_line;
103 }
104 }
105
106 // default copy / move
107 ViewerBase(const ViewerBase&) = default;
108 ViewerBase(ViewerBase&&) = default;
109 ViewerBase& operator=(const ViewerBase&) = default;
110 ViewerBase& operator=(ViewerBase&&) = default;
111
112 protected:
113 MUDA_INLINE MUDA_HOST void name(const char* n) MUDA_NOEXCEPT
114 {
115 if constexpr(muda::RUNTIME_CHECK_ON)
116 {
117 m_viewer_name = details::LaunchInfoCache::view_name(n);
118 }
119 }
120
121 MUDA_INLINE MUDA_GENERIC void name(details::StringPointer pointer) MUDA_NOEXCEPT
122 {
123 if constexpr(muda::RUNTIME_CHECK_ON)
124 {
125 m_viewer_name = pointer;
126 }
127 }
128};
129
130#define MUDA_VIEWER_COMMON_NAME(viewer_name) \
131 public: \
132 using this_type = viewer_name; \
133 \
134 MUDA_INLINE MUDA_HOST this_type& name(const char* n) noexcept \
135 { \
136 ::muda::ViewerBase<viewer_name::IsConst>::name(n); \
137 return *this; \
138 } \
139 \
140 MUDA_INLINE MUDA_GENERIC const char* name() const noexcept \
141 { \
142 return ::muda::ViewerBase<viewer_name::IsConst>::name(); \
143 } \
144 \
145 private:
146} // namespace muda
Definition viewer_base.h:18
Definition string_pointer.h:7