Skip to content

Commit e49e604

Browse files
committed
Move device properties inside QueueRegistry
1 parent 9d5e977 commit e49e604

File tree

5 files changed

+72
-75
lines changed

5 files changed

+72
-75
lines changed

include/alpaka/dev/DevCpu.hpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#pragma once
77

88
#include "alpaka/dev/Traits.hpp"
9-
#include "alpaka/dev/common/DeviceProperties.hpp"
109
#include "alpaka/dev/common/QueueRegistry.hpp"
1110
#include "alpaka/dev/cpu/SysInfo.hpp"
1211
#include "alpaka/mem/buf/Traits.hpp"
@@ -58,10 +57,7 @@ namespace alpaka
5857
friend struct trait::GetDevByIdx<PlatformCpu>;
5958

6059
protected:
61-
DevCpu()
62-
: m_spDevCpuImpl(std::make_shared<cpu::detail::DevCpuImpl>())
63-
, m_deviceProperties(std::make_shared<alpaka::DeviceProperties>())
64-
, m_mutex(std::make_shared<std::mutex>())
60+
DevCpu() : m_spDevCpuImpl(std::make_shared<cpu::detail::DevCpuImpl>())
6561
{
6662
}
6763

@@ -101,8 +97,6 @@ namespace alpaka
10197

10298
private:
10399
std::shared_ptr<cpu::detail::DevCpuImpl> m_spDevCpuImpl;
104-
std::shared_ptr<alpaka::DeviceProperties> m_deviceProperties;
105-
std::shared_ptr<std::mutex> m_mutex;
106100
};
107101

108102
namespace trait
@@ -114,13 +108,13 @@ namespace alpaka
114108
ALPAKA_FN_HOST static auto getName(DevCpu const& dev) -> std::string
115109
{
116110
{
117-
std::lock_guard<std::mutex> lock(*dev.m_mutex);
118-
if(!dev.m_deviceProperties->name.has_value())
111+
std::lock_guard<std::mutex> lock(dev.m_spDevCpuImpl->mutex());
112+
if(!dev.m_spDevCpuImpl->deviceProperties().name.has_value())
119113
{
120-
dev.m_deviceProperties->name = cpu::detail::getCpuName();
114+
dev.m_spDevCpuImpl->deviceProperties().name = cpu::detail::getCpuName();
121115
}
122116
}
123-
return dev.m_deviceProperties->name.value();
117+
return dev.m_spDevCpuImpl->deviceProperties().name.value();
124118
}
125119
};
126120

@@ -131,30 +125,24 @@ namespace alpaka
131125
ALPAKA_FN_HOST static auto getMemBytes(DevCpu const& dev) -> std::size_t
132126
{
133127
{
134-
std::lock_guard<std::mutex> lock(*dev.m_mutex);
135-
if(!dev.m_deviceProperties->totalGlobalMem.has_value())
128+
std::lock_guard<std::mutex> lock(dev.m_spDevCpuImpl->mutex());
129+
if(!dev.m_spDevCpuImpl->deviceProperties().totalGlobalMem.has_value())
136130
{
137-
dev.m_deviceProperties->totalGlobalMem = cpu::detail::getTotalGlobalMemSizeBytes();
131+
dev.m_spDevCpuImpl->deviceProperties().totalGlobalMem
132+
= cpu::detail::getTotalGlobalMemSizeBytes();
138133
}
139134
}
140-
return dev.m_deviceProperties->totalGlobalMem.value();
135+
return dev.m_spDevCpuImpl->deviceProperties().totalGlobalMem.value();
141136
}
142137
};
143138

144139
//! The CPU device free memory get trait specialization.
145140
template<>
146141
struct GetFreeMemBytes<DevCpu>
147142
{
148-
ALPAKA_FN_HOST static auto getFreeMemBytes(DevCpu const& dev) -> std::size_t
143+
ALPAKA_FN_HOST static auto getFreeMemBytes(DevCpu const& /* dev */) -> std::size_t
149144
{
150-
{
151-
std::lock_guard<std::mutex> lock(*dev.m_mutex);
152-
if(!dev.m_deviceProperties->freeGlobalMem.has_value())
153-
{
154-
dev.m_deviceProperties->freeGlobalMem = cpu::detail::getFreeGlobalMemSizeBytes();
155-
}
156-
}
157-
return dev.m_deviceProperties->freeGlobalMem.value();
145+
return cpu::detail::getFreeGlobalMemSizeBytes();
158146
}
159147
};
160148

include/alpaka/dev/DevGenericSycl.hpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,21 @@ namespace alpaka
113113
return m_context;
114114
}
115115

