Skip to content

Commit

Permalink
[SYCL] Support sycl_khr_default_context
Browse files Browse the repository at this point in the history
Spec: KhronosGroup/SYCL-Docs#624
sycl-cts: KhronosGroup/SYCL-CTS#960

This patch is intended to be merged only when there are sycl-cts tests
and the ratified khr extension.
  • Loading branch information
KornevNikita committed Oct 9, 2024
1 parent d3c5733 commit a093f05
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions sycl/include/sycl/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ class __SYCL_EXPORT platform : public detail::OwnerLessBase<platform> {

std::vector<device> ext_oneapi_get_composite_devices() const;

/// Returns a copy of the default context object for this platform.
///
/// \return the default context
context khr_get_default_context() const;

private:
ur_native_handle_t getNative() const;

Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/queue_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class queue_impl {
context{createSyclObjFromImpl<device>(Device), {}, {}});

ContextImplPtr DefaultContext = detail::getSyclObjImpl(
Device->get_platform().ext_oneapi_get_default_context());
Device->get_platform().khr_get_default_context());
if (DefaultContext->isDeviceValid(Device))
return DefaultContext;
return detail::getSyclObjImpl(
Expand Down
1 change: 1 addition & 0 deletions sycl/source/feature_test.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ inline namespace _V1 {
#define SYCL_EXT_ONEAPI_ENQUEUE_NATIVE_COMMAND 1
// In progress yet
#define SYCL_EXT_ONEAPI_ATOMIC16 0
#define SYCL_KHR_DEFAULT_CONTEXT 1

#ifndef __has_include
#define __has_include(x) 0
Expand Down
6 changes: 5 additions & 1 deletion sycl/source/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ platform::get_backend_info() const {

#undef __SYCL_PARAM_TRAITS_SPEC

context platform::ext_oneapi_get_default_context() const {
context platform::khr_get_default_context() const {
if (!detail::SYCLConfig<detail::SYCL_ENABLE_DEFAULT_CONTEXTS>::get())
throw std::runtime_error("SYCL default contexts are not enabled");

Expand All @@ -108,6 +108,10 @@ context platform::ext_oneapi_get_default_context() const {
return detail::createSyclObjFromImpl<context>(It->second);
}

context platform::ext_oneapi_get_default_context() const {
return khr_get_default_context();
}

std::vector<device> platform::ext_oneapi_get_composite_devices() const {
// Only GPU architectures can be composite devices.
auto GPUDevices = get_devices(info::device_type::gpu);
Expand Down
25 changes: 25 additions & 0 deletions sycl/test-e2e/Basic/khr_get_default_context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %{build} -o %t.out
// RUN: %{run} %t.out

// Test checks that the default context contains all of the root devices that
// are associated with this platform.

#include <sycl/detail/core.hpp>
#include <algorithm>

using namespace sycl;

int main() {
auto platforms = platform::get_platforms();

for (const auto &plt : platforms) {
auto def_ctx_devs = plt.khr_get_default_context().get_devices();
auto root_devs = plt.get_devices();

for (const auto &dev : root_devs)
if (std::find(def_ctx_devs.begin(), def_ctx_devs.end(), dev) == def_ctx_devs.end())
return 1;
}

return 0;
}
1 change: 1 addition & 0 deletions sycl/test/abi/sycl_symbols_linux.dump
Original file line number Diff line number Diff line change
Expand Up @@ -3986,6 +3986,7 @@ _ZNK4sycl3_V18platform13has_extensionENS0_6detail11string_viewE
_ZNK4sycl3_V18platform16get_backend_infoINS0_4info6device15backend_versionEEENS0_6detail20is_backend_info_descIT_E11return_typeEv
_ZNK4sycl3_V18platform16get_backend_infoINS0_4info6device7versionEEENS0_6detail20is_backend_info_descIT_E11return_typeEv
_ZNK4sycl3_V18platform16get_backend_infoINS0_4info8platform7versionEEENS0_6detail20is_backend_info_descIT_E11return_typeEv
_ZNK4sycl3_V18platform23khr_get_default_contextEv
_ZNK4sycl3_V18platform30ext_oneapi_get_default_contextEv
_ZNK4sycl3_V18platform32ext_oneapi_get_composite_devicesEv
_ZNK4sycl3_V18platform3getEv
Expand Down
13 changes: 13 additions & 0 deletions sycl/unittests/Extensions/DefaultContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ void test_default_context_enabled() {

ASSERT_EQ(Dev1.get_platform().ext_oneapi_get_default_context(),
Dev2.get_platform().ext_oneapi_get_default_context());

ASSERT_EQ(Dev1.get_platform().khr_get_default_context(),
Dev2.get_platform().khr_get_default_context());
}

void test_default_context_disabled() {
Expand All @@ -51,6 +54,16 @@ void test_default_context_disabled() {

ASSERT_TRUE(catchException)
<< "ext_oneapi_get_default_context did not throw an exception";

catchException = false;
try {
(void)Plt.khr_get_default_context();
} catch (const std::exception &) {
catchException = true;
}

ASSERT_TRUE(catchException)
<< "khr_get_default_context did not throw an exception";
}

TEST(DefaultContextTest, DefaultContextTest) {
Expand Down

0 comments on commit a093f05

Please sign in to comment.