Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions include/alpaka/dev/DevCpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma once

#include "alpaka/dev/Traits.hpp"
#include "alpaka/dev/common/DeviceProperties.hpp"
#include "alpaka/dev/common/QueueRegistry.hpp"
#include "alpaka/dev/cpu/SysInfo.hpp"
#include "alpaka/mem/buf/Traits.hpp"
Expand Down Expand Up @@ -57,7 +58,10 @@ namespace alpaka
friend struct trait::GetDevByIdx<PlatformCpu>;

protected:
DevCpu() : m_spDevCpuImpl(std::make_shared<cpu::detail::DevCpuImpl>())
DevCpu()
: m_spDevCpuImpl(std::make_shared<cpu::detail::DevCpuImpl>())
, m_deviceProperties(std::make_shared<alpaka::DeviceProperties>())
, m_mutex(std::make_shared<std::mutex>())
{
}

Expand Down Expand Up @@ -89,8 +93,16 @@ namespace alpaka
return 0;
}

friend struct trait::GetName<DevCpu>;
friend struct trait::GetMemBytes<DevCpu>;
friend struct trait::GetFreeMemBytes<DevCpu>;
friend struct trait::GetWarpSizes<DevCpu>;
friend struct trait::GetPreferredWarpSize<DevCpu>;

private:
std::shared_ptr<cpu::detail::DevCpuImpl> m_spDevCpuImpl;
std::shared_ptr<alpaka::DeviceProperties> m_deviceProperties;
std::shared_ptr<std::mutex> m_mutex;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move these into the cpu::detail::DevCpuImpl class ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, done

};

namespace trait
Expand All @@ -99,29 +111,50 @@ namespace alpaka
template<>
struct GetName<DevCpu>
{
ALPAKA_FN_HOST static auto getName(DevCpu const& /* dev */) -> std::string
ALPAKA_FN_HOST static auto getName(DevCpu const& dev) -> std::string
{
return cpu::detail::getCpuName();
{
std::lock_guard<std::mutex> lock(*dev.m_mutex);
if(!dev.m_deviceProperties->name.has_value())
{
dev.m_deviceProperties->name = cpu::detail::getCpuName();
}
}
return dev.m_deviceProperties->name.value();
}
};

//! The CPU device available memory get trait specialization.
template<>
struct GetMemBytes<DevCpu>
{
ALPAKA_FN_HOST static auto getMemBytes(DevCpu const& /* dev */) -> std::size_t
ALPAKA_FN_HOST static auto getMemBytes(DevCpu const& dev) -> std::size_t
{
return cpu::detail::getTotalGlobalMemSizeBytes();
{
std::lock_guard<std::mutex> lock(*dev.m_mutex);
if(!dev.m_deviceProperties->totalGlobalMem.has_value())
{
dev.m_deviceProperties->totalGlobalMem = cpu::detail::getTotalGlobalMemSizeBytes();
}
}
return dev.m_deviceProperties->totalGlobalMem.value();
}
};

//! The CPU device free memory get trait specialization.
template<>
struct GetFreeMemBytes<DevCpu>
{
ALPAKA_FN_HOST static auto getFreeMemBytes(DevCpu const& /* dev */) -> std::size_t
ALPAKA_FN_HOST static auto getFreeMemBytes(DevCpu const& dev) -> std::size_t
{
return cpu::detail::getFreeGlobalMemSizeBytes();
{
std::lock_guard<std::mutex> lock(*dev.m_mutex);
if(!dev.m_deviceProperties->freeGlobalMem.has_value())
{
dev.m_deviceProperties->freeGlobalMem = cpu::detail::getFreeGlobalMemSizeBytes();
}
}
return dev.m_deviceProperties->freeGlobalMem.value();
}
};