116+
std::shared_mutex& mutex()
117+
{
118+
return m_mutex;
119+
}
120+
121+
alpaka::DeviceProperties& deviceProperties()
122+
{
123+
return m_deviceProperties;
124+
}
125+
116126
private:
117127
sycl::device m_device;
118128
sycl::context m_context;
119129
std::vector<std::weak_ptr<QueueGenericSyclImpl>> m_queues;
130+
alpaka::DeviceProperties m_deviceProperties;
120131
std::shared_mutex mutable m_mutex;
121132
};
122133
} // namespace detail
@@ -132,8 +143,6 @@ namespace alpaka
132143
public:
133144
DevGenericSycl(sycl::device device, sycl::context context)
134145
: m_impl{std::make_shared<detail::DevGenericSyclImpl>(std::move(device), std::move(context))}
135-
, m_deviceProperties{std::make_shared<alpaka::DeviceProperties>()}
136-
, m_mutex{std::make_shared<std::mutex>()}
137146
{
138147
}
139148

@@ -153,8 +162,6 @@ namespace alpaka
153162
}
154163

155164
std::shared_ptr<detail::DevGenericSyclImpl> m_impl;
156-
std::shared_ptr<alpaka::DeviceProperties> m_deviceProperties;
157-
std::shared_ptr<std::mutex> m_mutex;
158165
};
159166

160167
namespace trait
@@ -166,14 +173,14 @@ namespace alpaka
166173
static auto getName(DevGenericSycl<TTag> const& dev) -> std::string
167174
{
168175
{
169-
std::lock_guard<std::mutex> lock(*dev.m_mutex);
170-
if(!dev.m_deviceProperties->name.has_value())
176+
std::lock_guard<std::shared_mutex> lock(dev.m_impl->mutex());
177+
if(!dev.m_impl->deviceProperties().name.has_value())
171178
{
172179
auto const device = dev.getNativeHandle().first;
173-
dev.m_deviceProperties->name = device.template get_info<sycl::info::device::name>();
180+
dev.m_impl->deviceProperties().name = device.template get_info<sycl::info::device::name>();
174181
}
175182
}
176-
return dev.m_deviceProperties->name.value();
183+
return dev.m_impl->deviceProperties().name.value();
177184
}
178185
};
179186

@@ -184,15 +191,15 @@ namespace alpaka
184191
static auto getMemBytes(DevGenericSycl<TTag> const& dev) -> std::size_t
185192
{
186193
{
187-
std::lock_guard<std::mutex> lock(*dev.m_mutex);
188-
if(!dev.m_deviceProperties->totalGlobalMem.has_value())
194+
std::lock_guard<std::shared_mutex> lock(dev.m_impl->mutex());
195+
if(!dev.m_impl->deviceProperties().totalGlobalMem.has_value())
189196
{
190197
auto const device = dev.getNativeHandle().first;
191-
dev.m_deviceProperties->totalGlobalMem
198+
dev.m_impl->deviceProperties().totalGlobalMem
192199
= device.template get_info<sycl::info::device::global_mem_size>();
193200
}
194201
}
195-
return dev.m_deviceProperties->totalGlobalMem.value();
202+
return dev.m_impl->deviceProperties().totalGlobalMem.value();
196203
}
197204
};
198205

@@ -216,8 +223,8 @@ namespace alpaka
216223
static auto getWarpSizes(DevGenericSycl<TTag> const& dev) -> std::vector<std::size_t>
217224
{
218225
{
219-
std::lock_guard<std::mutex> lock(*dev.m_mutex);
220-
if(!dev.m_deviceProperties->warpSizes.has_value())
226+
std::lock_guard<std::shared_mutex> lock(dev.m_impl->mutex());
227+
if(!dev.m_impl->deviceProperties().warpSizes.has_value())
221228
{
222229
auto const device = dev.getNativeHandle().first;
223230
std::vector<std::size_t> warp_sizes
@@ -229,10 +236,10 @@ namespace alpaka
229236
warp_sizes.erase(find64);
230237
// Sort the warp sizes in decreasing order
231238
std::sort(warp_sizes.begin(), warp_sizes.end(), std::greater<>{});
232-
dev.m_deviceProperties->warpSizes = std::move(warp_sizes);
239+
dev.m_impl->deviceProperties().warpSizes = std::move(warp_sizes);
233240
}
234241
}
235-
return dev.m_deviceProperties->warpSizes.value();
242+
return dev.m_impl->deviceProperties().warpSizes.value();
236243
}
237244
};
238245

