Skip to content

[SYCL] Support sycl_khr_default_context #15645

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

Merged
merged 9 commits into from
Feb 24, 2025
Merged
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
5 changes: 5 additions & 0 deletions sycl/include/sycl/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,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 @@ -85,7 +85,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 @@ -117,6 +117,7 @@ inline namespace _V1 {
#define SYCL_KHR_FREE_FUNCTION_COMMANDS 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
12 changes: 8 additions & 4 deletions sycl/source/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ platform::get_backend_info() const {

#undef __SYCL_PARAM_TRAITS_SPEC

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

context platform::khr_get_default_context() const {
// Keeping the default context for platforms in the global cache to avoid
// shared_ptr based circular dependency between platform and context classes
std::unordered_map<detail::PlatformImplPtr, detail::ContextImplPtr>
Expand All @@ -109,6 +106,13 @@ context platform::ext_oneapi_get_default_context() const {
return detail::createSyclObjFromImpl<context>(It->second);
}

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

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
26 changes: 26 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,26 @@
// 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 <algorithm>
#include <sycl/detail/core.hpp>

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 @@ -4025,6 +4025,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
1 change: 1 addition & 0 deletions sycl/test/abi/sycl_symbols_windows.dump
Original file line number Diff line number Diff line change
Expand Up @@ -3872,6 +3872,7 @@
?ext_oneapi_get_backend_content_view_impl@device_image_plain@detail@_V1@sycl@@IEBA?AU?$pair@PEBW4byte@std@@PEBW412@@std@@XZ
?ext_oneapi_get_composite_devices@platform@_V1@sycl@@QEBA?AV?$vector@Vdevice@_V1@sycl@@V?$allocator@Vdevice@_V1@sycl@@@std@@@std@@XZ
?ext_oneapi_get_default_context@platform@_V1@sycl@@QEBA?AVcontext@23@XZ
?khr_get_default_context@platform@_V1@sycl@@QEBA?AVcontext@23@XZ
?ext_oneapi_get_graph@queue@_V1@sycl@@QEBA?AV?$command_graph@$0A@@experimental@oneapi@ext@23@XZ
?ext_oneapi_get_kernel@kernel_bundle_plain@detail@_V1@sycl@@AEAA?AVkernel@34@Vstring_view@234@@Z
?ext_oneapi_get_kernel@kernel_bundle_plain@detail@_V1@sycl@@QEAA?AVkernel@34@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z
Expand Down
3 changes: 3 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 Down