[Openmp-commits] [openmp] 5105c7c - [OpenMP][CUDA] Fix an issue that multiple `CUmodule` are could be overwritten

Shilei Tian via Openmp-commits openmp-commits at lists.llvm.org
Wed Mar 9 11:55:23 PST 2022


Author: Shilei Tian
Date: 2022-03-09T14:55:20-05:00
New Revision: 5105c7cd78751d50975e7af2d4d614ebb799cbd4

URL: https://github.com/llvm/llvm-project/commit/5105c7cd78751d50975e7af2d4d614ebb799cbd4
DIFF: https://github.com/llvm/llvm-project/commit/5105c7cd78751d50975e7af2d4d614ebb799cbd4.diff

LOG: [OpenMP][CUDA] Fix an issue that multiple `CUmodule` are could be overwritten

This patch fixes the issue introduced in 14de0820e87f and D120089, that
if dynamic libraries are used, the `CUmodule` array could be overwritten.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D121308

Added: 
    

Modified: 
    openmp/libomptarget/plugins/cuda/src/rtl.cpp

Removed: 
    


################################################################################
diff  --git a/openmp/libomptarget/plugins/cuda/src/rtl.cpp b/openmp/libomptarget/plugins/cuda/src/rtl.cpp
index dab5b527f5210..9c3488790bed2 100644
--- a/openmp/libomptarget/plugins/cuda/src/rtl.cpp
+++ b/openmp/libomptarget/plugins/cuda/src/rtl.cpp
@@ -354,7 +354,7 @@ class DeviceRTLTy {
   std::vector<std::unique_ptr<EventPoolTy>> EventPool;
 
   std::vector<DeviceDataTy> DeviceData;
-  std::vector<CUmodule> Modules;
+  std::vector<std::vector<CUmodule>> Modules;
 
   /// Vector of flags indicating the initalization status of all associated
   /// devices.
@@ -777,25 +777,30 @@ class DeviceRTLTy {
     if (UseMemoryManager)
       MemoryManagers[DeviceId].release();
 
-    // Close module
-    if (CUmodule &M = Modules[DeviceId])
-      checkResult(cuModuleUnload(M), "Error returned from cuModuleUnload\n");
-
     StreamPool[DeviceId].reset();
     EventPool[DeviceId].reset();
 
-    // Destroy context
     DeviceDataTy &D = DeviceData[DeviceId];
-    if (D.Context) {
-      if (checkResult(cuCtxSetCurrent(D.Context),
-                      "Error returned from cuCtxSetCurrent\n")) {
-        CUdevice Device;
-        if (checkResult(cuCtxGetDevice(&Device),
-                        "Error returned from cuCtxGetDevice\n"))
-          checkResult(cuDevicePrimaryCtxRelease(Device),
-                      "Error returned from cuDevicePrimaryCtxRelease\n");
-      }
-    }
+    if (!checkResult(cuCtxSetCurrent(D.Context),
+                     "Error returned from cuCtxSetCurrent\n"))
+      return OFFLOAD_FAIL;
+
+    // Unload all modules.
+    for (auto &M : Modules[DeviceId])
+      if (!checkResult(cuModuleUnload(M),
+                       "Error returned from cuModuleUnload\n"))
+        return OFFLOAD_FAIL;
+
+    // Destroy context.
+    CUdevice Device;
+    if (!checkResult(cuCtxGetDevice(&Device),
+                     "Error returned from cuCtxGetDevice\n"))
+      return OFFLOAD_FAIL;
+
+    if (!checkResult(cuDevicePrimaryCtxRelease(Device),
+                     "Error returned from cuDevicePrimaryCtxRelease\n"))
+      return OFFLOAD_FAIL;
+
     return OFFLOAD_SUCCESS;
   }
 
@@ -818,7 +823,7 @@ class DeviceRTLTy {
 
     DP("CUDA module successfully loaded!\n");
 
-    Modules[DeviceId] = Module;
+    Modules[DeviceId].push_back(Module);
 
     // Find the symbols in the module by name.
     const __tgt_offload_entry *HostBegin = Image->EntriesBegin;


        


More information about the Openmp-commits mailing list