Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OpenMP][NFC] Encapsulate Devices.size() #74010

Merged
merged 1 commit into from
Dec 1, 2023

Conversation

jdoerfert
Copy link
Member

No description provided.

@jdoerfert jdoerfert added openmp openmp:libomptarget OpenMP offload runtime labels Dec 1, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 1, 2023

@llvm/pr-subscribers-openmp

Author: Johannes Doerfert (jdoerfert)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/74010.diff

2 Files Affected:

  • (modified) openmp/libomptarget/include/PluginManager.h (+5)
  • (modified) openmp/libomptarget/src/api.cpp (+8-16)
diff --git a/openmp/libomptarget/include/PluginManager.h b/openmp/libomptarget/include/PluginManager.h
index 3a1c97fc52c95df..e9b5169510fc4dc 100644
--- a/openmp/libomptarget/include/PluginManager.h
+++ b/openmp/libomptarget/include/PluginManager.h
@@ -143,6 +143,11 @@ struct PluginManager {
     DelayedBinDesc.clear();
   }
 
+  int getNumDevices() {
+    std::lock_guard<decltype(RTLsMtx)> Lock(RTLsMtx);
+    return Devices.size();
+  }
+
 private:
   bool RTLsLoaded = false;
   llvm::SmallVector<__tgt_bin_desc *> DelayedBinDesc;
diff --git a/openmp/libomptarget/src/api.cpp b/openmp/libomptarget/src/api.cpp
index e44421428adab44..cc4cca286df511e 100644
--- a/openmp/libomptarget/src/api.cpp
+++ b/openmp/libomptarget/src/api.cpp
@@ -28,13 +28,11 @@
 
 EXTERN int omp_get_num_devices(void) {
   TIMESCOPE();
-  PM->RTLsMtx.lock();
-  size_t DevicesSize = PM->Devices.size();
-  PM->RTLsMtx.unlock();
+  size_t NumDevices = PM->getNumDevices();
 
-  DP("Call to omp_get_num_devices returning %zd\n", DevicesSize);
+  DP("Call to omp_get_num_devices returning %zd\n", NumDevices);
 
-  return DevicesSize;
+  return NumDevices;
 }
 
 EXTERN int omp_get_device_num(void) {
@@ -112,10 +110,8 @@ EXTERN int omp_target_is_present(const void *Ptr, int DeviceNum) {
     return true;
   }
 
-  PM->RTLsMtx.lock();
-  size_t DevicesSize = PM->Devices.size();
-  PM->RTLsMtx.unlock();
-  if (DevicesSize <= (size_t)DeviceNum) {
+  size_t NumDevices = PM->getNumDevices();
+  if (NumDevices <= (size_t)DeviceNum) {
     DP("Call to omp_target_is_present with invalid device ID, returning "
        "false\n");
     return false;
@@ -562,18 +558,14 @@ EXTERN void *omp_get_mapped_ptr(const void *Ptr, int DeviceNum) {
     return nullptr;
   }
 
-  if (DeviceNum == omp_get_initial_device()) {
+  size_t NumDevices = omp_get_initial_device();
+  if (DeviceNum == NumDevices) {
     REPORT("Device %d is initial device, returning Ptr " DPxMOD ".\n",
            DeviceNum, DPxPTR(Ptr));
     return const_cast<void *>(Ptr);
   }
 
-  int DevicesSize = omp_get_initial_device();
-  {
-    std::lock_guard<std::mutex> LG(PM->RTLsMtx);
-    DevicesSize = PM->Devices.size();
-  }
-  if (DevicesSize <= DeviceNum) {
+  if (NumDevices <= DeviceNum) {
     DP("DeviceNum %d is invalid, returning nullptr.\n", DeviceNum);
     return nullptr;
   }

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

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

Unrelated, but wondering why we need a mutex for this when we could just have an atomic counter inside of the plugin manager.

@jdoerfert
Copy link
Member Author

Unrelated, but wondering why we need a mutex for this when we could just have an atomic counter inside of the plugin manager.

Should work too. Feel free ;)

@jdoerfert jdoerfert merged commit 1035cc7 into llvm:main Dec 1, 2023
5 checks passed
@jdoerfert jdoerfert deleted the offload_prep4 branch December 1, 2023 00:44
Guzhu-AMD pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Dec 7, 2023
Local branch amd-gfx b5901a7 Merged main:9a485b02f9e3 into amd-gfx:3660ee508c65
Remote branch main 1035cc7 [OpenMP][NFC] Encapsulate Devices.size() (llvm#74010)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openmp:libomptarget OpenMP offload runtime openmp
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants