Nektar++
Loading...
Searching...
No Matches
HIP_Host_API.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: HIP_Host_API.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#pragma once
36
38#include <hip/hiprtc.h>
39
40namespace Nektar
41{
42
43// Helper to check HIP driver errors
44#define CHECK_HIPCUDA_DRIVER_ERROR(err) \
45 if (err != hipSuccess) \
46 { \
47 std::cerr << "HIP Driver API Error at: " << __FILE__ << ":" \
48 << __LINE__ << std::endl; \
49 std::cerr << hipGetErrorString(err) << std::endl; \
50 exit(1); \
51 }
52// Helper to check HIPRTC errors
53#define CHECK_NEKRTC_ERROR(err) \
54 { \
55 if (err != HIPRTC_SUCCESS) \
56 { \
57 std::cerr << "HIP Runtime Error at: " << __FILE__ << ":" \
58 << __LINE__ << std::endl; \
59 std::cerr << "HIPRTC error: " << hiprtcGetErrorString(err) \
60 << std::endl; \
61 exit(1); \
62 } \
63 }
64#define nekLaunchKernel hipModuleLaunchKernel
65#define nekModuleLoadData hipModuleLoadData
66#define nekModuleGetFunction hipModuleGetFunction
67#define nekModuleUnload hipModuleUnload
68#define NEKdevice hipDevice_t
69#define NEKmodule hipModule_t
70#define NEKfunction hipFunction_t
71#define nekrtcProgram hiprtcProgram
72#define nekrtcCreateProgram hiprtcCreateProgram
73#define nekrtcDestroyProgram hiprtcDestroyProgram
74#define nekrtcAddNameExpression hiprtcAddNameExpression
75#define nekrtcGetLoweredName hiprtcGetLoweredName
76#define nekrtcCompileProgram hiprtcCompileProgram
77#define nekrtcGetCodeSize hiprtcGetCodeSize
78#define nekrtcGetCode hiprtcGetCode
79} // namespace Nektar
80
81#if defined(NEKTAR_ENABLE_HIP)
82namespace Nektar
83{
84template <unsigned int ndim> class hipcudaBlock
85{
86};
87
88// Kernel launcher on a one-dimensional GPU grid with shared memory provision.
89// KERNEL must be a kernel function decorated by NEK_DEVICE_KERNEL. The last two
90// arguments of the KERNEL function MUST be of type unsigned char * and
91// hipcudaBlock<1>. The shared memory size must be specified in bytes. The
92// shared memory is declared as unsigned char* type. The shmemptr must then cast
93// to the appropriate type before use (e.g. auto ptr = (TData *)shmemptr).
94#define DEVICE_1DGRID_KERNEL_LAUNCHER(KERNEL, GRIDSIZE, BLOCKSIZE, SHMEMSIZE, \
95 STREAMID, ...) \
96 { \
97 auto STREAM = HIPStream::GetInstance(STREAMID); \
98 unsigned char *shmemptr = nullptr; \
99 KERNEL<<<GRIDSIZE, BLOCKSIZE, SHMEMSIZE, STREAM>>>( \
100 __VA_ARGS__, shmemptr, hipcudaBlock<1>()); \
101 CHECK_LAST_HIPCUDA_ERROR(); \
102 }
103
104// Kernel launcher on a two-dimensional GPU grid with shared memory provision.
105// KERNEL must be a kernel function decorated by NEK_DEVICE_KERNEL. The last two
106// arguments of the KERNEL function MUST be of type unsigned char * and
107// hipcudaBlock<2>. The shared memory size must be specified in bytes. The
108// shared memory is declared as unsigned char* type. The shmemptr must then cast
109// to the appropriate type before use (e.g. auto ptr = (TData *)shmemptr).
110#define DEVICE_2DGRID_KERNEL_LAUNCHER(KERNEL, GRIDSIZEX, GRIDSIZEY, \
111 BLOCKSIZEX, BLOCKSIZEY, SHMEMSIZE, \
112 STREAMID, ...) \
113 { \
114 auto STREAM = HIPStream::GetInstance(STREAMID); \
115 unsigned char *shmemptr = nullptr; \
116 dim3 GRIDSIZE(GRIDSIZEX, GRIDSIZEY, 1); \
117 dim3 BLOCKSIZE(BLOCKSIZEX, BLOCKSIZEY, 1); \
118 KERNEL<<<GRIDSIZE, BLOCKSIZE, SHMEMSIZE, STREAM>>>( \
119 __VA_ARGS__, shmemptr, hipcudaBlock<2>()); \
120 CHECK_LAST_HIPCUDA_ERROR(); \
121 }
122
123// Kernel launcher on a three-dimensional GPU grid with shared memory provision.
124// KERNEL must be a kernel function decorated by NEK_DEVICE_KERNEL. The last two
125// arguments of the KERNEL function MUST be of type unsigned char * and
126// hipcudaBlock<3>. The shared memory size must be specified in bytes. The
127// shared memory is declared as unsigned char* type. The shmemptr must then cast
128// to the appropriate type before use (e.g. auto ptr = (TData *)shmemptr).
129#define DEVICE_3DGRID_KERNEL_LAUNCHER(KERNEL, GRIDSIZEX, GRIDSIZEY, GRIDSIZEZ, \
130 BLOCKSIZEX, BLOCKSIZEY, BLOCKSIZEZ, \
131 SHMEMSIZE, STREAMID, ...) \
132 { \
133 auto STREAM = HIPStream::GetInstance(STREAMID); \
134 unsigned char *shmemptr = nullptr; \
135 dim3 GRIDSIZE(GRIDSIZEX, GRIDSIZEY, GRIDSIZEZ); \
136 dim3 BLOCKSIZE(BLOCKSIZEX, BLOCKSIZEY, BLOCKSIZEZ); \
137 KERNEL<<<GRIDSIZE, BLOCKSIZE, SHMEMSIZE, STREAM>>>( \
138 __VA_ARGS__, shmemptr, hipcudaBlock<3>()); \
139 CHECK_LAST_HIPCUDA_ERROR(); \
140 }
141
142// Kernel launcher on a one-dimensional GPU grid without shared memory
143// provision. KERNEL must be a kernel function decorated by NEK_DEVICE_KERNEL.
144// The last argument of the KERNEL function MUST be of type hipcudaBlock<1>.
145#define DEVICE_1DGRID_KERNEL_LAUNCHER_NOSHMEM(KERNEL, GRIDSIZE, BLOCKSIZE, \
146 STREAMID, ...) \
147 { \
148 auto STREAM = HIPStream::GetInstance(STREAMID); \
149 KERNEL<<<GRIDSIZE, BLOCKSIZE, 0, STREAM>>>(__VA_ARGS__, \
150 hipcudaBlock<1>()); \
151 CHECK_LAST_HIPCUDA_ERROR(); \
152 }
153
154// Kernel launcher on a two-dimensional GPU grid without shared memory
155// provision. KERNEL must be a kernel function decorated by NEK_DEVICE_KERNEL.
156// The last argument of the KERNEL function MUST be of type hipcudaBlock<2>.
157#define DEVICE_2DGRID_KERNEL_LAUNCHER_NOSHMEM( \
158 KERNEL, GRIDSIZEX, GRIDSIZEY, BLOCKSIZEX, BLOCKSIZEY, STREAMID, ...) \
159 { \
160 auto STREAM = HIPStream::GetInstance(STREAMID); \
161 dim3 GRIDSIZE(GRIDSIZEX, GRIDSIZEY, 1); \
162 dim3 BLOCKSIZE(BLOCKSIZEX, BLOCKSIZEY, 1); \
163 KERNEL<<<GRIDSIZE, BLOCKSIZE, 0, STREAM>>>(__VA_ARGS__, \
164 hipcudaBlock<2>()); \
165 CHECK_LAST_HIPCUDA_ERROR(); \
166 }
167
168// Kernel launcher on a three-dimensional GPU grid without shared memory
169// provision. KERNEL must be a kernel function decorated by NEK_DEVICE_KERNEL.
170// The last argument of the KERNEL function MUST be of type hipcudaBlock<3>.
171#define DEVICE_3DGRID_KERNEL_LAUNCHER_NOSHMEM( \
172 KERNEL, GRIDSIZEX, GRIDSIZEY, GRIDSIZEZ, BLOCKSIZEX, BLOCKSIZEY, \
173 BLOCKSIZEZ, STREAMID, ...) \
174 { \
175 auto STREAM = HIPStream::GetInstance(STREAMID); \
176 dim3 GRIDSIZE(GRIDSIZEX, GRIDSIZEY, GRIDSIZEZ); \
177 dim3 BLOCKSIZE(BLOCKSIZEX, BLOCKSIZEY, BLOCKSIZEZ); \
178 KERNEL<<<GRIDSIZE, BLOCKSIZE, 0, STREAM>>>(__VA_ARGS__, \
179 hipcudaBlock<3>()); \
180 CHECK_LAST_HIPCUDA_ERROR(); \
181 }
182
183} // namespace Nektar
184#endif