forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kokkos_SYCL_Space.cpp
258 lines (221 loc) · 10.4 KB
/
Kokkos_SYCL_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
//@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_HostSpace.hpp>
#include <SYCL/Kokkos_SYCL.hpp>
#include <SYCL/Kokkos_SYCL_Space.hpp>
#include <SYCL/Kokkos_SYCL_DeepCopy.hpp>
#include <SYCL/Kokkos_SYCL_Instance.hpp>
#include <impl/Kokkos_Profiling.hpp>
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
namespace Kokkos {
namespace Impl {
void DeepCopySYCL(void* dst, const void* src, size_t n) {
Experimental::Impl::SYCLInternal::singleton().m_queue->memcpy(dst, src, n);
}
void DeepCopyAsyncSYCL(const Kokkos::Experimental::SYCL& instance, void* dst,
const void* src, size_t n) {
sycl::queue& q = *instance.impl_internal_space_instance()->m_queue;
auto event = q.memcpy(dst, src, n);
#ifndef KOKKOS_IMPL_SYCL_USE_IN_ORDER_QUEUES
q.ext_oneapi_submit_barrier(std::vector<sycl::event>{event});
#endif
}
void DeepCopyAsyncSYCL(void* dst, const void* src, size_t n) {
Experimental::Impl::SYCLInternal::singleton().m_queue->memcpy(dst, src, n);
Experimental::SYCL().fence(
"Kokkos::Impl::DeepCopyAsyncSYCL: fence after memcpy");
}
} // namespace Impl
} // namespace Kokkos
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
namespace Kokkos {
namespace Experimental {
SYCLDeviceUSMSpace::SYCLDeviceUSMSpace()
: m_queue(*SYCL().impl_internal_space_instance()->m_queue) {}
SYCLDeviceUSMSpace::SYCLDeviceUSMSpace(sycl::queue queue)
: m_queue(std::move(queue)) {}
SYCLSharedUSMSpace::SYCLSharedUSMSpace()
: m_queue(*SYCL().impl_internal_space_instance()->m_queue) {}
SYCLSharedUSMSpace::SYCLSharedUSMSpace(sycl::queue queue)
: m_queue(std::move(queue)) {}
SYCLHostUSMSpace::SYCLHostUSMSpace()
: m_queue(*SYCL().impl_internal_space_instance()->m_queue) {}
SYCLHostUSMSpace::SYCLHostUSMSpace(sycl::queue queue)
: m_queue(std::move(queue)) {}
void* allocate_sycl(
const char* arg_label, const size_t arg_alloc_size,
const size_t arg_logical_size, const Kokkos::Tools::SpaceHandle arg_handle,
const RawMemoryAllocationFailure::AllocationMechanism failure_tag,
const sycl::usm::alloc allocation_kind, const sycl::queue& queue) {
void* const hostPtr = sycl::malloc(arg_alloc_size, queue, allocation_kind);
if (hostPtr == nullptr)
throw RawMemoryAllocationFailure(
arg_alloc_size, 1, RawMemoryAllocationFailure::FailureMode::Unknown,
failure_tag);
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, hostPtr,
reported_size);
}
return hostPtr;
}
void* SYCLDeviceUSMSpace::allocate(const Kokkos::Experimental::SYCL& exec_space,
const size_t arg_alloc_size) const {
return allocate(exec_space, "[unlabeled]", arg_alloc_size);
}
void* SYCLDeviceUSMSpace::allocate(const Kokkos::Experimental::SYCL& exec_space,
const char* arg_label,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
return allocate_sycl(
arg_label, arg_alloc_size, arg_logical_size,
Kokkos::Tools::make_space_handle(name()),
RawMemoryAllocationFailure::AllocationMechanism::SYCLMallocDevice,
sycl::usm::alloc::device,
*exec_space.impl_internal_space_instance()->m_queue);
}
void* SYCLDeviceUSMSpace::allocate(const size_t arg_alloc_size) const {
return allocate("[unlabeled]", arg_alloc_size);
}
void* SYCLDeviceUSMSpace::allocate(const char* arg_label,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
return allocate_sycl(
arg_label, arg_alloc_size, arg_logical_size,
Kokkos::Tools::make_space_handle(name()),
RawMemoryAllocationFailure::AllocationMechanism::SYCLMallocDevice,
sycl::usm::alloc::device, m_queue);
}
void* SYCLSharedUSMSpace::allocate(const SYCL& exec_space,
const size_t arg_alloc_size) const {
return allocate(exec_space, "[unlabeled]", arg_alloc_size);
}
void* SYCLSharedUSMSpace::allocate(const SYCL& exec_space,
const char* arg_label,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
return allocate_sycl(
arg_label, arg_alloc_size, arg_logical_size,
Kokkos::Tools::make_space_handle(name()),
RawMemoryAllocationFailure::AllocationMechanism::SYCLMallocShared,
sycl::usm::alloc::shared,
*exec_space.impl_internal_space_instance()->m_queue);
}
void* SYCLSharedUSMSpace::allocate(const size_t arg_alloc_size) const {
return allocate("[unlabeled]", arg_alloc_size);
}
void* SYCLSharedUSMSpace::allocate(const char* arg_label,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
return allocate_sycl(
arg_label, arg_alloc_size, arg_logical_size,
Kokkos::Tools::make_space_handle(name()),
RawMemoryAllocationFailure::AllocationMechanism::SYCLMallocShared,
sycl::usm::alloc::shared, m_queue);
}
void* SYCLHostUSMSpace::allocate(const SYCL& exec_space,
const size_t arg_alloc_size) const {
return allocate(exec_space, "[unlabeled]", arg_alloc_size);
}
void* SYCLHostUSMSpace::allocate(const SYCL& exec_space, const char* arg_label,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
return allocate_sycl(
arg_label, arg_alloc_size, arg_logical_size,
Kokkos::Tools::make_space_handle(name()),
RawMemoryAllocationFailure::AllocationMechanism::SYCLMallocHost,
sycl::usm::alloc::host,
*exec_space.impl_internal_space_instance()->m_queue);
}
void* SYCLHostUSMSpace::allocate(const size_t arg_alloc_size) const {
return allocate("[unlabeled]", arg_alloc_size);
}
void* SYCLHostUSMSpace::allocate(const char* arg_label,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
return allocate_sycl(
arg_label, arg_alloc_size, arg_logical_size,
Kokkos::Tools::make_space_handle(name()),
RawMemoryAllocationFailure::AllocationMechanism::SYCLMallocHost,
sycl::usm::alloc::host, m_queue);
}
void sycl_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 sycl::queue& queue) {
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);
}
SYCL::impl_static_fence(
"Kokkos::Impl::sycl_deallocate: fence before deallocate");
sycl::free(arg_alloc_ptr, queue);
}
void SYCLDeviceUSMSpace::deallocate(void* const arg_alloc_ptr,
const size_t arg_alloc_size) const {
deallocate("[unlabeled]", arg_alloc_ptr, arg_alloc_size);
}
void SYCLDeviceUSMSpace::deallocate(const char* arg_label,
void* const arg_alloc_ptr,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
sycl_deallocate(arg_label, arg_alloc_ptr, arg_alloc_size, arg_logical_size,
Kokkos::Tools::make_space_handle(name()), m_queue);
}
void SYCLSharedUSMSpace::deallocate(void* const arg_alloc_ptr,
const size_t arg_alloc_size) const {
deallocate("[unlabeled]", arg_alloc_ptr, arg_alloc_size);
}
void SYCLSharedUSMSpace::deallocate(const char* arg_label,
void* const arg_alloc_ptr,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
sycl_deallocate(arg_label, arg_alloc_ptr, arg_alloc_size, arg_logical_size,
Kokkos::Tools::make_space_handle(name()), m_queue);
}
void SYCLHostUSMSpace::deallocate(void* const arg_alloc_ptr,
const size_t arg_alloc_size) const {
deallocate("[unlabeled]", arg_alloc_ptr, arg_alloc_size);
}
void SYCLHostUSMSpace::deallocate(const char* arg_label,
void* const arg_alloc_ptr,
const size_t arg_alloc_size,
const size_t arg_logical_size) const {
sycl_deallocate(arg_label, arg_alloc_ptr, arg_alloc_size, arg_logical_size,
Kokkos::Tools::make_space_handle(name()), m_queue);
}
} // namespace Experimental
} // namespace Kokkos
//==============================================================================
// <editor-fold desc="Explicit instantiations of CRTP Base classes"> {{{1
#include <impl/Kokkos_SharedAlloc_timpl.hpp>
KOKKOS_IMPL_HOST_INACCESSIBLE_SHARED_ALLOCATION_RECORD_EXPLICIT_INSTANTIATION(
Kokkos::Experimental::SYCLDeviceUSMSpace);
KOKKOS_IMPL_SHARED_ALLOCATION_RECORD_EXPLICIT_INSTANTIATION(
Kokkos::Experimental::SYCLSharedUSMSpace);
KOKKOS_IMPL_SHARED_ALLOCATION_RECORD_EXPLICIT_INSTANTIATION(
Kokkos::Experimental::SYCLHostUSMSpace);
// </editor-fold> end Explicit instantiations of CRTP Base classes }}}1
//==============================================================================