FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
cuda_util.hpp
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#pragma once
7
8#ifdef FEAT_HAVE_CUDA
9
10// includes, FEAT
12#include <kernel/util/string.hpp>
14
15#ifdef __CUDACC__
16#include <cusparse_v2.h>
17#include <cublas_v2.h>
18#include <cublasLt.h>
19#endif
20
21namespace FEAT
22{
23 namespace Util
24 {
26 namespace Intern
27 {
28#ifdef __CUDACC__
29 extern cusparseHandle_t cusparse_handle;
30 extern cublasHandle_t cublas_handle;
31 extern cublasLtMatmulAlgo_t * cublas_lt_algo_matmat;
32 extern bool * cublas_lt_algo_matmat_initialized;
33 extern size_t cuda_workspace_size;
34 extern void * cuda_workspace;
37 extern bool stream_ordered_supported;
38#endif
39 }
41
43 extern int cuda_device_number;
44
46 extern Index cuda_blocksize_misc;
47
49 extern Index cuda_blocksize_reduction;
50
52 extern Index cuda_blocksize_spmv;
53
55 extern Index cuda_blocksize_axpy;
56
58 extern Index cuda_blocksize_scalar_assembly;
59
61 extern Index cuda_blocksize_blocked_assembly;
62
64 extern Index cuda_blocksize_vanka_assembly;
65
67 struct RuntimeParameter
68 {
70 int shared_mem;
72 int blocksize;
74 int gridsize;
76 bool print_occupancy;
78 bool synchronize;
79 };
80
82 struct StreamWrapper
83 {
85 void* stream = nullptr;
87 std::uint32_t stream_idx = 0;
88 };
89
90 void cuda_set_blocksize(Index misc, Index reduction, Index spmv, Index axpy, Index scalar_assembly, Index blocked_assembly);
91 void cuda_set_device(const int device);
92 void cuda_check_last_error();
93 //void * cuda_get_device_pointer(void * host);
94 //void * cuda_malloc_managed(const Index bytes);
95 //void * cuda_malloc(const Index bytes);
96 //void * cuda_malloc_host(const Index bytes);
97 void * cuda_get_static_memory(const Index bytes);
98 //void cuda_free(void * address);
99 //void cuda_free_host(void * address);
100 void cuda_free_static_memory();
101 void cuda_initialize(int rank, int ranks_per_node, int ranks_per_uma, int gpus_per_node);
102 void cuda_finalize();
111 void cuda_register_finalize_callback(void (*callback)());
112 NOINLINE void cuda_synchronize();
113 NOINLINE void cuda_force_synchronize();
114 void cuda_reset_device();
115 //void cuda_copy(void * dest, const void * src, const Index bytes);
116 //void cuda_copy_host_to_device(void * dest, const void * src, const Index bytes);
117 //void cuda_copy_device_to_host(void * dest, const void * src, const Index bytes);
118 //void cuda_copy_device_to_device(void * dest, const void * src, const Index bytes);
119 void cuda_reset_algos();
120 //template <typename DT_>
121 //void cuda_set_memory(DT_ * address, const DT_ val, const Index count);
122 //template <typename DT1_, typename DT2_>
123 //void cuda_convert(DT1_ * dest, const DT2_ * src, const Index count);
124 int cuda_get_device_count();
125 int cuda_get_device_id();
126 String cuda_get_visible_devices();
127 std::size_t cuda_get_max_cache_thread();
128 void cuda_set_max_cache_thread(const std::size_t bytes);
129 void cuda_start_profiling();
130 void cuda_stop_profiling();
131 std::size_t cuda_get_shared_mem_per_sm();
132 std::size_t cuda_get_max_blocks_per_sm();
133 std::size_t cuda_get_sm_count();
134
137 bool cuda_get_stream_ordered_alloc_supported();
138
139 #ifdef __CUDACC__
143 template<typename T>
144 inline int cuda_get_occupancy(T kernel_func, int blocksize, int shared_memory = 0)
145 {
146 int num_blocks = 0;
147 if(cudaOccupancyMaxActiveBlocksPerMultiprocessor(&num_blocks, kernel_func, blocksize, shared_memory) != cudaSuccess)
148 {
149 throw InternalError(__func__, __FILE__, __LINE__, "cudaOccupancyMaxActiveBlockPerMultiprocessor failed!");
150 }
151 return num_blocks;
152 }
153 #endif
154
155 std::uint64_t cuda_start_range(const char* name);
156
157 void cuda_stop_range(std::uint64_t id);
158
159 void cuda_mark_event(const char* name);
160 }
161}
162#endif // FEAT_HAVE_CUDA
163
164// The following NVTX profiling helpers are always compiled (also without CUDA). When FEAT_HAVE_CUDA
165// is not defined they degrade to no-ops, so call sites may use them unconditionally without guards.
166#include <cstddef>
167#include <cstdint>
168#include <vector>
169
170namespace FEAT
171{
172 namespace Util
173 {
187 {
188 private:
190 std::vector<std::uint64_t> _ids;
191
192 public:
193 NvtxRangeStack() = default;
194
197 NvtxRangeStack& operator=(const NvtxRangeStack&) = delete;
198
201 {
202 while(!_ids.empty())
203 pop();
204 }
205
207 void push([[maybe_unused]] const char* name)
208 {
209#ifdef FEAT_HAVE_CUDA
210 _ids.push_back(cuda_start_range(name));
211#endif
212 }
213
215 void pop()
216 {
217#ifdef FEAT_HAVE_CUDA
218 if(!_ids.empty())
219 {
220 cuda_stop_range(_ids.back());
221 _ids.pop_back();
222 }
223#endif
224 }
225
227 std::size_t size() const
228 {
229 return _ids.size();
230 }
231 };
232
234 inline NvtxRangeStack& nvtx_thread_stack()
235 {
236 thread_local NvtxRangeStack stack;
237 return stack;
238 }
239
252 {
253 private:
256
257 public:
259 explicit ScopedNvtxRange(const char* name) :
260 _stack(nvtx_thread_stack())
261 {
262 _stack.push(name);
263 }
264
266 ScopedNvtxRange(NvtxRangeStack& stack, const char* name) :
267 _stack(stack)
268 {
269 _stack.push(name);
270 }
271
274 ScopedNvtxRange& operator=(const ScopedNvtxRange&) = delete;
275
278 {
279 _stack.pop();
280 }
281 };
282
284 inline void nvtx_mark([[maybe_unused]] const char* name)
285 {
286#ifdef FEAT_HAVE_CUDA
287 cuda_mark_event(name);
288#endif
289 }
290 } // namespace Util
291} // namespace FEAT
FEAT Kernel base header.
Stack manager for nested NVTX profiling ranges.
Definition: cuda_util.hpp:187
void push(const char *name)
opens a new named range and pushes it onto the stack
Definition: cuda_util.hpp:207
std::vector< std::uint64_t > _ids
the ids of the currently open ranges (LIFO order)
Definition: cuda_util.hpp:190
~NvtxRangeStack()
closes all ranges that are still open
Definition: cuda_util.hpp:200
NvtxRangeStack(const NvtxRangeStack &)=delete
stacks must not be copied (ownership of open range ids is unique)
std::size_t size() const
returns the number of currently open ranges
Definition: cuda_util.hpp:227
void pop()
closes the most recently opened range (no-op if the stack is empty)
Definition: cuda_util.hpp:215
RAII scope guard for an NVTX profiling range.
Definition: cuda_util.hpp:252
NvtxRangeStack & _stack
the stack this range was pushed onto
Definition: cuda_util.hpp:255
ScopedNvtxRange(NvtxRangeStack &stack, const char *name)
opens a range name on the given stack
Definition: cuda_util.hpp:266
ScopedNvtxRange(const ScopedNvtxRange &)=delete
scope guards must not be copied or moved
ScopedNvtxRange(const char *name)
opens a range name on the calling thread's default stack
Definition: cuda_util.hpp:259
~ScopedNvtxRange()
closes the range
Definition: cuda_util.hpp:277
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.