forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kokkos_SharedAlloc_timpl.hpp
370 lines (312 loc) · 12.7 KB
/
Kokkos_SharedAlloc_timpl.hpp
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
//@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_SHAREDALLOC_TIMPL_HPP
#define KOKKOS_IMPL_SHAREDALLOC_TIMPL_HPP
#include <Kokkos_Macros.hpp>
#include <Kokkos_Core_fwd.hpp>
#include <impl/Kokkos_SharedAlloc.hpp>
#include <Kokkos_HostSpace.hpp> // used with HostInaccessible specializations
#include <cstring>
#include <ostream>
#include <string>
namespace Kokkos {
namespace Impl {
template <class MemorySpace>
SharedAllocationRecordCommon<MemorySpace>::~SharedAllocationRecordCommon() {
auto alloc_ptr = SharedAllocationRecord<void, void>::m_alloc_ptr;
auto alloc_size = SharedAllocationRecord<void, void>::m_alloc_size;
auto label = SharedAllocationRecord<void, void>::m_label;
m_space.deallocate(label.c_str(), alloc_ptr, alloc_size,
alloc_size - sizeof(SharedAllocationHeader));
}
template <class MemorySpace>
HostInaccessibleSharedAllocationRecordCommon<
MemorySpace>::~HostInaccessibleSharedAllocationRecordCommon() {
auto alloc_ptr = SharedAllocationRecord<void, void>::m_alloc_ptr;
auto alloc_size = SharedAllocationRecord<void, void>::m_alloc_size;
auto label = SharedAllocationRecord<void, void>::m_label;
m_space.deallocate(label.c_str(), alloc_ptr, alloc_size,
alloc_size - sizeof(SharedAllocationHeader));
}
template <class MemorySpace>
SharedAllocationRecordCommon<MemorySpace>::SharedAllocationRecordCommon(
MemorySpace const& space, std::string const& label, std::size_t alloc_size,
SharedAllocationRecord<void, void>::function_type dealloc)
: SharedAllocationRecord<void, void>(
#ifdef KOKKOS_ENABLE_DEBUG
&s_root_record,
#endif
checked_allocation_with_header(space, label, alloc_size),
sizeof(SharedAllocationHeader) + alloc_size, dealloc, label),
m_space(space) {
auto& header = *SharedAllocationRecord<void, void>::m_alloc_ptr;
fill_host_accessible_header_info(this, header, label);
}
template <class MemorySpace>
HostInaccessibleSharedAllocationRecordCommon<MemorySpace>::
HostInaccessibleSharedAllocationRecordCommon(
MemorySpace const& space, std::string const& label,
std::size_t alloc_size,
SharedAllocationRecord<void, void>::function_type dealloc)
: SharedAllocationRecord<void, void>(
#ifdef KOKKOS_ENABLE_DEBUG
&s_root_record,
#endif
checked_allocation_with_header(space, label, alloc_size),
sizeof(SharedAllocationHeader) + alloc_size, dealloc, label),
m_space(space) {
SharedAllocationHeader header;
fill_host_accessible_header_info(this, header, label);
typename MemorySpace::execution_space exec;
Kokkos::Impl::DeepCopy<MemorySpace, HostSpace>(
exec, SharedAllocationRecord<void, void>::m_alloc_ptr, &header,
sizeof(SharedAllocationHeader));
exec.fence(std::string("SharedAllocationRecord<Kokkos::") +
MemorySpace::name() +
"Space, void>::SharedAllocationRecord(): "
"fence after copying header from HostSpace");
}
template <class MemorySpace>
auto SharedAllocationRecordCommon<MemorySpace>::allocate(
MemorySpace const& arg_space, std::string const& arg_label,
size_t arg_alloc_size) -> derived_t* {
return new derived_t(arg_space, arg_label, arg_alloc_size);
}
template <class MemorySpace>
void* SharedAllocationRecordCommon<MemorySpace>::allocate_tracked(
const MemorySpace& arg_space, const std::string& arg_alloc_label,
size_t arg_alloc_size) {
if (!arg_alloc_size) return nullptr;
SharedAllocationRecord* const r =
allocate(arg_space, arg_alloc_label, arg_alloc_size);
record_base_t::increment(r);
return r->data();
}
template <class MemorySpace>
void SharedAllocationRecordCommon<MemorySpace>::deallocate(
SharedAllocationRecordCommon::record_base_t* arg_rec) {
delete static_cast<derived_t*>(arg_rec);
}
template <class MemorySpace>
void SharedAllocationRecordCommon<MemorySpace>::deallocate_tracked(
void* arg_alloc_ptr) {
if (arg_alloc_ptr != nullptr) {
SharedAllocationRecord* const r = derived_t::get_record(arg_alloc_ptr);
record_base_t::decrement(r);
}
}
template <class MemorySpace>
void* SharedAllocationRecordCommon<MemorySpace>::reallocate_tracked(
void* arg_alloc_ptr, size_t arg_alloc_size) {
derived_t* const r_old = derived_t::get_record(arg_alloc_ptr);
derived_t* const r_new =
allocate(r_old->m_space, r_old->get_label(), arg_alloc_size);
Kokkos::Impl::DeepCopy<MemorySpace, MemorySpace>(
r_new->data(), r_old->data(), std::min(r_old->size(), r_new->size()));
Kokkos::fence(
"SharedAllocationRecord<Kokkos::Experimental::HBWSpace, "
"void>::reallocate_tracked(): fence after copying data");
record_base_t::increment(r_new);
record_base_t::decrement(r_old);
return r_new->data();
}
template <class MemorySpace>
auto HostInaccessibleSharedAllocationRecordCommon<MemorySpace>::allocate(
MemorySpace const& arg_space, std::string const& arg_label,
size_t arg_alloc_size) -> derived_t* {
return new derived_t(arg_space, arg_label, arg_alloc_size);
}
template <class MemorySpace>
void* HostInaccessibleSharedAllocationRecordCommon<
MemorySpace>::allocate_tracked(const MemorySpace& arg_space,
const std::string& arg_alloc_label,
size_t arg_alloc_size) {
if (!arg_alloc_size) return nullptr;
SharedAllocationRecord* const r =
allocate(arg_space, arg_alloc_label, arg_alloc_size);
record_base_t::increment(r);
return r->data();
}
template <class MemorySpace>
void HostInaccessibleSharedAllocationRecordCommon<MemorySpace>::deallocate(
HostInaccessibleSharedAllocationRecordCommon::record_base_t* arg_rec) {
delete static_cast<derived_t*>(arg_rec);
}
template <class MemorySpace>
void HostInaccessibleSharedAllocationRecordCommon<
MemorySpace>::deallocate_tracked(void* arg_alloc_ptr) {
if (arg_alloc_ptr != nullptr) {
SharedAllocationRecord* const r = derived_t::get_record(arg_alloc_ptr);
record_base_t::decrement(r);
}
}
template <class MemorySpace>
void* HostInaccessibleSharedAllocationRecordCommon<
MemorySpace>::reallocate_tracked(void* arg_alloc_ptr,
size_t arg_alloc_size) {
derived_t* const r_old = derived_t::get_record(arg_alloc_ptr);
derived_t* const r_new =
allocate(r_old->m_space, r_old->get_label(), arg_alloc_size);
Kokkos::Impl::DeepCopy<MemorySpace, MemorySpace>(
r_new->data(), r_old->data(), std::min(r_old->size(), r_new->size()));
Kokkos::fence(
"SharedAllocationRecord<Kokkos::Experimental::HBWSpace, "
"void>::reallocate_tracked(): fence after copying data");
record_base_t::increment(r_new);
record_base_t::decrement(r_old);
return r_new->data();
}
template <class MemorySpace>
auto SharedAllocationRecordCommon<MemorySpace>::get_record(void* alloc_ptr)
-> derived_t* {
using Header = SharedAllocationHeader;
Header const* const h = alloc_ptr ? Header::get_header(alloc_ptr) : nullptr;
if (!alloc_ptr || h->m_record->m_alloc_ptr != h) {
Kokkos::Impl::throw_runtime_exception(
std::string("Kokkos::Impl::SharedAllocationRecordCommon<") +
std::string(MemorySpace::name()) +
std::string(">::get_record() ERROR"));
}
return static_cast<derived_t*>(h->m_record);
}
template <class MemorySpace>
std::string SharedAllocationRecordCommon<MemorySpace>::get_label() const {
return record_base_t::m_label;
}
template <class MemorySpace>
void SharedAllocationRecordCommon<MemorySpace>::print_records(
std::ostream& s, const MemorySpace&, bool detail) {
(void)s;
(void)detail;
#ifdef KOKKOS_ENABLE_DEBUG
SharedAllocationRecord<void, void>::print_host_accessible_records(
s, MemorySpace::name(), &derived_t::s_root_record, detail);
#else
Kokkos::Impl::throw_runtime_exception(
std::string("SharedAllocationHeader<") +
std::string(MemorySpace::name()) +
std::string(
">::print_records only works with KOKKOS_ENABLE_DEBUG enabled"));
#endif
}
template <class MemorySpace>
void HostInaccessibleSharedAllocationRecordCommon<MemorySpace>::print_records(
std::ostream& s, const MemorySpace&, bool detail) {
(void)s;
(void)detail;
#ifdef KOKKOS_ENABLE_DEBUG
SharedAllocationRecord<void, void>* r = &derived_t::s_root_record;
char buffer[256];
SharedAllocationHeader head;
if (detail) {
do {
if (r->m_alloc_ptr) {
Kokkos::Impl::DeepCopy<HostSpace, MemorySpace>(
&head, r->m_alloc_ptr, sizeof(SharedAllocationHeader));
Kokkos::fence(
"HostInaccessibleSharedAllocationRecordCommon::print_records(): "
"fence after copying header to HostSpace");
} else {
head.m_label[0] = 0;
}
// Formatting dependent on sizeof(uintptr_t)
const char* format_string;
if (sizeof(uintptr_t) == sizeof(unsigned long)) {
format_string =
"%s addr( 0x%.12lx ) list( 0x%.12lx 0x%.12lx ) extent[ 0x%.12lx "
"+ %.8ld ] count(%d) dealloc(0x%.12lx) %s\n";
} else if (sizeof(uintptr_t) == sizeof(unsigned long long)) {
format_string =
"%s addr( 0x%.12llx ) list( 0x%.12llx 0x%.12llx ) extent[ "
"0x%.12llx + %.8ld ] count(%d) dealloc(0x%.12llx) %s\n";
}
snprintf(buffer, 256, format_string, MemorySpace::execution_space::name(),
reinterpret_cast<uintptr_t>(r),
reinterpret_cast<uintptr_t>(r->m_prev),
reinterpret_cast<uintptr_t>(r->m_next),
reinterpret_cast<uintptr_t>(r->m_alloc_ptr), r->m_alloc_size,
r->m_count, reinterpret_cast<uintptr_t>(r->m_dealloc),
head.m_label);
s << buffer;
r = r->m_next;
} while (r != &derived_t::s_root_record);
} else {
do {
if (r->m_alloc_ptr) {
Kokkos::Impl::DeepCopy<HostSpace, MemorySpace>(
&head, r->m_alloc_ptr, sizeof(SharedAllocationHeader));
Kokkos::fence(
"HostInaccessibleSharedAllocationRecordCommon::print_records(): "
"fence after copying header to HostSpace");
// Formatting dependent on sizeof(uintptr_t)
const char* format_string;
if (sizeof(uintptr_t) == sizeof(unsigned long)) {
format_string = "%s [ 0x%.12lx + %ld ] %s\n";
} else if (sizeof(uintptr_t) == sizeof(unsigned long long)) {
format_string = "%s [ 0x%.12llx + %ld ] %s\n";
}
snprintf(
buffer, 256, format_string, MemorySpace::execution_space::name(),
reinterpret_cast<uintptr_t>(r->data()), r->size(), head.m_label);
} else {
snprintf(buffer, 256, "%s [ 0 + 0 ]\n",
MemorySpace::execution_space::name());
}
s << buffer;
r = r->m_next;
} while (r != &derived_t::s_root_record);
}
#else
Kokkos::Impl::throw_runtime_exception(
std::string("SharedAllocationHeader<") +
std::string(MemorySpace::name()) +
std::string(
">::print_records only works with KOKKOS_ENABLE_DEBUG enabled"));
#endif
}
template <class MemorySpace>
auto HostInaccessibleSharedAllocationRecordCommon<MemorySpace>::get_record(
void* alloc_ptr) -> derived_t* {
// Copy the header from the allocation
SharedAllocationHeader head;
SharedAllocationHeader const* const head_cuda =
alloc_ptr ? SharedAllocationHeader::get_header(alloc_ptr) : nullptr;
if (alloc_ptr) {
typename MemorySpace::execution_space exec_space;
Kokkos::Impl::DeepCopy<HostSpace, MemorySpace, decltype(exec_space)>(
exec_space, &head, head_cuda, sizeof(SharedAllocationHeader));
exec_space.fence(
"HostInaccessibleSharedAllocationRecordCommon::get_record(): fence "
"after copying header to HostSpace");
}
derived_t* const record =
alloc_ptr ? static_cast<derived_t*>(head.m_record) : nullptr;
if (!alloc_ptr || record->m_alloc_ptr != head_cuda) {
Kokkos::Impl::throw_runtime_exception(
std::string("Kokkos::Impl::SharedAllocationRecord<") +
std::string(MemorySpace::name()) +
std::string(", void>::get_record ERROR"));
}
return record;
}
template <class MemorySpace>
std::string
HostInaccessibleSharedAllocationRecordCommon<MemorySpace>::get_label() const {
return record_base_t::m_label;
}
} // end namespace Impl
} // end namespace Kokkos
#endif // KOKKOS_IMPL_SHAREDALLOC_TIMPL_HPP