forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kokkos_HIP_Instance.cpp
421 lines (339 loc) · 14.1 KB
/
Kokkos_HIP_Instance.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//@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
/*--------------------------------------------------------------------------*/
/* Kokkos interfaces */
#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
#define KOKKOS_IMPL_PUBLIC_INCLUDE
#endif
#include <Kokkos_Core.hpp>
#include <HIP/Kokkos_HIP_Instance.hpp>
#include <HIP/Kokkos_HIP.hpp>
#include <HIP/Kokkos_HIP_Space.hpp>
#include <impl/Kokkos_CheckedIntegerOps.hpp>
#include <impl/Kokkos_Error.hpp>
/*--------------------------------------------------------------------------*/
/* Standard 'C' libraries */
#include <stdlib.h>
/* Standard 'C++' libraries */
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#ifdef KOKKOS_ENABLE_HIP_RELOCATABLE_DEVICE_CODE
__device__ __constant__ unsigned long kokkos_impl_hip_constant_memory_buffer
[Kokkos::Impl::HIPTraits::ConstantMemoryUsage / sizeof(unsigned long)];
#endif
namespace Kokkos {
namespace Impl {
Kokkos::View<uint32_t *, HIPSpace> hip_global_unique_token_locks(
bool deallocate) {
static Kokkos::View<uint32_t *, HIPSpace> locks =
Kokkos::View<uint32_t *, HIPSpace>();
if (!deallocate && locks.extent(0) == 0)
locks = Kokkos::View<uint32_t *, HIPSpace>(
"Kokkos::UniqueToken<HIP>::m_locks", HIPInternal::concurrency());
if (deallocate) locks = Kokkos::View<uint32_t *, HIPSpace>();
return locks;
}
} // namespace Impl
} // namespace Kokkos
namespace Kokkos {
namespace Impl {
namespace {
using ScratchGrain = Kokkos::HIP::size_type[Impl::HIPTraits::WarpSize];
constexpr auto sizeScratchGrain = sizeof(ScratchGrain);
std::size_t scratch_count(const std::size_t size) {
return (size + sizeScratchGrain - 1) / sizeScratchGrain;
}
} // namespace
//----------------------------------------------------------------------------
int HIPInternal::concurrency() {
static int const concurrency = m_maxThreadsPerSM * m_multiProcCount;
return concurrency;
}
void HIPInternal::print_configuration(std::ostream &s) const {
s << "macro KOKKOS_ENABLE_HIP : defined" << '\n';
#if defined(HIP_VERSION)
s << "macro HIP_VERSION = " << HIP_VERSION << " = version "
<< HIP_VERSION_MAJOR << '.' << HIP_VERSION_MINOR << '.' << HIP_VERSION_PATCH
<< '\n';
#endif
int hipDevCount;
KOKKOS_IMPL_HIP_SAFE_CALL(hipGetDeviceCount(&hipDevCount));
for (int i = 0; i < hipDevCount; ++i) {
hipDeviceProp_t hipProp;
KOKKOS_IMPL_HIP_SAFE_CALL(hipGetDeviceProperties(&hipProp, i));
std::string gpu_type = hipProp.integrated == 1 ? "APU" : "dGPU";
s << "Kokkos::HIP[ " << i << " ] "
<< "gcnArch " << hipProp.gcnArchName << ", Total Global Memory: "
<< ::Kokkos::Impl::human_memory_size(hipProp.totalGlobalMem)
<< ", Shared Memory per Block: "
<< ::Kokkos::Impl::human_memory_size(hipProp.sharedMemPerBlock)
<< ", APU or dGPU: " << gpu_type
<< ", Is Large Bar: " << hipProp.isLargeBar
<< ", Supports Managed Memory: " << hipProp.managedMemory
<< ", Wavefront Size: " << hipProp.warpSize;
if (m_hipDev == i) s << " : Selected";
s << '\n';
}
}
//----------------------------------------------------------------------------
HIPInternal::~HIPInternal() {
if (m_scratchSpace || m_scratchFlags) {
std::cerr << "Kokkos::HIP ERROR: Failed to call "
"Kokkos::HIP::finalize()"
<< std::endl;
std::cerr.flush();
}
m_scratchSpaceCount = 0;
m_scratchFlagsCount = 0;
m_scratchSpace = nullptr;
m_scratchFlags = nullptr;
m_stream = nullptr;
}
int HIPInternal::verify_is_initialized(const char *const label) const {
if (m_hipDev < 0) {
Kokkos::abort((std::string("Kokkos::HIP::") + label +
" : ERROR device not initialized\n")
.c_str());
}
return 0 <= m_hipDev;
}
uint32_t HIPInternal::impl_get_instance_id() const noexcept {
return m_instance_id;
}
HIPInternal &HIPInternal::singleton() {
static HIPInternal *self = nullptr;
if (!self) {
self = new HIPInternal();
}
return *self;
}
void HIPInternal::fence() const {
fence("Kokkos::HIPInternal::fence: Unnamed Internal Fence");
}
void HIPInternal::fence(const std::string &name) const {
Kokkos::Tools::Experimental::Impl::profile_fence_event<Kokkos::HIP>(
name,
Kokkos::Tools::Experimental::Impl::DirectFenceIDHandle{
impl_get_instance_id()},
[&]() { KOKKOS_IMPL_HIP_SAFE_CALL(hipStreamSynchronize(m_stream)); });
}
void HIPInternal::initialize(hipStream_t stream, bool manage_stream) {
KOKKOS_EXPECTS(!is_initialized());
if (was_finalized)
Kokkos::abort("Calling HIP::initialize after HIP::finalize is illegal\n");
m_stream = stream;
m_manage_stream = manage_stream;
//----------------------------------
// Multiblock reduction uses scratch flags for counters
// and scratch space for partial reduction values.
// Allocate some initial space. This will grow as needed.
{
const unsigned reduce_block_count =
m_maxWarpCount * Impl::HIPTraits::WarpSize;
(void)scratch_flags(reduce_block_count * 2 * sizeof(size_type));
(void)scratch_space(reduce_block_count * 16 * sizeof(size_type));
}
m_num_scratch_locks = concurrency();
KOKKOS_IMPL_HIP_SAFE_CALL(
hipMalloc(&m_scratch_locks, sizeof(int32_t) * m_num_scratch_locks));
KOKKOS_IMPL_HIP_SAFE_CALL(
hipMemset(m_scratch_locks, 0, sizeof(int32_t) * m_num_scratch_locks));
}
//----------------------------------------------------------------------------
Kokkos::HIP::size_type *HIPInternal::scratch_space(const std::size_t size) {
if (verify_is_initialized("scratch_space") &&
m_scratchSpaceCount < scratch_count(size)) {
m_scratchSpaceCount = scratch_count(size);
using Record = Kokkos::Impl::SharedAllocationRecord<Kokkos::HIPSpace, void>;
if (m_scratchSpace) Record::decrement(Record::get_record(m_scratchSpace));
std::size_t alloc_size =
multiply_overflow_abort(m_scratchSpaceCount, sizeScratchGrain);
Record *const r = Record::allocate(
Kokkos::HIPSpace(), "Kokkos::InternalScratchSpace", alloc_size);
Record::increment(r);
m_scratchSpace = reinterpret_cast<size_type *>(r->data());
}
return m_scratchSpace;
}
Kokkos::HIP::size_type *HIPInternal::scratch_flags(const std::size_t size) {
if (verify_is_initialized("scratch_flags") &&
m_scratchFlagsCount < scratch_count(size)) {
m_scratchFlagsCount = scratch_count(size);
using Record = Kokkos::Impl::SharedAllocationRecord<Kokkos::HIPSpace, void>;
if (m_scratchFlags) Record::decrement(Record::get_record(m_scratchFlags));
std::size_t alloc_size =
multiply_overflow_abort(m_scratchFlagsCount, sizeScratchGrain);
Record *const r = Record::allocate(
Kokkos::HIPSpace(), "Kokkos::InternalScratchFlags", alloc_size);
Record::increment(r);
m_scratchFlags = reinterpret_cast<size_type *>(r->data());
KOKKOS_IMPL_HIP_SAFE_CALL(hipMemset(m_scratchFlags, 0, alloc_size));
}
return m_scratchFlags;
}
Kokkos::HIP::size_type *HIPInternal::stage_functor_for_execution(
void const *driver, std::size_t const size) const {
if (verify_is_initialized("scratch_functor") && m_scratchFunctorSize < size) {
m_scratchFunctorSize = size;
using Record = Kokkos::Impl::SharedAllocationRecord<Kokkos::HIPSpace, void>;
using RecordHost =
Kokkos::Impl::SharedAllocationRecord<Kokkos::HIPHostPinnedSpace, void>;
if (m_scratchFunctor) {
Record::decrement(Record::get_record(m_scratchFunctor));
RecordHost::decrement(RecordHost::get_record(m_scratchFunctorHost));
}
Record *const r =
Record::allocate(Kokkos::HIPSpace(), "Kokkos::InternalScratchFunctor",
m_scratchFunctorSize);
RecordHost *const r_host = RecordHost::allocate(
Kokkos::HIPHostPinnedSpace(), "Kokkos::InternalScratchFunctorHost",
m_scratchFunctorSize);
Record::increment(r);
RecordHost::increment(r_host);
m_scratchFunctor = reinterpret_cast<size_type *>(r->data());
m_scratchFunctorHost = reinterpret_cast<size_type *>(r_host->data());
}
// When using HSA_XNACK=1, it is necessary to copy the driver to the host to
// ensure that the driver is not destroyed before the computation is done.
// Without this fix, all the atomic tests fail. It is not obvious that this
// problem is limited to HSA_XNACK=1 even if all the tests pass when
// HSA_XNACK=0. That's why we always copy the driver.
KOKKOS_IMPL_HIP_SAFE_CALL(hipStreamSynchronize(m_stream));
std::memcpy(m_scratchFunctorHost, driver, size);
KOKKOS_IMPL_HIP_SAFE_CALL(hipMemcpyAsync(m_scratchFunctor,
m_scratchFunctorHost, size,
hipMemcpyDefault, m_stream));
return m_scratchFunctor;
}
int HIPInternal::acquire_team_scratch_space() {
int current_team_scratch = 0;
int zero = 0;
while (!m_team_scratch_pool[current_team_scratch].compare_exchange_weak(
zero, 1, std::memory_order_release, std::memory_order_relaxed)) {
current_team_scratch = (current_team_scratch + 1) % m_n_team_scratch;
}
return current_team_scratch;
}
void *HIPInternal::resize_team_scratch_space(int scratch_pool_id,
std::int64_t bytes,
bool force_shrink) {
// Multiple ParallelFor/Reduce Teams can call this function at the same time
// and invalidate the m_team_scratch_ptr. We use a pool to avoid any race
// condition.
if (m_team_scratch_current_size[scratch_pool_id] == 0) {
m_team_scratch_current_size[scratch_pool_id] = bytes;
m_team_scratch_ptr[scratch_pool_id] =
Kokkos::kokkos_malloc<Kokkos::HIPSpace>(
"Kokkos::HIPSpace::TeamScratchMemory",
m_team_scratch_current_size[scratch_pool_id]);
}
if ((bytes > m_team_scratch_current_size[scratch_pool_id]) ||
((bytes < m_team_scratch_current_size[scratch_pool_id]) &&
(force_shrink))) {
m_team_scratch_current_size[scratch_pool_id] = bytes;
m_team_scratch_ptr[scratch_pool_id] =
Kokkos::kokkos_realloc<Kokkos::HIPSpace>(
m_team_scratch_ptr[scratch_pool_id],
m_team_scratch_current_size[scratch_pool_id]);
}
return m_team_scratch_ptr[scratch_pool_id];
}
void HIPInternal::release_team_scratch_space(int scratch_pool_id) {
m_team_scratch_pool[scratch_pool_id] = 0;
}
//----------------------------------------------------------------------------
void HIPInternal::finalize() {
this->fence("Kokkos::HIPInternal::finalize: fence on finalization");
was_finalized = true;
if (this == &singleton()) {
(void)Kokkos::Impl::hip_global_unique_token_locks(true);
desul::Impl::finalize_lock_arrays(); // FIXME
KOKKOS_IMPL_HIP_SAFE_CALL(hipHostFree(constantMemHostStaging));
KOKKOS_IMPL_HIP_SAFE_CALL(hipEventDestroy(constantMemReusable));
}
if (nullptr != m_scratchSpace || nullptr != m_scratchFlags) {
using RecordHIP = Kokkos::Impl::SharedAllocationRecord<Kokkos::HIPSpace>;
RecordHIP::decrement(RecordHIP::get_record(m_scratchFlags));
RecordHIP::decrement(RecordHIP::get_record(m_scratchSpace));
if (m_scratchFunctorSize > 0) {
RecordHIP::decrement(RecordHIP::get_record(m_scratchFunctor));
RecordHIP::decrement(RecordHIP::get_record(m_scratchFunctorHost));
}
}
for (int i = 0; i < m_n_team_scratch; ++i) {
if (m_team_scratch_current_size[i] > 0)
Kokkos::kokkos_free<Kokkos::HIPSpace>(m_team_scratch_ptr[i]);
}
if (m_manage_stream && m_stream != nullptr)
KOKKOS_IMPL_HIP_SAFE_CALL(hipStreamDestroy(m_stream));
m_scratchSpaceCount = 0;
m_scratchFlagsCount = 0;
m_scratchSpace = nullptr;
m_scratchFlags = nullptr;
m_stream = nullptr;
for (int i = 0; i < m_n_team_scratch; ++i) {
m_team_scratch_current_size[i] = 0;
m_team_scratch_ptr[i] = nullptr;
}
KOKKOS_IMPL_HIP_SAFE_CALL(hipFree(m_scratch_locks));
m_scratch_locks = nullptr;
m_num_scratch_locks = 0;
}
//----------------------------------------------------------------------------
Kokkos::HIP::size_type hip_internal_multiprocessor_count() {
return HIPInternal::singleton().m_multiProcCount;
}
Kokkos::HIP::size_type hip_internal_maximum_warp_count() {
return HIPInternal::singleton().m_maxWarpCount;
}
std::array<Kokkos::HIP::size_type, 3> hip_internal_maximum_grid_count() {
return HIPInternal::singleton().m_maxBlock;
}
Kokkos::HIP::size_type *hip_internal_scratch_space(const HIP &instance,
const std::size_t size) {
return instance.impl_internal_space_instance()->scratch_space(size);
}
Kokkos::HIP::size_type *hip_internal_scratch_flags(const HIP &instance,
const std::size_t size) {
return instance.impl_internal_space_instance()->scratch_flags(size);
}
} // namespace Impl
} // namespace Kokkos
//----------------------------------------------------------------------------
namespace Kokkos {
namespace Impl {
void hip_internal_error_throw(hipError_t e, const char *name, const char *file,
const int line) {
std::ostringstream out;
out << name << " error( " << hipGetErrorName(e)
<< "): " << hipGetErrorString(e);
if (file) {
out << " " << file << ":" << line;
}
throw_runtime_exception(out.str());
}
} // namespace Impl
} // namespace Kokkos
//----------------------------------------------------------------------------
void Kokkos::Impl::create_HIP_instances(std::vector<HIP> &instances) {
for (int s = 0; s < int(instances.size()); s++) {
hipStream_t stream;
KOKKOS_IMPL_HIP_SAFE_CALL(hipStreamCreate(&stream));
instances[s] = HIP(stream, ManageStream::yes);
}
}