Expand Down
62 changes: 49 additions & 13 deletions include/alpaka/dev/DevGenericSycl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "alpaka/core/Common.hpp"
#include "alpaka/core/Sycl.hpp"
#include "alpaka/dev/Traits.hpp"
#include "alpaka/dev/common/DeviceProperties.hpp"
#include "alpaka/mem/buf/Traits.hpp"
#include "alpaka/platform/Traits.hpp"
#include "alpaka/queue/Properties.hpp"
Expand Down Expand Up @@ -131,6 +132,8 @@ namespace alpaka
public:
DevGenericSycl(sycl::device device, sycl::context context)
: m_impl{std::make_shared<detail::DevGenericSyclImpl>(std::move(device), std::move(context))}
, m_deviceProperties{std::make_shared<alpaka::DeviceProperties>()}
, m_mutex{std::make_shared<std::mutex>()}
{
}

Expand All @@ -150,6 +153,8 @@ namespace alpaka
}

std::shared_ptr<detail::DevGenericSyclImpl> m_impl;
std::shared_ptr<alpaka::DeviceProperties> m_deviceProperties;
std::shared_ptr<std::mutex> m_mutex;
};

namespace trait
Expand All @@ -160,8 +165,15 @@ namespace alpaka
{
static auto getName(DevGenericSycl<TTag> const& dev) -> std::string
{
auto const device = dev.getNativeHandle().first;
return device.template get_info<sycl::info::device::name>();
{
std::lock_guard<std::mutex> lock(*dev.m_mutex);
if(!dev.m_deviceProperties->name.has_value())
{
auto const device = dev.getNativeHandle().first;
dev.m_deviceProperties->name = device.template get_info<sycl::info::device::name>();
}
}
return dev.m_deviceProperties->name.value();
}
};

Expand All @@ -171,8 +183,16 @@ namespace alpaka
{
static auto getMemBytes(DevGenericSycl<TTag> const& dev) -> std::size_t
{
auto const device = dev.getNativeHandle().first;
return device.template get_info<sycl::info::device::global_mem_size>();
{
std::lock_guard<std::mutex> lock(*dev.m_mutex);
if(!dev.m_deviceProperties->totalGlobalMem.has_value())
{
auto const device = dev.getNativeHandle().first;
dev.m_deviceProperties->totalGlobalMem
= device.template get_info<sycl::info::device::global_mem_size>();
}
}
return dev.m_deviceProperties->totalGlobalMem.value();
}
};

Expand All @@ -195,15 +215,24 @@ namespace alpaka
{
static auto getWarpSizes(DevGenericSycl<TTag> const& dev) -> std::vector<std::size_t>
{
auto const device = dev.getNativeHandle().first;
std::vector<std::size_t> warp_sizes = device.template get_info<sycl::info::device::sub_group_sizes>();
// The CPU runtime supports a sub-group size of 64, but the SYCL implementation currently does not
auto find64 = std::find(warp_sizes.begin(), warp_sizes.end(), 64);
if(find64 != warp_sizes.end())
warp_sizes.erase(find64);
// Sort the warp sizes in decreasing order
std::sort(warp_sizes.begin(), warp_sizes.end(), std::greater<>{});
return warp_sizes;
{
std::lock_guard<std::mutex> lock(*dev.m_mutex);
if(!dev.m_deviceProperties->warpSizes.has_value())
{
auto const device = dev.getNativeHandle().first;
std::vector<std::size_t> warp_sizes
= device.template get_info<sycl::info::device::sub_group_sizes>();
// The CPU runtime supports a sub-group size of 64, but the SYCL implementation currently does
// not
auto find64 = std::find(warp_sizes.begin(), warp_sizes.end(), 64);
if(find64 != warp_sizes.end())
warp_sizes.erase(find64);
// Sort the warp sizes in decreasing order
std::sort(warp_sizes.begin(), warp_sizes.end(), std::greater<>{});
dev.m_deviceProperties->warpSizes = std::move(warp_sizes);
}
}
return dev.m_deviceProperties->warpSizes.value();
}
};

