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-2023, 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 #define HIPCUB_NAMESPACE hipcub
36 
37 #define BEGIN_HIPCUB_NAMESPACE \
38  namespace hipcub {
39 
40 #define END_HIPCUB_NAMESPACE \
41  } /* hipcub */
42 
43 #ifdef __HIP_PLATFORM_AMD__
44  #define HIPCUB_ROCPRIM_API 1
45  #define HIPCUB_RUNTIME_FUNCTION __host__
46 
47  #include <rocprim/device/config_types.hpp>
48 
49 namespace detail
50 {
51 inline unsigned int host_warp_size_wrapper()
52 {
53  int device_id = 0;
54  unsigned int host_warp_size = 0;
55  hipError_t error = hipGetDevice(&device_id);
56  if(error != hipSuccess)
57  {
58  fprintf(stderr, "HIP error: %d line: %d: %s\n", error, __LINE__, hipGetErrorString(error));
59  fflush(stderr);
60  }
61  if(::rocprim::host_warp_size(device_id, host_warp_size) != hipSuccess)
62  {
63  return 0u;
64  }
65  return host_warp_size;
66 }
67 } // namespace detail
68 
69  #define HIPCUB_WARP_THREADS ::rocprim::warp_size()
70  #define HIPCUB_DEVICE_WARP_THREADS ::rocprim::device_warp_size()
71  #define HIPCUB_HOST_WARP_THREADS detail::host_warp_size_wrapper()
72  #define HIPCUB_ARCH 1 // ignored with rocPRIM backend
73 #elif defined(__HIP_PLATFORM_NVIDIA__)
74  #define HIPCUB_CUB_API 1
75  #define HIPCUB_RUNTIME_FUNCTION CUB_RUNTIME_FUNCTION
76 
77  #include <cub/util_arch.cuh>
78  #define HIPCUB_WARP_THREADS CUB_PTX_WARP_THREADS
79  #define HIPCUB_DEVICE_WARP_THREADS CUB_PTX_WARP_THREADS
80  #define HIPCUB_HOST_WARP_THREADS CUB_PTX_WARP_THREADS
81  #define HIPCUB_ARCH CUB_PTX_ARCH
82  BEGIN_HIPCUB_NAMESPACE
83  using namespace cub;
84  END_HIPCUB_NAMESPACE
85 #endif
86 
88 #define HIPCUB_WARP_SIZE_32 32u
89 #define HIPCUB_WARP_SIZE_64 64u
90 #define HIPCUB_MAX_WARP_SIZE HIPCUB_WARP_SIZE_64
91 
92 #define HIPCUB_HOST __host__
93 #define HIPCUB_DEVICE __device__
94 #define HIPCUB_HOST_DEVICE __host__ __device__
95 #define HIPCUB_SHARED_MEMORY __shared__
96 
97 // Helper macros to disable warnings in clang
98 #ifdef __clang__
99 #define HIPCUB_PRAGMA_TO_STR(x) _Pragma(#x)
100 #define HIPCUB_CLANG_SUPPRESS_WARNING_PUSH _Pragma("clang diagnostic push")
101 #define HIPCUB_CLANG_SUPPRESS_WARNING(w) HIPCUB_PRAGMA_TO_STR(clang diagnostic ignored w)
102 #define HIPCUB_CLANG_SUPPRESS_WARNING_POP _Pragma("clang diagnostic pop")
103 #define HIPCUB_CLANG_SUPPRESS_WARNING_WITH_PUSH(w) \
104  HIPCUB_CLANG_SUPPRESS_WARNING_PUSH HIPCUB_CLANG_SUPPRESS_WARNING(w)
105 #else // __clang__
106 #define HIPCUB_CLANG_SUPPRESS_WARNING_PUSH
107 #define HIPCUB_CLANG_SUPPRESS_WARNING(w)
108 #define HIPCUB_CLANG_SUPPRESS_WARNING_POP
109 #define HIPCUB_CLANG_SUPPRESS_WARNING_WITH_PUSH(w)
110 #endif // __clang__
111 
112 BEGIN_HIPCUB_NAMESPACE
113 
115 #if (defined(DEBUG) || defined(_DEBUG)) && !defined(HIPCUB_STDERR)
116  #define HIPCUB_STDERR
117 #endif
118 
119 inline
120 hipError_t Debug(
121  hipError_t error,
122  const char* filename,
123  int line)
124 {
125  (void)filename;
126  (void)line;
127 #ifdef HIPCUB_STDERR
128  if (error)
129  {
130  fprintf(stderr, "HIP error %d [%s, %d]: %s\n", error, filename, line, hipGetErrorString(error));
131  fflush(stderr);
132  }
133 #endif
134  return error;
135 }
136 
137 #ifndef HipcubDebug
138  #define HipcubDebug(e) hipcub::Debug((hipError_t) (e), __FILE__, __LINE__)
139 #endif
140 
141 #if __cpp_if_constexpr
142  #define HIPCUB_IF_CONSTEXPR constexpr
143 #else
144  #if defined(_MSC_VER) && !defined(__clang__)
145  // MSVC (and not Clang pretending to be MSVC) unconditionally exposes if constexpr (even in C++14 mode),
146  // moreover it triggers warning C4127 (conditional expression is constant) when not using it. nvcc will
147  // be calling cl.exe for host-side codegen.
148  #define HIPCUB_IF_CONSTEXPR constexpr
149  #else
150  #define HIPCUB_IF_CONSTEXPR
151  #endif
152 #endif
153 
154 END_HIPCUB_NAMESPACE
155 
156 #endif // HIPCUB_CONFIG_HPP_