Collective Variables Module - Developer Documentation
colvar_gpu_support.h
1 #ifndef COLVAR_GPU_SUPPORT_H
2 #define COLVAR_GPU_SUPPORT_H
3 
4 #include <vector>
5 #include <iostream>
6 #include <unordered_map>
7 
8 class colvarmodule;
9 
10 #define COLVARS_STRINGIFY(s) STRINGIFY_HELPER(s)
11 #define STRINGIFY_HELPER(s) #s
12 
13 #if defined(COLVARS_CUDA)
14 #include <cuda_runtime.h>
15 #ifdef COLVARS_NVTX_PROFILING
16 #include <nvtx3/nvToolsExt.h>
17 #include <nvtx3/nvToolsExtCudaRt.h>
18 #endif
19 #define COLVARS_SYNC_WARP __syncwarp()
20 #endif // defined(COLVARS_CUDA)
21 
22 #if defined(COLVARS_HIP)
23 #include <hip/hip_runtime.h>
24 #if defined(__HIP_PLATFORM_AMD__)
31  #define COLVARS_SYNC_WARP do {\
32  __builtin_amdgcn_fence(__ATOMIC_RELEASE, "wavefront"); \
33  __builtin_amdgcn_wave_barrier(); \
34  __builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "wavefront"); \
35  } while (0)
36 #elif defined(__HIP_PLATFORM_NVIDIA__)
37  #define COLVARS_SYNC_WARP __syncwarp()
38 #else
39  #error "Unknown HIP platform"
40 #endif
41 #endif // defined(COLVARS_HIP)
42 
43 #if defined(COLVARS_HIP)
44 #ifndef cudaError_t
45 #define cudaError_t hipError_t
46 #endif // cudaError_t
47 
48 #ifndef cudaFree
49 #define cudaFree hipFree
50 #endif // cudaFree
51 
52 #ifndef cudaFreeHost
53 #define cudaFreeHost hipFreeHost
54 #endif // cudaFreeHost
55 
56 #ifndef cudaFreeAsync
57 #define cudaFreeAsync hipFreeAsync
58 #endif // cudaFreeAsync
59 
60 #ifndef cudaGetErrorString
61 #define cudaGetErrorString hipGetErrorString
62 #endif // cudaGetErrorString
63 
64 #ifndef cudaGraphAddChildGraphNode
65 #define cudaGraphAddChildGraphNode hipGraphAddChildGraphNode
66 #endif // cudaGraphAddChildGraphNode
67 
68 #ifndef cudaGraphAddKernelNode
69 #define cudaGraphAddKernelNode hipGraphAddKernelNode
70 #endif // cudaGraphAddKernelNode
71 
72 #ifndef cudaGraphAddMemcpyNode
73 #define cudaGraphAddMemcpyNode hipGraphAddMemcpyNode
74 #endif // cudaGraphAddMemcpyNode
75 
76 #ifndef cudaGraphAddMemsetNode
77 #define cudaGraphAddMemsetNode hipGraphAddMemsetNode
78 #endif // cudaGraphAddMemsetNode
79 
80 #ifndef cudaGraphCreate
81 #define cudaGraphCreate hipGraphCreate
82 #endif // cudaGraphCreate
83 
84 #ifndef cudaGraphDestroy
85 #define cudaGraphDestroy hipGraphDestroy
86 #endif // cudaGraphDestroy
87 
88 #ifndef cudaGraphExecDestroy
89 #define cudaGraphExecDestroy hipGraphExecDestroy
90 #endif // cudaGraphExecDestroy
91 
92 #ifndef cudaGraphExecMemcpyNodeSetParams
93 #define cudaGraphExecMemcpyNodeSetParams hipGraphExecMemcpyNodeSetParams
94 #endif // cudaGraphExecMemcpyNodeSetParams
95 
96 #ifndef cudaGraphExec_t
97 #define cudaGraphExec_t hipGraphExec_t
98 #endif // cudaGraphExec_t
99 
100 #ifndef cudaGraphInstantiate
101 #define cudaGraphInstantiate hipGraphInstantiate
102 #endif // cudaGraphInstantiate
103 
104 #ifndef cudaGraphInstantiateWithParams
105 #define cudaGraphInstantiateWithParams hipGraphInstantiateWithParams
106 #endif // cudaGraphInstantiateWithParams
107 
108 #ifndef cudaGraphInstantiateParams
109 #define cudaGraphInstantiateParams hipGraphInstantiateParams
110 #endif // cudaGraphInstantiateParams
111 
112 #ifndef cudaGraphInstantiateFlagUpload
113 #define cudaGraphInstantiateFlagUpload hipGraphInstantiateFlagUpload
114 #endif // cudaGraphInstantiateFlagUpload
115 
116 #ifndef cudaGraphInstantiateSuccess
117 #define cudaGraphInstantiateSuccess hipGraphInstantiateSuccess
118 #endif // cudaGraphInstantiateSuccess
119 
120 #ifndef cudaGraphLaunch
121 #define cudaGraphLaunch hipGraphLaunch
122 #endif // cudaGraphLaunch
123 
124 #ifndef cudaGraphNode_t
125 #define cudaGraphNode_t hipGraphNode_t
126 #endif // cudaGraphNode_t
127 
128 #ifndef cudaGraph_t
129 #define cudaGraph_t hipGraph_t
130 #endif // cudaGraph_t
131 
132 #ifndef cudaGraphDebugDotPrint
133 #define cudaGraphDebugDotPrint hipGraphDebugDotPrint
134 #endif // cudaGraphDebugDotPrint
135 
136 #ifndef cudaGraphDebugDotFlags
137 #define cudaGraphDebugDotFlags hipGraphDebugDotFlags
138 #endif // cudaGraphDebugDotFlags
139 
140 #ifndef cudaGraphDebugDotFlagsVerbose
141 #define cudaGraphDebugDotFlagsVerbose hipGraphDebugDotFlagsVerbose
142 #endif // cudaGraphDebugDotFlagsVerbose
143 
144 #ifndef cudaHostAllocMapped
145 #define cudaHostAllocMapped hipHostAllocMapped
146 #endif // cudaHostAllocMapped
147 
148 #ifndef cudaHostAllocDefault
149 #define cudaHostAllocDefault hipHostAllocDefault
150 #endif // cudaHostAllocDefault
151 
152 #ifndef cudaHostAlloc
153 #define cudaHostAlloc hipHostAlloc
154 #endif // cudaHostAlloc
155 
156 #ifndef cudaLaunchKernel
157 #define cudaLaunchKernel hipLaunchKernel
158 #endif // cudaLaunchKernel
159 
160 #ifndef cudaKernelNodeParams
161 #define cudaKernelNodeParams hipKernelNodeParams
162 #endif // cudaKernelNodeParams
163 
164 #ifndef cudaMalloc
165 #define cudaMalloc hipMalloc
166 #endif // cudaMalloc
167 
168 #ifndef cudaMallocAsync
169 #define cudaMallocAsync hipMallocAsync
170 #endif // cudaMallocAsync
171 
172 #ifndef cudaMallocHost
173 #define cudaMallocHost hipMallocHost
174 #endif // cudaMallocHost
175 
176 #ifndef cudaGraphAddMemcpyNode1D
177 #define cudaGraphAddMemcpyNode1D hipGraphAddMemcpyNode1D
178 #endif
179 
180 #ifndef cudaMemcpy
181 #define cudaMemcpy hipMemcpy
182 #endif // cudaMemcpy
183 
184 #ifndef cudaMemcpy3DParms
185 #define cudaMemcpy3DParms hipMemcpy3DParms
186 #endif // cudaMemcpy3DParms
187 
188 #ifndef cudaMemcpyAsync
189 #define cudaMemcpyAsync hipMemcpyAsync
190 #endif // cudaMemcpyAsync
191 
192 #ifndef cudaMemcpyDeviceToDevice
193 #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
194 #endif // cudaMemcpyDeviceToDevice
195 
196 #ifndef cudaMemcpyDeviceToHost
197 #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
198 #endif // cudaMemcpyDeviceToHost
199 
200 #ifndef cudaMemcpyHostToDevice
201 #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
202 #endif // cudaMemcpyHostToDevice
203 
204 #ifndef cudaMemcpyKind
205 #define cudaMemcpyKind hipMemcpyKind
206 #endif // cudaMemcpyKind
207 
208 #ifndef cudaMemset
209 #define cudaMemset hipMemset
210 #endif // cudaMemset
211 
212 #ifndef cudaMemsetParams
213 #define cudaMemsetParams hipMemsetParams
214 #endif // cudaMemsetParams
215 
216 #ifndef cudaMemsetAsync
217 #define cudaMemsetAsync hipMemsetAsync
218 #endif // cudaMemsetAsync
219 
220 #ifndef cudaStreamCreate
221 #define cudaStreamCreate hipStreamCreate
222 #endif // cudaStreamCreate
223 
224 #ifndef cudaStreamDestroy
225 #define cudaStreamDestroy hipStreamDestroy
226 #endif // cudaStreamDestroy
227 
228 #ifndef cudaStreamSynchronize
229 #define cudaStreamSynchronize hipStreamSynchronize
230 #endif // cudaStreamSynchronize
231 
232 #ifndef cudaStream_t
233 #define cudaStream_t hipStream_t
234 #endif // cudaStream_t
235 
236 #ifndef cudaSuccess
237 #define cudaSuccess hipSuccess
238 #endif // cudaSuccess
239 
240 #ifndef make_cudaExtent
241 #define make_cudaExtent make_hipExtent
242 #endif // make_cudaExtent
243 
244 #ifndef make_cudaPitchedPtr
245 #define make_cudaPitchedPtr make_hipPitchedPtr
246 #endif // make_cudaPitchedPtr
247 
248 #ifndef make_cudaPos
249 #define make_cudaPos make_hipPos
250 #endif // make_cudaPos
251 
252 #ifndef cudaEvent_t
253 #define cudaEvent_t hipEvent_t
254 #endif // cudaEvent_t
255 
256 #ifndef cudaEventCreateWithFlags
257 #define cudaEventCreateWithFlags hipEventCreateWithFlags
258 #endif // cudaEventCreate
259 
260 #ifndef cudaEventDisableTiming
261 #define cudaEventDisableTiming hipEventDisableTiming
262 #endif // cudaEventDisableTiming
263 
264 #ifndef cudaEventDestroy
265 #define cudaEventDestroy hipEventDestroy
266 #endif // cudaEventDestroy
267 
268 #ifndef cudaEventRecord
269 #define cudaEventRecord hipEventRecord
270 #endif // cudaEventRecord
271 
272 #ifndef cudaEventSynchronize
273 #define cudaEventSynchronize hipEventSynchronize
274 #endif // cudaEventSynchronize
275 
276 #ifndef cudaStreamWaitEvent
277 #define cudaStreamWaitEvent hipStreamWaitEvent
278 #endif // cudaStreamWaitEvent
279 
280 #ifndef cudaGraphAddEventRecordNode
281 #define cudaGraphAddEventRecordNode hipGraphAddEventRecordNode
282 #endif // cudaGraphAddEventRecordNode
283 
284 #ifndef cudaGetDevice
285 #define cudaGetDevice hipGetDevice
286 #endif // cudaGetDevice
287 
288 #ifndef cudaDeviceProp
289 #define cudaDeviceProp hipDeviceProp_t
290 #endif // cudaDeviceProp
291 
292 #ifndef cudaDeviceGetPCIBusId
293 #define cudaDeviceGetPCIBusId hipDeviceGetPCIBusId
294 #endif // cudaDeviceGetPCIBusId
295 
296 #ifndef cudaGetDeviceProperties
297 #define cudaGetDeviceProperties hipGetDeviceProperties
298 #endif // cudaGetDeviceProperties
299 
300 #ifndef cudaDeviceGetAttribute
301 #define cudaDeviceGetAttribute hipDeviceGetAttribute
302 #endif // cudaDeviceGetAttribute
303 
304 #ifndef cudaDevAttrWarpSize
305 #define cudaDevAttrWarpSize hipDeviceAttributeWarpSize
306 #endif // cudaDevAttrWarpSize
307 
308 #endif // defined(COLVARS_HIP)
309 
310 class colvarmodule;
311 
312 namespace colvars_gpu {
313 
314 #if defined(COLVARS_CUDA) || defined(COLVARS_HIP)
316 constexpr static unsigned int default_block_size = 128;
318 constexpr static unsigned int default_reduce_max_num_blocks = 64;
319 // static unsigned int default_atom_wise_num_blocks = 64;
320 #endif
321 
322 #if defined(COLVARS_CUDA) || defined(COLVARS_HIP)
323 #define COLVARS_HOST_DEVICE __device__ __host__
324 #define COLVARS_DEVICE __device__
325 #else
326 #define COLVARS_HOST_DEVICE
327 #define COLVARS_DEVICE
328 #endif
329 
330 // HIP does not have cuda::std::array since libhipcxx is not a part of the ROCm distribution,
331 // so reinvent the wheel...
332 #if defined(COLVARS_CUDA) || defined(COLVARS_HIP)
333 template <typename T, unsigned long N>
334 class array1d {
335 public:
336  T m_data[N];
337  using value_type = T;
338  using size_type = decltype(N);
339  using reference = value_type&;
340  using const_reference = const value_type&;
341  using pointer = T*;
342  using const_pointer = const T*;
343  COLVARS_HOST_DEVICE constexpr size_type size() const {return N;}
344  COLVARS_HOST_DEVICE reference operator[](size_type pos) {return m_data[pos];}
345  COLVARS_HOST_DEVICE const_reference operator[](size_type pos) const {return m_data[pos];}
346 };
347 #endif
348 
349 // TODO: What about SYCL?
350 #if ( defined(COLVARS_CUDA) || defined(COLVARS_HIP) )
359 template <typename T>
361 public:
362  using value_type = T;
363 
364  CudaHostAllocator() = default;
365 
366  template<typename U>
367  constexpr CudaHostAllocator(const CudaHostAllocator<U>&) noexcept {}
368 
369  friend bool operator==(const CudaHostAllocator&, const CudaHostAllocator&) { return true; }
370  friend bool operator!=(const CudaHostAllocator&, const CudaHostAllocator&) { return false; }
371 
372  T* allocate(size_t n) {
373  T* ptr;
374  if (cudaHostAlloc(&ptr, n * sizeof(T), cudaHostAllocMapped) != cudaSuccess) {
375  throw std::bad_alloc();
376  }
377  return ptr;
378  }
379  void deallocate(T* ptr, size_t n) noexcept {
380  (void)cudaFreeHost(ptr);
381  }
382  template<typename U, typename... Args>
383  void construct(U* p, Args&&... args) {
384  new(p) U(std::forward<Args>(args)...);
385  }
386 
387  template<typename U>
388  void destroy(U* p) noexcept {
389  p->~U();
390  }
391 };
392 #endif
393 
394 
395 #if defined(COLVARS_CUDA) || defined (COLVARS_HIP)
404 int gpuAssert(colvarmodule* cvmodule, cudaError_t code, const char *file, int line);
405 #endif
406 
407 } // namespace colvars_gpu
408 
409 #if defined(COLVARS_CUDA) || defined (COLVARS_HIP)
412 #define checkGPUError(ans) colvars_gpu::gpuAssert(cvmodule, (ans), __FILE__, __LINE__);
413 #endif
414 
415 namespace colvars_gpu {
416 #if defined(COLVARS_CUDA) || defined (COLVARS_HIP)
417 
421 class gpu_graph_t {
422 public:
424  gpu_graph_t(colvarmodule* cvmodule_in): cvmodule(cvmodule_in) {}
426  int reset();
428  ~gpu_graph_t();
430  int dump_graph(const std::string& filename);
432  int init_graph_exec(cudaStream_t stream);
436  cudaGraph_t graph = nullptr;
438  cudaGraphExec_t graph_exec = nullptr;
440  std::unordered_map<std::string, cudaGraphNode_t> nodes = {};
441 private:
442  colvarmodule* cvmodule;
443 };
444 
448 int add_clear_array_node_impl(
449  colvarmodule* cvmodule,
450  void* dst, const size_t num_elements, const size_t sizeofT,
451  cudaGraphNode_t& node_out, cudaGraph_t& graph,
452  const std::vector<cudaGraphNode_t>& dependencies);
453 
457 int add_copy_node_impl(
458  colvarmodule* cvmodule,
459  const void* src, void* dst, const size_t num_elements, const size_t sizeofT,
460  cudaMemcpyKind kind, cudaGraphNode_t& node_out, cudaGraph_t& graph,
461  const std::vector<cudaGraphNode_t>& dependencies);
462 
474 template <typename T>
475 int add_clear_array_node(
476  colvarmodule* cvmodule,
477  T* dst, const size_t num_elements,
478  cudaGraphNode_t& node_out, cudaGraph_t& graph,
479  const std::vector<cudaGraphNode_t>& dependencies) {
480  return add_clear_array_node_impl(
481  cvmodule, dst, num_elements, sizeof(T), node_out, graph, dependencies);
482 }
483 
497 template <typename T>
498 int add_copy_node(
499  colvarmodule* cvmodule,
500  const T* src, T* dst, size_t num_elements,
501  cudaMemcpyKind kind, cudaGraphNode_t& node_out, cudaGraph_t& graph,
502  const std::vector<cudaGraphNode_t>& dependencies) {
503  return add_copy_node_impl(cvmodule, src, dst, num_elements, sizeof(T),
504  kind, node_out, graph, dependencies);
505 }
506 
520 int prepare_dependencies(
521  colvarmodule* cvmodule,
522  const std::vector<std::pair<std::string, bool>>& node_names,
523  std::vector<cudaGraphNode_t>& dependencies,
524  const std::unordered_map<std::string, cudaGraphNode_t>& map,
525  const std::string& caller_operation_name = "");
526 
527 #endif // defined(COLVARS_CUDA) || defined (COLVARS_HIP)
528 }
529 
530 #endif // COLVAR_GPU_SUPPORT_H
Collective variables module (main class)
Definition: colvarmodule.h:87
Allocator for pinned host memory using cudaHostAlloc.
Definition: colvar_gpu_support.h:360
Definition: colvar_gpu_support.h:334
A struct for holding a CUDA graph and its execution object.
Definition: colvar_gpu_support.h:421
int init_graph_exec(cudaStream_t stream)
Initialize CUDA graph execution instance.
Definition: colvar_gpu_support.cpp:132
cudaGraph_t graph
CUDA graph object.
Definition: colvar_gpu_support.h:436
gpu_graph_t(colvarmodule *cvmodule_in)
Constructor.
Definition: colvar_gpu_support.h:424
~gpu_graph_t()
Destructor.
Definition: colvar_gpu_support.cpp:112
int reset()
Reset the CUDA graph.
Definition: colvar_gpu_support.cpp:116
cudaGraphExec_t graph_exec
CUDA graph execution instance object.
Definition: colvar_gpu_support.h:438
int dump_graph(const std::string &filename)
Dump the CUDA graph to a dot file for debugging.
Definition: colvar_gpu_support.cpp:177
bool graph_exec_initialized
Flag to describe whether the graph execution instance has been initialized.
Definition: colvar_gpu_support.h:434
std::unordered_map< std::string, cudaGraphNode_t > nodes
List of compute nodes.
Definition: colvar_gpu_support.h:440