FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
cuda_util.cu
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6// includes, FEAT
7#include <kernel/base_header.hpp>
8#include <kernel/util/cuda_util.hpp>
9#include <kernel/util/half.hpp>
10#include <kernel/util/string.hpp>
11#include <kernel/util/assertion.hpp>
12#include "cuda_profiler_api.h"
13
14#include <vector>
15
16
17#if FEAT_CUDA_VERSION_MAJOR >= 13
18#include <nvtx3/nvToolsExt.h>
19#else
20#include <nvToolsExt.h>
21#endif
22
23
24#ifdef FEAT_HAVE_CUDSS
25#include <kernel/solver/cudss.hpp>
26#endif
27
28using namespace FEAT;
29
30namespace
31{
32 // Callbacks registered by higher-level modules to release their device resources during
33 // cuda_finalize(). See Util::cuda_register_finalize_callback().
34 std::vector<void (*)()> cuda_finalize_callbacks;
35}
36
37void FEAT::Util::cuda_register_finalize_callback(void (*callback)())
38{
39 cuda_finalize_callbacks.push_back(callback);
40}
41
42Index FEAT::Util::cuda_blocksize_misc = 256;
43Index FEAT::Util::cuda_blocksize_reduction = 256;
44Index FEAT::Util::cuda_blocksize_spmv = 256;
45Index FEAT::Util::cuda_blocksize_axpy = 256;
46Index FEAT::Util::cuda_blocksize_scalar_assembly = 256;
47Index FEAT::Util::cuda_blocksize_blocked_assembly = 128;
48Index FEAT::Util::cuda_blocksize_vanka_assembly = 64;
49
50int FEAT::Util::cuda_device_number = 0;
51
52cusparseHandle_t FEAT::Util::Intern::cusparse_handle;
53cublasHandle_t FEAT::Util::Intern::cublas_handle;
54cublasLtMatmulAlgo_t * FEAT::Util::Intern::cublas_lt_algo_matmat;
55bool * FEAT::Util::Intern::cublas_lt_algo_matmat_initialized;
56size_t FEAT::Util::Intern::cuda_workspace_size;
57void * FEAT::Util::Intern::cuda_workspace;
58bool FEAT::Util::Intern::stream_ordered_supported = false;
59
60namespace FEAT
61{
62 namespace Util
63 {
64 namespace Intern
65 {
66 template <typename DT_>
67 __global__ void cuda_set_memory(DT_ * ptr, const DT_ val, const Index count)
68 {
69 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
70 if (idx >= count)
71 return;
72 ptr[idx] = val;
73 }
74
75 template <typename DT1_, typename DT2_>
76 __global__ void cuda_convert(DT1_ * dest, const DT2_ * src, const Index count)
77 {
78 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
79 if (idx >= count)
80 return;
81 dest[idx] = src[idx];
82 }
83 }
84 }
85}
86
87void FEAT::Util::cuda_set_device(const int device)
88{
89 if (cudaSuccess != cudaSetDevice(device))
90 throw InternalError(__func__, __FILE__, __LINE__, "cudaSetDevice failed!");
91}
92
93void FEAT::Util::cuda_check_last_error()
94{
95 if constexpr(!async_operations)
96 cudaDeviceSynchronize();
97 cudaError_t last_error(cudaGetLastError());
98 if (cudaSuccess != last_error)
99 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
100}
101
102void * FEAT::Util::cuda_get_static_memory(const Index bytes)
103{
104 if(Intern::cuda_workspace_size < bytes)
105 {
106 cudaFree(Intern::cuda_workspace);
107 auto status = cudaMalloc(&Intern::cuda_workspace, bytes);
108 if (status != cudaSuccess)
109 throw InternalError(__func__, __FILE__, __LINE__, "Util::cuda_get_static_memory allocation error\n" + stringify(cudaGetErrorString(status)));
110 if (Intern::cuda_workspace == nullptr)
111 throw InternalError(__func__, __FILE__, __LINE__, "Util::cuda_get_static_memory allocation error (null pointer returned)");
112 Intern::cuda_workspace_size = bytes;
113 }
114 return Intern::cuda_workspace;
115}
116
117void FEAT::Util::cuda_free_static_memory()
118{
119 if (Intern::cuda_workspace == nullptr)
120 return;
121
122 auto status = cudaFree(Intern::cuda_workspace);
123 if (cudaSuccess != status)
124 throw InternalError(__func__, __FILE__, __LINE__, "Util::cuda_free_static_memory: cudaFree failed!\n" + stringify(cudaGetErrorString(status)));
125 Intern::cuda_workspace_size = size_t(0u);
126 Intern::cuda_workspace = nullptr;
127}
128
129void FEAT::Util::cuda_initialize(int rank, int /*ranks_per_node*/, int /*ranks_per_uma*/, int gpus_per_node)
130{
131 /// \todo enable non cuda ranks and ensure balance of ranks per numa section
132 FEAT::Util::cuda_device_number = rank % gpus_per_node;
133 if (cudaSuccess != cudaSetDevice(cuda_device_number))
134 throw InternalError(__func__, __FILE__, __LINE__, "cudaSetDevice failed!");
135
136 //int mm_support = 0;
137 //if (cudaSuccess != cudaDeviceGetAttribute(&mm_support, cudaDevAttrManagedMemory, cuda_device_number))
138 // throw InternalError(__func__, __FILE__, __LINE__, "cudaGetAttribute failed!");
139 //XASSERTM(mm_support == 1, "selected cuda device does not support managed memory!");
140
141 if (CUBLAS_STATUS_SUCCESS != cublasCreate(&Util::Intern::cublas_handle))
142 throw InternalError(__func__, __FILE__, __LINE__, "cublasCreate failed!");
143 if (CUSPARSE_STATUS_SUCCESS != cusparseCreate(&Util::Intern::cusparse_handle))
144 throw InternalError(__func__, __FILE__, __LINE__, "cusparseCreate failed!");
145 if (CUBLAS_STATUS_SUCCESS != cublasSetPointerMode(Util::Intern::cublas_handle, CUBLAS_POINTER_MODE_HOST))
146 throw InternalError(__func__, __FILE__, __LINE__, "cublasSetPointerMode failed!");
147 if (CUSPARSE_STATUS_SUCCESS != cusparseSetPointerMode(Util::Intern::cusparse_handle, CUSPARSE_POINTER_MODE_HOST))
148 throw InternalError(__func__, __FILE__, __LINE__, "cusparseSetPointerMode failed!");
149
150 if (CUBLAS_STATUS_SUCCESS != cublasSetMathMode(Util::Intern::cublas_handle, CUBLAS_TF32_TENSOR_OP_MATH))
151 throw InternalError(__func__, __FILE__, __LINE__, "cublasSetMathMode failed!");
152
153 Util::Intern::cublas_lt_algo_matmat = new cublasLtMatmulAlgo_t[6];
154 Util::Intern::cublas_lt_algo_matmat_initialized = new bool[6];
155 for (int i(0) ; i < 6 ; ++i)
156 {
157 Util::Intern::cublas_lt_algo_matmat_initialized[i] = false;
158 }
159
160 //Util::Intern::cuda_workspace_size = 1024ul * 1024ul * 1024ul * 2ul;
161 Util::Intern::cuda_workspace_size = 0;
162 {
163 int supported = 0;
164 if(cudaDeviceGetAttribute(&supported, cudaDevAttrMemoryPoolsSupported, Util::cuda_device_number) != cudaSuccess)
165 {
166 throw InternalError(__func__, __FILE__, __LINE__, "cudaDeviceGetAttribute failed!");
167 }
168
169 FEAT::Util::Intern::stream_ordered_supported = (supported != 0);
170 }
171
172 /*auto status = cudaMalloc(&(Util::Intern::cuda_workspace), Util::Intern::cuda_workspace_size);
173 if (status != cudaSuccess)
174 throw InternalError(__func__, __FILE__, __LINE__, "cudaMalloc failed: " + stringify(cudaGetErrorString(status)));*/
175}
176
177void FEAT::Util::cuda_finalize()
178{
179 if (cudaSuccess != cudaDeviceSynchronize())
180 throw InternalError(__func__, __FILE__, __LINE__, "cudaDeviceSynchronize failed!");
181
182 // release device resources of higher-level modules (registered via
183 // cuda_register_finalize_callback) while the CUDA context is still alive
184 for (auto callback : cuda_finalize_callbacks)
185 callback();
186 cuda_finalize_callbacks.clear();
187
188 if (CUBLAS_STATUS_SUCCESS != cublasDestroy(Util::Intern::cublas_handle))
189 throw InternalError(__func__, __FILE__, __LINE__, "cublasDestroy failed!");
190 if (CUSPARSE_STATUS_SUCCESS != cusparseDestroy(Util::Intern::cusparse_handle))
191 throw InternalError(__func__, __FILE__, __LINE__, "cusparseDestroy failed!");
192
193 delete[] Util::Intern::cublas_lt_algo_matmat;
194 delete[] Util::Intern::cublas_lt_algo_matmat_initialized;
195
196 cuda_free_static_memory();
197
198 cudaError_t last_error(cudaGetLastError());
199 if (cudaSuccess != last_error)
200 throw InternalError(__func__, __FILE__, __LINE__, "Pending cuda errors occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
201}
202
203void FEAT::Util::cuda_synchronize()
204{
205 auto status = cudaDeviceSynchronize();
206 if (status != cudaSuccess)
207 throw InternalError(__func__, __FILE__, __LINE__, "cudaDeviceSynchronize failed: " + stringify(cudaGetErrorString(status)));
208}
209
210void FEAT::Util::cuda_force_synchronize()
211{
212 auto status = cudaDeviceSynchronize();
213 if (status != cudaSuccess)
214 throw InternalError(__func__, __FILE__, __LINE__, "cudaDeviceSynchronize failed: " + stringify(cudaGetErrorString(status)));
215}
216
217void FEAT::Util::cuda_reset_device()
218{
219 auto status = cudaDeviceReset();
220 if (status != cudaSuccess)
221 throw InternalError(__func__, __FILE__, __LINE__, "cudaDeviceReset failed: " + stringify(cudaGetErrorString(status)));
222}
223
224void FEAT::Util::cuda_set_blocksize(Index misc, Index reduction, Index spmv, Index axpy, Index scalar_assembly, Index blocked_assembly)
225{
226 FEAT::Util::cuda_blocksize_misc = misc;
227
228 FEAT::Util::cuda_blocksize_reduction = reduction;
229
230 FEAT::Util::cuda_blocksize_spmv = spmv;
231
232 FEAT::Util::cuda_blocksize_axpy = axpy;
233
234 FEAT::Util::cuda_blocksize_scalar_assembly = scalar_assembly;
235
236 FEAT::Util::cuda_blocksize_blocked_assembly = blocked_assembly;
237}
238
239void FEAT::Util::cuda_reset_algos()
240{
241 for (int i(0) ; i < 6 ; ++i)
242 {
243 Util::Intern::cublas_lt_algo_matmat_initialized[i] = false;
244 }
245}
246
247int FEAT::Util::cuda_get_device_count()
248{
249 int numDevices(-1);
250 if (cudaSuccess != cudaGetDeviceCount(&numDevices))
251 throw InternalError(__func__, __FILE__, __LINE__, "cudaGetDeviceCount failed!");
252 return numDevices;
253}
254
255int FEAT::Util::cuda_get_device_id()
256{
257 int device(-1);
258 if (cudaSuccess != cudaGetDevice(&device))
259 throw InternalError(__func__, __FILE__, __LINE__, "cudaGetDevice failed!");
260 return device;
261}
262
263String FEAT::Util::cuda_get_visible_devices()
264{
265 String result("");
266 int numDevices(-1);
267 if (cudaSuccess != cudaGetDeviceCount(&numDevices))
268 throw InternalError(__func__, __FILE__, __LINE__, "cudaGetDeviceCount failed!");
269 result += "Number of visible cuda devices: " + stringify(numDevices) + "\n" ;
270
271 for (int idevice(0); idevice<numDevices; ++idevice)
272 {
273 // get device properties
274 cudaDeviceProp prop;
275 if (cudaSuccess != cudaGetDeviceProperties (&prop, idevice))
276 throw InternalError(__func__, __FILE__, __LINE__, "cudaGetDeviceProperties failed!");
277 // print out device name and compute capabilities
278 result += "Device " + stringify(idevice) + ": " + stringify(prop.name) + "\n";
279 }
280 return result;
281}
282
283std::size_t Util::cuda_get_max_cache_thread()
284{
285 std::size_t value = 0;
286 auto status = cudaDeviceGetLimit(&value, cudaLimit::cudaLimitStackSize);
287 if(cudaSuccess != status)
288 throw InternalError(__func__, __FILE__, __LINE__, "cudaDeviceSetLimit failed!");
289 return value;
290}
291
292void FEAT::Util::cuda_set_max_cache_thread(const std::size_t bytes)
293{
294 std::size_t value = bytes;
295 auto status = cudaDeviceSetLimit(cudaLimit::cudaLimitStackSize, value);
296 if(cudaSuccess != status)
297 throw InternalError(__func__, __FILE__, __LINE__, "cudaDeviceSetLimit failed!");
298 status = cudaDeviceGetLimit(&value, cudaLimit::cudaLimitStackSize);
299 if(cudaSuccess != status)
300 throw InternalError(__func__, __FILE__, __LINE__, "cudaDeviceGetLimit failed!");
301 if(value != bytes)
302 XABORTM("Could not set max cache per thread (" +stringify(value)+") to expected amount(" + stringify(bytes) + ")");
303}
304
305void FEAT::Util::cuda_start_profiling()
306{
307 cudaProfilerStart();
308}
309
310void FEAT::Util::cuda_stop_profiling()
311{
312 cudaProfilerStop();
313}
314
315std::size_t FEAT::Util::cuda_get_shared_mem_per_sm()
316{
317 int max_shared_mem_sm = 0;
318 if(cudaDeviceGetAttribute(&max_shared_mem_sm, cudaDevAttrMaxSharedMemoryPerMultiprocessor, Util::cuda_device_number) != cudaSuccess)
319 {
320 throw InternalError(__func__, __FILE__, __LINE__, "cudaDeviceGetAttribute failed!");
321 }
322 return std::size_t(max_shared_mem_sm);
323
324}
325
326std::size_t FEAT::Util::cuda_get_max_blocks_per_sm()
327{
328 int max_blocks_per_sm = 0;
329 if(cudaDeviceGetAttribute(&max_blocks_per_sm, cudaDevAttrMaxBlocksPerMultiprocessor, Util::cuda_device_number) != cudaSuccess)
330 {
331 throw InternalError(__func__, __FILE__, __LINE__, "cudaDeviceGetAttribute failed!");
332 }
333 return std::size_t(max_blocks_per_sm);
334
335}
336
337std::size_t FEAT::Util::cuda_get_sm_count()
338{
339 int max_sm_per_device = 0;
340 if(cudaDeviceGetAttribute(&max_sm_per_device, cudaDevAttrMultiProcessorCount, Util::cuda_device_number) != cudaSuccess)
341 {
342 throw InternalError(__func__, __FILE__, __LINE__, "cudaDeviceGetAttribute failed!");
343 }
344 return std::size_t(max_sm_per_device);
345
346}
347
348bool FEAT::Util::cuda_get_stream_ordered_alloc_supported()
349{
350 return async_operations ? FEAT::Util::Intern::stream_ordered_supported : false;
351}
352
353std::uint64_t FEAT::Util::cuda_start_range(const char* name)
354{
355 return std::uint64_t(nvtxRangeStartA(name));
356}
357
358void FEAT::Util::cuda_stop_range(std::uint64_t id)
359{
360 nvtxRangeEnd(nvtxRangeId_t(id));
361}
362
363void FEAT::Util::cuda_mark_event(const char* name)
364{
365 nvtxMarkA(name);
366}