| 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_HIP_SEPARABLE_COMPILATION |
| 15 |
namespace desul { |
| 16 |
namespace Impl { |
| 17 |
__device__ __constant__ int32_t* HIP_SPACE_ATOMIC_LOCKS_DEVICE = nullptr; |
| 18 |
__device__ __constant__ int32_t* HIP_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_hip_kernel() { |
| 28 |
unsigned i = blockIdx.x * blockDim.x + threadIdx.x; |
| 29 |
if (i < HIP_SPACE_ATOMIC_MASK + 1) { |
| 30 |
Impl::HIP_SPACE_ATOMIC_LOCKS_DEVICE[i] = 0; |
| 31 |
Impl::HIP_SPACE_ATOMIC_LOCKS_NODE[i] = 0; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
} // namespace |
| 36 |
|
| 37 |
namespace Impl { |
| 38 |
|
| 39 |
int32_t* HIP_SPACE_ATOMIC_LOCKS_DEVICE_h = nullptr; |
| 40 |
int32_t* HIP_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_hip(hipError_t e, const std::string msg) { |
| 47 |
if (e != hipSuccess) { |
| 48 |
std::ostringstream out; |
| 49 |
out << "Desul::Error: " << msg << " error(" << hipGetErrorName(e) |
| 50 |
<< "): " << hipGetErrorString(e); |
| 51 |
throw std::runtime_error(out.str()); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
} // namespace |
| 56 |
|
| 57 |
template <typename T> |
| 58 |
void init_lock_arrays_hip() { |
| 59 |
if (HIP_SPACE_ATOMIC_LOCKS_DEVICE_h != nullptr) return; |
| 60 |
|
| 61 |
auto error_malloc1 = hipMalloc(&HIP_SPACE_ATOMIC_LOCKS_DEVICE_h, |
| 62 |
sizeof(int32_t) * (HIP_SPACE_ATOMIC_MASK + 1)); |
| 63 |
check_error_and_throw_hip(error_malloc1, |
| 64 |
"init_lock_arrays_hip: hipMalloc device locks"); |
| 65 |
|
| 66 |
auto error_malloc2 = hipHostMalloc(&HIP_SPACE_ATOMIC_LOCKS_NODE_h, |
| 67 |
sizeof(int32_t) * (HIP_SPACE_ATOMIC_MASK + 1)); |
| 68 |
check_error_and_throw_hip(error_malloc2, |
| 69 |
"init_lock_arrays_hip: hipMallocHost host locks"); |
| 70 |
|
| 71 |
auto error_sync1 = hipDeviceSynchronize(); |
| 72 |
copy_hip_lock_arrays_to_device(); |
| 73 |
check_error_and_throw_hip(error_sync1, "init_lock_arrays_hip: post malloc"); |
| 74 |
|
| 75 |
init_lock_arrays_hip_kernel<<<(HIP_SPACE_ATOMIC_MASK + 1 + 255) / 256, 256>>>(); |
| 76 |
|
| 77 |
auto error_sync2 = hipDeviceSynchronize(); |
| 78 |
check_error_and_throw_hip(error_sync2, "init_lock_arrays_hip: post init"); |
| 79 |
} |
| 80 |
|
| 81 |
template <typename T> |
| 82 |
void finalize_lock_arrays_hip() { |
| 83 |
if (HIP_SPACE_ATOMIC_LOCKS_DEVICE_h == nullptr) return; |
| 84 |
auto error_free1 = hipFree(HIP_SPACE_ATOMIC_LOCKS_DEVICE_h); |
| 85 |
check_error_and_throw_hip(error_free1, "finalize_lock_arrays_hip: free device locks"); |
| 86 |
auto error_free2 = hipHostFree(HIP_SPACE_ATOMIC_LOCKS_NODE_h); |
| 87 |
check_error_and_throw_hip(error_free2, "finalize_lock_arrays_hip: free host locks"); |
| 88 |
HIP_SPACE_ATOMIC_LOCKS_DEVICE_h = nullptr; |
| 89 |
HIP_SPACE_ATOMIC_LOCKS_NODE_h = nullptr; |
| 90 |
#ifdef DESUL_ATOMICS_ENABLE_HIP_SEPARABLE_COMPILATION |
| 91 |
copy_hip_lock_arrays_to_device(); |
| 92 |
#endif |
| 93 |
} |
| 94 |
|
| 95 |
template void init_lock_arrays_hip<int>(); |
| 96 |
template void finalize_lock_arrays_hip<int>(); |
| 97 |
|
| 98 |
} // namespace Impl |
| 99 |
|
| 100 |
} // namespace desul |