From 7d53b8292b3038645faa85de34f2e38a43787c21 Mon Sep 17 00:00:00 2001 From: James Brodman Date: Thu, 19 Nov 2020 16:30:16 -0500 Subject: [PATCH 01/41] Add the notion of a default context to a platform. Queue creation will use the platform's default context instead of creating a new context for the specified device. Signed-off-by: James Brodman --- sycl/include/CL/sycl/platform.hpp | 16 ++++++++++++++++ sycl/source/detail/platform_impl.cpp | 19 +++++++++++++++++++ sycl/source/detail/platform_impl.hpp | 7 +++++++ sycl/source/detail/queue_impl.hpp | 6 +----- sycl/source/platform.cpp | 10 ++++++++++ 5 files changed, 53 insertions(+), 5 deletions(-) diff --git a/sycl/include/CL/sycl/platform.hpp b/sycl/include/CL/sycl/platform.hpp index 6d2c36ef2cf27..af5f7147ce61f 100644 --- a/sycl/include/CL/sycl/platform.hpp +++ b/sycl/include/CL/sycl/platform.hpp @@ -126,6 +126,22 @@ class __SYCL_EXPORT platform { /// given feature. bool has(aspect Aspect) const; + /// Return this platform's default context + /// + /// \return the default context + context get_default_context() const; + + /// Pushes a new default context + /// + /// \param the new default context + void push_default_context(context Context); + + /// Pops the current default context, setting the default + /// context to the previous context. + /// Throws an error if popping would result in no + /// default context. + void pop_default_context(); + private: pi_native_handle getNative() const; diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index f86809d1e024e..1491b913fff4c 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -389,6 +390,24 @@ bool platform_impl::has(aspect Aspect) const { #include #undef __SYCL_PARAM_TRAITS_SPEC +context platform_impl::getDefaultContext() { + if (!MDefaultContextStack.empty()) + return MDefaultContextStack.back(); + else { + auto Devices = get_devices(); + context DefaultContext(Devices); + MDefaultContextStack.push_back(DefaultContext); + + return DefaultContext; + } +} + +void platform_impl::pushDefaultContext(context Context) { + MDefaultContextStack.push_back(Context); +} + +void platform_impl::popDefaultContext() { MDefaultContextStack.pop_back(); } + } // namespace detail } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/detail/platform_impl.hpp b/sycl/source/detail/platform_impl.hpp index a16f5de53456a..8cc2d97b4a998 100644 --- a/sycl/source/detail/platform_impl.hpp +++ b/sycl/source/detail/platform_impl.hpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #pragma once +#include #include #include #include @@ -191,12 +192,18 @@ class platform_impl { static std::shared_ptr getPlatformFromPiDevice(RT::PiDevice PiDevice, const plugin &Plugin); + context getDefaultContext(); + + void pushDefaultContext(context Context); + void popDefaultContext(); + private: bool MHostPlatform = false; RT::PiPlatform MPlatform = 0; std::shared_ptr MPlugin; std::vector> MDeviceCache; std::mutex MDeviceMapMutex; + std::vector MDefaultContextStack; }; } // namespace detail diff --git a/sycl/source/detail/queue_impl.hpp b/sycl/source/detail/queue_impl.hpp index 1441a9f3e864d..4289695b6d98e 100644 --- a/sycl/source/detail/queue_impl.hpp +++ b/sycl/source/detail/queue_impl.hpp @@ -59,11 +59,7 @@ class queue_impl { const property_list &PropList) : queue_impl(Device, detail::getSyclObjImpl( - context(createSyclObjFromImpl(Device), {}, - (DefaultContextType == CUDAContextT::primary) - ? property_list{property::context::cuda:: - use_primary_context()} - : property_list{})), + Device->get_platform().get_default_context()), AsyncHandler, PropList){}; /// Constructs a SYCL queue with an async_handler and property_list provided diff --git a/sycl/source/platform.cpp b/sycl/source/platform.cpp index 3ab9f5b020d6d..a13f8252a619f 100644 --- a/sycl/source/platform.cpp +++ b/sycl/source/platform.cpp @@ -63,5 +63,15 @@ bool platform::has(aspect Aspect) const { return impl->has(Aspect); } #undef __SYCL_PARAM_TRAITS_SPEC +context platform::get_default_context() const { + return impl->getDefaultContext(); +} + +void platform::push_default_context(context Context) { + impl->pushDefaultContext(Context); +} + +void platform::pop_default_context() { impl->popDefaultContext(); } + } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) From 664e6e1e0700133ae85e50b91aa61503abbf676e Mon Sep 17 00:00:00 2001 From: James Brodman Date: Thu, 19 Nov 2020 16:43:53 -0500 Subject: [PATCH 02/41] Update ABI symbols Signed-off-by: James Brodman --- sycl/test/abi/sycl_symbols_linux.dump | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sycl/test/abi/sycl_symbols_linux.dump b/sycl/test/abi/sycl_symbols_linux.dump index 0c9f6da10cdcc..0411fa1eac9d6 100644 --- a/sycl/test/abi/sycl_symbols_linux.dump +++ b/sycl/test/abi/sycl_symbols_linux.dump @@ -3862,6 +3862,8 @@ _ZN2cl4sycl7samplerC1EP11_cl_samplerRKNS0_7contextE _ZN2cl4sycl7samplerC2ENS0_29coordinate_normalization_modeENS0_15addressing_modeENS0_14filtering_modeERKNS0_13property_listE _ZN2cl4sycl7samplerC2EP11_cl_samplerRKNS0_7contextE _ZN2cl4sycl8platform13get_platformsEv +_ZN2cl4sycl8platform19pop_default_contextEv +_ZN2cl4sycl8platform20push_default_contextENS0_7contextE _ZN2cl4sycl8platformC1EP15_cl_platform_id _ZN2cl4sycl8platformC1ERKNS0_15device_selectorE _ZN2cl4sycl8platformC1Ev @@ -4158,6 +4160,7 @@ _ZNK2cl4sycl7samplerneERKS1_ _ZNK2cl4sycl8platform11get_backendEv _ZNK2cl4sycl8platform11get_devicesENS0_4info11device_typeE _ZNK2cl4sycl8platform13has_extensionERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE +_ZNK2cl4sycl8platform19get_default_contextEv _ZNK2cl4sycl8platform3getEv _ZNK2cl4sycl8platform3hasENS0_6aspectE _ZNK2cl4sycl8platform7is_hostEv From 42927063abfb619eef3d1a41a3f48c31915e21ad Mon Sep 17 00:00:00 2001 From: James Brodman Date: Thu, 19 Nov 2020 16:50:30 -0500 Subject: [PATCH 03/41] Update tests to the new behavior Signed-off-by: James Brodman --- sycl/test/on-device/basic_tests/buffer/buffer_dev_to_dev.cpp | 1 - sycl/test/on-device/basic_tests/kernel_interop.cpp | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sycl/test/on-device/basic_tests/buffer/buffer_dev_to_dev.cpp b/sycl/test/on-device/basic_tests/buffer/buffer_dev_to_dev.cpp index c84a06bbbace4..f41377ae9dfa2 100644 --- a/sycl/test/on-device/basic_tests/buffer/buffer_dev_to_dev.cpp +++ b/sycl/test/on-device/basic_tests/buffer/buffer_dev_to_dev.cpp @@ -29,7 +29,6 @@ int main() { queue FirstQueue(Selector); queue SecondQueue(Selector); - assert(FirstQueue.get_context() != SecondQueue.get_context()); FirstQueue.submit([&](handler &Cgh) { auto Accessor = Buffer.get_access(Cgh); Cgh.parallel_for( diff --git a/sycl/test/on-device/basic_tests/kernel_interop.cpp b/sycl/test/on-device/basic_tests/kernel_interop.cpp index a20b882e5c382..114c89a40aaa1 100644 --- a/sycl/test/on-device/basic_tests/kernel_interop.cpp +++ b/sycl/test/on-device/basic_tests/kernel_interop.cpp @@ -52,7 +52,8 @@ int main() { // Try to create kernel with another context bool Pass = false; - queue Queue1; + device Device = Queue.get_device(); + queue Queue1(context(Device), Device); context Context1 = Queue1.get_context(); try { kernel Kernel(ClKernel, Context1); From 8ef4616cb2b1d0a59a0ccd8d1b60fef0fc8cb4bf Mon Sep 17 00:00:00 2001 From: James Brodman Date: Thu, 19 Nov 2020 17:09:01 -0500 Subject: [PATCH 04/41] Update unit test for the new behavior Signed-off-by: James Brodman --- sycl/unittests/scheduler/CommandsWaitForEvents.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sycl/unittests/scheduler/CommandsWaitForEvents.cpp b/sycl/unittests/scheduler/CommandsWaitForEvents.cpp index 51c9c0abc0611..d3e0a102eec91 100644 --- a/sycl/unittests/scheduler/CommandsWaitForEvents.cpp +++ b/sycl/unittests/scheduler/CommandsWaitForEvents.cpp @@ -72,7 +72,8 @@ TEST_F(SchedulerTest, CommandsWaitForEvents) { } queue Q1; - queue Q2; + auto Device = Q1.get_device(); + queue Q2(context(Device), Device); unittest::PiMock Mock1(Q1); unittest::PiMock Mock2(Q2); From 8d5bc047a5cccdc99c93859519f64fae30d205f7 Mon Sep 17 00:00:00 2001 From: James Brodman Date: Fri, 20 Nov 2020 11:21:51 -0500 Subject: [PATCH 05/41] Make contexts in interop test explicit Signed-off-by: James Brodman --- sycl/test/on-device/host-interop-task/interop-task.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sycl/test/on-device/host-interop-task/interop-task.cpp b/sycl/test/on-device/host-interop-task/interop-task.cpp index 191f473e8bb77..3152803d98afb 100644 --- a/sycl/test/on-device/host-interop-task/interop-task.cpp +++ b/sycl/test/on-device/host-interop-task/interop-task.cpp @@ -118,15 +118,16 @@ void test2() { buffer Buffer1{BUFFER_SIZE}; buffer Buffer2{BUFFER_SIZE}; + auto Device = default_selector().select_device(); // init the buffer with a'priori invalid data { - queue Q; + queue Q(context(Device), Device); init(Buffer1, Buffer2, Q); } // Repeat a couple of times for (size_t Idx = 0; Idx < COUNT; ++Idx) { - queue Q; + queue Q(context(Device), Device); copy(Buffer1, Buffer2, Q); modify(Buffer2, Q); copy(Buffer2, Buffer1, Q); From 0bb715e073d589d4c05ae6c09047a5799fb289ee Mon Sep 17 00:00:00 2001 From: James Brodman Date: Fri, 20 Nov 2020 13:15:40 -0500 Subject: [PATCH 06/41] Change tests to use explicit contexts to keep exercising the original code path Signed-off-by: James Brodman --- .../plugins/enqueue-arg-order-buffer.cpp | 15 +++++++++------ .../on-device/plugins/enqueue-arg-order-image.cpp | 15 +++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/sycl/test/on-device/plugins/enqueue-arg-order-buffer.cpp b/sycl/test/on-device/plugins/enqueue-arg-order-buffer.cpp index 3378e5d22a9e7..95cc0613b54c1 100644 --- a/sycl/test/on-device/plugins/enqueue-arg-order-buffer.cpp +++ b/sycl/test/on-device/plugins/enqueue-arg-order-buffer.cpp @@ -235,8 +235,9 @@ void testcopyH2DBuffer() { { buffer buffer_from_1D(data_from_1D.data(), range<1>(width)); buffer buffer_to_1D(data_to_1D.data(), range<1>(width)); - queue myQueue; - queue otherQueue; + auto Device = default_selector().select_device(); + queue myQueue(context(Device), Device); + queue otherQueue(context(Device), Device); myQueue.submit([&](handler &cgh) { auto read = buffer_from_1D.get_access(cgh); auto write = buffer_to_1D.get_access(cgh); @@ -259,8 +260,9 @@ void testcopyH2DBuffer() { buffer buffer_from_2D(data_from_2D.data(), range<2>(height, width)); buffer buffer_to_2D(data_to_2D.data(), range<2>(height, width)); - queue myQueue; - queue otherQueue; + auto Device = default_selector().select_device(); + queue myQueue(context(Device), Device); + queue otherQueue(context(Device), Device); myQueue.submit([&](handler &cgh) { auto read = buffer_from_2D.get_access(cgh); auto write = buffer_to_2D.get_access(cgh); @@ -283,8 +285,9 @@ void testcopyH2DBuffer() { range<3>(depth, height, width)); buffer buffer_to_3D(data_to_3D.data(), range<3>(depth, height, width)); - queue myQueue; - queue otherQueue; + auto Device = default_selector().select_device(); + queue myQueue(context(Device), Device); + queue otherQueue(context(Device), Device); myQueue.submit([&](handler &cgh) { auto read = buffer_from_3D.get_access(cgh); auto write = buffer_to_3D.get_access(cgh); diff --git a/sycl/test/on-device/plugins/enqueue-arg-order-image.cpp b/sycl/test/on-device/plugins/enqueue-arg-order-image.cpp index 194a9efe63e81..d0c258cf9fbae 100644 --- a/sycl/test/on-device/plugins/enqueue-arg-order-image.cpp +++ b/sycl/test/on-device/plugins/enqueue-arg-order-image.cpp @@ -163,8 +163,9 @@ void testcopyH2DImage() { ImgSize_1D); sycl::image<1> image_to_1D(data_to_1D.data(), ChanOrder, ChanType, ImgSize_1D); - queue Q; - queue otherQueue; + auto Device = default_selector().select_device(); + queue Q(context(Device), Device); + queue otherQueue(context(Device), Device); // first op Q.submit([&](sycl::handler &CGH) { auto readAcc = image_from_1D.get_access(CGH); @@ -198,8 +199,9 @@ void testcopyH2DImage() { ImgSize_2D); sycl::image<2> image_to_2D(data_to_2D.data(), ChanOrder, ChanType, ImgSize_2D); - queue Q; - queue otherQueue; + auto Device = default_selector().select_device(); + queue Q(context(Device), Device); + queue otherQueue(context(Device), Device); Q.submit([&](sycl::handler &CGH) { auto readAcc = image_from_2D.get_access(CGH); auto writeAcc = image_to_2D.get_access(CGH); @@ -232,8 +234,9 @@ void testcopyH2DImage() { ImgSize_3D); sycl::image<3> image_to_3D(data_to_3D.data(), ChanOrder, ChanType, ImgSize_3D); - queue Q; - queue otherQueue; + auto Device = default_selector().select_device(); + queue Q(context(Device), Device); + queue otherQueue(context(Device), Device); Q.submit([&](sycl::handler &CGH) { auto readAcc = image_from_3D.get_access(CGH); auto writeAcc = image_to_3D.get_access(CGH); From f13c91020443f83c8b642ebd504cd34f62ebfa1c Mon Sep 17 00:00:00 2001 From: James Brodman Date: Fri, 20 Nov 2020 13:41:24 -0500 Subject: [PATCH 07/41] Add on-device tests and error when popping default context stack would result in no default context Signed-off-by: James Brodman --- sycl/source/detail/platform_impl.cpp | 9 ++++- .../test/on-device/defaultcontext/default.cpp | 30 +++++++++++++++ sycl/test/on-device/defaultcontext/error.cpp | 29 ++++++++++++++ .../test/on-device/defaultcontext/pushpop.cpp | 38 +++++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 sycl/test/on-device/defaultcontext/default.cpp create mode 100644 sycl/test/on-device/defaultcontext/error.cpp create mode 100644 sycl/test/on-device/defaultcontext/pushpop.cpp diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 1491b913fff4c..951e4251ca79e 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -406,7 +406,14 @@ void platform_impl::pushDefaultContext(context Context) { MDefaultContextStack.push_back(Context); } -void platform_impl::popDefaultContext() { MDefaultContextStack.pop_back(); } +void platform_impl::popDefaultContext() { + if (MDefaultContextStack.size() == 1) { + throw runtime_error("Popping the platform default context would result in " + "no default context", + PI_INVALID_OPERATION); + } + MDefaultContextStack.pop_back(); +} } // namespace detail } // namespace sycl diff --git a/sycl/test/on-device/defaultcontext/default.cpp b/sycl/test/on-device/defaultcontext/default.cpp new file mode 100644 index 0000000000000..85f37d06a30a3 --- /dev/null +++ b/sycl/test/on-device/defaultcontext/default.cpp @@ -0,0 +1,30 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RUN: env SYCL_DEVICE_TYPE=HOST %t.out +// RUN: %CPU_RUN_PLACEHOLDER %t.out +// RUN: %GPU_RUN_PLACEHOLDER %t.out + +//==----------- default.cpp - SYCL default context test --------------------==// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include + +int main() { + sycl::queue q; + sycl::queue q2; + + auto c1 = q.get_context(); + auto c2 = q2.get_context(); + + auto d1 = q.get_device(); + auto d2 = q2.get_device(); + + assert(d1 == d2); + assert(c1 == c2); + + return 0; +} diff --git a/sycl/test/on-device/defaultcontext/error.cpp b/sycl/test/on-device/defaultcontext/error.cpp new file mode 100644 index 0000000000000..34d9f337e2790 --- /dev/null +++ b/sycl/test/on-device/defaultcontext/error.cpp @@ -0,0 +1,29 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RUN: env SYCL_DEVICE_TYPE=HOST %t.out +// RUN: %CPU_RUN_PLACEHOLDER %t.out +// RUN: %GPU_RUN_PLACEHOLDER %t.out + +//==------------- error.cpp - SYCL default context test --------------------==// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include + +int main() { + sycl::queue q; + + auto Device = q.get_device(); + auto Platform = Device.get_platform(); + + try { + Platform.pop_default_context(); + } catch (sycl::runtime_error) { + return 0; + } + + return -1; +} diff --git a/sycl/test/on-device/defaultcontext/pushpop.cpp b/sycl/test/on-device/defaultcontext/pushpop.cpp new file mode 100644 index 0000000000000..6cae4d8f5d5cd --- /dev/null +++ b/sycl/test/on-device/defaultcontext/pushpop.cpp @@ -0,0 +1,38 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RUN: env SYCL_DEVICE_TYPE=HOST %t.out +// RUN: %CPU_RUN_PLACEHOLDER %t.out +// RUN: %GPU_RUN_PLACEHOLDER %t.out + +//==----------- pushpop.cpp - SYCL default context test --------------------==// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include + +int main() { + sycl::queue q; + auto C1 = q.get_context(); + auto Device = q.get_device(); + auto Platform = Device.get_platform(); + + sycl::context NewContext(Device); + Platform.push_default_context(NewContext); + + sycl::queue q2; + auto C2 = q2.get_context(); + assert(C1 != C2); + assert(C2 == NewContext); + + Platform.pop_default_context(); + + sycl::queue q3; + auto C3 = q3.get_context(); + assert (C1 == C3); + assert (C2 != C3); + + return 0; +} From 634af84426e5ac6cdebb5d7ddec1400c20701dcc Mon Sep 17 00:00:00 2001 From: James Brodman Date: Fri, 22 Jan 2021 13:51:27 -0500 Subject: [PATCH 08/41] Formatting/style changes Signed-off-by: James Brodman --- sycl/source/detail/platform_impl.cpp | 12 ++++++------ sycl/source/detail/platform_impl.hpp | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 2eba4538ebd21..f220a1ed06641 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -391,28 +391,28 @@ bool platform_impl::has(aspect Aspect) const { #undef __SYCL_PARAM_TRAITS_SPEC context platform_impl::getDefaultContext() { - if (!MDefaultContextStack.empty()) - return MDefaultContextStack.back(); + if (!MDefaultContexts.empty()) + return MDefaultContexts.back(); else { auto Devices = get_devices(); context DefaultContext(Devices); - MDefaultContextStack.push_back(DefaultContext); + MDefaultContexts.push_back(DefaultContext); return DefaultContext; } } void platform_impl::pushDefaultContext(context Context) { - MDefaultContextStack.push_back(Context); + MDefaultContexts.push_back(Context); } void platform_impl::popDefaultContext() { - if (MDefaultContextStack.size() == 1) { + if (MDefaultContexts.size() == 1) { throw runtime_error("Popping the platform default context would result in " "no default context", PI_INVALID_OPERATION); } - MDefaultContextStack.pop_back(); + MDefaultContexts.pop_back(); } } // namespace detail diff --git a/sycl/source/detail/platform_impl.hpp b/sycl/source/detail/platform_impl.hpp index 8cc2d97b4a998..24e4583dc3ffc 100644 --- a/sycl/source/detail/platform_impl.hpp +++ b/sycl/source/detail/platform_impl.hpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #pragma once + #include #include #include @@ -203,7 +204,7 @@ class platform_impl { std::shared_ptr MPlugin; std::vector> MDeviceCache; std::mutex MDeviceMapMutex; - std::vector MDefaultContextStack; + std::vector MDefaultContexts; }; } // namespace detail From 5e55c1cef68f67cbb51f1e6052ddb3ff03a8d68d Mon Sep 17 00:00:00 2001 From: James Brodman Date: Fri, 22 Jan 2021 15:43:00 -0500 Subject: [PATCH 09/41] clang-format tests Signed-off-by: James Brodman --- sycl/test/on-device/defaultcontext/default.cpp | 2 +- sycl/test/on-device/defaultcontext/error.cpp | 6 +++--- sycl/test/on-device/defaultcontext/pushpop.cpp | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sycl/test/on-device/defaultcontext/default.cpp b/sycl/test/on-device/defaultcontext/default.cpp index 85f37d06a30a3..a1593932d2ae3 100644 --- a/sycl/test/on-device/defaultcontext/default.cpp +++ b/sycl/test/on-device/defaultcontext/default.cpp @@ -25,6 +25,6 @@ int main() { assert(d1 == d2); assert(c1 == c2); - + return 0; } diff --git a/sycl/test/on-device/defaultcontext/error.cpp b/sycl/test/on-device/defaultcontext/error.cpp index 34d9f337e2790..3601e70f38665 100644 --- a/sycl/test/on-device/defaultcontext/error.cpp +++ b/sycl/test/on-device/defaultcontext/error.cpp @@ -18,12 +18,12 @@ int main() { auto Device = q.get_device(); auto Platform = Device.get_platform(); - + try { - Platform.pop_default_context(); + Platform.pop_default_context(); } catch (sycl::runtime_error) { return 0; } - + return -1; } diff --git a/sycl/test/on-device/defaultcontext/pushpop.cpp b/sycl/test/on-device/defaultcontext/pushpop.cpp index 6cae4d8f5d5cd..1d45b42dab551 100644 --- a/sycl/test/on-device/defaultcontext/pushpop.cpp +++ b/sycl/test/on-device/defaultcontext/pushpop.cpp @@ -21,18 +21,18 @@ int main() { sycl::context NewContext(Device); Platform.push_default_context(NewContext); - + sycl::queue q2; auto C2 = q2.get_context(); assert(C1 != C2); assert(C2 == NewContext); Platform.pop_default_context(); - + sycl::queue q3; auto C3 = q3.get_context(); - assert (C1 == C3); - assert (C2 != C3); - + assert(C1 == C3); + assert(C2 != C3); + return 0; } From 76c94be2cff0a4e835fb61e14507d122740d463a Mon Sep 17 00:00:00 2001 From: James Brodman Date: Mon, 25 Jan 2021 13:48:21 -0500 Subject: [PATCH 10/41] Add extension doc for platform contexts Signed-off-by: James Brodman --- .../PlatformContext/PlatformContext.adoc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 sycl/doc/extensions/PlatformContext/PlatformContext.adoc diff --git a/sycl/doc/extensions/PlatformContext/PlatformContext.adoc b/sycl/doc/extensions/PlatformContext/PlatformContext.adoc new file mode 100644 index 0000000000000..6b8451a58fb3d --- /dev/null +++ b/sycl/doc/extensions/PlatformContext/PlatformContext.adoc @@ -0,0 +1,17 @@ += SYCL(TM) Proposals: Platform Default Contexts +James Brodman +v0.1 +:source-highlighter: pygments +:icons: font +:y: icon:check[role="green"] +:n: icon:times[role="red"] + +== Platform Default Contexts + +This extension adds the notion of a default SYCL context per SYCL platform. The default context for each platform contains all devices in the platform. + +The platform class gains three new methods: `get_default_context`, `push_default_context`, and `pop_default_context`. + +These methods may be used to query and modify a platform's default context. Pushing a new default context replaces the existing one. Popping a default context returns either the previous context or throws a `runtime_error` if popping would result in no context. + +This extension also modifies the behavior of `queue` constructors. Queues will no longer create a new `context` upon consruction. Instead, they will use the default context from the device's platform. From ea071c9b7bb6dcbe3ac9670a927e35a8074f7027 Mon Sep 17 00:00:00 2001 From: James Brodman Date: Wed, 27 Jan 2021 14:41:03 -0500 Subject: [PATCH 11/41] Add mutex and update ABI Signed-off-by: James Brodman --- sycl/include/CL/sycl/platform.hpp | 2 +- sycl/source/detail/platform_impl.cpp | 8 +++++++- sycl/source/detail/platform_impl.hpp | 3 ++- sycl/source/platform.cpp | 2 +- sycl/test/abi/sycl_symbols_linux.dump | 8 ++++---- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/sycl/include/CL/sycl/platform.hpp b/sycl/include/CL/sycl/platform.hpp index af5f7147ce61f..8e41328d2668c 100644 --- a/sycl/include/CL/sycl/platform.hpp +++ b/sycl/include/CL/sycl/platform.hpp @@ -134,7 +134,7 @@ class __SYCL_EXPORT platform { /// Pushes a new default context /// /// \param the new default context - void push_default_context(context Context); + void push_default_context(const context &Context); /// Pops the current default context, setting the default /// context to the previous context. diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index f220a1ed06641..0915c2c32e4a0 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -391,6 +391,8 @@ bool platform_impl::has(aspect Aspect) const { #undef __SYCL_PARAM_TRAITS_SPEC context platform_impl::getDefaultContext() { + const std::lock_guard Guard(MContextMutex); + if (!MDefaultContexts.empty()) return MDefaultContexts.back(); else { @@ -402,11 +404,15 @@ context platform_impl::getDefaultContext() { } } -void platform_impl::pushDefaultContext(context Context) { +void platform_impl::pushDefaultContext(const context &Context) { + const std::lock_guard Guard(MContextMutex); + MDefaultContexts.push_back(Context); } void platform_impl::popDefaultContext() { + const std::lock_guard Guard(MContextMutex); + if (MDefaultContexts.size() == 1) { throw runtime_error("Popping the platform default context would result in " "no default context", diff --git a/sycl/source/detail/platform_impl.hpp b/sycl/source/detail/platform_impl.hpp index 24e4583dc3ffc..280bd18a0a8cf 100644 --- a/sycl/source/detail/platform_impl.hpp +++ b/sycl/source/detail/platform_impl.hpp @@ -195,7 +195,7 @@ class platform_impl { context getDefaultContext(); - void pushDefaultContext(context Context); + void pushDefaultContext(const context &Context); void popDefaultContext(); private: @@ -205,6 +205,7 @@ class platform_impl { std::vector> MDeviceCache; std::mutex MDeviceMapMutex; std::vector MDefaultContexts; + std::mutex MContextMutex; }; } // namespace detail diff --git a/sycl/source/platform.cpp b/sycl/source/platform.cpp index a13f8252a619f..6ffbba5428563 100644 --- a/sycl/source/platform.cpp +++ b/sycl/source/platform.cpp @@ -67,7 +67,7 @@ context platform::get_default_context() const { return impl->getDefaultContext(); } -void platform::push_default_context(context Context) { +void platform::push_default_context(const context &Context) { impl->pushDefaultContext(Context); } diff --git a/sycl/test/abi/sycl_symbols_linux.dump b/sycl/test/abi/sycl_symbols_linux.dump index 8dfdeb36c46b5..156ffa34d7c8f 100644 --- a/sycl/test/abi/sycl_symbols_linux.dump +++ b/sycl/test/abi/sycl_symbols_linux.dump @@ -3615,6 +3615,8 @@ _ZN2cl4sycl20aligned_alloc_sharedEmmRKNS0_5queueE _ZN2cl4sycl20aligned_alloc_sharedEmmRKNS0_6deviceERKNS0_7contextE _ZN2cl4sycl4freeEPvRKNS0_5queueE _ZN2cl4sycl4freeEPvRKNS0_7contextE +_ZN2cl4sycl5INTEL15online_compilerILNS1_15source_languageE0EE7compileIJSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISC_EEEEES6_IhSaIhEERKSC_DpRKT_ +_ZN2cl4sycl5INTEL15online_compilerILNS1_15source_languageE1EE7compileIJSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISC_EEEEES6_IhSaIhEERKSC_DpRKT_ _ZN2cl4sycl5event13get_wait_listEv _ZN2cl4sycl5event14wait_and_throwERKSt6vectorIS1_SaIS1_EE _ZN2cl4sycl5event14wait_and_throwEv @@ -3626,8 +3628,6 @@ _ZN2cl4sycl5eventC1Ev _ZN2cl4sycl5eventC2EP9_cl_eventRKNS0_7contextE _ZN2cl4sycl5eventC2ESt10shared_ptrINS0_6detail10event_implEE _ZN2cl4sycl5eventC2Ev -_ZN2cl4sycl5INTEL15online_compilerILNS1_15source_languageE0EE7compileIJSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISC_EEEEES6_IhSaIhEERKSC_DpRKT_ -_ZN2cl4sycl5INTEL15online_compilerILNS1_15source_languageE1EE7compileIJSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISC_EEEEES6_IhSaIhEERKSC_DpRKT_ _ZN2cl4sycl5queue10mem_adviseEPKvm14_pi_mem_advice _ZN2cl4sycl5queue10wait_proxyERKNS0_6detail13code_locationE _ZN2cl4sycl5queue11submit_implESt8functionIFvRNS0_7handlerEEERKNS0_6detail13code_locationE @@ -3869,7 +3869,7 @@ _ZN2cl4sycl7samplerC2ENS0_29coordinate_normalization_modeENS0_15addressing_modeE _ZN2cl4sycl7samplerC2EP11_cl_samplerRKNS0_7contextE _ZN2cl4sycl8platform13get_platformsEv _ZN2cl4sycl8platform19pop_default_contextEv -_ZN2cl4sycl8platform20push_default_contextENS0_7contextE +_ZN2cl4sycl8platform20push_default_contextERKNS0_7contextE _ZN2cl4sycl8platformC1EP15_cl_platform_id _ZN2cl4sycl8platformC1ERKNS0_15device_selectorE _ZN2cl4sycl8platformC1Ev @@ -3965,6 +3965,7 @@ _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE16785EEENS3_12param_traitsIS4_XT_ _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE16786EEENS3_12param_traitsIS4_XT_EE11return_typeEv _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE16787EEENS3_12param_traitsIS4_XT_EE11return_typeEv _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE16788EEENS3_12param_traitsIS4_XT_EE11return_typeEv +_ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE16915EEENS3_12param_traitsIS4_XT_EE11return_typeEv _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE4096EEENS3_12param_traitsIS4_XT_EE11return_typeEv _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE4097EEENS3_12param_traitsIS4_XT_EE11return_typeEv _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE4098EEENS3_12param_traitsIS4_XT_EE11return_typeEv @@ -4040,7 +4041,6 @@ _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE4168EEENS3_12param_traitsIS4_XT_E _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE4169EEENS3_12param_traitsIS4_XT_EE11return_typeEv _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE4188EEENS3_12param_traitsIS4_XT_EE11return_typeEv _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE4189EEENS3_12param_traitsIS4_XT_EE11return_typeEv -_ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE16915EEENS3_12param_traitsIS4_XT_EE11return_typeEv _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE65568EEENS3_12param_traitsIS4_XT_EE11return_typeEv _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE65569EEENS3_12param_traitsIS4_XT_EE11return_typeEv _ZNK2cl4sycl6device8get_infoILNS0_4info6deviceE65570EEENS3_12param_traitsIS4_XT_EE11return_typeEv From 6db087f5b3a85fe17a937314d16619a3aa1dbd65 Mon Sep 17 00:00:00 2001 From: James Brodman Date: Wed, 27 Jan 2021 15:03:56 -0500 Subject: [PATCH 12/41] Update spec to have a table Signed-off-by: James Brodman --- .../PlatformContext/PlatformContext.adoc | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/sycl/doc/extensions/PlatformContext/PlatformContext.adoc b/sycl/doc/extensions/PlatformContext/PlatformContext.adoc index 6b8451a58fb3d..a99488631008b 100644 --- a/sycl/doc/extensions/PlatformContext/PlatformContext.adoc +++ b/sycl/doc/extensions/PlatformContext/PlatformContext.adoc @@ -10,8 +10,35 @@ v0.1 This extension adds the notion of a default SYCL context per SYCL platform. The default context for each platform contains all devices in the platform. -The platform class gains three new methods: `get_default_context`, `push_default_context`, and `pop_default_context`. +The platform class gains three new methods: -These methods may be used to query and modify a platform's default context. Pushing a new default context replaces the existing one. Popping a default context returns either the previous context or throws a `runtime_error` if popping would result in no context. +[cols="^60a,40"] +|=== +| Member Function | Description + +| +[source,c++] +---- +context get_default_context() +---- + +| Returns the current default context for this `platform` + +| +[source,c++] +---- +void push_default_context(const context &NewContext) +---- + +| Sets the current default context to `NewContext`. The previous default context is saved. + +| +[source,c++] +---- +void pop_default_context() +---- + +| Removes the current default context and restores the previous default context if applicable or throws a `runtime_error` if there is no valid context to restore. +|=== This extension also modifies the behavior of `queue` constructors. Queues will no longer create a new `context` upon consruction. Instead, they will use the default context from the device's platform. From 5546f9b1ac782cd9f06809016d6a1e591dc5f2fb Mon Sep 17 00:00:00 2001 From: James Brodman Date: Wed, 17 Mar 2021 16:20:36 -0400 Subject: [PATCH 13/41] Remove push/pop methods Signed-off-by: James Brodman --- .../PlatformContext/PlatformContext.adoc | 19 ++-------------- sycl/include/CL/sycl/platform.hpp | 11 ---------- sycl/source/detail/platform_impl.cpp | 22 ++----------------- sycl/source/detail/platform_impl.hpp | 3 --- sycl/source/platform.cpp | 6 ----- sycl/test/abi/sycl_symbols_linux.dump | 9 +++----- 6 files changed, 7 insertions(+), 63 deletions(-) diff --git a/sycl/doc/extensions/PlatformContext/PlatformContext.adoc b/sycl/doc/extensions/PlatformContext/PlatformContext.adoc index a99488631008b..b569d05bc6f30 100644 --- a/sycl/doc/extensions/PlatformContext/PlatformContext.adoc +++ b/sycl/doc/extensions/PlatformContext/PlatformContext.adoc @@ -10,7 +10,7 @@ v0.1 This extension adds the notion of a default SYCL context per SYCL platform. The default context for each platform contains all devices in the platform. -The platform class gains three new methods: +The platform class gains one new method: [cols="^60a,40"] |=== @@ -24,21 +24,6 @@ context get_default_context() | Returns the current default context for this `platform` -| -[source,c++] ----- -void push_default_context(const context &NewContext) ----- - -| Sets the current default context to `NewContext`. The previous default context is saved. - -| -[source,c++] ----- -void pop_default_context() ----- - -| Removes the current default context and restores the previous default context if applicable or throws a `runtime_error` if there is no valid context to restore. |=== -This extension also modifies the behavior of `queue` constructors. Queues will no longer create a new `context` upon consruction. Instead, they will use the default context from the device's platform. +This extension also modifies the behavior of `queue` constructors. Queues will no longer create a new `context` upon construction. Instead, they will use the default context from the device's platform. diff --git a/sycl/include/CL/sycl/platform.hpp b/sycl/include/CL/sycl/platform.hpp index 8e41328d2668c..13e9d3cc77294 100644 --- a/sycl/include/CL/sycl/platform.hpp +++ b/sycl/include/CL/sycl/platform.hpp @@ -131,17 +131,6 @@ class __SYCL_EXPORT platform { /// \return the default context context get_default_context() const; - /// Pushes a new default context - /// - /// \param the new default context - void push_default_context(const context &Context); - - /// Pops the current default context, setting the default - /// context to the previous context. - /// Throws an error if popping would result in no - /// default context. - void pop_default_context(); - private: pi_native_handle getNative() const; diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 0915c2c32e4a0..85654188e9f74 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -397,28 +397,10 @@ context platform_impl::getDefaultContext() { return MDefaultContexts.back(); else { auto Devices = get_devices(); - context DefaultContext(Devices); - MDefaultContexts.push_back(DefaultContext); + MDefaultContexts.emplace_back(Devices); - return DefaultContext; - } -} - -void platform_impl::pushDefaultContext(const context &Context) { - const std::lock_guard Guard(MContextMutex); - - MDefaultContexts.push_back(Context); -} - -void platform_impl::popDefaultContext() { - const std::lock_guard Guard(MContextMutex); - - if (MDefaultContexts.size() == 1) { - throw runtime_error("Popping the platform default context would result in " - "no default context", - PI_INVALID_OPERATION); + return MDefaultContexts.back(); } - MDefaultContexts.pop_back(); } } // namespace detail diff --git a/sycl/source/detail/platform_impl.hpp b/sycl/source/detail/platform_impl.hpp index 90e15f3862f52..b246c02f5a559 100644 --- a/sycl/source/detail/platform_impl.hpp +++ b/sycl/source/detail/platform_impl.hpp @@ -184,9 +184,6 @@ class platform_impl { context getDefaultContext(); - void pushDefaultContext(const context &Context); - void popDefaultContext(); - private: bool MHostPlatform = false; RT::PiPlatform MPlatform = 0; diff --git a/sycl/source/platform.cpp b/sycl/source/platform.cpp index 61ee8725d8b27..602e2270da2e2 100644 --- a/sycl/source/platform.cpp +++ b/sycl/source/platform.cpp @@ -69,11 +69,5 @@ context platform::get_default_context() const { return impl->getDefaultContext(); } -void platform::push_default_context(const context &Context) { - impl->pushDefaultContext(Context); -} - -void platform::pop_default_context() { impl->popDefaultContext(); } - } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/test/abi/sycl_symbols_linux.dump b/sycl/test/abi/sycl_symbols_linux.dump index 7bfda07c86230..fa34a5b9d41a2 100644 --- a/sycl/test/abi/sycl_symbols_linux.dump +++ b/sycl/test/abi/sycl_symbols_linux.dump @@ -5,7 +5,6 @@ # RUN: env LLVM_BIN_PATH=%llvm_build_bin_dir python %sycl_tools_src_dir/abi_check.py --mode check_symbols --reference %s %sycl_libs_dir/libsycl.so # REQUIRES: linux -# UNSUPPORTED: libcxx _Z20__spirv_ocl_prefetchPKcm _Z21__spirv_MemoryBarrierN5__spv5ScopeEj @@ -3874,8 +3873,6 @@ _ZN2cl4sycl7samplerC1EP11_cl_samplerRKNS0_7contextE _ZN2cl4sycl7samplerC2ENS0_29coordinate_normalization_modeENS0_15addressing_modeENS0_14filtering_modeERKNS0_13property_listE _ZN2cl4sycl7samplerC2EP11_cl_samplerRKNS0_7contextE _ZN2cl4sycl8platform13get_platformsEv -_ZN2cl4sycl8platform19pop_default_contextEv -_ZN2cl4sycl8platform20push_default_contextERKNS0_7contextE _ZN2cl4sycl8platformC1EP15_cl_platform_id _ZN2cl4sycl8platformC1ERKNS0_15device_selectorE _ZN2cl4sycl8platformC1Ev @@ -4121,6 +4118,7 @@ _ZNK2cl4sycl7context12get_propertyINS0_8property6buffer13context_boundEEET_v _ZNK2cl4sycl7context12get_propertyINS0_8property6buffer9use_mutexEEET_v _ZNK2cl4sycl7context12get_propertyINS0_8property6noinitEEET_v _ZNK2cl4sycl7context12get_propertyINS0_8property7context4cuda19use_primary_contextEEET_v +_ZNK2cl4sycl7context12get_propertyINS0_8property9reduction22initialize_to_identityEEET_v _ZNK2cl4sycl7context12has_propertyINS0_3ext6oneapi8property6buffer22use_pinned_host_memoryEEEbv _ZNK2cl4sycl7context12has_propertyINS0_8property5image12use_host_ptrEEEbv _ZNK2cl4sycl7context12has_propertyINS0_8property5image13context_boundEEEbv @@ -4131,7 +4129,6 @@ _ZNK2cl4sycl7context12has_propertyINS0_8property6buffer13context_boundEEEbv _ZNK2cl4sycl7context12has_propertyINS0_8property6buffer9use_mutexEEEbv _ZNK2cl4sycl7context12has_propertyINS0_8property6noinitEEEbv _ZNK2cl4sycl7context12has_propertyINS0_8property7context4cuda19use_primary_contextEEEbv -_ZNK2cl4sycl7context12get_propertyINS0_8property9reduction22initialize_to_identityEEET_v _ZNK2cl4sycl7context12has_propertyINS0_8property9reduction22initialize_to_identityEEEbv _ZNK2cl4sycl7context3getEv _ZNK2cl4sycl7context7is_hostEv @@ -4157,6 +4154,7 @@ _ZNK2cl4sycl7program12get_propertyINS0_8property6buffer13context_boundEEET_v _ZNK2cl4sycl7program12get_propertyINS0_8property6buffer9use_mutexEEET_v _ZNK2cl4sycl7program12get_propertyINS0_8property6noinitEEET_v _ZNK2cl4sycl7program12get_propertyINS0_8property7context4cuda19use_primary_contextEEET_v +_ZNK2cl4sycl7program12get_propertyINS0_8property9reduction22initialize_to_identityEEET_v _ZNK2cl4sycl7program12has_propertyINS0_3ext6oneapi8property6buffer22use_pinned_host_memoryEEEbv _ZNK2cl4sycl7program12has_propertyINS0_8property5image12use_host_ptrEEEbv _ZNK2cl4sycl7program12has_propertyINS0_8property5image13context_boundEEEbv @@ -4167,7 +4165,6 @@ _ZNK2cl4sycl7program12has_propertyINS0_8property6buffer13context_boundEEEbv _ZNK2cl4sycl7program12has_propertyINS0_8property6buffer9use_mutexEEEbv _ZNK2cl4sycl7program12has_propertyINS0_8property6noinitEEEbv _ZNK2cl4sycl7program12has_propertyINS0_8property7context4cuda19use_primary_contextEEEbv -_ZNK2cl4sycl7program12get_propertyINS0_8property9reduction22initialize_to_identityEEET_v _ZNK2cl4sycl7program12has_propertyINS0_8property9reduction22initialize_to_identityEEEbv _ZNK2cl4sycl7program16get_link_optionsB5cxx11Ev _ZNK2cl4sycl7program17get_build_optionsB5cxx11Ev @@ -4189,6 +4186,7 @@ _ZNK2cl4sycl7sampler12get_propertyINS0_8property6buffer13context_boundEEET_v _ZNK2cl4sycl7sampler12get_propertyINS0_8property6buffer9use_mutexEEET_v _ZNK2cl4sycl7sampler12get_propertyINS0_8property6noinitEEET_v _ZNK2cl4sycl7sampler12get_propertyINS0_8property7context4cuda19use_primary_contextEEET_v +_ZNK2cl4sycl7sampler12get_propertyINS0_8property9reduction22initialize_to_identityEEET_v _ZNK2cl4sycl7sampler12has_propertyINS0_3ext6oneapi8property6buffer22use_pinned_host_memoryEEEbv _ZNK2cl4sycl7sampler12has_propertyINS0_8property5image12use_host_ptrEEEbv _ZNK2cl4sycl7sampler12has_propertyINS0_8property5image13context_boundEEEbv @@ -4199,7 +4197,6 @@ _ZNK2cl4sycl7sampler12has_propertyINS0_8property6buffer13context_boundEEEbv _ZNK2cl4sycl7sampler12has_propertyINS0_8property6buffer9use_mutexEEEbv _ZNK2cl4sycl7sampler12has_propertyINS0_8property6noinitEEEbv _ZNK2cl4sycl7sampler12has_propertyINS0_8property7context4cuda19use_primary_contextEEEbv -_ZNK2cl4sycl7sampler12get_propertyINS0_8property9reduction22initialize_to_identityEEET_v _ZNK2cl4sycl7sampler12has_propertyINS0_8property9reduction22initialize_to_identityEEEbv _ZNK2cl4sycl7sampler18get_filtering_modeEv _ZNK2cl4sycl7sampler19get_addressing_modeEv From 76d9322ac084c57db0c4a9123d3618349c29c035 Mon Sep 17 00:00:00 2001 From: James Brodman Date: Wed, 17 Mar 2021 16:21:39 -0400 Subject: [PATCH 14/41] Remove tests for push/pop Signed-off-by: James Brodman --- sycl/test/on-device/defaultcontext/error.cpp | 29 -------------- .../test/on-device/defaultcontext/pushpop.cpp | 38 ------------------- 2 files changed, 67 deletions(-) delete mode 100644 sycl/test/on-device/defaultcontext/error.cpp delete mode 100644 sycl/test/on-device/defaultcontext/pushpop.cpp diff --git a/sycl/test/on-device/defaultcontext/error.cpp b/sycl/test/on-device/defaultcontext/error.cpp deleted file mode 100644 index 3601e70f38665..0000000000000 --- a/sycl/test/on-device/defaultcontext/error.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out -// RUN: env SYCL_DEVICE_TYPE=HOST %t.out -// RUN: %CPU_RUN_PLACEHOLDER %t.out -// RUN: %GPU_RUN_PLACEHOLDER %t.out - -//==------------- error.cpp - SYCL default context test --------------------==// -// -// 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 -// -//===----------------------------------------------------------------------===// - -#include - -int main() { - sycl::queue q; - - auto Device = q.get_device(); - auto Platform = Device.get_platform(); - - try { - Platform.pop_default_context(); - } catch (sycl::runtime_error) { - return 0; - } - - return -1; -} diff --git a/sycl/test/on-device/defaultcontext/pushpop.cpp b/sycl/test/on-device/defaultcontext/pushpop.cpp deleted file mode 100644 index 1d45b42dab551..0000000000000 --- a/sycl/test/on-device/defaultcontext/pushpop.cpp +++ /dev/null @@ -1,38 +0,0 @@ -// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out -// RUN: env SYCL_DEVICE_TYPE=HOST %t.out -// RUN: %CPU_RUN_PLACEHOLDER %t.out -// RUN: %GPU_RUN_PLACEHOLDER %t.out - -//==----------- pushpop.cpp - SYCL default context test --------------------==// -// -// 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 -// -//===----------------------------------------------------------------------===// - -#include - -int main() { - sycl::queue q; - auto C1 = q.get_context(); - auto Device = q.get_device(); - auto Platform = Device.get_platform(); - - sycl::context NewContext(Device); - Platform.push_default_context(NewContext); - - sycl::queue q2; - auto C2 = q2.get_context(); - assert(C1 != C2); - assert(C2 == NewContext); - - Platform.pop_default_context(); - - sycl::queue q3; - auto C3 = q3.get_context(); - assert(C1 == C3); - assert(C2 != C3); - - return 0; -} From 1b98a82279f6205ab29a708521c98daae25ae87e Mon Sep 17 00:00:00 2001 From: James Brodman Date: Tue, 18 May 2021 11:10:55 -0400 Subject: [PATCH 15/41] Address feedback. Change vector of contexts to a single one. Signed-off-by: James Brodman --- sycl/source/detail/platform_impl.cpp | 16 ++++++++-------- sycl/source/detail/platform_impl.hpp | 4 ++-- sycl/test/abi/sycl_symbols_linux.dump | 1 + sycl/test/on-device/defaultcontext/default.cpp | 3 +++ 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 85654188e9f74..1c72746092c60 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -391,16 +391,16 @@ bool platform_impl::has(aspect Aspect) const { #undef __SYCL_PARAM_TRAITS_SPEC context platform_impl::getDefaultContext() { - const std::lock_guard Guard(MContextMutex); + const std::lock_guard Guard(MDefaultContextMutex); - if (!MDefaultContexts.empty()) - return MDefaultContexts.back(); - else { - auto Devices = get_devices(); - MDefaultContexts.emplace_back(Devices); + if (!MDefaultContext) + return *MDefaultContext; - return MDefaultContexts.back(); - } + // Lazily instantiate default context + auto Devices = get_devices(); + MDefaultContext = std::make_shared(Devices); + + return *MDefaultContext; } } // namespace detail diff --git a/sycl/source/detail/platform_impl.hpp b/sycl/source/detail/platform_impl.hpp index b246c02f5a559..60f2c25f9c5d4 100644 --- a/sycl/source/detail/platform_impl.hpp +++ b/sycl/source/detail/platform_impl.hpp @@ -190,8 +190,8 @@ class platform_impl { std::shared_ptr MPlugin; std::vector> MDeviceCache; std::mutex MDeviceMapMutex; - std::vector MDefaultContexts; - std::mutex MContextMutex; + std::shared_ptr MDefaultContext; + std::mutex MDefaultContextMutex; }; } // namespace detail diff --git a/sycl/test/abi/sycl_symbols_linux.dump b/sycl/test/abi/sycl_symbols_linux.dump index fa34a5b9d41a2..2473021c94b6c 100644 --- a/sycl/test/abi/sycl_symbols_linux.dump +++ b/sycl/test/abi/sycl_symbols_linux.dump @@ -5,6 +5,7 @@ # RUN: env LLVM_BIN_PATH=%llvm_build_bin_dir python %sycl_tools_src_dir/abi_check.py --mode check_symbols --reference %s %sycl_libs_dir/libsycl.so # REQUIRES: linux +# UNSUPPORTED: libcxx _Z20__spirv_ocl_prefetchPKcm _Z21__spirv_MemoryBarrierN5__spv5ScopeEj diff --git a/sycl/test/on-device/defaultcontext/default.cpp b/sycl/test/on-device/defaultcontext/default.cpp index a1593932d2ae3..186c5130c249a 100644 --- a/sycl/test/on-device/defaultcontext/default.cpp +++ b/sycl/test/on-device/defaultcontext/default.cpp @@ -23,8 +23,11 @@ int main() { auto d1 = q.get_device(); auto d2 = q2.get_device(); + auto c3 = d1.get_platform().get_default_context(); + assert(d1 == d2); assert(c1 == c2); + assert(c1 == c3); return 0; } From 774a0bee0f898a19abade87a13bfa848a65bf20b Mon Sep 17 00:00:00 2001 From: James Brodman Date: Tue, 18 May 2021 11:16:36 -0400 Subject: [PATCH 16/41] Add Platform Context extension to README.md Signed-off-by: James Brodman --- sycl/doc/extensions/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sycl/doc/extensions/README.md b/sycl/doc/extensions/README.md index e4b4a7bdb52be..8036df6046e27 100755 --- a/sycl/doc/extensions/README.md +++ b/sycl/doc/extensions/README.md @@ -37,6 +37,7 @@ DPC++ extensions status: | [Use Pinned Memory Property](UsePinnedMemoryProperty/UsePinnedMemoryPropery.adoc) | Supported | | | [Level-Zero backend specification](LevelZeroBackend/LevelZeroBackend.md) | Supported | | | [ITT annotations support](ITTAnnotations/ITTAnnotations.rst) | Supported | | +| [Platform Context](PlatformContext/PlatformContext.adoc) | Proposal | | Legend: From fc86ff0b0f4228c4ec69bcaa2e06b5e2c3f7304d Mon Sep 17 00:00:00 2001 From: James Brodman Date: Wed, 9 Jun 2021 15:44:23 -0400 Subject: [PATCH 17/41] Fix logic bug. Returning nullptr all the time wasn't the intended behavior. Signed-off-by: James Brodman --- sycl/source/detail/platform_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 39c3554657e7f..755150db25812 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -300,7 +300,7 @@ bool platform_impl::has(aspect Aspect) const { context platform_impl::getDefaultContext() { const std::lock_guard Guard(MDefaultContextMutex); - if (!MDefaultContext) + if (MDefaultContext) return *MDefaultContext; // Lazily instantiate default context From c5d9ae784a94676d114dd96b57fe16cf5c20e906 Mon Sep 17 00:00:00 2001 From: James Brodman Date: Wed, 9 Jun 2021 15:48:37 -0400 Subject: [PATCH 18/41] Remove auto in a function Signed-off-by: James Brodman --- sycl/source/detail/platform_impl.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 755150db25812..a4a837530dc00 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -304,8 +304,7 @@ context platform_impl::getDefaultContext() { return *MDefaultContext; // Lazily instantiate default context - auto Devices = get_devices(); - MDefaultContext = std::make_shared(Devices); + MDefaultContext = std::make_shared(get_devices()); return *MDefaultContext; } From c576a08a6bdf5a87da62b63ae9bf3bf23b818eba Mon Sep 17 00:00:00 2001 From: James Brodman Date: Thu, 10 Jun 2021 16:03:52 -0400 Subject: [PATCH 19/41] Change to using shared_ptr of context_impl, but keep context() logic Signed-off-by: James Brodman --- sycl/source/detail/platform_impl.cpp | 10 +++++++--- sycl/source/detail/platform_impl.hpp | 3 ++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index a4a837530dc00..b50cd9e97509c 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -301,12 +302,15 @@ context platform_impl::getDefaultContext() { const std::lock_guard Guard(MDefaultContextMutex); if (MDefaultContext) - return *MDefaultContext; + return detail::createSyclObjFromImpl(MDefaultContext); // Lazily instantiate default context - MDefaultContext = std::make_shared(get_devices()); + // using context constructor b/c there's lots of logic there that isn't in + // the context_impl constructor + context NewDefaultContext(get_devices()); + MDefaultContext = detail::getSyclObjImpl(NewDefaultContext); - return *MDefaultContext; + return NewDefaultContext; } } // namespace detail diff --git a/sycl/source/detail/platform_impl.hpp b/sycl/source/detail/platform_impl.hpp index 60f2c25f9c5d4..096ae4c342d54 100644 --- a/sycl/source/detail/platform_impl.hpp +++ b/sycl/source/detail/platform_impl.hpp @@ -25,6 +25,7 @@ class device; enum class aspect; namespace detail { +class context_impl; class device_impl; // TODO: implement extension management for host device @@ -190,7 +191,7 @@ class platform_impl { std::shared_ptr MPlugin; std::vector> MDeviceCache; std::mutex MDeviceMapMutex; - std::shared_ptr MDefaultContext; + std::shared_ptr MDefaultContext; std::mutex MDefaultContextMutex; }; From 82ab84a7b7f5dc2c7241caea981e34259cc4475d Mon Sep 17 00:00:00 2001 From: James Brodman Date: Fri, 11 Jun 2021 13:14:41 -0400 Subject: [PATCH 20/41] Better conform to extension guidelines. Signed-off-by: James Brodman --- sycl/doc/extensions/PlatformContext/PlatformContext.adoc | 7 ++++++- sycl/include/CL/sycl/platform.hpp | 5 ++++- sycl/source/detail/queue_impl.hpp | 2 +- sycl/source/platform.cpp | 2 +- sycl/test/abi/sycl_symbols_linux.dump | 2 +- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/sycl/doc/extensions/PlatformContext/PlatformContext.adoc b/sycl/doc/extensions/PlatformContext/PlatformContext.adoc index b569d05bc6f30..6b9e764f1b446 100644 --- a/sycl/doc/extensions/PlatformContext/PlatformContext.adoc +++ b/sycl/doc/extensions/PlatformContext/PlatformContext.adoc @@ -19,7 +19,7 @@ The platform class gains one new method: | [source,c++] ---- -context get_default_context() +context ext_oneapi_get_default_context() ---- | Returns the current default context for this `platform` @@ -27,3 +27,8 @@ context get_default_context() |=== This extension also modifies the behavior of `queue` constructors. Queues will no longer create a new `context` upon construction. Instead, they will use the default context from the device's platform. + +== Feature Test Macro + +This extension defines the macro `SYCL_EXT_ONEAPI_DEFAULT_CONTEXT` to `1` to indicate that it is enabled. + diff --git a/sycl/include/CL/sycl/platform.hpp b/sycl/include/CL/sycl/platform.hpp index 7a37720924c11..86a54a9fe3dea 100644 --- a/sycl/include/CL/sycl/platform.hpp +++ b/sycl/include/CL/sycl/platform.hpp @@ -25,6 +25,9 @@ namespace detail { class platform_impl; } +// Feature test macro for Default Context +#define SYCL_EXT_ONEAPI_DEFAULT_CONTEXT 1 + /// Encapsulates a SYCL platform on which kernels may be executed. /// /// \ingroup sycl_api @@ -132,7 +135,7 @@ class __SYCL_EXPORT platform { /// Return this platform's default context /// /// \return the default context - context get_default_context() const; + context ext_oneapi_get_default_context() const; private: pi_native_handle getNative() const; diff --git a/sycl/source/detail/queue_impl.hpp b/sycl/source/detail/queue_impl.hpp index 543a9766eca69..4282c8a5a5759 100644 --- a/sycl/source/detail/queue_impl.hpp +++ b/sycl/source/detail/queue_impl.hpp @@ -59,7 +59,7 @@ class queue_impl { const property_list &PropList) : queue_impl(Device, detail::getSyclObjImpl( - Device->get_platform().get_default_context()), + Device->get_platform().ext_oneapi_get_default_context()), AsyncHandler, PropList){}; /// Constructs a SYCL queue with an async_handler and property_list provided diff --git a/sycl/source/platform.cpp b/sycl/source/platform.cpp index 602e2270da2e2..96110fa66f5bb 100644 --- a/sycl/source/platform.cpp +++ b/sycl/source/platform.cpp @@ -65,7 +65,7 @@ bool platform::has(aspect Aspect) const { return impl->has(Aspect); } #undef __SYCL_PARAM_TRAITS_SPEC -context platform::get_default_context() const { +context platform::ext_oneapi_get_default_context() const { return impl->getDefaultContext(); } diff --git a/sycl/test/abi/sycl_symbols_linux.dump b/sycl/test/abi/sycl_symbols_linux.dump index e22150994c3fd..65d9c84ae80a8 100644 --- a/sycl/test/abi/sycl_symbols_linux.dump +++ b/sycl/test/abi/sycl_symbols_linux.dump @@ -4245,7 +4245,7 @@ _ZNK2cl4sycl7samplerneERKS1_ _ZNK2cl4sycl8platform11get_backendEv _ZNK2cl4sycl8platform11get_devicesENS0_4info11device_typeE _ZNK2cl4sycl8platform13has_extensionERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE -_ZNK2cl4sycl8platform19get_default_contextEv +_ZNK2cl4sycl8platform30ext_oneapi_get_default_contextEv _ZNK2cl4sycl8platform3getEv _ZNK2cl4sycl8platform3hasENS0_6aspectE _ZNK2cl4sycl8platform7is_hostEv From c191b8a292c0720ca1c90e62806d28bf6bd02da1 Mon Sep 17 00:00:00 2001 From: James Brodman Date: Wed, 30 Jun 2021 13:40:15 -0400 Subject: [PATCH 21/41] Handle host default context slightly specially Signed-off-by: James Brodman --- sycl/source/detail/platform_impl.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index b50cd9e97509c..b3a0eef4b0c91 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -307,10 +307,15 @@ context platform_impl::getDefaultContext() { // Lazily instantiate default context // using context constructor b/c there's lots of logic there that isn't in // the context_impl constructor - context NewDefaultContext(get_devices()); - MDefaultContext = detail::getSyclObjImpl(NewDefaultContext); + if (is_host()) { + context NewDefaultHostContext({device()}); + MDefaultContext = detail::getSyclObjImpl(NewDefaultHostContext); + } else { + context NewDefaultContext(get_devices()); + MDefaultContext = detail::getSyclObjImpl(NewDefaultContext); + } - return NewDefaultContext; + return detail::createSyclObjFromImpl(MDefaultContext); } } // namespace detail From 6654c0a8aefdf0d2e7c96762fffde01803cc5e7a Mon Sep 17 00:00:00 2001 From: Sergey Kanaev Date: Fri, 2 Jul 2021 15:18:10 +0300 Subject: [PATCH 22/41] Fix issue Signed-off-by: Sergey Kanaev --- sycl/test/on-device/defaultcontext/default.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/test/on-device/defaultcontext/default.cpp b/sycl/test/on-device/defaultcontext/default.cpp index 186c5130c249a..89f6959519bb2 100644 --- a/sycl/test/on-device/defaultcontext/default.cpp +++ b/sycl/test/on-device/defaultcontext/default.cpp @@ -23,7 +23,7 @@ int main() { auto d1 = q.get_device(); auto d2 = q2.get_device(); - auto c3 = d1.get_platform().get_default_context(); + auto c3 = d1.get_platform().ext_oneapi_get_default_context(); assert(d1 == d2); assert(c1 == c2); From 53d11ea01c14d83eadf0cb6689a4d50764ad47fa Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Thu, 8 Jul 2021 12:00:42 +0300 Subject: [PATCH 23/41] draft --- sycl/source/CMakeLists.txt | 2 ++ sycl/source/detail/global_handler.cpp | 9 +++++ sycl/source/detail/global_handler.hpp | 11 +++++++ sycl/source/detail/platform_impl.cpp | 20 ----------- sycl/source/detail/platform_impl.hpp | 4 --- sycl/source/platform.cpp | 18 +++++++++- .../test/on-device/defaultcontext/default.cpp | 33 ------------------- sycl/unittests/misc/CMakeLists.txt | 1 + 8 files changed, 40 insertions(+), 58 deletions(-) delete mode 100644 sycl/test/on-device/defaultcontext/default.cpp diff --git a/sycl/source/CMakeLists.txt b/sycl/source/CMakeLists.txt index 22adb1037bbb0..6b45e76916806 100644 --- a/sycl/source/CMakeLists.txt +++ b/sycl/source/CMakeLists.txt @@ -4,6 +4,8 @@ #cmake_policy(SET CMP0057 NEW) #include(AddLLVM) +add_definitions(-g -O0) + configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index e172d6f05b7f3..f0648b47be0dc 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -50,6 +50,15 @@ ProgramManager &GlobalHandler::getProgramManager() { return getOrCreate(MProgramManager); } +std::unordered_map & +GlobalHandler::getPlatformToDefaultContextCache() { + return getOrCreate(MPlatformToDefaultContextCache); +} + +std::mutex &GlobalHandler::getPlatformToDefaultContextCacheMutex() { + return getOrCreate(MPlatformToDefaultContextCacheMutex); +} + Sync &GlobalHandler::getSync() { return getOrCreate(MSync); } std::vector &GlobalHandler::getPlatformCache() { diff --git a/sycl/source/detail/global_handler.hpp b/sycl/source/detail/global_handler.hpp index 9e3e3305c1f19..6ceba340a4827 100644 --- a/sycl/source/detail/global_handler.hpp +++ b/sycl/source/detail/global_handler.hpp @@ -11,12 +11,14 @@ #include #include +#include #include __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { namespace detail { class platform_impl; +class context_impl; class Scheduler; class ProgramManager; class Sync; @@ -24,6 +26,7 @@ class plugin; class device_filter_list; using PlatformImplPtr = std::shared_ptr; +using ContextImplPtr = std::shared_ptr; /// Wrapper class for global data structures with non-trivial destructors. /// @@ -52,6 +55,11 @@ class GlobalHandler { ProgramManager &getProgramManager(); Sync &getSync(); std::vector &getPlatformCache(); + + std::unordered_map & + getPlatformToDefaultContextCache(); + + std::mutex &getPlatformToDefaultContextCacheMutex(); std::mutex &getPlatformMapMutex(); std::mutex &getFilterMutex(); std::vector &getPlugins(); @@ -78,6 +86,9 @@ class GlobalHandler { InstWithLock MProgramManager; InstWithLock MSync; InstWithLock> MPlatformCache; + InstWithLock> + MPlatformToDefaultContextCache; + InstWithLock MPlatformToDefaultContextCacheMutex; InstWithLock MPlatformMapMutex; InstWithLock MFilterMutex; InstWithLock> MPlugins; diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 982c758c71504..19e659d033e8f 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -298,26 +298,6 @@ bool platform_impl::has(aspect Aspect) const { #include #undef __SYCL_PARAM_TRAITS_SPEC -context platform_impl::getDefaultContext() { - const std::lock_guard Guard(MDefaultContextMutex); - - if (MDefaultContext) - return detail::createSyclObjFromImpl(MDefaultContext); - - // Lazily instantiate default context - // using context constructor b/c there's lots of logic there that isn't in - // the context_impl constructor - if (is_host()) { - context NewDefaultHostContext({device()}); - MDefaultContext = detail::getSyclObjImpl(NewDefaultHostContext); - } else { - context NewDefaultContext(get_devices()); - MDefaultContext = detail::getSyclObjImpl(NewDefaultContext); - } - - return detail::createSyclObjFromImpl(MDefaultContext); -} - } // namespace detail } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/detail/platform_impl.hpp b/sycl/source/detail/platform_impl.hpp index 46f8d2b259072..26abc7a5c8701 100644 --- a/sycl/source/detail/platform_impl.hpp +++ b/sycl/source/detail/platform_impl.hpp @@ -183,16 +183,12 @@ class platform_impl { static std::shared_ptr getPlatformFromPiDevice(RT::PiDevice PiDevice, const plugin &Plugin); - context getDefaultContext(); - private: bool MHostPlatform = false; RT::PiPlatform MPlatform = 0; std::shared_ptr MPlugin; std::vector> MDeviceCache; std::mutex MDeviceMapMutex; - std::shared_ptr MDefaultContext; - std::mutex MDefaultContextMutex; }; } // namespace detail diff --git a/sycl/source/platform.cpp b/sycl/source/platform.cpp index 84d0b90e69219..59559215e14dd 100644 --- a/sycl/source/platform.cpp +++ b/sycl/source/platform.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include __SYCL_INLINE_NAMESPACE(cl) { @@ -66,7 +67,22 @@ bool platform::has(aspect Aspect) const { return impl->has(Aspect); } #undef __SYCL_PARAM_TRAITS_SPEC context platform::ext_oneapi_get_default_context() const { - return impl->getDefaultContext(); + + // 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 + &PlatformToDefaultContextCache = + detail::GlobalHandler::instance().getPlatformToDefaultContextCache(); + + std::lock_guard Lock{detail::GlobalHandler::instance() + .getPlatformToDefaultContextCacheMutex()}; + + auto It = PlatformToDefaultContextCache.find(impl); + if (PlatformToDefaultContextCache.end() == It) + std::tie(It, std::ignore) = PlatformToDefaultContextCache.insert( + {impl, detail::getSyclObjImpl(context{get_devices()})}); + + return detail::createSyclObjFromImpl(It->second); } } // namespace sycl diff --git a/sycl/test/on-device/defaultcontext/default.cpp b/sycl/test/on-device/defaultcontext/default.cpp deleted file mode 100644 index 89f6959519bb2..0000000000000 --- a/sycl/test/on-device/defaultcontext/default.cpp +++ /dev/null @@ -1,33 +0,0 @@ -// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out -// RUN: env SYCL_DEVICE_TYPE=HOST %t.out -// RUN: %CPU_RUN_PLACEHOLDER %t.out -// RUN: %GPU_RUN_PLACEHOLDER %t.out - -//==----------- default.cpp - SYCL default context test --------------------==// -// -// 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 -// -//===----------------------------------------------------------------------===// - -#include - -int main() { - sycl::queue q; - sycl::queue q2; - - auto c1 = q.get_context(); - auto c2 = q2.get_context(); - - auto d1 = q.get_device(); - auto d2 = q2.get_device(); - - auto c3 = d1.get_platform().ext_oneapi_get_default_context(); - - assert(d1 == d2); - assert(c1 == c2); - assert(c1 == c3); - - return 0; -} diff --git a/sycl/unittests/misc/CMakeLists.txt b/sycl/unittests/misc/CMakeLists.txt index 127089d85ebaa..3c51e7e378cc8 100644 --- a/sycl/unittests/misc/CMakeLists.txt +++ b/sycl/unittests/misc/CMakeLists.txt @@ -1,6 +1,7 @@ set(sycl_lib_dir $) add_definitions(-DSYCL_LIB_DIR="${sycl_lib_dir}") add_sycl_unittest(MiscTests SHARED + DefaultContext.cpp CircularBuffer.cpp KernelBuildOptions.cpp OsUtils.cpp From 5ec61b0bf1607c79ef66152b5ba03cfbe8b23f98 Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Sun, 11 Jul 2021 18:34:06 +0300 Subject: [PATCH 24/41] remove leftovers --- sycl/source/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/sycl/source/CMakeLists.txt b/sycl/source/CMakeLists.txt index 6b45e76916806..22adb1037bbb0 100644 --- a/sycl/source/CMakeLists.txt +++ b/sycl/source/CMakeLists.txt @@ -4,8 +4,6 @@ #cmake_policy(SET CMP0057 NEW) #include(AddLLVM) -add_definitions(-g -O0) - configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc From 9b4f707369b20b24aade1b78b9cb76a8de99f2e5 Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Sun, 11 Jul 2021 18:40:45 +0300 Subject: [PATCH 25/41] Apply comments --- sycl/source/detail/platform_impl.cpp | 1 - sycl/source/detail/platform_impl.hpp | 2 -- sycl/unittests/misc/DefaultContext.cpp | 36 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 sycl/unittests/misc/DefaultContext.cpp diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 19e659d033e8f..de3f489217657 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include diff --git a/sycl/source/detail/platform_impl.hpp b/sycl/source/detail/platform_impl.hpp index 26abc7a5c8701..bb2343f57d653 100644 --- a/sycl/source/detail/platform_impl.hpp +++ b/sycl/source/detail/platform_impl.hpp @@ -8,7 +8,6 @@ #pragma once -#include #include #include #include @@ -25,7 +24,6 @@ class device; enum class aspect; namespace detail { -class context_impl; class device_impl; // TODO: implement extension management for host device diff --git a/sycl/unittests/misc/DefaultContext.cpp b/sycl/unittests/misc/DefaultContext.cpp new file mode 100644 index 0000000000000..8c383daadbe60 --- /dev/null +++ b/sycl/unittests/misc/DefaultContext.cpp @@ -0,0 +1,36 @@ +//==---- CircularBuffer.cpp ------------------------------------------------==// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include + +#include +#include + +#include + +TEST(DefaultContextTest, DefaultContextTest) { + + sycl::platform Plt1{sycl::default_selector()}; + sycl::unittest::PiMock Mock1{Plt1}; + setupDefaultMockAPIs(Mock1); + + sycl::platform Plt2{sycl::default_selector()}; + sycl::unittest::PiMock Mock2{Plt2}; + setupDefaultMockAPIs(Mock2); + + const sycl::device Dev1 = Plt1.get_devices()[0]; + const sycl::device Dev2 = Plt2.get_devices()[0]; + + sycl::queue Queue1{Dev1}; + sycl::queue Queue2{Dev2}; + + ASSERT_EQ(Queue1.get_context(), Queue2.get_context()); + + ASSERT_EQ(Dev1.get_platform().ext_oneapi_get_default_context(), + Dev2.get_platform().ext_oneapi_get_default_context()); +} From 914710d016f2aedb02b267166dfc737a69ad4645 Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Sun, 11 Jul 2021 18:59:01 +0300 Subject: [PATCH 26/41] Add missing include --- sycl/include/CL/sycl/platform.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sycl/include/CL/sycl/platform.hpp b/sycl/include/CL/sycl/platform.hpp index abd366e5a5055..14ec6dc60be1a 100644 --- a/sycl/include/CL/sycl/platform.hpp +++ b/sycl/include/CL/sycl/platform.hpp @@ -7,7 +7,9 @@ //===----------------------------------------------------------------------===// #pragma once + #include +#include #include #include #include From 6d3868c72caed50641c5c0d343e3e8d7833d079e Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Sun, 11 Jul 2021 19:00:18 +0300 Subject: [PATCH 27/41] clang-format --- sycl/source/detail/global_handler.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/global_handler.hpp b/sycl/source/detail/global_handler.hpp index 6ceba340a4827..c539fdf2f093c 100644 --- a/sycl/source/detail/global_handler.hpp +++ b/sycl/source/detail/global_handler.hpp @@ -11,8 +11,8 @@ #include #include -#include #include +#include __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { From f6f18069c5205a6050ae896edaa4bb2bec32fb71 Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Mon, 12 Jul 2021 07:08:12 +0300 Subject: [PATCH 28/41] Avoid using default context when creating host queue --- sycl/source/detail/scheduler/scheduler.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sycl/source/detail/scheduler/scheduler.cpp b/sycl/source/detail/scheduler/scheduler.cpp index 3b0bc2485648d..03928e8306d0a 100644 --- a/sycl/source/detail/scheduler/scheduler.cpp +++ b/sycl/source/detail/scheduler/scheduler.cpp @@ -367,8 +367,10 @@ void Scheduler::deallocateStreamBuffers(stream_impl *Impl) { Scheduler::Scheduler() { sycl::device HostDevice; + sycl::context HostContext{HostDevice}; DefaultHostQueue = QueueImplPtr( - new queue_impl(detail::getSyclObjImpl(HostDevice), /*AsyncHandler=*/{}, + new queue_impl(detail::getSyclObjImpl(HostDevice), + detail::getSyclObjImpl(HostContext), /*AsyncHandler=*/{}, /*PropList=*/{})); } From ac3aa59241f536f3412d14d6bbef40896445b9f0 Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Mon, 12 Jul 2021 08:22:28 +0300 Subject: [PATCH 29/41] Do not use default contexts for sub-devices --- sycl/source/detail/queue_impl.hpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/sycl/source/detail/queue_impl.hpp b/sycl/source/detail/queue_impl.hpp index bd455fd4b5cd2..3d439badfed1e 100644 --- a/sycl/source/detail/queue_impl.hpp +++ b/sycl/source/detail/queue_impl.hpp @@ -49,6 +49,18 @@ enum QueueOrder { Ordered, OOO }; class queue_impl { public: + // \return a default context for the platform if it includes the device + // passed, a new context otherwise. + static ContextImplPtr getDefaultOrNew(const DeviceImplPtr &Device) { + ContextImplPtr DefaultContext = detail::getSyclObjImpl( + Device->get_platform().ext_oneapi_get_default_context()); + + if (DefaultContext->hasDevice(Device)) + return DefaultContext; + + return detail::getSyclObjImpl( + context{createSyclObjFromImpl(Device), {}, {}}); + } /// Constructs a SYCL queue from a device using an async_handler and /// property_list provided. /// @@ -58,10 +70,7 @@ class queue_impl { /// \param PropList is a list of properties to use for queue construction. queue_impl(const DeviceImplPtr &Device, const async_handler &AsyncHandler, const property_list &PropList) - : queue_impl(Device, - detail::getSyclObjImpl( - Device->get_platform().ext_oneapi_get_default_context()), - AsyncHandler, PropList){}; + : queue_impl(Device, getDefaultOrNew(Device), AsyncHandler, PropList){}; /// Constructs a SYCL queue with an async_handler and property_list provided /// form a device and a context. From b3012a36f92fa227560f16598cdaabf46e981531 Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Tue, 13 Jul 2021 11:09:38 +0300 Subject: [PATCH 30/41] Release default context cache before releasing plugins because cache releasing requires plugins to be alive. --- sycl/source/detail/global_handler.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index f0648b47be0dc..4ef013e2f1f82 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -88,6 +88,7 @@ void shutdown() { // First, release resources, that may access plugins. GlobalHandler::instance().MScheduler.Inst.reset(nullptr); GlobalHandler::instance().MProgramManager.Inst.reset(nullptr); + GlobalHandler::instance().MPlatformToDefaultContextCache.Inst.reset(nullptr); GlobalHandler::instance().MPlatformCache.Inst.reset(nullptr); // Call to GlobalHandler::instance().getPlugins() initializes plugins. If From 3463c1e260ec6fdc1378a1c49362a55d30d0d97b Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Wed, 14 Jul 2021 07:36:25 +0300 Subject: [PATCH 31/41] Workaround CUDA fails during shutdown --- sycl/plugins/cuda/pi_cuda.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sycl/plugins/cuda/pi_cuda.cpp b/sycl/plugins/cuda/pi_cuda.cpp index a5f8f2faacd4a..e6804411b2696 100644 --- a/sycl/plugins/cuda/pi_cuda.cpp +++ b/sycl/plugins/cuda/pi_cuda.cpp @@ -1705,7 +1705,12 @@ pi_result cuda_piContextRelease(pi_context ctxt) { std::unique_ptr<_pi_context> context{ctxt}; - PI_CHECK_ERROR(cuEventDestroy(context->evBase_)); + // Can do nothing since the CUDA driver is not responsive anymore. TODO: Find + // a better way or exttend this logic to all PI APIs that call CUDA API + if (CUDA_ERROR_DEINITIALIZED == errCode) + return PI_SUCCESS; + + PI_CHECK_ERROR(errCode); if (!ctxt->is_primary()) { CUcontext cuCtxt = ctxt->get(); From ca6640e958411a42082ea4022d40c6b3c4c866ff Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Wed, 14 Jul 2021 13:18:52 +0300 Subject: [PATCH 32/41] Fix workaround for cuda --- sycl/plugins/cuda/pi_cuda.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sycl/plugins/cuda/pi_cuda.cpp b/sycl/plugins/cuda/pi_cuda.cpp index e6804411b2696..22bef69a73805 100644 --- a/sycl/plugins/cuda/pi_cuda.cpp +++ b/sycl/plugins/cuda/pi_cuda.cpp @@ -1705,8 +1705,11 @@ pi_result cuda_piContextRelease(pi_context ctxt) { std::unique_ptr<_pi_context> context{ctxt}; - // Can do nothing since the CUDA driver is not responsive anymore. TODO: Find - // a better way or exttend this logic to all PI APIs that call CUDA API + CUresult errCode = cuEventDestroy(context->evBase_); + + // Can do nothing since the CUDA driver is not responsive anymore. + // TODO: Find a better way or exttend this logic to all PI APIs that call CUDA + // API if (CUDA_ERROR_DEINITIALIZED == errCode) return PI_SUCCESS; From 78cc85bb5cfba2676c74ab2384fa881c2a76c6cd Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Mon, 19 Jul 2021 16:25:24 +0300 Subject: [PATCH 33/41] Test treating CUDA_ERROR_DEINITIALIZED as succsess in all cases --- sycl/plugins/cuda/pi_cuda.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/plugins/cuda/pi_cuda.cpp b/sycl/plugins/cuda/pi_cuda.cpp index 22bef69a73805..01db3ad90eaf4 100644 --- a/sycl/plugins/cuda/pi_cuda.cpp +++ b/sycl/plugins/cuda/pi_cuda.cpp @@ -111,7 +111,7 @@ pi_result forLatestEvents(const pi_event *event_wait_list, /// pi_result check_error(CUresult result, const char *function, int line, const char *file) { - if (result == CUDA_SUCCESS) { + if (result == CUDA_SUCCESS || result == CUDA_ERROR_DEINITIALIZED) { return PI_SUCCESS; } From 32462b520bf74c82723a974741225d939ac61b19 Mon Sep 17 00:00:00 2001 From: Alexander Batashev Date: Wed, 25 Aug 2021 11:47:46 +0300 Subject: [PATCH 34/41] Make default contexts optional --- sycl/source/detail/config.def | 1 + sycl/source/detail/config.hpp | 20 ++++++++++++++++ sycl/source/detail/queue_impl.hpp | 7 +++++- sycl/source/platform.cpp | 3 +++ sycl/unittests/misc/DefaultContext.cpp | 33 ++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) diff --git a/sycl/source/detail/config.def b/sycl/source/detail/config.def index a685e64076783..d3d6baf60a625 100644 --- a/sycl/source/detail/config.def +++ b/sycl/source/detail/config.def @@ -31,3 +31,4 @@ CONFIG(SYCL_CACHE_THRESHOLD, 16, __SYCL_CACHE_THRESHOLD) CONFIG(SYCL_CACHE_MIN_DEVICE_IMAGE_SIZE, 16, __SYCL_CACHE_MIN_DEVICE_IMAGE_SIZE) CONFIG(SYCL_CACHE_MAX_DEVICE_IMAGE_SIZE, 16, __SYCL_CACHE_MAX_DEVICE_IMAGE_SIZE) CONFIG(INTEL_ENABLE_OFFLOAD_ANNOTATIONS, 1, __SYCL_INTEL_ENABLE_OFFLOAD_ANNOTATIONS) +CONFIG(SYCL_ENABLE_DEFAULT_CONTEXTS, 1, __SYCL_ENABLE_DEFAULT_CONTEXTS) diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index dd91d24eba642..e00337a7c5ef3 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -233,6 +233,26 @@ template <> class SYCLConfig { } }; +template <> class SYCLConfig { + using BaseT = SYCLConfigBase; + +public: + static bool get() { +#ifdef WIN32 + constexpr bool DefaultValue = false; +#else + constexpr bool DefaultValue = true; +#endif + + const char *ValStr = BaseT::getRawValue(); + + if (!ValStr) + return DefaultValue; + + return ValStr[0] == '1'; + } +}; + } // namespace detail } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/detail/queue_impl.hpp b/sycl/source/detail/queue_impl.hpp index 3d439badfed1e..fd4dc41c0b42f 100644 --- a/sycl/source/detail/queue_impl.hpp +++ b/sycl/source/detail/queue_impl.hpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -50,8 +51,12 @@ enum QueueOrder { Ordered, OOO }; class queue_impl { public: // \return a default context for the platform if it includes the device - // passed, a new context otherwise. + // passed and defaualt contexts are enabled, a new context otherwise. static ContextImplPtr getDefaultOrNew(const DeviceImplPtr &Device) { + if (!SYCLConfig::get()) + return detail::getSyclObjImpl( + context{createSyclObjFromImpl(Device), {}, {}}); + ContextImplPtr DefaultContext = detail::getSyclObjImpl( Device->get_platform().ext_oneapi_get_default_context()); diff --git a/sycl/source/platform.cpp b/sycl/source/platform.cpp index 59559215e14dd..a78bb18798ead 100644 --- a/sycl/source/platform.cpp +++ b/sycl/source/platform.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -67,6 +68,8 @@ bool platform::has(aspect Aspect) const { return impl->has(Aspect); } #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"); // Keeping the default context for platforms in the global cache to avoid // shared_ptr based circular dependency between platform and context classes diff --git a/sycl/unittests/misc/DefaultContext.cpp b/sycl/unittests/misc/DefaultContext.cpp index 8c383daadbe60..ca33896058d55 100644 --- a/sycl/unittests/misc/DefaultContext.cpp +++ b/sycl/unittests/misc/DefaultContext.cpp @@ -13,7 +13,22 @@ #include +#include + +// Same as defined in config.def +inline constexpr auto EnableDefaultContextsName = + "SYCL_ENABLE_DEFAULT_CONTEXTS"; + +static void set_env(const char *name, const char *value) { +#ifdef _WIN32 + (void)_putenv_s(name, value); +#else + (void)setenv(name, value, /*overwrite*/ 1); +#endif +} + TEST(DefaultContextTest, DefaultContextTest) { + set_env(EnableDefaultContextsName, "1"); sycl::platform Plt1{sycl::default_selector()}; sycl::unittest::PiMock Mock1{Plt1}; @@ -34,3 +49,21 @@ TEST(DefaultContextTest, DefaultContextTest) { ASSERT_EQ(Dev1.get_platform().ext_oneapi_get_default_context(), Dev2.get_platform().ext_oneapi_get_default_context()); } + +TEST(DefaultContextTest, DefaultContextCanBeDisabled) { + set_env(EnableDefaultContextsName, "0"); + + sycl::platform Plt{sycl::default_selector()}; + sycl::unittest::PiMock Mock{Plt}; + setupDefaultMockAPIs(Mock); + + bool catchException = false; + try { + (void)Plt.ext_oneapi_get_default_context(); + } catch (std::runtime_error) { + catchException = true; + } + + ASSERT_TRUE(catchException) + << "ext_oneapi_get_default_context did not throw and exception"; +} From def28c0e80fa53df25b90ab93181636260323d96 Mon Sep 17 00:00:00 2001 From: Alexander Batashev Date: Wed, 25 Aug 2021 11:50:25 +0300 Subject: [PATCH 35/41] Add docs --- sycl/doc/EnvironmentVariables.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 752231f0e4aef..fa73c8e96a217 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -46,6 +46,7 @@ subject to change. Do not rely on these variables in production code. | `SYCL_CACHE_THRESHOLD` | Positive integer | Cache eviction threshold in days (default value is 7 for 1 week). Set to 0 for disabling time-based cache eviction. | | `SYCL_CACHE_MIN_DEVICE_IMAGE_SIZE` | Positive integer | Minimum size of device code image in bytes which is reasonable to cache on disk because disk access operation may take more time than do JIT compilation for it. Default value is 0 to cache all images. | | `SYCL_CACHE_MAX_DEVICE_IMAGE_SIZE` | Positive integer | Maximum size of device image in bytes which is cached. Too big kernels may overload disk too fast. Default value is 1 GB. | +| `SYCL_ENABLE_DEFAULT_CONTEXTS` | '1' or '0' | Enable ('1') or disable ('0') creation of default contexts in SYCL runtime. Enabled by default on Linux and disabled on Windows. | `(*) Note: Any means this environment variable is effective when set to any non-null value.` From 91a48826ddc2eed84365069be6d23ca9d748db13 Mon Sep 17 00:00:00 2001 From: Alexander Batashev Date: Wed, 25 Aug 2021 13:37:17 +0300 Subject: [PATCH 36/41] Address test failures and review comments --- sycl/doc/EnvironmentVariables.md | 2 +- sycl/test/abi/sycl_symbols_windows.dump | 1 + sycl/unittests/misc/DefaultContext.cpp | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index d85a5a0c24e02..ae21006aa6093 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -47,7 +47,7 @@ subject to change. Do not rely on these variables in production code. | `SYCL_CACHE_THRESHOLD` | Positive integer | Cache eviction threshold in days (default value is 7 for 1 week). Set to 0 for disabling time-based cache eviction. | | `SYCL_CACHE_MIN_DEVICE_IMAGE_SIZE` | Positive integer | Minimum size of device code image in bytes which is reasonable to cache on disk because disk access operation may take more time than do JIT compilation for it. Default value is 0 to cache all images. | | `SYCL_CACHE_MAX_DEVICE_IMAGE_SIZE` | Positive integer | Maximum size of device image in bytes which is cached. Too big kernels may overload disk too fast. Default value is 1 GB. | -| `SYCL_ENABLE_DEFAULT_CONTEXTS` | '1' or '0' | Enable ('1') or disable ('0') creation of default contexts in SYCL runtime. Enabled by default on Linux and disabled on Windows. | +| `SYCL_ENABLE_DEFAULT_CONTEXTS` | '1' or '0' | Enable ('1') or disable ('0') creation of default platform contexts in SYCL runtime. The default context for each platform contains all devices in the platform. Refer to [Platform Default Contexts](extensions/PlatformContext/PlatformContext.adoc) extension to learn more. Enabled by default on Linux and disabled on Windows. | `(*) Note: Any means this environment variable is effective when set to any non-null value.` diff --git a/sycl/test/abi/sycl_symbols_windows.dump b/sycl/test/abi/sycl_symbols_windows.dump index 81161742f0910..cd8b4fdb1345d 100644 --- a/sycl/test/abi/sycl_symbols_windows.dump +++ b/sycl/test/abi/sycl_symbols_windows.dump @@ -1741,6 +1741,7 @@ ?extractArgsAndReqs@handler@sycl@cl@@AEAAXXZ ?extractArgsAndReqsFromLambda@handler@sycl@cl@@AEAAXPEAD_KPEBUkernel_param_desc_t@detail@23@@Z ?extractArgsAndReqsFromLambda@handler@sycl@cl@@AEAAXPEAD_KPEBUkernel_param_desc_t@detail@23@_N@Z +?ext_oneapi_get_default_context@platform@sycl@cl@@QEBA?AVcontext@23@XZ ?fabs@__host_std@cl@@YA?AV?$vec@M$00@sycl@2@V342@@Z ?fabs@__host_std@cl@@YA?AV?$vec@M$01@sycl@2@V342@@Z ?fabs@__host_std@cl@@YA?AV?$vec@M$02@sycl@2@V342@@Z diff --git a/sycl/unittests/misc/DefaultContext.cpp b/sycl/unittests/misc/DefaultContext.cpp index ca33896058d55..ec6a98ac3fe18 100644 --- a/sycl/unittests/misc/DefaultContext.cpp +++ b/sycl/unittests/misc/DefaultContext.cpp @@ -60,7 +60,7 @@ TEST(DefaultContextTest, DefaultContextCanBeDisabled) { bool catchException = false; try { (void)Plt.ext_oneapi_get_default_context(); - } catch (std::runtime_error) { + } catch (const std::runtime_error &) { catchException = true; } From 9ef815e2a483d3853c5942af1d58bd67e4515c3e Mon Sep 17 00:00:00 2001 From: Alexander Batashev Date: Thu, 26 Aug 2021 13:12:05 +0300 Subject: [PATCH 37/41] fix env var cache --- sycl/source/detail/config.hpp | 14 ++++- sycl/unittests/SYCL2020/CMakeLists.txt | 1 + .../ExtDefaultContext.cpp} | 24 ++++----- sycl/unittests/SYCL2020/GetNativeOpenCL.cpp | 12 ++++- sycl/unittests/helpers/ScopedEnvVar.hpp | 51 +++++++++++++++++++ sycl/unittests/misc/CMakeLists.txt | 3 +- 6 files changed, 87 insertions(+), 18 deletions(-) rename sycl/unittests/{misc/DefaultContext.cpp => SYCL2020/ExtDefaultContext.cpp} (76%) create mode 100644 sycl/unittests/helpers/ScopedEnvVar.hpp diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index c62e2c6f13399..d8e6396adfdd3 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -293,13 +293,25 @@ template <> class SYCLConfig { constexpr bool DefaultValue = true; #endif - const char *ValStr = BaseT::getRawValue(); + const char *ValStr = getCachedValue(); if (!ValStr) return DefaultValue; return ValStr[0] == '1'; } + + static void reset() { (void)getCachedValue(/*ResetCache=*/true); } + + static const char *getName() { return BaseT::MConfigName; } + +private: + static const char *getCachedValue(bool ResetCache = false) { + static const char *ValStr = BaseT::getRawValue(); + if (ResetCache) + ValStr = BaseT::getRawValue(); + return ValStr; + } }; } // namespace detail diff --git a/sycl/unittests/SYCL2020/CMakeLists.txt b/sycl/unittests/SYCL2020/CMakeLists.txt index 7d8be599bfb5e..43ee2e714792c 100644 --- a/sycl/unittests/SYCL2020/CMakeLists.txt +++ b/sycl/unittests/SYCL2020/CMakeLists.txt @@ -3,6 +3,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) # Enable exception handling for these unit tests set(LLVM_REQUIRES_EH 1) add_sycl_unittest(SYCL2020Tests OBJECT + ExtDefaultContext.cpp GetNativeOpenCL.cpp SpecConstDefaultValues.cpp KernelBundle.cpp diff --git a/sycl/unittests/misc/DefaultContext.cpp b/sycl/unittests/SYCL2020/ExtDefaultContext.cpp similarity index 76% rename from sycl/unittests/misc/DefaultContext.cpp rename to sycl/unittests/SYCL2020/ExtDefaultContext.cpp index ec6a98ac3fe18..5d7cde6b7536d 100644 --- a/sycl/unittests/misc/DefaultContext.cpp +++ b/sycl/unittests/SYCL2020/ExtDefaultContext.cpp @@ -1,4 +1,4 @@ -//==---- CircularBuffer.cpp ------------------------------------------------==// +//==------------------ ExtDefaultContext.cpp -------------------------------==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -8,27 +8,22 @@ #include +#include #include #include +#include #include -#include - // Same as defined in config.def inline constexpr auto EnableDefaultContextsName = "SYCL_ENABLE_DEFAULT_CONTEXTS"; -static void set_env(const char *name, const char *value) { -#ifdef _WIN32 - (void)_putenv_s(name, value); -#else - (void)setenv(name, value, /*overwrite*/ 1); -#endif -} - TEST(DefaultContextTest, DefaultContextTest) { - set_env(EnableDefaultContextsName, "1"); + using namespace sycl::detail; + using namespace sycl::unittest; + ScopedEnvVar var(EnableDefaultContextsName, "1", + SYCLConfig::reset); sycl::platform Plt1{sycl::default_selector()}; sycl::unittest::PiMock Mock1{Plt1}; @@ -51,7 +46,10 @@ TEST(DefaultContextTest, DefaultContextTest) { } TEST(DefaultContextTest, DefaultContextCanBeDisabled) { - set_env(EnableDefaultContextsName, "0"); + using namespace sycl::detail; + using namespace sycl::unittest; + ScopedEnvVar var(EnableDefaultContextsName, "0", + SYCLConfig::reset); sycl::platform Plt{sycl::default_selector()}; sycl::unittest::PiMock Mock{Plt}; diff --git a/sycl/unittests/SYCL2020/GetNativeOpenCL.cpp b/sycl/unittests/SYCL2020/GetNativeOpenCL.cpp index 343cb522faa6d..c6d5e95fa6d82 100644 --- a/sycl/unittests/SYCL2020/GetNativeOpenCL.cpp +++ b/sycl/unittests/SYCL2020/GetNativeOpenCL.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -112,6 +113,13 @@ TEST(GetNative, GetNativeHandle) { get_native(Event); // When creating a context, the piDeviceRetain is called so here is the 6 - // retain calls - ASSERT_EQ(TestCounter, 6) << "Not all the retain methods was called"; + // retain calls. However, if default contexts are enabled, that is not the + // case. + const int numRetainCalls = [] { + using namespace sycl::detail; + return SYCLConfig::get() ? 5 : 6; + }(); + + ASSERT_EQ(TestCounter, numRetainCalls) + << "Not all the retain methods was called"; } diff --git a/sycl/unittests/helpers/ScopedEnvVar.hpp b/sycl/unittests/helpers/ScopedEnvVar.hpp new file mode 100644 index 0000000000000..0f6822ac2df20 --- /dev/null +++ b/sycl/unittests/helpers/ScopedEnvVar.hpp @@ -0,0 +1,51 @@ +//==----------------------- ScopedEnvVar.hpp -------------------------------==// +// +// 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 +#include + +__SYCL_INLINE_NAMESPACE(cl) { +namespace sycl::unittest { +inline void set_env(const char *name, const char *value) { +#ifdef _WIN32 + (void)_putenv_s(name, value); +#else + (void)setenv(name, value, /*overwrite*/ 1); +#endif +} + +inline void unset_env(const char *name) { +#ifdef _WIN32 + (void)_putenv_s(name, ""); +#else + unsetenv(name); +#endif +} + +class ScopedEnvVar { +public: + ScopedEnvVar(const char *name, const char *value, + std::function configReset) + : mName(name), mConfigReset(configReset) { + set_env(name, value); + mConfigReset(); + } + + ~ScopedEnvVar() { + unset_env(mName); + mConfigReset(); + } + +private: + const char *mName; + std::function mConfigReset; +}; +} // namespace sycl::unittest +} // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/unittests/misc/CMakeLists.txt b/sycl/unittests/misc/CMakeLists.txt index 3c51e7e378cc8..523c23e606ed8 100644 --- a/sycl/unittests/misc/CMakeLists.txt +++ b/sycl/unittests/misc/CMakeLists.txt @@ -1,7 +1,6 @@ set(sycl_lib_dir $) add_definitions(-DSYCL_LIB_DIR="${sycl_lib_dir}") -add_sycl_unittest(MiscTests SHARED - DefaultContext.cpp +add_sycl_unittest(MiscSharedTests SHARED CircularBuffer.cpp KernelBuildOptions.cpp OsUtils.cpp From a97621513a173f5e32b27278ea82a469eff137a4 Mon Sep 17 00:00:00 2001 From: Alexander Batashev Date: Thu, 26 Aug 2021 13:35:23 +0300 Subject: [PATCH 38/41] Update sycl/unittests/misc/CMakeLists.txt --- sycl/unittests/misc/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/unittests/misc/CMakeLists.txt b/sycl/unittests/misc/CMakeLists.txt index 523c23e606ed8..127089d85ebaa 100644 --- a/sycl/unittests/misc/CMakeLists.txt +++ b/sycl/unittests/misc/CMakeLists.txt @@ -1,6 +1,6 @@ set(sycl_lib_dir $) add_definitions(-DSYCL_LIB_DIR="${sycl_lib_dir}") -add_sycl_unittest(MiscSharedTests SHARED +add_sycl_unittest(MiscTests SHARED CircularBuffer.cpp KernelBuildOptions.cpp OsUtils.cpp From 9f4afb74cfd18f0298d116c5eaaa6d670534ffc1 Mon Sep 17 00:00:00 2001 From: Alexander Batashev Date: Mon, 30 Aug 2021 11:54:12 +0300 Subject: [PATCH 39/41] Fix some issues --- sycl/source/detail/global_handler.cpp | 9 +++++++++ sycl/unittests/CMakeLists.txt | 1 + sycl/unittests/Extensions/CMakeLists.txt | 8 ++++++++ .../DefaultContext.cpp} | 2 +- sycl/unittests/SYCL2020/CMakeLists.txt | 1 - sycl/unittests/SYCL2020/GetNativeOpenCL.cpp | 19 +++++++------------ 6 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 sycl/unittests/Extensions/CMakeLists.txt rename sycl/unittests/{SYCL2020/ExtDefaultContext.cpp => Extensions/DefaultContext.cpp} (96%) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index 77719687efb6f..05afebb707888 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -88,7 +88,16 @@ void shutdown() { // First, release resources, that may access plugins. GlobalHandler::instance().MScheduler.Inst.reset(nullptr); GlobalHandler::instance().MProgramManager.Inst.reset(nullptr); +#ifndef _WIN32 GlobalHandler::instance().MPlatformToDefaultContextCache.Inst.reset(nullptr); +#else + // Windows does not maintain dependencies between dynamically loaded libraries + // and can unload SYCL runtime dependencies before sycl.dll's DllMain has + // finished. To avoid calls to nowhere, intentionally leak platform to device + // cache. This will prevent destructors from being called, thus no PI cleanup + // routines will be called in the end. + GlobalHandler::instance().MPlatformToDefaultContextCache.Inst.release(); +#endif GlobalHandler::instance().MPlatformCache.Inst.reset(nullptr); // Call to GlobalHandler::instance().getPlugins() initializes plugins. If diff --git a/sycl/unittests/CMakeLists.txt b/sycl/unittests/CMakeLists.txt index 8120f8ee44599..8acf5d4ed1f0d 100644 --- a/sycl/unittests/CMakeLists.txt +++ b/sycl/unittests/CMakeLists.txt @@ -18,3 +18,4 @@ add_subdirectory(scheduler) add_subdirectory(SYCL2020) add_subdirectory(thread_safety) add_subdirectory(program_manager) +add_subdirectory(Extensions) diff --git a/sycl/unittests/Extensions/CMakeLists.txt b/sycl/unittests/Extensions/CMakeLists.txt new file mode 100644 index 0000000000000..934e5ac53c784 --- /dev/null +++ b/sycl/unittests/Extensions/CMakeLists.txt @@ -0,0 +1,8 @@ +set(CMAKE_CXX_EXTENSIONS OFF) + +# Enable exception handling for these unit tests +set(LLVM_REQUIRES_EH 1) +add_sycl_unittest(ExtensionsTests OBJECT + DefaultContext.cpp +) + diff --git a/sycl/unittests/SYCL2020/ExtDefaultContext.cpp b/sycl/unittests/Extensions/DefaultContext.cpp similarity index 96% rename from sycl/unittests/SYCL2020/ExtDefaultContext.cpp rename to sycl/unittests/Extensions/DefaultContext.cpp index 5d7cde6b7536d..9552b2e4b47dc 100644 --- a/sycl/unittests/SYCL2020/ExtDefaultContext.cpp +++ b/sycl/unittests/Extensions/DefaultContext.cpp @@ -1,4 +1,4 @@ -//==------------------ ExtDefaultContext.cpp -------------------------------==// +//==--------------------- DefaultContext.cpp -------------------------------==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/sycl/unittests/SYCL2020/CMakeLists.txt b/sycl/unittests/SYCL2020/CMakeLists.txt index 43ee2e714792c..7d8be599bfb5e 100644 --- a/sycl/unittests/SYCL2020/CMakeLists.txt +++ b/sycl/unittests/SYCL2020/CMakeLists.txt @@ -3,7 +3,6 @@ set(CMAKE_CXX_EXTENSIONS OFF) # Enable exception handling for these unit tests set(LLVM_REQUIRES_EH 1) add_sycl_unittest(SYCL2020Tests OBJECT - ExtDefaultContext.cpp GetNativeOpenCL.cpp SpecConstDefaultValues.cpp KernelBundle.cpp diff --git a/sycl/unittests/SYCL2020/GetNativeOpenCL.cpp b/sycl/unittests/SYCL2020/GetNativeOpenCL.cpp index c6d5e95fa6d82..77835923dbd0f 100644 --- a/sycl/unittests/SYCL2020/GetNativeOpenCL.cpp +++ b/sycl/unittests/SYCL2020/GetNativeOpenCL.cpp @@ -10,7 +10,6 @@ #include #include -#include #include #include @@ -23,7 +22,8 @@ using namespace cl::sycl; -int TestCounter; +int TestCounter = 0; +int DeviceRetainCounter = 0; static pi_result redefinedContextRetain(pi_context c) { ++TestCounter; @@ -37,6 +37,7 @@ static pi_result redefinedQueueRetain(pi_queue c) { static pi_result redefinedDeviceRetain(pi_device c) { ++TestCounter; + ++DeviceRetainCounter; return PI_SUCCESS; } @@ -112,14 +113,8 @@ TEST(GetNative, GetNativeHandle) { get_native(Device); get_native(Event); - // When creating a context, the piDeviceRetain is called so here is the 6 - // retain calls. However, if default contexts are enabled, that is not the - // case. - const int numRetainCalls = [] { - using namespace sycl::detail; - return SYCLConfig::get() ? 5 : 6; - }(); - - ASSERT_EQ(TestCounter, numRetainCalls) - << "Not all the retain methods was called"; + // Depending on global caches state, piDeviceRetain is called either once or + // twice, so there'll be 5 or 6 calls. + ASSERT_EQ(TestCounter, 5 + DeviceRetainCounter - 1) + << "Not all the retain methods were called"; } From 12504a11872e56c7a1ee29a00f5abaa524352b20 Mon Sep 17 00:00:00 2001 From: Alexander Batashev Date: Mon, 30 Aug 2021 13:31:25 +0300 Subject: [PATCH 40/41] fix rest of the issues --- sycl/plugins/cuda/pi_cuda.cpp | 10 +------ sycl/unittests/helpers/ScopedEnvVar.hpp | 38 +++++++++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/sycl/plugins/cuda/pi_cuda.cpp b/sycl/plugins/cuda/pi_cuda.cpp index c082d213467d4..42555b7d59a29 100644 --- a/sycl/plugins/cuda/pi_cuda.cpp +++ b/sycl/plugins/cuda/pi_cuda.cpp @@ -1782,15 +1782,7 @@ pi_result cuda_piContextRelease(pi_context ctxt) { std::unique_ptr<_pi_context> context{ctxt}; - CUresult errCode = cuEventDestroy(context->evBase_); - - // Can do nothing since the CUDA driver is not responsive anymore. - // TODO: Find a better way or exttend this logic to all PI APIs that call CUDA - // API - if (CUDA_ERROR_DEINITIALIZED == errCode) - return PI_SUCCESS; - - PI_CHECK_ERROR(errCode); + PI_CHECK_ERROR(cuEventDestroy(context->evBase_)); if (!ctxt->is_primary()) { CUcontext cuCtxt = ctxt->get(); diff --git a/sycl/unittests/helpers/ScopedEnvVar.hpp b/sycl/unittests/helpers/ScopedEnvVar.hpp index 0f6822ac2df20..a078eeab2f537 100644 --- a/sycl/unittests/helpers/ScopedEnvVar.hpp +++ b/sycl/unittests/helpers/ScopedEnvVar.hpp @@ -13,39 +13,47 @@ __SYCL_INLINE_NAMESPACE(cl) { namespace sycl::unittest { -inline void set_env(const char *name, const char *value) { +inline void set_env(const char *Name, const char *Value) { #ifdef _WIN32 - (void)_putenv_s(name, value); + (void)_putenv_s(Name, Value); #else - (void)setenv(name, value, /*overwrite*/ 1); + (void)setenv(Name, Value, /*overwrite*/ 1); #endif } -inline void unset_env(const char *name) { +inline void unset_env(const char *Name) { #ifdef _WIN32 - (void)_putenv_s(name, ""); + (void)_putenv_s(Name, ""); #else - unsetenv(name); + unsetenv(Name); #endif } class ScopedEnvVar { public: - ScopedEnvVar(const char *name, const char *value, - std::function configReset) - : mName(name), mConfigReset(configReset) { - set_env(name, value); - mConfigReset(); + ScopedEnvVar(const char *Name, const char *Value, + std::function ConfigReset) + : MName(Name), MConfigReset(ConfigReset) { + if (getenv(Name)) { + MOriginalValue = std::string(getenv(Name)); + } + set_env(Name, Value); + MConfigReset(); } ~ScopedEnvVar() { - unset_env(mName); - mConfigReset(); + if (!MOriginalValue.empty()) { + set_env(MName, MOriginalValue.c_str()); + } else { + unset_env(MName); + } + MConfigReset(); } private: - const char *mName; - std::function mConfigReset; + std::string MOriginalValue; + const char *MName; + std::function MConfigReset; }; } // namespace sycl::unittest } // __SYCL_INLINE_NAMESPACE(cl) From 08c72e713866880c93d29400a673696f026e16a8 Mon Sep 17 00:00:00 2001 From: Alexander Batashev Date: Wed, 8 Sep 2021 13:13:19 +0300 Subject: [PATCH 41/41] Update sycl/source/detail/queue_impl.hpp Co-authored-by: Alexey Bader --- sycl/source/detail/queue_impl.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/queue_impl.hpp b/sycl/source/detail/queue_impl.hpp index d46cf7e2db420..1f1b678885b46 100644 --- a/sycl/source/detail/queue_impl.hpp +++ b/sycl/source/detail/queue_impl.hpp @@ -52,7 +52,7 @@ enum QueueOrder { Ordered, OOO }; class queue_impl { public: // \return a default context for the platform if it includes the device - // passed and defaualt contexts are enabled, a new context otherwise. + // passed and default contexts are enabled, a new context otherwise. static ContextImplPtr getDefaultOrNew(const DeviceImplPtr &Device) { if (!SYCLConfig::get()) return detail::getSyclObjImpl(