@@ -243,10 +250,10 @@ namespace alpaka
243250
static auto getPreferredWarpSize(DevGenericSycl<TTag> const& dev) -> std::size_t
244251
{
245252
{
246-
std::lock_guard<std::mutex> lock(*dev.m_mutex);
247-
if(dev.m_deviceProperties->warpSizes.has_value())
253+
std::lock_guard<std::shared_mutex> lock(dev.m_impl->mutex());
254+
if(dev.m_impl->deviceProperties().warpSizes.has_value())
248255
{
249-
return dev.m_deviceProperties->warpSizes.value().front();
256+
return dev.m_impl->deviceProperties().warpSizes.value().front();
250257
}
251258
}
252259
return GetWarpSizes<DevGenericSycl<TTag>>::getWarpSizes(dev).front();

include/alpaka/dev/DevUniformCudaHipRt.hpp

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ namespace alpaka
6464
using IDeviceQueue = uniform_cuda_hip::detail::QueueUniformCudaHipRtImpl<TApi>;
6565

6666
protected:
67-
DevUniformCudaHipRt()
68-
: m_QueueRegistry{std::make_shared<alpaka::detail::QueueRegistry<IDeviceQueue>>()}
69-
, m_deviceProperties{std::make_shared<alpaka::DeviceProperties>()}
70-
, m_mutex(std::make_shared<std::mutex>())
67+
DevUniformCudaHipRt() : m_QueueRegistry{std::make_shared<alpaka::detail::QueueRegistry<IDeviceQueue>>()}
7168
{
7269
}
7370

@@ -109,16 +106,12 @@ namespace alpaka
109106
DevUniformCudaHipRt(int iDevice)
110107
: m_iDevice(iDevice)
111108
, m_QueueRegistry(std::make_shared<alpaka::detail::QueueRegistry<IDeviceQueue>>())
112-
, m_deviceProperties(std::make_shared<alpaka::DeviceProperties>())
113-
, m_mutex(std::make_shared<std::mutex>())
114109
{
115110
}
116111

117112
int m_iDevice;
118113

119114
std::shared_ptr<alpaka::detail::QueueRegistry<IDeviceQueue>> m_QueueRegistry;
120-
std::shared_ptr<alpaka::DeviceProperties> m_deviceProperties;
121-
std::shared_ptr<std::mutex> m_mutex;
122115
};
123116

124117
namespace trait
@@ -130,18 +123,18 @@ namespace alpaka
130123
ALPAKA_FN_HOST static auto getName(DevUniformCudaHipRt<TApi> const& dev) -> std::string
131124
{
132125
{
133-
std::lock_guard<std::mutex> lock(*dev.m_mutex);
134-
if(!dev.m_deviceProperties->name.has_value())
126+
std::lock_guard<std::mutex> lock(dev.m_QueueRegistry->mutex());
127+
if(!dev.m_QueueRegistry->deviceProperties().name.has_value())
135128
{
136129
// There is cuda/hip-DeviceGetAttribute as faster alternative to cuda/hip-GetDeviceProperties
137130
// to get a single device property but it has no option to get the name
138131
typename TApi::DeviceProp_t devProp;
139132
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::getDeviceProperties(&devProp, dev.getNativeHandle()));
140-
dev.m_deviceProperties->name = std::string(devProp.name);
133+
dev.m_QueueRegistry->deviceProperties().name = std::string(devProp.name);
141134
}
142135
}
143136

144-
return dev.m_deviceProperties->name.value();
137+
return dev.m_QueueRegistry->deviceProperties().name.value();
145138
}
146139
};
147140

@@ -152,8 +145,8 @@ namespace alpaka
152145
ALPAKA_FN_HOST static auto getMemBytes(DevUniformCudaHipRt<TApi> const& dev) -> std::size_t
153146
{
154147
{
155-
std::lock_guard<std::mutex> lock(*dev.m_mutex);
156-
if(!dev.m_deviceProperties->freeGlobalMem.has_value())
148+
std::lock_guard<std::mutex> lock(dev.m_QueueRegistry->mutex());
149+
if(!dev.m_QueueRegistry->deviceProperties().totalGlobalMem.has_value())
157150
{
158151
// Set the current device to wait for.
159152
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::setDevice(dev.getNativeHandle()));
@@ -163,12 +156,11 @@ namespace alpaka
163156

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

166-
dev.m_deviceProperties->totalGlobalMem = totalInternal;
167-
dev.m_deviceProperties->freeGlobalMem = freeInternal;
159+
dev.m_QueueRegistry->deviceProperties().totalGlobalMem = totalInternal;
168160
}
169161
}
170162

171-
return dev.m_deviceProperties->totalGlobalMem.value();
163+
return dev.m_QueueRegistry->deviceProperties().totalGlobalMem.value();
172164
}
173165
};
174166

