Debian Codegraph @

packages/kokkos/tpls/desul/src/Lock_Array_CUDA.cpp

raw
1 /*
2 Copyright (c) 2019, Lawrence Livermore National Security, LLC
3 and DESUL project contributors. See the COPYRIGHT file for details.
4 Source: https://github.com/desul/desul
5
6 SPDX-License-Identifier: (BSD-3-Clause)
7 */
8
9 #include <cinttypes>
10 #include <desul/atomics/Lock_Array.hpp>
11 #include <sstream>
12 #include <string>
13
14 #ifdef DESUL_ATOMICS_ENABLE_CUDA_SEPARABLE_COMPILATION
15 namespace desul {
16 namespace Impl {
17 __device__ __constant__ int32_t* CUDA_SPACE_ATOMIC_LOCKS_DEVICE = nullptr;
18 __device__ __constant__ int32_t* CUDA_SPACE_ATOMIC_LOCKS_NODE = nullptr;
19 } // namespace Impl
20 } // namespace desul
21 #endif
22
23 namespace desul {
24
25 namespace {
26
27 __global__ void init_lock_arrays_cuda_kernel() {
28 unsigned i = blockIdx.x * blockDim.x + threadIdx.x;
29 if (i < CUDA_SPACE_ATOMIC_MASK + 1) {
30 Impl::CUDA_SPACE_ATOMIC_LOCKS_DEVICE[i] = 0;
31 Impl::CUDA_SPACE_ATOMIC_LOCKS_NODE[i] = 0;
32 }
33 }
34
35 } // namespace
36
37 namespace Impl {
38
39 int32_t* CUDA_SPACE_ATOMIC_LOCKS_DEVICE_h = nullptr;
40 int32_t* CUDA_SPACE_ATOMIC_LOCKS_NODE_h = nullptr;
41
42 // Putting this into anonymous namespace so we don't have multiple defined symbols
43 // When linking in more than one copy of the object file
44 namespace {
45
46 void check_error_and_throw_cuda(cudaError e, const std::string msg) {
47 if (e != cudaSuccess) {
48 std::ostringstream out;
49 out << "Desul::Error: " << msg << " error(" << cudaGetErrorName(e)
50 << "): " << cudaGetErrorString(e);
51 throw std::runtime_error(out.str());
52 }
53 }
54
55 } // namespace
56
57 // define functions
58 template <typename T>
59 void init_lock_arrays_cuda() {
60 if (CUDA_SPACE_ATOMIC_LOCKS_DEVICE_h != nullptr) return;
61 auto error_malloc1 = cudaMalloc(&CUDA_SPACE_ATOMIC_LOCKS_DEVICE_h,
62 sizeof(int32_t) * (CUDA_SPACE_ATOMIC_MASK + 1));
63 check_error_and_throw_cuda(error_malloc1,
64 "init_lock_arrays_cuda: cudaMalloc device locks");
65
66 auto error_malloc2 = cudaMallocHost(&CUDA_SPACE_ATOMIC_LOCKS_NODE_h,
67 sizeof(int32_t) * (CUDA_SPACE_ATOMIC_MASK + 1));
68 check_error_and_throw_cuda(error_malloc2,
69 "init_lock_arrays_cuda: cudaMalloc host locks");
70
71 auto error_sync1 = cudaDeviceSynchronize();
72 copy_cuda_lock_arrays_to_device();
73 check_error_and_throw_cuda(error_sync1, "init_lock_arrays_cuda: post mallocs");
74 init_lock_arrays_cuda_kernel<<<(CUDA_SPACE_ATOMIC_MASK + 1 + 255) / 256, 256>>>();
75 auto error_sync2 = cudaDeviceSynchronize();
76 check_error_and_throw_cuda(error_sync2, "init_lock_arrays_cuda: post init kernel");
77 }
78
79 template <typename T>
80 void finalize_lock_arrays_cuda() {
81 if (CUDA_SPACE_ATOMIC_LOCKS_DEVICE_h == nullptr) return;
82 cudaFree(CUDA_SPACE_ATOMIC_LOCKS_DEVICE_h);
83 cudaFreeHost(CUDA_SPACE_ATOMIC_LOCKS_NODE_h);
84 CUDA_SPACE_ATOMIC_LOCKS_DEVICE_h = nullptr;
85 CUDA_SPACE_ATOMIC_LOCKS_NODE_h = nullptr;
86 #ifdef DESUL_ATOMICS_ENABLE_CUDA_SEPARABLE_COMPILATION
87 copy_cuda_lock_arrays_to_device();
88 #endif
89 }
90
91 // Instantiate functions
92 template void init_lock_arrays_cuda<int>();
93 template void finalize_lock_arrays_cuda<int>();
94
95 } // namespace Impl
96
97 } // namespace desul