Skip to content

[SYCL] Make sycl::usm_allocator::construct conformant to C++11 #2179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions sycl/include/CL/sycl/usm/usm_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ class usm_allocator {
/// object.
/// \param Val is a value to initialize the newly constructed object.
template <
usm::alloc AllocT = AllocKind,
typename... ArgsT, usm::alloc AllocT = AllocKind,
typename std::enable_if<AllocT != usm::alloc::device, int>::type = 0>
void construct(pointer Ptr, const_reference Val) {
new (Ptr) value_type(Val);
void construct(pointer Ptr, ArgsT &&... Args) {
new (Ptr) value_type(std::forward<ArgsT>(Args)...);
}

template <
usm::alloc AllocT = AllocKind,
typename... ArgsT, usm::alloc AllocT = AllocKind,
typename std::enable_if<AllocT == usm::alloc::device, int>::type = 0>
void construct(pointer, const_reference) {
void construct(pointer, ArgsT &&...) {
throw feature_not_supported(
"Device pointers do not support construct on host",
PI_INVALID_OPERATION);
Expand All @@ -87,7 +87,9 @@ class usm_allocator {
usm::alloc AllocT = AllocKind,
typename std::enable_if<AllocT == usm::alloc::device, int>::type = 0>
void destroy(pointer) {
// This method must be a NOP for device pointers.
throw feature_not_supported(
"Device pointers do not support construct on host",
PI_INVALID_OPERATION);
}

/// Note:: AllocKind == alloc::device is not allowed.
Expand Down
14 changes: 7 additions & 7 deletions sycl/test/usm/allocator_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <CL/sycl.hpp>

#include <memory>
#include <vector>

using namespace cl::sycl;
Expand Down Expand Up @@ -91,15 +92,14 @@ int main() {
if (dev.get_info<info::device::usm_device_allocations>()) {
usm_allocator<int, usm::alloc::device> alloc(ctxt, dev);

std::vector<int, decltype(alloc)> vec(alloc);
vec.resize(N);
auto AllocDeleter = [&](int *ptr) { alloc.deallocate(ptr, N); };
std::unique_ptr<int, decltype(AllocDeleter)> mem(alloc.allocate(N),
AllocDeleter);

int *res = &vec[0];
int *vals = &vec[0];
int *vals = mem.get();

auto e0 = q.submit([=](handler &h) {
h.single_task<class baz_init>([=]() {
res[0] = 0;
for (int i = 0; i < N; i++) {
vals[i] = i;
}
Expand All @@ -110,7 +110,7 @@ int main() {
h.depends_on(e0);
h.single_task<class baz>([=]() {
for (int i = 1; i < N; i++) {
res[0] += vals[i];
vals[0] += vals[i];
}
});
});
Expand All @@ -119,7 +119,7 @@ int main() {

int answer = (N * (N - 1)) / 2;
int result;
q.memcpy(&result, res, sizeof(int));
q.memcpy(&result, vals, sizeof(int));
q.wait();

if (result != answer)
Expand Down