hipcub/config.hpp Source File

hipcub/config.hpp Source File#

hipCUB: hipcub/config.hpp Source File
config.hpp
1 /******************************************************************************
2  * Copyright (c) 2010-2011, Duane Merrill. All rights reserved.
3  * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
4  * Modifications Copyright (c) 2019-2025, Advanced Micro Devices, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the NVIDIA CORPORATION nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  ******************************************************************************/
29 
30 #ifndef HIPCUB_CONFIG_HPP_
31 #define HIPCUB_CONFIG_HPP_
32 
33 #include <hip/hip_runtime.h>
34 
35 // Version
36 #include "hipcub_version.hpp" // IWYU pragma: export
37 
38 #define HIPCUB_NAMESPACE hipcub
39 
40 // Inline namespace (e.g. HIPCUB_300400_NS where 300400 is the hipCUB version) is used to
41 // eliminate issues when shared libraries are built with different versions of hipCUB so they may
42 // have symbols with the same name but different content.
43 // HIPCUB_DISABLE_INLINE_NAMESPACE can be defined to disable inline namespaces (the old behavior).
44 // HIPCUB_INLINE_NAMESPACE can be defined to override the standard inline namespace name.
45 // Additionally, all kernels have hidden visibility.
46 // For rocPRIM backend, see rocprim/config.hpp for similar definitions.
47 #if defined(DOXYGEN_SHOULD_SKIP_THIS) || defined(HIPCUB_DISABLE_INLINE_NAMESPACE)
48  #define HIPCUB_INLINE_NAMESPACE
49  #define BEGIN_HIPCUB_INLINE_NAMESPACE
50  #define END_HIPCUB_INLINE_NAMESPACE
51 #else
52  #define HIPCUB_CONCAT_(SEP, A, B) A##SEP##B
53  #define HIPCUB_CONCAT(SEP, A, B) HIPCUB_CONCAT_(SEP, A, B)
54 
55  #ifndef HIPCUB_INLINE_NAMESPACE
56  #define HIPCUB_INLINE_NAMESPACE \
57  HIPCUB_CONCAT(_, HIPCUB, HIPCUB_CONCAT(_, HIPCUB_VERSION, NS))
58  #endif
59  #define BEGIN_HIPCUB_INLINE_NAMESPACE \
60  inline namespace HIPCUB_INLINE_NAMESPACE \
61  {
62  #define END_HIPCUB_INLINE_NAMESPACE } /* inline namespace */
63 #endif
64 
65 #define BEGIN_HIPCUB_NAMESPACE \
66  namespace HIPCUB_NAMESPACE \
67  { \
68  BEGIN_HIPCUB_INLINE_NAMESPACE
69 
70 #define END_HIPCUB_NAMESPACE \
71  END_HIPCUB_INLINE_NAMESPACE \
72  } /* namespace hipcub */
73 
74 #ifdef __HIP_PLATFORM_AMD__
75  #define HIPCUB_ROCPRIM_API 1
76  #define HIPCUB_RUNTIME_FUNCTION __host__
77 
78  #include <rocprim/device/config_types.hpp>
79  #include <rocprim/intrinsics/arch.hpp>
80  #include <rocprim/intrinsics/thread.hpp>
81 
82 BEGIN_HIPCUB_NAMESPACE
83 namespace detail
84 {
85 inline unsigned int host_warp_size_wrapper()
86 {
87  int device_id = 0;
88  unsigned int host_warp_size = 0;
89  hipError_t error = hipGetDevice(&device_id);
90  if(error != hipSuccess)
91  {
92  fprintf(stderr, "HIP error: %d line: %d: %s\n", error, __LINE__, hipGetErrorString(error));
93  fflush(stderr);
94  }
95  if(::rocprim::host_warp_size(device_id, host_warp_size) != hipSuccess)
96  {
97  return 0u;
98  }
99  return host_warp_size;
100 }
101 } // namespace detail
102 END_HIPCUB_NAMESPACE
103  #include <rocprim/intrinsics/arch.hpp>
104 
105  #define HIPCUB_WARP_THREADS ::rocprim::warp_size()
106  // HIPCUB (and CUB) don't have a method to express min and max warp size.
107  #define HIPCUB_DEVICE_WARP_THREADS ::rocprim::arch::wavefront::max_size()
108  #define HIPCUB_HOST_WARP_THREADS ::hipcub::detail::host_warp_size_wrapper()
109  #define HIPCUB_ARCH 1 // ignored with rocPRIM backend
110 #elif defined(__HIP_PLATFORM_NVIDIA__)
111  #define HIPCUB_CUB_API 1
112  #define HIPCUB_RUNTIME_FUNCTION CUB_RUNTIME_FUNCTION
113 
114  #include <cub/util_arch.cuh>
115  #define HIPCUB_WARP_THREADS CUB_PTX_WARP_THREADS
116  #define HIPCUB_DEVICE_WARP_THREADS CUB_PTX_WARP_THREADS
117  #define HIPCUB_HOST_WARP_THREADS CUB_PTX_WARP_THREADS
118  #define HIPCUB_ARCH CUB_PTX_ARCH
119  BEGIN_HIPCUB_NAMESPACE
120  using namespace cub;
121  END_HIPCUB_NAMESPACE
122 #endif
123 
125 #define HIPCUB_WARP_SIZE_32 32u
126 #define HIPCUB_WARP_SIZE_64 64u
127 #define HIPCUB_MAX_WARP_SIZE HIPCUB_WARP_SIZE_64
128 
129 #define HIPCUB_HOST __host__
130 #define HIPCUB_DEVICE __device__
131 #define HIPCUB_HOST_DEVICE __host__ __device__
132 #define HIPCUB_FORCEINLINE __forceinline__
133 #define HIPCUB_SHARED_MEMORY __shared__
134 
135 // Helper macros to disable warnings in clang
136 #ifdef __clang__
137 #define HIPCUB_PRAGMA_TO_STR(x) _Pragma(#x)
138 #define HIPCUB_CLANG_SUPPRESS_WARNING_PUSH _Pragma("clang diagnostic push")
139 #define HIPCUB_CLANG_SUPPRESS_WARNING(w) HIPCUB_PRAGMA_TO_STR(clang diagnostic ignored w)
140 #define HIPCUB_CLANG_SUPPRESS_WARNING_POP _Pragma("clang diagnostic pop")
141 #define HIPCUB_CLANG_SUPPRESS_WARNING_WITH_PUSH(w) \
142  HIPCUB_CLANG_SUPPRESS_WARNING_PUSH HIPCUB_CLANG_SUPPRESS_WARNING(w)
143 #else // __clang__
144 #define HIPCUB_CLANG_SUPPRESS_WARNING_PUSH
145 #define HIPCUB_CLANG_SUPPRESS_WARNING(w)
146 #define HIPCUB_CLANG_SUPPRESS_WARNING_POP
147 #define HIPCUB_CLANG_SUPPRESS_WARNING_WITH_PUSH(w)
148 #endif // __clang__
149 
150 #define HIPCUB_CLANG_SUPPRESS_DEPRECATED_PUSH \
151  HIPCUB_CLANG_SUPPRESS_WARNING_PUSH \
152  HIPCUB_CLANG_SUPPRESS_WARNING("-Wdeprecated") \
153  HIPCUB_CLANG_SUPPRESS_WARNING("-Wdeprecated-declarations")
154 #define HIPCUB_CLANG_SUPPRESS_DEPRECATED_POP HIPCUB_CLANG_SUPPRESS_WARNING_POP
155 
157 #if (defined(DEBUG) || defined(_DEBUG)) && !defined(HIPCUB_STDERR)
158  #define HIPCUB_STDERR
159 #endif
160 
161 BEGIN_HIPCUB_NAMESPACE
162 
167 inline
168 hipError_t Debug(
169  hipError_t error,
170  const char* filename,
171  int line)
172 {
173  (void)filename;
174  (void)line;
175 #ifdef HIPCUB_STDERR
176  if (error)
177  {
178  fprintf(stderr, "HIP error %d [%s, %d]: %s\n", error, filename, line, hipGetErrorString(error));
179  fflush(stderr);
180  }
181 #endif
182  return error;
183 }
184 
188 inline void Log(const char* message, const char* filename, int line)
189 {
190  printf("hipcub: %s [%s:%d]\n", message, filename, line);
191 }
192 
193 END_HIPCUB_NAMESPACE
194 
195 #ifndef HipcubDebug
196  #define HipcubDebug(e) ::hipcub::Debug((hipError_t)(e), __FILE__, __LINE__)
197 #endif
198 
199 #ifndef HipcubLog
200  #define HipcubLog(msg) ::hipcub::Log(msg, __FILE__, __LINE__)
201 #endif
202 
203 #if __cpp_if_constexpr
204  #define HIPCUB_IF_CONSTEXPR constexpr
205 #else
206  #if defined(_MSC_VER) && !defined(__clang__)
207  // MSVC (and not Clang pretending to be MSVC) unconditionally exposes if constexpr (even in C++14 mode),
208  // moreover it triggers warning C4127 (conditional expression is constant) when not using it. nvcc will
209  // be calling cl.exe for host-side codegen.
210  #define HIPCUB_IF_CONSTEXPR constexpr
211  #else
212  #define HIPCUB_IF_CONSTEXPR
213  #endif
214 #endif
215 
216 #ifdef DOXYGEN_SHOULD_SKIP_THIS // Documentation only
217 
222  #define HIPCUB_DEBUG_SYNC
223 
224 #endif // DOXYGEN_SHOULD_SKIP_THIS
225 
226 #if defined(HIPCUB_CUB_API) && defined(HIPCUB_DEBUG_SYNC) && !defined(CUB_DEBUG_SYNC)
227  #define CUB_DEBUG_SYNC
228 #endif
229 
230 #if !defined(HIPCUB_DEBUG_SYNC) \
231  && (defined(CUB_DEBUG_SYNC) || defined(CUB_DEBUG_HOST_ASSERTIONS) \
232  || defined(CUB_DEBUG_DEVICE_ASSERTIONS) || defined(CUB_DEBUG_ALL))
233  #define HIPCUB_DEBUG_SYNC
234 #endif
235 
236 #ifdef HIPCUB_ROCPRIM_API
237  // TODO C++17: use an inline constexpr variable
238  #ifdef HIPCUB_DEBUG_SYNC
239  #define HIPCUB_DETAIL_DEBUG_SYNC_VALUE true
240  #else
241  #define HIPCUB_DETAIL_DEBUG_SYNC_VALUE false
242  #endif
243 #endif // HIPCUB_ROCPRIM_API
244 
245 #endif // HIPCUB_CONFIG_HPP_
hipError_t Debug(hipError_t error, const char *filename, int line)
Don't use this function directly, but via the HipcubDebug macro instead. If error is not hipSuccess,...
Definition: config.hpp:168
void Log(const char *message, const char *filename, int line)
Don't use this function directly, but via the HipcubLog macro instead. Prints the provided message co...
Definition: config.hpp:188