Skip to content

Commit 53430c8

Browse files
[SYCL] Add buffer_allocator SYCL 2020 conformant variant (#6200)
These changes makes `buffer_allocator` a template class taking the data type of the associated buffer. Likewise it changes the default allocator used by buffer to be `buffer_allocator<std::remove_const_t<T>>`. Since these changes are API breaking they are only applied if SYCL2020_CONFORMANT_APIS is defined. Signed-off-by: Larsen, Steffen <[email protected]>
1 parent f20f52a commit 53430c8

File tree

9 files changed

+78
-63
lines changed

9 files changed

+78
-63
lines changed

sycl/doc/PreprocessorMacros.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ This file describes macros that have effect on SYCL compiler and run-time.
6565
should be `std::vector<cl_mem>` instead of `cl_mem`. Defining this macro
6666
will change the behavior of `interop_handle::get_native_mem()` and `sycl::get_native()` functions
6767
and using type for `BackendReturn<backend::opencl, buffer>` to be in line with the spec.
68+
3) According to spec, `sycl::buffer_allocator` should be a template class taking a single
69+
type parameter denoting the data type of the associated buffer. Likewise, `sycl::buffer`
70+
with that take an allocator as a constructor argument should use
71+
`sycl::buffer_allocator<std::remove_const_t<T>>` by default, where `T` is the data type of
72+
that buffer. Defining this macro will change the definition of `sycl::buffer_allocator` to
73+
be templated and `sycl::buffer` will be using `sycl::buffer_allocator<std::remove_const_t<T>>`
74+
by default, where `T` is the data type of that buffer, if it is not explicitly given an
75+
allocator.
6876

6977
## Version macros
7078

sycl/include/CL/sycl/backend.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ typename std::enable_if<
318318
}
319319

320320
template <backend Backend, typename T, int Dimensions = 1,
321-
typename AllocatorT = buffer_allocator>
321+
typename AllocatorT = detail::default_buffer_allocator<T>>
322322
typename std::enable_if<detail::InteropFeatureSupportMap<Backend>::MakeBuffer ==
323323
true &&
324324
Backend != backend::ext_oneapi_level_zero,

sycl/include/CL/sycl/buffer.hpp

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,27 @@ class handler;
2323
class queue;
2424
template <int dimensions> class range;
2525