@@ -178,24 +170,23 @@ namespace alpaka
178170
{
179171
ALPAKA_FN_HOST static auto getFreeMemBytes(DevUniformCudaHipRt<TApi> const& dev) -> std::size_t
180172
{
173+
std::size_t freeInternal(0u);
181174
{
182-
std::lock_guard<std::mutex> lock(*dev.m_mutex);
183-
if(!dev.m_deviceProperties->totalGlobalMem.has_value())
175+
std::lock_guard<std::mutex> lock(dev.m_QueueRegistry->mutex());
176+
if(!dev.m_QueueRegistry->deviceProperties().totalGlobalMem.has_value())
184177
{
185178
// Set the current device to wait for.
186179
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::setDevice(dev.getNativeHandle()));
187180

188-
std::size_t freeInternal(0u);
189181
std::size_t totalInternal(0u);
190182

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

193-
dev.m_deviceProperties->totalGlobalMem = totalInternal;
194-
dev.m_deviceProperties->freeGlobalMem = freeInternal;
185+
dev.m_QueueRegistry->deviceProperties().totalGlobalMem = totalInternal;
195186
}
196187
}
197188

198-
return dev.m_deviceProperties->freeGlobalMem.value();
189+
return freeInternal;
199190
}
200191
};
201192

@@ -206,14 +197,14 @@ namespace alpaka
206197
ALPAKA_FN_HOST static auto getWarpSizes(DevUniformCudaHipRt<TApi> const& dev) -> std::vector<std::size_t>
207198
{
208199
{
209-
std::lock_guard<std::mutex> lock(*dev.m_mutex);
210-
if(!dev.m_deviceProperties->warpSizes.has_value())
200+
std::lock_guard<std::mutex> lock(dev.m_QueueRegistry->mutex());
201+
if(!dev.m_QueueRegistry->deviceProperties().warpSizes.has_value())
211202
{
212-
dev.m_deviceProperties->warpSizes = std::vector<std::size_t>{
203+
dev.m_QueueRegistry->deviceProperties().warpSizes = std::vector<std::size_t>{
213204
GetPreferredWarpSize<DevUniformCudaHipRt<TApi>>::getPreferredWarpSize(dev)};
214205
}
215206
}
216-
return dev.m_deviceProperties->warpSizes.value();
207+
return dev.m_QueueRegistry->deviceProperties().warpSizes.value();
217208
}
218209
};
219210

@@ -224,18 +215,18 @@ namespace alpaka
224215
ALPAKA_FN_HOST static auto getPreferredWarpSize(DevUniformCudaHipRt<TApi> const& dev) -> std::size_t
225216
{
226217
{
227-
std::lock_guard<std::mutex> lock(*dev.m_mutex);
228-
if(!dev.m_deviceProperties->preferredWarpSize.has_value())
218+
std::lock_guard<std::mutex> lock(dev.m_QueueRegistry->mutex());
219+
if(!dev.m_QueueRegistry->deviceProperties().preferredWarpSize.has_value())
229220
{
230221
int warpSize = 0;
231222

232223
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(
233224
TApi::deviceGetAttribute(&warpSize, TApi::deviceAttributeWarpSize, dev.getNativeHandle()));
234-
dev.m_deviceProperties->preferredWarpSize = static_cast<std::size_t>(warpSize);
225+
dev.m_QueueRegistry->deviceProperties().preferredWarpSize = static_cast<std::size_t>(warpSize);
235226
}
236227
}
237228

238-
return dev.m_deviceProperties->preferredWarpSize.value();
229+
return dev.m_QueueRegistry->deviceProperties().preferredWarpSize.value();
239230
}
240231
};
241232

include/alpaka/dev/common/DeviceProperties.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace alpaka
1313
{
1414
std::optional<std::string> name;
1515
std::optional<std::size_t> totalGlobalMem;
16-
std::optional<std::size_t> freeGlobalMem;
1716
std::optional<std::vector<std::size_t>> warpSizes;
1817
std::optional<std::size_t> preferredWarpSize;
1918

include/alpaka/dev/common/QueueRegistry.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include "alpaka/core/Common.hpp"
8+
#include "alpaka/dev/common/DeviceProperties.hpp"
89

910
#include <deque>
1011
#include <functional>
@@ -52,8 +53,19 @@ namespace alpaka::detail
5253
m_queues.push_back(spQueue);
5354
}
5455

56+
std::mutex& mutex()
57+
{
58+
return m_Mutex;
59+
}
60+
61+
alpaka::DeviceProperties& deviceProperties()
62+
{
63+
return m_deviceProperties;
64+
}
65+
5566
private:
5667
std::mutex mutable m_Mutex;
68+
alpaka::DeviceProperties m_deviceProperties;
5769
std::deque<std::weak_ptr<TQueue>> mutable m_queues;
5870
};
5971
} // namespace alpaka::detail

0 commit comments

Comments
 (0)