Expand All @@ -213,6 +242,13 @@ namespace alpaka
{
static auto getPreferredWarpSize(DevGenericSycl<TTag> const& dev) -> std::size_t
{
{
std::lock_guard<std::mutex> lock(*dev.m_mutex);
if(dev.m_deviceProperties->warpSizes.has_value())
{
return dev.m_deviceProperties->warpSizes.value().front();
}
}
return GetWarpSizes<DevGenericSycl<TTag>>::getWarpSizes(dev).front();
}
};
Expand Down
106 changes: 81 additions & 25 deletions include/alpaka/dev/DevUniformCudaHipRt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "alpaka/core/Hip.hpp"
#include "alpaka/core/Interface.hpp"
#include "alpaka/dev/Traits.hpp"
#include "alpaka/dev/common/DeviceProperties.hpp"
#include "alpaka/dev/common/QueueRegistry.hpp"
#include "alpaka/mem/buf/Traits.hpp"
#include "alpaka/platform/Traits.hpp"
Expand All @@ -20,6 +21,7 @@
#include "alpaka/wait/Traits.hpp"

#include <cstddef>
#include <mutex>
#include <string>
#include <vector>

Expand Down Expand Up @@ -62,7 +64,10 @@ namespace alpaka
using IDeviceQueue = uniform_cuda_hip::detail::QueueUniformCudaHipRtImpl<TApi>;

protected:
DevUniformCudaHipRt() : m_QueueRegistry{std::make_shared<alpaka::detail::QueueRegistry<IDeviceQueue>>()}
DevUniformCudaHipRt()
: m_QueueRegistry{std::make_shared<alpaka::detail::QueueRegistry<IDeviceQueue>>()}
, m_deviceProperties{std::make_shared<alpaka::DeviceProperties>()}
, m_mutex(std::make_shared<std::mutex>())
{
}

Expand Down Expand Up @@ -94,16 +99,26 @@ namespace alpaka
m_QueueRegistry->registerQueue(spQueue);
}

friend struct trait::GetName<DevUniformCudaHipRt<TApi>>;
friend struct trait::GetMemBytes<DevUniformCudaHipRt<TApi>>;
friend struct trait::GetFreeMemBytes<DevUniformCudaHipRt<TApi>>;
friend struct trait::GetWarpSizes<DevUniformCudaHipRt<TApi>>;
friend struct trait::GetPreferredWarpSize<DevUniformCudaHipRt<TApi>>;

private:
DevUniformCudaHipRt(int iDevice)
: m_iDevice(iDevice)
, m_QueueRegistry(std::make_shared<alpaka::detail::QueueRegistry<IDeviceQueue>>())
, m_deviceProperties(std::make_shared<alpaka::DeviceProperties>())
, m_mutex(std::make_shared<std::mutex>())
{
}

int m_iDevice;

std::shared_ptr<alpaka::detail::QueueRegistry<IDeviceQueue>> m_QueueRegistry;
std::shared_ptr<alpaka::DeviceProperties> m_deviceProperties;
std::shared_ptr<std::mutex> m_mutex;
};

namespace trait
Expand All @@ -114,12 +129,19 @@ namespace alpaka
{
ALPAKA_FN_HOST static auto getName(DevUniformCudaHipRt<TApi> const& dev) -> std::string
{
// There is cuda/hip-DeviceGetAttribute as faster alternative to cuda/hip-GetDeviceProperties to get a
// single device property but it has no option to get the name
typename TApi::DeviceProp_t devProp;
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::getDeviceProperties(&devProp, dev.getNativeHandle()));

return std::string(devProp.name);
{
std::lock_guard<std::mutex> lock(*dev.m_mutex);
if(!dev.m_deviceProperties->name.has_value())
{
// There is cuda/hip-DeviceGetAttribute as faster alternative to cuda/hip-GetDeviceProperties
// to get a single device property but it has no option to get the name
typename TApi::DeviceProp_t devProp;
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::getDeviceProperties(&devProp, dev.getNativeHandle()));
dev.m_deviceProperties->name = std::string(devProp.name);
}
}

