Skip to content

[SYCL] Implement sycl_ext_oneapi_if_device #10169

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
wants to merge 2 commits into from
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
50 changes: 50 additions & 0 deletions sycl/include/sycl/ext/oneapi/experimental/if_device.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//==------ if_device.hpp --- SYCL ext header file -----------=--*- C++ -*---==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#pragma once

#include <sycl/detail/common.hpp>

namespace sycl {
__SYCL_INLINE_VER_NAMESPACE(_V1) {
namespace ext::oneapi::experimental {
namespace detail {
// Helper object used to implement "otherwise". The "MakeCall" template
// parameter tells whether the previous call to "if_device" or "if_host" called
// its "fn". When "MakeCall" is true, the previous call to "fn" did not
// happen, so the "otherwise" should call "fn".
template <bool MakeCall> class if_device_or_host_helper {
public:
template <typename T> void otherwise(T fn) {
if constexpr (MakeCall) {
fn();
}
}
};
} // namespace detail

template <typename T> static auto if_device(T fn) {
#ifdef __SYCL_DEVICE_ONLY__
fn();
return detail::if_device_or_host_helper<false>{};
#else
return detail::if_device_or_host_helper<true>{};
#endif
}

template <typename T> static auto if_host(T fn) {
#ifdef __SYCL_DEVICE_ONLY__
return detail::if_device_or_host_helper<true>{};
#else
fn();
return detail::if_device_or_host_helper<false>{};
#endif
}
} // namespace ext::oneapi::experimental
} // __SYCL_INLINE_VER_NAMESPACE(_V1)
} // namespace sycl
1 change: 1 addition & 0 deletions sycl/include/sycl/sycl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include <sycl/ext/oneapi/experimental/builtins.hpp>
#include <sycl/ext/oneapi/experimental/cuda/barrier.hpp>
#include <sycl/ext/oneapi/experimental/fixed_size_group.hpp>
#include <sycl/ext/oneapi/experimental/if_device.hpp>
#include <sycl/ext/oneapi/experimental/opportunistic_group.hpp>
#include <sycl/ext/oneapi/experimental/tangle_group.hpp>
#include <sycl/ext/oneapi/filter_selector.hpp>
Expand Down
45 changes: 45 additions & 0 deletions sycl/test-e2e/Basic/if_device.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// RUN: %{build} -o %t.out
// RUN: %{run} %t.out

#include <sycl/sycl.hpp>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, include minimal required set of headers: queue, buffer, accessor and <sycl/ext/oneapi/experimental/if_device.hpp>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly disagree. The right process to do that is to update the spec/create extension, allocate resources to the activity, cleanup the headers and create automation/checks to automatically cleanup includes and only after that can we start requesting such changes in arbitrary PRs. Until then we have to treat end-to-end tests same way as examples and write them how customers would write their code according to the specification.


namespace syclex = sycl::ext::oneapi::experimental;

int main() {
sycl::queue q;
sycl::buffer<int, 1> b(1);

{
sycl::host_accessor a{b};
a[0] = 1;
syclex::if_device([&]() { a[0] = 2; });
assert(a[0] == 1);
syclex::if_device([&]() { a[0] = 2; }).otherwise([&]() { a[0] = 3; });
assert(a[0] == 3);
syclex::if_host([&]() { a[0] = 2; });
assert(a[0] == 2);
syclex::if_host([&]() { a[0] = 1; }).otherwise([&]() { a[0] = 3; });
assert(a[0] == 1);
}
auto Do = [&](auto Fn) {
q.submit([&](sycl::handler &cgh) {
sycl::accessor a{b, cgh};
cgh.single_task([=]() { Fn(a); });
});
};

Do([&](auto a) { syclex::if_device([&]() { a[0] = 2; }); });
assert(sycl::host_accessor{b}[0] == 2);
Do([&](auto a) {
syclex::if_device([&]() { a[0] = 3; }).otherwise([&]() { a[0] = 1; });
});
assert(sycl::host_accessor{b}[0] == 3);
Do([&](auto a) { syclex::if_host([&]() { a[0] = 2; }); });
assert(sycl::host_accessor{b}[0] == 3);
Do([&](auto a) {
syclex::if_host([&]() { a[0] = 2; }).otherwise([&]() { a[0] = 1; });
});
assert(sycl::host_accessor{b}[0] == 1);

return 0;
}