diff --git a/sycl/include/sycl/platform.hpp b/sycl/include/sycl/platform.hpp index af259afe8050e..21605d6c56370 100644 --- a/sycl/include/sycl/platform.hpp +++ b/sycl/include/sycl/platform.hpp @@ -208,6 +208,11 @@ class __SYCL_EXPORT platform : public detail::OwnerLessBase { std::vector 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; diff --git a/sycl/source/detail/queue_impl.hpp b/sycl/source/detail/queue_impl.hpp index bf18e97c50fca..710e48be35909 100644 --- a/sycl/source/detail/queue_impl.hpp +++ b/sycl/source/detail/queue_impl.hpp @@ -85,7 +85,7 @@ class queue_impl { context{createSyclObjFromImpl(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( diff --git a/sycl/source/feature_test.hpp.in b/sycl/source/feature_test.hpp.in index 17496691a4a97..6845b3eed52dc 100644 --- a/sycl/source/feature_test.hpp.in +++ b/sycl/source/feature_test.hpp.in @@ -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 diff --git a/sycl/source/platform.cpp b/sycl/source/platform.cpp index 841aeb76f8d4b..71c89460c9322 100644 --- a/sycl/source/platform.cpp +++ b/sycl/source/platform.cpp @@ -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::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 @@ -109,6 +106,13 @@ context platform::ext_oneapi_get_default_context() const { return detail::createSyclObjFromImpl(It->second); } +context platform::ext_oneapi_get_default_context() const { + if (!detail::SYCLConfig::get()) + throw std::runtime_error("SYCL default contexts are not enabled"); + + return khr_get_default_context(); +} + std::vector platform::ext_oneapi_get_composite_devices() const { // Only GPU architectures can be composite devices. auto GPUDevices = get_devices(info::device_type::gpu); diff --git a/sycl/test-e2e/Basic/khr_get_default_context.cpp b/sycl/test-e2e/Basic/khr_get_default_context.cpp new file mode 100644 index 0000000000000..c69d6587aeb49 --- /dev/null +++ b/sycl/test-e2e/Basic/khr_get_default_context.cpp @@ -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 +#include + +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; +} diff --git a/sycl/test/abi/sycl_symbols_linux.dump b/sycl/test/abi/sycl_symbols_linux.dump index 144e77736de98..d762b1243217d 100644 --- a/sycl/test/abi/sycl_symbols_linux.dump +++ b/sycl/test/abi/sycl_symbols_linux.dump @@ -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 diff --git a/sycl/test/abi/sycl_symbols_windows.dump b/sycl/test/abi/sycl_symbols_windows.dump index 0762eb9b27d3a..9704acd6c643f 100644 --- a/sycl/test/abi/sycl_symbols_windows.dump +++ b/sycl/test/abi/sycl_symbols_windows.dump @@ -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 diff --git a/sycl/unittests/Extensions/DefaultContext.cpp b/sycl/unittests/Extensions/DefaultContext.cpp index e0b85ce1a4e85..070aedc985bd0 100644 --- a/sycl/unittests/Extensions/DefaultContext.cpp +++ b/sycl/unittests/Extensions/DefaultContext.cpp @@ -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() {