From a326eeaada649b3515ea992959459d0960c1b1a1 Mon Sep 17 00:00:00 2001 From: Andrea Bocci Date: Wed, 26 Jul 2023 13:37:02 +0200 Subject: [PATCH] DO NOT MERGE: minimal revert of #2020 and #1988 for the SYCL platform --- include/alpaka/pltf/PltfGenericSycl.hpp | 121 +++++++++++++++++------- 1 file changed, 86 insertions(+), 35 deletions(-) diff --git a/include/alpaka/pltf/PltfGenericSycl.hpp b/include/alpaka/pltf/PltfGenericSycl.hpp index 8c67f5f50c31..c2aeec69e3ce 100644 --- a/include/alpaka/pltf/PltfGenericSycl.hpp +++ b/include/alpaka/pltf/PltfGenericSycl.hpp @@ -14,6 +14,8 @@ #if ALPAKA_DEBUG >= ALPAKA_DEBUG_MINIMAL # include #endif +#include +#include #include #include #include @@ -29,64 +31,113 @@ namespace alpaka struct PltfGenericSycl : concepts::Implements> { PltfGenericSycl() - : platform{TSelector{}} - , devices(platform.get_devices()) - , context{sycl::context{ - devices, - [](sycl::exception_list exceptions) - { - auto ss_err = std::stringstream{}; - ss_err << "Caught asynchronous SYCL exception(s):\n"; - for(std::exception_ptr e : exceptions) - { - try - { - std::rethrow_exception(e); - } - catch(sycl::exception const& err) - { - ss_err << err.what() << " (" << err.code() << ")\n"; - } - } - throw std::runtime_error(ss_err.str()); - }}} { } [[nodiscard]] auto syclPlatform() -> sycl::platform& { - return platform; + auto lock = std::scoped_lock{mutex}; + + if(!initialized) + do_initialization(); + + return *platform_opt; } [[nodiscard]] auto syclPlatform() const -> sycl::platform const& { - return platform; + auto lock = std::scoped_lock{mutex}; + + if(!initialized) + do_initialization(); + + return *platform_opt; } [[nodiscard]] auto syclDevices() -> std::vector& { + auto lock = std::scoped_lock{mutex}; + + if(!initialized) + do_initialization(); + return devices; } [[nodiscard]] auto syclDevices() const -> std::vector const& { + auto lock = std::scoped_lock{mutex}; + + if(!initialized) + do_initialization(); + return devices; } [[nodiscard]] auto syclContext() -> sycl::context& { - return context; + auto lock = std::scoped_lock{mutex}; + + if(!initialized) + do_initialization(); + + return *context_opt; } [[nodiscard]] auto syclContext() const -> sycl::context const& { - return context; + auto lock = std::scoped_lock{mutex}; + + if(!initialized) + do_initialization(); + + return *context_opt; } private: - sycl::platform platform; - std::vector devices; - sycl::context context; + static auto do_initialization() + { + if(initialized) + return; + + platform_opt = sycl::platform{TSelector{}}; + devices = platform_opt->get_devices(); + context_opt = sycl::context{ + devices, + [](sycl::exception_list exceptions) + { + auto ss_err = std::stringstream{}; + ss_err << "Caught asynchronous SYCL exception(s):\n"; + for(std::exception_ptr e : exceptions) + { + try + { + std::rethrow_exception(e); + } + catch(sycl::exception const& err) + { + ss_err << err.what() << " (" << err.code() << ")\n"; + } + } + throw std::runtime_error(ss_err.str()); + }}; + + initialized = true; + } + +# if BOOST_COMP_CLANG +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wexit-time-destructors" +# endif + inline static std::mutex mutex; + inline static bool initialized{false}; + + inline static std::optional platform_opt{std::nullopt}; + inline static std::vector devices; + inline static std::optional context_opt{std::nullopt}; +# if BOOST_COMP_CLANG +# pragma clang diagnostic pop +# endif }; } // namespace alpaka @@ -106,22 +157,23 @@ namespace alpaka::trait //! The SYCL platform device get trait specialization. template - struct GetDevByIdx> + struct GetDevByIdx> { static auto getDevByIdx(PltfGenericSycl const& platform, std::size_t const& devIdx) { ALPAKA_DEBUG_FULL_LOG_SCOPE; - auto const& devices = platform.syclDevices(); - if(devIdx >= devices.size()) + using SyclPltf = PltfGenericSycl; + auto const dev_num = platform.syclDevices().size(); + if(devIdx >= dev_num) { auto ss_err = std::stringstream{}; - ss_err << "Unable to return device handle for device " << devIdx << ". There are only " - << devices.size() << " SYCL devices!"; + ss_err << "Unable to return device handle for device " << devIdx << ". There are only " << dev_num + << " SYCL devices!"; throw std::runtime_error(ss_err.str()); } - auto sycl_dev = devices.at(devIdx); + auto sycl_dev = platform.syclDevices().at(devIdx); // Log this device. # if ALPAKA_DEBUG >= ALPAKA_DEBUG_FULL @@ -129,7 +181,6 @@ namespace alpaka::trait # elif ALPAKA_DEBUG >= ALPAKA_DEBUG_MINIMAL std::cout << __func__ << sycl_dev.template get_info() << '\n'; # endif - using SyclPltf = alpaka::PltfGenericSycl; return typename DevType::type{sycl_dev, platform.syclContext()}; }