26+
// Guard SYCL 2020 buffer_allocator with template arguments behind the
27+
// SYCL2020_CONFORMANT_APIS macro.
28+
#ifdef SYCL2020_CONFORMANT_APIS
29+
template <typename DataT>
30+
using buffer_allocator = detail::sycl_memory_object_allocator<DataT>;
31+
#else
32+
using buffer_allocator = detail::sycl_memory_object_allocator<char>;
33+
#endif
34+
2635
namespace detail {
36+
37+
// Generalized implementation of the default allocator used by buffers.
38+
// TODO: When the SYCL 1.2.1 version of buffer_allocator is removed, this should
39+
// be removed.
40+
#ifdef SYCL2020_CONFORMANT_APIS
41+
template <typename DataT>
42+
using default_buffer_allocator = buffer_allocator<std::remove_const_t<DataT>>;
43+
#else
44+
template <typename> using default_buffer_allocator = buffer_allocator;
45+
#endif
46+
2747
template <typename T, int Dimensions, typename AllocatorT>
2848
buffer<T, Dimensions, AllocatorT, void>
2949
make_buffer_helper(pi_native_handle Handle, const context &Ctx, event Evt = {},
@@ -39,7 +59,7 @@ auto get_native_buffer(const buffer<DataT, Dimensions, Allocator, void> &Obj)
3959
buffer<DataT, Dimensions, Allocator, void>>;
4060

4161
template <backend Backend, typename DataT, int Dimensions,
42-
typename AllocatorT = cl::sycl::buffer_allocator>
62+
typename AllocatorT = detail::default_buffer_allocator<DataT>>
4363
struct BufferInterop;
4464
} // namespace detail
4565

@@ -52,7 +72,7 @@ struct BufferInterop;
5272
///
5373
/// \ingroup sycl_api
5474
template <typename T, int dimensions = 1,
55-
typename AllocatorT = cl::sycl::buffer_allocator,
75+
typename AllocatorT = detail::default_buffer_allocator<T>,
5676
typename __Enabled = typename detail::enable_if_t<(dimensions > 0) &&
5777
(dimensions <= 3)>>
5878
class buffer {
@@ -96,7 +116,7 @@ class buffer {
96116
: Range(bufferRange) {
97117
impl = std::make_shared<detail::buffer_impl>(
98118
size() * sizeof(T), detail::getNextPowerOfTwo(sizeof(T)), propList,
99-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>());
119+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>());
100120
impl->constructorNotification(CodeLoc, (void *)impl.get(), nullptr,
101121
(const void *)typeid(T).name(), dimensions,
102122
sizeof(T), rangeToArray(Range).data());
@@ -108,7 +128,7 @@ class buffer {
108128
: Range(bufferRange) {
109129
impl = std::make_shared<detail::buffer_impl>(
110130
size() * sizeof(T), detail::getNextPowerOfTwo(sizeof(T)), propList,
111-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
131+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>(
112132
allocator));
113133
impl->constructorNotification(CodeLoc, (void *)impl.get(), nullptr,
114134
(const void *)typeid(T).name(), dimensions,
@@ -122,7 +142,7 @@ class buffer {
122142
impl = std::make_shared<detail::buffer_impl>(
123143
hostData, size() * sizeof(T), detail::getNextPowerOfTwo(sizeof(T)),
124144
propList,
125-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>());
145+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>());
126146
impl->constructorNotification(CodeLoc, (void *)impl.get(), hostData,
127147
(const void *)typeid(T).name(), dimensions,
128148
sizeof(T), rangeToArray(Range).data());
@@ -135,7 +155,7 @@ class buffer {
135155
impl = std::make_shared<detail::buffer_impl>(
136156
hostData, size() * sizeof(T), detail::getNextPowerOfTwo(sizeof(T)),
137157
propList,
138-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
158+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>(
139159
allocator));
140160
impl->constructorNotification(CodeLoc, (void *)impl.get(), hostData,
141161
(const void *)typeid(T).name(), dimensions,
@@ -151,7 +171,7 @@ class buffer {
151171
impl = std::make_shared<detail::buffer_impl>(
152172
hostData, size() * sizeof(T), detail::getNextPowerOfTwo(sizeof(T)),
153173
propList,
154-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>());
174+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>());
155175
impl->constructorNotification(CodeLoc, (void *)impl.get(), hostData,
156176
(const void *)typeid(T).name(), dimensions,
157177
sizeof(T), rangeToArray(Range).data());
@@ -166,7 +186,7 @@ class buffer {
166186
impl = std::make_shared<detail::buffer_impl>(
167187
hostData, size() * sizeof(T), detail::getNextPowerOfTwo(sizeof(T)),
168188
propList,
169-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
189+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>(
170190
allocator));
171191
impl->constructorNotification(CodeLoc, (void *)impl.get(), hostData,
172192
(const void *)typeid(T).name(), dimensions,
@@ -181,7 +201,7 @@ class buffer {
181201
impl = std::make_shared<detail::buffer_impl>(
182202
hostData, size() * sizeof(T), detail::getNextPowerOfTwo(sizeof(T)),
183203
propList,
184-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
204+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>(
185205
allocator));
186206
impl->constructorNotification(CodeLoc, (void *)impl.get(),
187207
(void *)hostData.get(),
@@ -197,7 +217,7 @@ class buffer {
197217
impl = std::make_shared<detail::buffer_impl>(
198218
hostData, size() * sizeof(T), detail::getNextPowerOfTwo(sizeof(T)),
199219
propList,
200-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
220+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>(
201221
allocator));
202222
impl->constructorNotification(CodeLoc, (void *)impl.get(),
203223
(void *)hostData.get(),
@@ -213,7 +233,7 @@ class buffer {
213233
impl = std::make_shared<detail::buffer_impl>(
214234
hostData, size() * sizeof(T), detail::getNextPowerOfTwo(sizeof(T)),
215235
propList,
216-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>());
236+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>());
217237
impl->constructorNotification(CodeLoc, (void *)impl.get(),
218238
(void *)hostData.get(),
219239
(const void *)typeid(T).name(), dimensions,
@@ -228,7 +248,7 @@ class buffer {
228248
impl = std::make_shared<detail::buffer_impl>(
229249
hostData, size() * sizeof(T), detail::getNextPowerOfTwo(sizeof(T)),
230250
propList,
231-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>());
251+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>());
232252
impl->constructorNotification(CodeLoc, (void *)impl.get(),
233253
(void *)hostData.get(),
234254
(const void *)typeid(T).name(), dimensions,
@@ -245,7 +265,7 @@ class buffer {
245265
impl = std::make_shared<detail::buffer_impl>(
246266
first, last, size() * sizeof(T), detail::getNextPowerOfTwo(sizeof(T)),
247267
propList,
248-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
268+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>(
249269
allocator));
250270
size_t r[3] = {Range[0], 0, 0};
251271
impl->constructorNotification(CodeLoc, (void *)impl.get(), &first,
@@ -263,7 +283,7 @@ class buffer {
263283
impl = std::make_shared<detail::buffer_impl>(
264284
first, last, size() * sizeof(T), detail::getNextPowerOfTwo(sizeof(T)),
265285
propList,
266-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>());
286+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>());
267287
size_t r[3] = {Range[0], 0, 0};
268288
impl->constructorNotification(CodeLoc, (void *)impl.get(), &first,
269289
(const void *)typeid(T).name(), dimensions,
@@ -281,7 +301,7 @@ class buffer {
281301
impl = std::make_shared<detail::buffer_impl>(
282302
container.data(), size() * sizeof(T),
283303
detail::getNextPowerOfTwo(sizeof(T)), propList,
284-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
304+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>(
285305
allocator));
286306
size_t r[3] = {Range[0], 0, 0};
287307
impl->constructorNotification(CodeLoc, (void *)impl.get(), container.data(),
@@ -328,7 +348,7 @@ class buffer {
328348

329349
impl = std::make_shared<detail::buffer_impl>(
330350
detail::pi::cast<pi_native_handle>(MemObject), SyclContext,
331-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(),
351+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>(),
332352
/* OwnNativeHandle */ true, AvailableEvent);
333353
Range[0] = impl->getSize() / sizeof(T);
334354
impl->constructorNotification(CodeLoc, (void *)impl.get(), &MemObject,
@@ -556,7 +576,7 @@ class buffer {
556576

557577
impl = std::make_shared<detail::buffer_impl>(
558578
MemObject, SyclContext,
559-
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(),
579+
make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT, T>>(),
560580
OwnNativeHandle, AvailableEvent);
561581
Range[0] = impl->getSize() / sizeof(T);
562582
impl->constructorNotification(CodeLoc, (void *)impl.get(), &MemObject,

sycl/include/CL/sycl/detail/buffer_impl.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ class buffer;
3636
template <typename DataT, int Dimensions, access::mode AccessMode>
3737
class host_accessor;
3838

39-
using buffer_allocator = detail::sycl_memory_object_allocator;
40-
4139
namespace detail {
4240

4341
class __SYCL_EXPORT buffer_impl final : public SYCLMemObjT {

sycl/include/CL/sycl/detail/sycl_mem_obj_allocator.hpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ namespace detail {
1515
template <typename T>
1616
class aligned_allocator;
1717

18+
template <typename DataT>
19+
using sycl_memory_object_allocator = aligned_allocator<DataT>;
20+
1821
class SYCLMemObjAllocator {
1922

2023
protected:
@@ -31,18 +34,8 @@ class SYCLMemObjAllocator {
3134
}
3235
};
3336

34-
template <typename AllocatorT>
37+
template <typename AllocatorT, typename OwnerDataT>
3538
class SYCLMemObjAllocatorHolder : public SYCLMemObjAllocator {
36-
using sycl_memory_object_allocator = detail::aligned_allocator<char>;
37-
38-
template <typename T>
39-
using EnableIfDefaultAllocator =
40-
enable_if_t<std::is_same<T, sycl_memory_object_allocator>::value>;
41-
42-
template <typename T>
43-
using EnableIfNonDefaultAllocator =
44-
enable_if_t<!std::is_same<T, sycl_memory_object_allocator>::value>;
45-
4639
public:
4740
SYCLMemObjAllocatorHolder(AllocatorT Allocator)
4841
: MAllocator(Allocator),
@@ -73,6 +66,14 @@ class SYCLMemObjAllocatorHolder : public SYCLMemObjAllocator {
7366
virtual void *getAllocatorImpl() override { return &MAllocator; }
7467

7568
private:
69+
template <typename T>
70+
using EnableIfDefaultAllocator = enable_if_t<
71+
std::is_same<T, sycl_memory_object_allocator<OwnerDataT>>::value>;
72+
73+
template <typename T>
74+
using EnableIfNonDefaultAllocator = enable_if_t<
75+
!std::is_same<T, sycl_memory_object_allocator<OwnerDataT>>::value>;
76+
7677
template <typename T = AllocatorT>
7778
EnableIfNonDefaultAllocator<T> setAlignImpl(std::size_t) {
7879
// Do nothing in case of user's allocator.

sycl/include/CL/sycl/detail/sycl_mem_obj_t.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ class plugin;
3535
using ContextImplPtr = std::shared_ptr<context_impl>;
3636
using EventImplPtr = std::shared_ptr<event_impl>;
3737

38-
template <typename T>
39-
class aligned_allocator;
40-
using sycl_memory_object_allocator = aligned_allocator<char>;
41-
4238
// The class serves as a base for all SYCL memory objects.
4339
class __SYCL_EXPORT SYCLMemObjT : public SYCLMemObjI {
4440

@@ -54,14 +50,6 @@ class __SYCL_EXPORT SYCLMemObjT : public SYCLMemObjI {
5450
using EnableIfOutputIteratorT = enable_if_t<
5551
/*is_output_iterator<T>::value &&*/ !std::is_pointer<T>::value>;
5652

57-
template <typename T>
58-
using EnableIfDefaultAllocator =
59-
enable_if_t<std::is_same<T, sycl_memory_object_allocator>::value>;
60-
61-
template <typename T>
62-
using EnableIfNonDefaultAllocator =
63-
enable_if_t<!std::is_same<T, sycl_memory_object_allocator>::value>;
64-
6553
public:
6654
SYCLMemObjT(const size_t SizeInBytes, const property_list &Props,
6755
std::unique_ptr<SYCLMemObjAllocator> Allocator)

0 commit comments

Comments
 (0)