forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kokkos_HIP_Space.cpp
287 lines (253 loc) · 11.3 KB
/
Kokkos_HIP_Space.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
#define KOKKOS_IMPL_PUBLIC_INCLUDE
#endif
#include <Kokkos_Macros.hpp>
#include <Kokkos_Core.hpp>
#include <HIP/Kokkos_HIP_Space.hpp>
#include <HIP/Kokkos_HIP_DeepCopy.hpp>
#include <impl/Kokkos_Error.hpp>
#include <impl/Kokkos_DeviceManagement.hpp>
#include <impl/Kokkos_ExecSpaceManager.hpp>
#include <hip/hip_runtime_api.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <atomic>
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
namespace {
static std::atomic<bool> is_first_hip_managed_allocation(true);
} // namespace
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
namespace Kokkos {
HIPSpace::HIPSpace() : m_device(HIP().hip_device()) {}
HIPHostPinnedSpace::HIPHostPinnedSpace() {}
HIPManagedSpace::HIPManagedSpace() : m_device(HIP().hip_device()) {}
void* HIPSpace::allocate(const size_t arg_alloc_size) const {
return allocate("[unlabeled]", arg_alloc_size);
}
void* HIPSpace::allocate(
const char* arg_label, const size_t arg_alloc_size,
const size_t arg_logical_size) const {
return impl_allocate(arg_label, arg_alloc_size, arg_logical_size);
}
void* HIPSpace::impl_allocate(
const char* arg_label, const size_t arg_alloc_size,
const size_t arg_logical_size,
const Kokkos::Tools::SpaceHandle arg_handle) const {
void* ptr = nullptr;
auto const error_code = hipMalloc(&ptr, arg_alloc_size);
if (error_code != hipSuccess) {
// This is the only way to clear the last error, which we should do here
// since we're turning it into an exception here
(void)hipGetLastError();
throw Experimental::HIPRawMemoryAllocationFailure(
arg_alloc_size, error_code,
Experimental::RawMemoryAllocationFailure::AllocationMechanism::
HIPMalloc);
}
if (Kokkos::Profiling::profileLibraryLoaded()) {
const size_t reported_size =
(arg_logical_size > 0) ? arg_logical_size : arg_alloc_size;
Kokkos::Profiling::allocateData(arg_handle, arg_label, ptr, reported_size);
}
return ptr;
}
void* HIPHostPinnedSpace::allocate(const size_t arg_alloc_size) const {
return allocate("[unlabeled]", arg_alloc_size);
}
void* HIPHostPinnedSpace::allocate(const char* arg_label,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
return impl_allocate(arg_label, arg_alloc_size, arg_logical_size);
}
void* HIPHostPinnedSpace::impl_allocate(
const char* arg_label, const size_t arg_alloc_size,
const size_t arg_logical_size,
const Kokkos::Tools::SpaceHandle arg_handle) const {
void* ptr = nullptr;
auto const error_code =
hipHostMalloc(&ptr, arg_alloc_size, hipHostMallocNonCoherent);
if (error_code != hipSuccess) {
// This is the only way to clear the last error, which we should do here
// since we're turning it into an exception here
(void)hipGetLastError();
throw Experimental::HIPRawMemoryAllocationFailure(
arg_alloc_size, error_code,
Experimental::RawMemoryAllocationFailure::AllocationMechanism::
HIPHostMalloc);
}
if (Kokkos::Profiling::profileLibraryLoaded()) {
const size_t reported_size =
(arg_logical_size > 0) ? arg_logical_size : arg_alloc_size;
Kokkos::Profiling::allocateData(arg_handle, arg_label, ptr, reported_size);
}
return ptr;
}
void* HIPManagedSpace::allocate(const size_t arg_alloc_size) const {
return allocate("[unlabeled]", arg_alloc_size);
}
void* HIPManagedSpace::allocate(const char* arg_label,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
return impl_allocate(arg_label, arg_alloc_size, arg_logical_size);
}
void* HIPManagedSpace::impl_allocate(
const char* arg_label, const size_t arg_alloc_size,
const size_t arg_logical_size,
const Kokkos::Tools::SpaceHandle arg_handle) const {
void* ptr = nullptr;
if (arg_alloc_size > 0) {
if (is_first_hip_managed_allocation.exchange(false) &&
Kokkos::show_warnings()) {
do { // hack to avoid spamming users with too many warnings
if (!impl_hip_driver_check_page_migration()) {
std::cerr << R"warning(
Kokkos::HIP::allocation WARNING: The combination of device and system configuration
does not support page migration between device and host.
HIPManagedSpace might not work as expected.
Please refer to the ROCm documentation on unified/managed memory.)warning"
<< std::endl;
break; // do not warn about HSA_XNACK environement variable
}
// check for correct runtime environment
const char* hsa_xnack = std::getenv("HSA_XNACK");
if (!hsa_xnack)
std::cerr << R"warning(
Kokkos::HIP::runtime WARNING: Kokkos did not find an environment variable 'HSA_XNACK'
for the current process.
Nevertheless, xnack is enabled for all processes if
amdgpu.noretry=0 was set in the Linux kernel boot line.
Without xnack enabled, Kokkos::HIPManaged might not behave
as expected.)warning"
<< std::endl;
else if (Kokkos::Impl::strcmp(hsa_xnack, "1") != 0)
std::cerr
<< "Kokkos::HIP::runtime WARNING: Kokkos detected the "
"environement variable "
<< "'HSA_XNACK'=" << hsa_xnack << "\n"
<< "Kokkos advises to set it to '1' to enable it per process."
<< std::endl;
} while (false);
}
auto const error_code = hipMallocManaged(&ptr, arg_alloc_size);
if (error_code != hipSuccess) {
// This is the only way to clear the last error, which we should do here
// since we're turning it into an exception here
(void)hipGetLastError();
throw Experimental::HIPRawMemoryAllocationFailure(
arg_alloc_size, error_code,
Experimental::RawMemoryAllocationFailure::AllocationMechanism::
HIPMallocManaged);
}
KOKKOS_IMPL_HIP_SAFE_CALL(hipMemAdvise(
ptr, arg_alloc_size, hipMemAdviseSetCoarseGrain, m_device));
}
if (Kokkos::Profiling::profileLibraryLoaded()) {
const size_t reported_size =
(arg_logical_size > 0) ? arg_logical_size : arg_alloc_size;
Kokkos::Profiling::allocateData(arg_handle, arg_label, ptr, reported_size);
}
return ptr;
}
bool HIPManagedSpace::impl_hip_driver_check_page_migration() const {
// check with driver if page migrating memory is available
// this driver query is copied from the hip documentation
int hasManagedMemory = 0; // false by default
KOKKOS_IMPL_HIP_SAFE_CALL(hipDeviceGetAttribute(
&hasManagedMemory, hipDeviceAttributeManagedMemory, m_device));
if (!static_cast<bool>(hasManagedMemory)) return false;
// next, check pageableMemoryAccess
int hasPageableMemory = 0; // false by default
KOKKOS_IMPL_HIP_SAFE_CALL(hipDeviceGetAttribute(
&hasPageableMemory, hipDeviceAttributePageableMemoryAccess, m_device));
return static_cast<bool>(hasPageableMemory);
}
void HIPSpace::deallocate(void* const arg_alloc_ptr,
const size_t arg_alloc_size) const {
deallocate("[unlabeled]", arg_alloc_ptr, arg_alloc_size);
}
void HIPSpace::deallocate(const char* arg_label, void* const arg_alloc_ptr,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
impl_deallocate(arg_label, arg_alloc_ptr, arg_alloc_size, arg_logical_size);
}
void HIPSpace::impl_deallocate(
const char* arg_label, void* const arg_alloc_ptr,
const size_t arg_alloc_size, const size_t arg_logical_size,
const Kokkos::Tools::SpaceHandle arg_handle) const {
if (Kokkos::Profiling::profileLibraryLoaded()) {
const size_t reported_size =
(arg_logical_size > 0) ? arg_logical_size : arg_alloc_size;
Kokkos::Profiling::deallocateData(arg_handle, arg_label, arg_alloc_ptr,
reported_size);
}
KOKKOS_IMPL_HIP_SAFE_CALL(hipFree(arg_alloc_ptr));
}
void HIPHostPinnedSpace::deallocate(void* const arg_alloc_ptr,
const size_t arg_alloc_size) const {
deallocate("[unlabeled]", arg_alloc_ptr, arg_alloc_size);
}
void HIPHostPinnedSpace::deallocate(const char* arg_label,
void* const arg_alloc_ptr,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
impl_deallocate(arg_label, arg_alloc_ptr, arg_alloc_size, arg_logical_size);
}
void HIPHostPinnedSpace::impl_deallocate(
const char* arg_label, void* const arg_alloc_ptr,
const size_t arg_alloc_size, const size_t arg_logical_size,
const Kokkos::Tools::SpaceHandle arg_handle) const {
if (Kokkos::Profiling::profileLibraryLoaded()) {
const size_t reported_size =
(arg_logical_size > 0) ? arg_logical_size : arg_alloc_size;
Kokkos::Profiling::deallocateData(arg_handle, arg_label, arg_alloc_ptr,
reported_size);
}
KOKKOS_IMPL_HIP_SAFE_CALL(hipHostFree(arg_alloc_ptr));
}
void HIPManagedSpace::deallocate(void* const arg_alloc_ptr,
const size_t arg_alloc_size) const {
deallocate("[unlabeled]", arg_alloc_ptr, arg_alloc_size);
}
void HIPManagedSpace::deallocate(const char* arg_label,
void* const arg_alloc_ptr,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
impl_deallocate(arg_label, arg_alloc_ptr, arg_alloc_size, arg_logical_size);
}
void HIPManagedSpace::impl_deallocate(
const char* arg_label, void* const arg_alloc_ptr,
const size_t arg_alloc_size, const size_t arg_logical_size,
const Kokkos::Tools::SpaceHandle arg_handle) const {
if (Kokkos::Profiling::profileLibraryLoaded()) {
const size_t reported_size =
(arg_logical_size > 0) ? arg_logical_size : arg_alloc_size;
Kokkos::Profiling::deallocateData(arg_handle, arg_label, arg_alloc_ptr,
reported_size);
}
// We have to unset the CoarseGrain property manually as hipFree does not take
// care of it. Otherwise, the allocation would continue to linger in the
// kernel mem page table.
KOKKOS_IMPL_HIP_SAFE_CALL(hipMemAdvise(
arg_alloc_ptr, arg_alloc_size, hipMemAdviseUnsetCoarseGrain, m_device));
KOKKOS_IMPL_HIP_SAFE_CALL(hipFree(arg_alloc_ptr));
}
} // namespace Kokkos