return dev.m_deviceProperties->name.value();
}
};

Expand All @@ -129,15 +151,24 @@ namespace alpaka
{
ALPAKA_FN_HOST static auto getMemBytes(DevUniformCudaHipRt<TApi> const& dev) -> std::size_t
{
// Set the current device to wait for.
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::setDevice(dev.getNativeHandle()));
{
std::lock_guard<std::mutex> lock(*dev.m_mutex);
if(!dev.m_deviceProperties->freeGlobalMem.has_value())
{
// Set the current device to wait for.
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::setDevice(dev.getNativeHandle()));

std::size_t freeInternal(0u);
std::size_t totalInternal(0u);

std::size_t freeInternal(0u);
std::size_t totalInternal(0u);
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::memGetInfo(&freeInternal, &totalInternal));

ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::memGetInfo(&freeInternal, &totalInternal));
dev.m_deviceProperties->totalGlobalMem = totalInternal;
dev.m_deviceProperties->freeGlobalMem = freeInternal;
}
}

return totalInternal;
return dev.m_deviceProperties->totalGlobalMem.value();
}
};

Expand All @@ -147,15 +178,24 @@ namespace alpaka
{
ALPAKA_FN_HOST static auto getFreeMemBytes(DevUniformCudaHipRt<TApi> const& dev) -> std::size_t
{
// Set the current device to wait for.
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::setDevice(dev.getNativeHandle()));
{
std::lock_guard<std::mutex> lock(*dev.m_mutex);
if(!dev.m_deviceProperties->totalGlobalMem.has_value())
{
// Set the current device to wait for.
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::setDevice(dev.getNativeHandle()));

std::size_t freeInternal(0u);
std::size_t totalInternal(0u);
std::size_t freeInternal(0u);
std::size_t totalInternal(0u);

ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::memGetInfo(&freeInternal, &totalInternal));
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::memGetInfo(&freeInternal, &totalInternal));

return freeInternal;
dev.m_deviceProperties->totalGlobalMem = totalInternal;
dev.m_deviceProperties->freeGlobalMem = freeInternal;
}
}

return dev.m_deviceProperties->freeGlobalMem.value();
}
};

Expand All @@ -165,7 +205,15 @@ namespace alpaka
{
ALPAKA_FN_HOST static auto getWarpSizes(DevUniformCudaHipRt<TApi> const& dev) -> std::vector<std::size_t>
{
return {GetPreferredWarpSize<DevUniformCudaHipRt<TApi>>::getPreferredWarpSize(dev)};
{
std::lock_guard<std::mutex> lock(*dev.m_mutex);
if(!dev.m_deviceProperties->warpSizes.has_value())
{
dev.m_deviceProperties->warpSizes = std::vector<std::size_t>{
GetPreferredWarpSize<DevUniformCudaHipRt<TApi>>::getPreferredWarpSize(dev)};
}
}
return dev.m_deviceProperties->warpSizes.value();
}
};

Expand All @@ -175,11 +223,19 @@ namespace alpaka
{
ALPAKA_FN_HOST static auto getPreferredWarpSize(DevUniformCudaHipRt<TApi> const& dev) -> std::size_t
{
int warpSize = 0;

ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(
TApi::deviceGetAttribute(&warpSize, TApi::deviceAttributeWarpSize, dev.getNativeHandle()));
return static_cast<std::size_t>(warpSize);
{
std::lock_guard<std::mutex> lock(*dev.m_mutex);
if(!dev.m_deviceProperties->preferredWarpSize.has_value())
{
int warpSize = 0;

ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(
TApi::deviceGetAttribute(&warpSize, TApi::deviceAttributeWarpSize, dev.getNativeHandle()));
dev.m_deviceProperties->preferredWarpSize = static_cast<std::size_t>(warpSize);
}
}

return dev.m_deviceProperties->preferredWarpSize.value();
}
};

Expand Down
Loading