Skip to content

Commit 1035cc7

Browse files
authored
[OpenMP][NFC] Encapsulate Devices.size() (#74010)
1 parent b6cad75 commit 1035cc7

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

openmp/libomptarget/include/PluginManager.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ struct PluginManager {
143143
DelayedBinDesc.clear();
144144
}
145145

146+
int getNumDevices() {
147+
std::lock_guard<decltype(RTLsMtx)> Lock(RTLsMtx);
148+
return Devices.size();
149+
}
150+
146151
private:
147152
bool RTLsLoaded = false;
148153
llvm::SmallVector<__tgt_bin_desc *> DelayedBinDesc;

openmp/libomptarget/src/api.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@
2828

2929
EXTERN int omp_get_num_devices(void) {
3030
TIMESCOPE();
31-
PM->RTLsMtx.lock();
32-
size_t DevicesSize = PM->Devices.size();
33-
PM->RTLsMtx.unlock();
31+
size_t NumDevices = PM->getNumDevices();
3432

35-
DP("Call to omp_get_num_devices returning %zd\n", DevicesSize);
33+
DP("Call to omp_get_num_devices returning %zd\n", NumDevices);
3634

37-
return DevicesSize;
35+
return NumDevices;
3836
}
3937

4038
EXTERN int omp_get_device_num(void) {
@@ -112,10 +110,8 @@ EXTERN int omp_target_is_present(const void *Ptr, int DeviceNum) {
112110
return true;
113111
}
114112

115-
PM->RTLsMtx.lock();
116-
size_t DevicesSize = PM->Devices.size();
117-
PM->RTLsMtx.unlock();
118-
if (DevicesSize <= (size_t)DeviceNum) {
113+
size_t NumDevices = PM->getNumDevices();
114+
if (NumDevices <= (size_t)DeviceNum) {
119115
DP("Call to omp_target_is_present with invalid device ID, returning "
120116
"false\n");
121117
return false;
@@ -562,18 +558,14 @@ EXTERN void *omp_get_mapped_ptr(const void *Ptr, int DeviceNum) {
562558
return nullptr;
563559
}
564560

565-
if (DeviceNum == omp_get_initial_device()) {
561+
size_t NumDevices = omp_get_initial_device();
562+
if (DeviceNum == NumDevices) {
566563
REPORT("Device %d is initial device, returning Ptr " DPxMOD ".\n",
567564
DeviceNum, DPxPTR(Ptr));
568565
return const_cast<void *>(Ptr);
569566
}
570567

571-
int DevicesSize = omp_get_initial_device();
572-
{
573-
std::lock_guard<std::mutex> LG(PM->RTLsMtx);
574-
DevicesSize = PM->Devices.size();
575-
}
576-
if (DevicesSize <= DeviceNum) {
568+
if (NumDevices <= DeviceNum) {
577569
DP("DeviceNum %d is invalid, returning nullptr.\n", DeviceNum);
578570
return nullptr;
579571
}

0 commit comments

Comments
 (0)