opm-common
gpuDecorators.hpp
1 /*
2  Copyright 2024 SINTEF Digital
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 /*
20  The point of this file is defining macros like OPM_HOST_DEVICE to be empty if
21  we do not compile for GPU architectures, otherwise we will
22  set it to "__device__ __host__" to decorate functions that can
23  be called from a hip/cuda kernel.
24 
25  The file also provides some definitions that will probably become useful later,
26  such as OPM_IS_INSIDE_DEVICE_FUNCTION for specializing code that can be called
27  from both GPUs and CPUs.
28 */
29 
33 #ifndef OPM_GPUDECORATORS_HPP
34  #define OPM_GPUDECORATORS_HPP
35 
36  // On CUDA we get some warnings that will yield compilation regardless, so we can ignore them
37  #ifdef __CUDACC__
38  #pragma nv_diag_suppress 20011,20014
39  #endif
40 
41  //TODO Should probably include config.h if config.h becomes installable
42 
43  // true if using nvcc/hipcc gpu compiler
44  #if defined(__NVCC__) || defined(__HIPCC__)
45  #define OPM_IS_COMPILING_WITH_GPU_COMPILER 1
46  #else
47  #define OPM_IS_COMPILING_WITH_GPU_COMPILER 0
48  #endif
49 
50  // true inside device version of functions marked __device__
51  #if defined(__CUDA_ARCH__) || (defined(__HIP_DEVICE_COMPILE__) && __HIP_DEVICE_COMPILE__ > 0)
52  #define OPM_IS_INSIDE_DEVICE_FUNCTION 1
53  #define OPM_IS_INSIDE_HOST_FUNCTION 0
54  #else
55  #define OPM_IS_INSIDE_DEVICE_FUNCTION 0
56  #define OPM_IS_INSIDE_HOST_FUNCTION 1
57  #endif
58 
59  #if HAVE_CUDA // if we will compile with GPU support
60 
61  //handle inclusion of correct gpu runtime headerfiles
62  #if USE_HIP // use HIP if we compile for AMD architectures
63  #include <hip/hip_runtime.h>
64  #else // otherwise include cuda
65  #include <cuda_runtime.h>
66  #endif // END USE_HIP
67 
68  #define OPM_HOST_DEVICE __device__ __host__
69  #define OPM_DEVICE __device__
70  #define OPM_HOST __host__
71  #define OPM_IS_USING_GPU 1
72  #else
73  #define OPM_HOST_DEVICE
74  #define OPM_DEVICE
75  #define OPM_HOST
76  #define OPM_IS_USING_GPU 0
77  #endif // END ELSE
78 
79 #endif // END HEADER GUARD