[Openmp-commits] [PATCH] D72576: [OpenMP][Offloading] Fix the issue that omp_get_num_devices returns wrong number of devices

Shilei Tian via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Mon Jan 13 19:37:14 PST 2020


tianshilei1992 updated this revision to Diff 237835.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72576/new/

https://reviews.llvm.org/D72576

Files:
  openmp/libomptarget/src/rtl.cpp


Index: openmp/libomptarget/src/rtl.cpp
===================================================================
--- openmp/libomptarget/src/rtl.cpp
+++ openmp/libomptarget/src/rtl.cpp
@@ -231,31 +231,12 @@
 }
 
 void RTLsTy::RegisterLib(__tgt_bin_desc *desc) {
-  // Attempt to load all plugins available in the system.
-  std::call_once(initFlag, &RTLsTy::LoadRTLs, this);
-
-  if (desc->HostEntriesBegin == desc->HostEntriesEnd)
-    return;
-  RTLsMtx.lock();
-  // Register the images with the RTLs that understand them, if any.
-  for (int32_t i = 0; i < desc->NumDeviceImages; ++i) {
-    // Obtain the image.
-    __tgt_device_image *img = &desc->DeviceImages[i];
-
-    RTLInfoTy *FoundRTL = NULL;
-
-    // Scan the RTLs that have associated images until we find one that supports
-    // the current image.
+  // Load all plugins available in the system and initialize them
+  std::call_once(initFlag, [this]() {
+    // 1. Load plugins
+    this->LoadRTLs();
+    // 2. Initialize them if any one has not been initialized yet
     for (auto &R : RTLs.AllRTLs) {
-      if (!R.is_valid_binary(img)) {
-        DP("Image " DPxMOD " is NOT compatible with RTL %s!\n",
-            DPxPTR(img->ImageStart), R.RTLName.c_str());
-        continue;
-      }
-
-      DP("Image " DPxMOD " is compatible with RTL %s!\n",
-          DPxPTR(img->ImageStart), R.RTLName.c_str());
-
       // If this RTL is not already in use, initialize it.
       if (!R.isUsed) {
         // Initialize the device information for the RTL we are about to use.
@@ -263,7 +244,7 @@
         size_t start = Devices.size();
         Devices.resize(start + R.NumberOfDevices, device);
         for (int32_t device_id = 0; device_id < R.NumberOfDevices;
-            device_id++) {
+             device_id++) {
           // global device ID
           Devices[start + device_id].DeviceID = start + device_id;
           // RTL local device ID
@@ -275,17 +256,43 @@
                     ? 0
                     : RTLs.UsedRTLs.back()->Idx +
                           RTLs.UsedRTLs.back()->NumberOfDevices;
-        assert((size_t) R.Idx == start &&
-            "RTL index should equal the number of devices used so far.");
+        assert((size_t)R.Idx == start &&
+               "RTL index should equal the number of devices used so far.");
         R.isUsed = true;
         RTLs.UsedRTLs.push_back(&R);
 
         DP("RTL " DPxMOD " has index %d!\n", DPxPTR(R.LibraryHandler), R.Idx);
       }
+    }
+  });
+
+  if (desc->HostEntriesBegin == desc->HostEntriesEnd)
+    return;
+  RTLsMtx.lock();
+  // Register the images with the RTLs that understand them, if any.
+  for (int32_t i = 0; i < desc->NumDeviceImages; ++i) {
+    // Obtain the image.
+    __tgt_device_image *img = &desc->DeviceImages[i];
+
+    RTLInfoTy *FoundRTL = NULL;
+
+    // Scan the RTLs that have associated images until we find one that supports
+    // the current image.
+    for (auto &R : RTLs.AllRTLs) {
+      if (!R.is_valid_binary(img)) {
+        DP("Image " DPxMOD " is NOT compatible with RTL %s!\n",
+           DPxPTR(img->ImageStart), R.RTLName.c_str());
+        continue;
+      }
+
+      DP("Image " DPxMOD " is compatible with RTL %s!\n",
+         DPxPTR(img->ImageStart), R.RTLName.c_str());
+
+      assert(R.isUsed && "RTL should have already been initialized!");
 
       // Initialize (if necessary) translation table for this library.
       TrlTblMtx.lock();
-      if(!HostEntriesBeginToTransTable.count(desc->HostEntriesBegin)){
+      if (!HostEntriesBeginToTransTable.count(desc->HostEntriesBegin)) {
         TranslationTable &tt =
             HostEntriesBeginToTransTable[desc->HostEntriesBegin];
         tt.HostTable.EntriesBegin = desc->HostEntriesBegin;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72576.237835.patch
Type: text/x-patch
Size: 3750 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20200114/64ecbab8/attachment-0001.bin>


More information about the Openmp-commits mailing list