Skip to content

Commit

Permalink
DO NOT MERGE: minimal revert of #2020 and #1988 for the SYCL platform
Browse files Browse the repository at this point in the history
  • Loading branch information
fwyzard committed Jul 26, 2023
1 parent 4492633 commit 7eff280
Showing 1 changed file with 91 additions and 52 deletions.
143 changes: 91 additions & 52 deletions include/alpaka/pltf/PltfGenericSycl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#if ALPAKA_DEBUG >= ALPAKA_DEBUG_MINIMAL
# include <iostream>
#endif
#include <mutex>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <vector>
Expand All @@ -26,67 +28,104 @@ namespace alpaka
{
//! The SYCL device manager.
template<typename TSelector>
struct PltfGenericSycl : concepts::Implements<ConceptPltf, PltfGenericSycl<TSelector>>
class PltfGenericSycl : public concepts::Implements<ConceptPltf, PltfGenericSycl<TSelector>>
{
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());
}}}
{
}
public:
PltfGenericSycl() = default;

[[nodiscard]] auto syclPlatform() -> sycl::platform&
[[nodiscard]] static auto syclPlatform() -> sycl::platform&
{
return platform;
}
auto lock = std::scoped_lock<std::mutex>{mutex};

[[nodiscard]] auto syclPlatform() const -> sycl::platform const&
{
return platform;
if(!initialized)
do_initialization();

return *platform_opt;
}

[[nodiscard]] auto syclDevices() -> std::vector<sycl::device>&
[[nodiscard]] static auto syclDevices() -> std::vector<sycl::device>&
{
auto lock = std::scoped_lock<std::mutex>{mutex};

if(!initialized)
do_initialization();

return devices;
}

[[nodiscard]] auto syclDevices() const -> std::vector<sycl::device> const&
[[nodiscard]] static auto syclContext() -> sycl::context&
{
return devices;
auto lock = std::scoped_lock<std::mutex>{mutex};

if(!initialized)
do_initialization();

return *context_opt;
}

[[nodiscard]] auto syclContext() -> sycl::context&
static auto initialize() -> void
{
return context;
auto lock = std::scoped_lock<std::mutex>{mutex};
do_initialization();
}

[[nodiscard]] auto syclContext() const -> sycl::context const&
static auto reset() -> void
{
return context;
auto lock = std::scoped_lock<std::mutex>{mutex};

if(!initialized)
return;

context_opt.reset();
devices.clear();
platform_opt.reset();
initialized = false;
}

private:
sycl::platform platform;
std::vector<sycl::device> 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<sycl::platform> platform_opt{std::nullopt};
inline static std::vector<sycl::device> devices;
inline static std::optional<sycl::context> context_opt{std::nullopt};
# if BOOST_COMP_CLANG
# pragma clang diagnostic pop
# endif
};
} // namespace alpaka

Expand All @@ -96,41 +135,41 @@ namespace alpaka::trait
template<typename TSelector>
struct GetDevCount<PltfGenericSycl<TSelector>>
{
static auto getDevCount(PltfGenericSycl<TSelector> const& platform) -> std::size_t
static auto getDevCount(PltfGenericSycl<TSelector> const&) -> std::size_t
{
ALPAKA_DEBUG_FULL_LOG_SCOPE;

return platform.syclDevices().size();
return PltfGenericSycl<TSelector>::syclDevices().size();
}
};

//! The SYCL platform device get trait specialization.
template<typename TSelector>
struct GetDevByIdx<alpaka::PltfGenericSycl<TSelector>>
struct GetDevByIdx<PltfGenericSycl<TSelector>>
{
static auto getDevByIdx(PltfGenericSycl<TSelector> const& platform, std::size_t const& devIdx)
static auto getDevByIdx(PltfGenericSycl<TSelector> const&, std::size_t const& devIdx)
{
ALPAKA_DEBUG_FULL_LOG_SCOPE;

auto const& devices = platform.syclDevices();
if(devIdx >= devices.size())
using SyclPltf = PltfGenericSycl<TSelector>;
auto const dev_num = SyclPltf::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 = SyclPltf::syclDevices().at(devIdx);

// Log this device.
# if ALPAKA_DEBUG >= ALPAKA_DEBUG_FULL
printDeviceProperties(sycl_dev);
# elif ALPAKA_DEBUG >= ALPAKA_DEBUG_MINIMAL
std::cout << __func__ << sycl_dev.template get_info<sycl::info::device::name>() << '\n';
# endif
using SyclPltf = alpaka::PltfGenericSycl<TSelector>;
return typename DevType<SyclPltf>::type{sycl_dev, platform.syclContext()};
return typename DevType<SyclPltf>::type{sycl_dev, SyclPltf::syclContext()};
}

private:
Expand Down

0 comments on commit 7eff280

Please sign in to comment.