alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Common.hpp
Go to the documentation of this file.
1
/* Copyright 2024 Axel Hübl, Benjamin Worpitz, Matthias Werner, Jan Stephan, René Widera, Andrea Bocci, Aurora Perego
2
* SPDX-License-Identifier: MPL-2.0
3
*/
4
5
#pragma once
6
7
#include "
alpaka/core/Config.hpp
"
8
#include "
alpaka/core/Debug.hpp
"
9
10
// Boost.Uuid errors with VS2017 when intrin.h is not included
11
#if defined(_MSC_VER) && _MSC_VER >= 1910
12
# include <intrin.h>
13
#endif
14
15
#if ALPAKA_LANG_HIP
16
# if ALPAKA_COMP_HIP >= ALPAKA_VERSION_NUMBER(6, 2, 0) && ALPAKA_COMP_HIP < ALPAKA_VERSION_NUMBER(7, 0, 0)
17
# define HIP_ENABLE_WARP_SYNC_BUILTINS
18
# endif
19
// HIP defines some keywords like __forceinline__ in header files.
20
# include <hip/hip_runtime.h>
21
#endif
22
23
//! All functions that can be used on an accelerator have to be attributed with ALPAKA_FN_ACC or ALPAKA_FN_HOST_ACC.
24
//!
25
//! \code{.cpp}
26
//! Usage:
27
//! ALPAKA_FN_ACC
28
//! auto add(std::int32_t a, std::int32_t b)
29
//! -> std::int32_t;
30
//! \endcode
31
//! @{
32
#if ALPAKA_LANG_CUDA || ALPAKA_LANG_HIP
33
# if defined(ALPAKA_ACC_GPU_CUDA_ONLY_MODE) || defined(ALPAKA_ACC_GPU_HIP_ONLY_MODE)
34
# define ALPAKA_FN_ACC __device__
35
# else
36
# define ALPAKA_FN_ACC __device__ __host__
37
# endif
38
# define ALPAKA_FN_HOST_ACC __device__ __host__
39
# define ALPAKA_FN_HOST __host__
40
#else
41
# define ALPAKA_FN_ACC
42
# define ALPAKA_FN_HOST_ACC
43
# define ALPAKA_FN_HOST
44
#endif
45
//! @}
46
47
//! All functions marked with ALPAKA_FN_ACC or ALPAKA_FN_HOST_ACC that are exported to / imported from different
48
//! translation units have to be attributed with ALPAKA_FN_EXTERN. Note that this needs to be applied to both the
49
//! declaration and the definition.
50
//!
51
//! Usage:
52
//! ALPAKA_FN_ACC ALPAKA_FN_EXTERN auto add(std::int32_t a, std::int32_t b) -> std::int32_t;
53
//!
54
//! Warning: If this is used together with the SYCL back-end make sure that your SYCL runtime supports generic
55
//! address spaces. Otherwise it is forbidden to use pointers as parameter or return type for functions marked
56
//! with ALPAKA_FN_EXTERN.
57
#ifdef ALPAKA_ACC_SYCL_ENABLED
58
/*
59
This is required by the SYCL standard, section 5.10.1 "SYCL functions and member functions linkage":
60
61
The default behavior in SYCL applications is that all the definitions and declarations of the functions and member
62
functions are available to the SYCL compiler, in the same translation unit. When this is not the case, all the
63
symbols that need to be exported to a SYCL library or from a C++ library to a SYCL application need to be defined
64
using the macro: SYCL_EXTERNAL.
65
*/
66
# define ALPAKA_FN_EXTERN SYCL_EXTERNAL
67
#else
68
# define ALPAKA_FN_EXTERN
69
#endif
70
71
//! Disable nvcc warning:
72
//! 'calling a __host__ function from __host__ __device__ function.'
73
//! Usage:
74
//! ALPAKA_NO_HOST_ACC_WARNING
75
//! ALPAKA_FN_HOST_ACC function_declaration()
76
//! WARNING: Only use this method if there is no other way.
77
//! Most cases can be solved by #if ALPAKA_ARCH_PTX or #if ALPAKA_LANG_CUDA.
78
#if(ALPAKA_LANG_CUDA && !ALPAKA_COMP_CLANG_CUDA)
79
# if ALPAKA_COMP_MSVC
80
# define ALPAKA_NO_HOST_ACC_WARNING __pragma(hd_warning_disable)
81
# else
82
# define ALPAKA_NO_HOST_ACC_WARNING _Pragma("hd_warning_disable")
83
# endif
84
#else
85
# define ALPAKA_NO_HOST_ACC_WARNING
86
#endif
87
88
//! Macro defining the inline function attribute.
89
//!
90
//! The macro should stay on the left hand side of keywords, e.g. 'static', 'constexpr', 'explicit' or the return type.
91
#if ALPAKA_LANG_CUDA || ALPAKA_LANG_HIP
92
# define ALPAKA_FN_INLINE __forceinline__
93
#elif ALPAKA_COMP_MSVC
94
// TODO: With C++20 [[msvc::forceinline]] can be used.
95
# define ALPAKA_FN_INLINE __forceinline
96
#else
97
// For gcc, clang, and clang-based compilers like Intel icpx
98
# define ALPAKA_FN_INLINE [[gnu::always_inline]] inline
99
#endif
100
101
//! This macro defines a variable lying in global accelerator device memory.
102
//!
103
//! Example:
104
//! ALPAKA_STATIC_ACC_MEM_GLOBAL alpaka::DevGlobal<TAcc, int> variable;
105
//!
106
//! Those variables behave like ordinary variables when used in file-scope,
107
//! but inside kernels the get() method must be used to access the variable.
108
//! They are declared inline to resolve to a single instance across multiple
109
//! translation units.
110
//! Like ordinary variables, only one definition is allowed (ODR)
111
//! Failure to do so might lead to linker errors.
112
//!
113
//! In contrast to ordinary variables, you can not define such variables
114
//! as static compilation unit local variables with internal linkage
115
//! because this is forbidden by CUDA.
116
//!
117
//! \attention It is not allowed to initialize the variable together with the declaration.
118
//! To initialize the variable alpaka::memcpy must be used.
119
//! \code{.cpp}
120
//! ALPAKA_STATIC_ACC_MEM_GLOBAL alpaka::DevGlobal<TAcc, int> foo;
121
//!
122
//! struct DeviceMemoryKernel
123
//! {
124
//! ALPAKA_NO_HOST_ACC_WARNING
125
//! template<typename TAcc>
126
//! ALPAKA_FN_ACC void operator()(TAcc const& acc) const
127
//! {
128
//! auto a = foo<TAcc>.get();
129
//! }
130
//! }
131
//!
132
//! void initFoo() {
133
//! auto extent = alpaka::Vec<alpaka::DimInt<1u>, size_t>{1};
134
//! int initialValue = 42;
135
//! alpaka::ViewPlainPtr<DevHost, int, alpaka::DimInt<1u>, size_t> bufHost(&initialValue, devHost, extent);
136
//! alpaka::memcpy(queue, foo<Acc>, bufHost, extent);
137
//! }
138
//! \endcode
139
#if((ALPAKA_LANG_CUDA && ALPAKA_COMP_CLANG_CUDA) || (ALPAKA_LANG_CUDA && ALPAKA_COMP_NVCC && ALPAKA_ARCH_PTX) \
140
|| ALPAKA_LANG_HIP)
141
# if defined(__CUDACC_RDC__) || defined(__CLANG_RDC__)
142
# define ALPAKA_STATIC_ACC_MEM_GLOBAL \
143
template<typename TAcc> \
144
__device__ inline
145
# else
146
# define ALPAKA_STATIC_ACC_MEM_GLOBAL \
147
template<typename TAcc> \
148
__device__ static
149
# endif
150
#else
151
# define ALPAKA_STATIC_ACC_MEM_GLOBAL \
152
template<typename TAcc> \
153
inline
154
#endif
155
156
//! This macro defines a variable lying in constant accelerator device memory.
157
//!
158
//! Example:
159
//! ALPAKA_STATIC_ACC_MEM_CONSTANT alpaka::DevGlobal<TAcc, const int> variable;
160
//!
161
//! Those variables behave like ordinary variables when used in file-scope,
162
//! but inside kernels the get() method must be used to access the variable.
163
//! They are declared inline to resolve to a single instance across multiple
164
//! translation units.
165
//! Like ordinary variables, only one definition is allowed (ODR)
166
//! Failure to do so might lead to linker errors.
167
//!
168
//! In contrast to ordinary variables, you can not define such variables
169
//! as static compilation unit local variables with internal linkage
170
//! because this is forbidden by CUDA.
171
//!
172
//! \attention It is not allowed to initialize the variable together with the declaration.
173
//! To initialize the variable alpaka::memcpy must be used.
174
//! \code{.cpp}
175
//! ALPAKA_STATIC_ACC_MEM_CONSTANT alpaka::DevGlobal<TAcc, const int> foo;
176
//!
177
//! struct DeviceMemoryKernel
178
//! {
179
//! ALPAKA_NO_HOST_ACC_WARNING
180
//! template<typename TAcc>
181
//! ALPAKA_FN_ACC void operator()(TAcc const& acc) const
182
//! {
183
//! auto a = foo<TAcc>.get();
184
//! }
185
//! }
186
//!
187
//! void initFoo() {
188
//! auto extent = alpaka::Vec<alpaka::DimInt<1u>, size_t>{1};
189
//! int initialValue = 42;
190
//! alpaka::ViewPlainPtr<DevHost, int, alpaka::DimInt<1u>, size_t> bufHost(&initialValue, devHost, extent);
191
//! alpaka::memcpy(queue, foo<Acc>, bufHost, extent);
192
//! }
193
//! \endcode
194
#if((ALPAKA_LANG_CUDA && ALPAKA_COMP_CLANG_CUDA) || (ALPAKA_LANG_CUDA && ALPAKA_COMP_NVCC && ALPAKA_ARCH_PTX) \
195
|| ALPAKA_LANG_HIP)
196
# if defined(__CUDACC_RDC__) || defined(__CLANG_RDC__)
197
# define ALPAKA_STATIC_ACC_MEM_CONSTANT \
198
template<typename TAcc> \
199
__constant__ inline
200
# else
201
# define ALPAKA_STATIC_ACC_MEM_CONSTANT \
202
template<typename TAcc> \
203
__constant__ static
204
# endif
205
#else
206
# define ALPAKA_STATIC_ACC_MEM_CONSTANT \
207
template<typename TAcc> \
208
inline
209
#endif
210
211
//! This macro disables memory optimizations for annotated device memory.
212
//!
213
//! Example:
214
//! ALPAKA_DEVICE_VOLATILE float* ptr;
215
//!
216
//! This is useful for pointers, (shared) variables and shared memory which are used in combination with
217
//! the alpaka::mem_fence() function. It ensures that memory annotated with this macro will always be written directly
218
//! to memory (and not to a register or cache because of compiler optimizations).
219
#if(ALPAKA_LANG_CUDA && ALPAKA_ARCH_PTX) \
220
|| (ALPAKA_LANG_HIP && defined(__HIP_DEVICE_COMPILE__) && __HIP_DEVICE_COMPILE__ == 1)
221
# define ALPAKA_DEVICE_VOLATILE volatile
222
#else
223
# define ALPAKA_DEVICE_VOLATILE
224
#endif
Config.hpp
Debug.hpp
include
alpaka
core
Common.hpp
Generated on Tue Jan 20 2026 15:01:54 for alpaka by
1.9.8