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 a326eea
Showing 1 changed file with 86 additions and 35 deletions.
121 changes: 86 additions & 35 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 @@ -29,64 +31,113 @@ namespace alpaka
struct PltfGenericSycl : 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());
}}}
{
}

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

if(!initialized)
do_initialization();

return *platform_opt;
}

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

if(!initialized)
do_initialization();

return *platform_opt;
}

[[nodiscard]] 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&
{
auto lock = std::scoped_lock<std::mutex>{mutex};

if(!initialized)
do_initialization();

return devices;
}

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

if(!initialized)
do_initialization();

return *context_opt;
}

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

if(!initialized)
do_initialization();

return *context_opt;
}

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 @@ -106,30 +157,30 @@ namespace alpaka::trait

//! 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)
{
ALPAKA_DEBUG_FULL_LOG_SCOPE;

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

Expand Down

0 comments on commit a326eea

Please sign in to comment.