Skip to content

Commit 7d70a99

Browse files
Mark getDeviceCache noexcept, handle exception in get_platforms
1 parent 6dd0231 commit 7d70a99

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

libsyclinterface/source/dpctl_sycl_device_manager.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,18 @@ struct DeviceCacheBuilder
113113
* post-creation we do not need any further protection to ensure
114114
* thread-safety.
115115
*/
116-
static const DeviceCache &getDeviceCache()
116+
static const DeviceCache &getDeviceCache() noexcept
117117
{
118118
static DeviceCache *cache = new DeviceCache([] {
119-
DeviceCache cache_l;
119+
DeviceCache cache_l{};
120120
dpctl_default_selector mRanker;
121-
auto Platforms = platform::get_platforms();
121+
std::vector<platform> Platforms{};
122+
try {
123+
Platforms = platform::get_platforms();
124+
} catch (std::exception const &e) {
125+
error_handler(e, __FILE__, __func__, __LINE__);
126+
return cache_l;
127+
}
122128
for (const auto &P : Platforms) {
123129
auto Devices = P.get_devices();
124130
for (const auto &D : Devices) {
@@ -169,7 +175,15 @@ DPCTLDeviceMgr_GetCachedContext(__dpctl_keep const DPCTLSyclDeviceRef DRef)
169175
__FILE__, __func__, __LINE__);
170176
return CRef;
171177
}
172-
auto &cache = DeviceCacheBuilder::getDeviceCache();
178+
179+
using CacheT = typename DeviceCacheBuilder::DeviceCache;
180+
CacheT const &cache = DeviceCacheBuilder::getDeviceCache();
181+
182+
if (cache.empty()) {
183+
// an exception was caught and logged by getDeviceCache
184+
return nullptr;
185+
}
186+
173187
auto entry = cache.find(*Device);
174188
if (entry != cache.end()) {
175189
context *ContextPtr = nullptr;
@@ -289,7 +303,13 @@ int DPCTLDeviceMgr_GetPositionInDevices(__dpctl_keep DPCTLSyclDeviceRef DRef,
289303
size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier)
290304
{
291305
size_t nDevices = 0;
292-
auto &cache = DeviceCacheBuilder::getDeviceCache();
306+
using CacheT = typename DeviceCacheBuilder::DeviceCache;
307+
CacheT const &cache = DeviceCacheBuilder::getDeviceCache();
308+
309+
if (cache.empty()) {
310+
// an exception was caught and logged by getDeviceCache
311+
return 0;
312+
}
293313

294314
device_identifier = to_canonical_device_id(device_identifier);
295315
if (!device_identifier)

0 commit comments

Comments
 (0)