[Openmp-commits] [openmp] 7b2745b - [OpenMP][libomptarget] Process resources when getting/returning from managers

Kevin Sala via Openmp-commits openmp-commits at lists.llvm.org
Thu Jul 27 15:39:43 PDT 2023


Author: Kevin Sala
Date: 2023-07-28T00:37:08+02:00
New Revision: 7b2745b424beb684ea084f3ced374b7fc4f68c9e

URL: https://github.com/llvm/llvm-project/commit/7b2745b424beb684ea084f3ced374b7fc4f68c9e
DIFF: https://github.com/llvm/llvm-project/commit/7b2745b424beb684ea084f3ced374b7fc4f68c9e.diff

LOG: [OpenMP][libomptarget] Process resources when getting/returning from managers

This patch adds the functionality to process with a lambda the resources
obtained and returned by the resource managers in the plugins. These
processing lambdas are empty for the moment. The idea is to process them
when the resource manager mutex is acquired.

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

Added: 
    

Modified: 
    openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h

Removed: 
    


################################################################################
diff  --git a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h
index ff6d86e7725be6..d670fa305ba99e 100644
--- a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h
+++ b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h
@@ -1182,15 +1182,36 @@ template <typename ResourceRef> class GenericDeviceResourceManagerTy {
     return Plugin::success();
   }
 
-  /// Get a resource from the pool or create new ones. If the function succeeds,
-  /// the handle to the resource is saved in \p Handle.
-  Error getResource(ResourceHandleTy &Handle) {
-    return getResources(1, &Handle);
+  /// Get a resource from the pool or create new ones. If the function
+  /// succeeeds, the handle to the resource is saved in \p Handle.
+  virtual Error getResource(ResourceHandleTy &Handle) {
+    // Get a resource with an empty resource processor.
+    return getResourcesImpl(1, &Handle,
+                            [](ResourceHandleTy) { return Plugin::success(); });
   }
 
   /// Get multiple resources from the pool or create new ones. If the function
   /// succeeeds, the handles to the resources are saved in \p Handles.
-  Error getResources(uint32_t Num, ResourceHandleTy *Handles) {
+  virtual Error getResources(uint32_t Num, ResourceHandleTy *Handles) {
+    // Get resources with an empty resource processor.
+    return getResourcesImpl(Num, Handles,
+                            [](ResourceHandleTy) { return Plugin::success(); });
+  }
+
+  /// Return resource to the pool.
+  virtual Error returnResource(ResourceHandleTy Handle) {
+    // Return a resource with an empty resource processor.
+    return returnResourceImpl(
+        Handle, [](ResourceHandleTy) { return Plugin::success(); });
+  }
+
+protected:
+  /// Get multiple resources from the pool or create new ones. If the function
+  /// succeeeds, the handles to the resources are saved in \p Handles. Also
+  /// process each of the obtained resources with \p Processor.
+  template <typename FuncTy>
+  Error getResourcesImpl(uint32_t Num, ResourceHandleTy *Handles,
+                         FuncTy Processor) {
     const std::lock_guard<std::mutex> Lock(Mutex);
 
     assert(NextAvailable <= ResourcePool.size() &&
@@ -1206,15 +1227,25 @@ template <typename ResourceRef> class GenericDeviceResourceManagerTy {
     for (uint32_t r = 0; r < Num; ++r)
       Handles[r] = ResourcePool[NextAvailable + r];
 
+    // Process all obtained resources.
+    for (uint32_t r = 0; r < Num; ++r)
+      if (auto Err = Processor(Handles[r]))
+        return Err;
+
     NextAvailable += Num;
 
     return Plugin::success();
   }
 
-  /// Return resource to the pool.
-  Error returnResource(ResourceHandleTy Handle) {
+  /// Return resource to the pool and process the resource with \p Processor.
+  template <typename FuncTy>
+  Error returnResourceImpl(ResourceHandleTy Handle, FuncTy Processor) {
     const std::lock_guard<std::mutex> Lock(Mutex);
 
+    // Process the returned resource.
+    if (auto Err = Processor(Handle))
+      return Err;
+
     assert(NextAvailable > 0 && "Resource pool is corrupted");
     ResourcePool[--NextAvailable] = Handle;
 


        


More information about the Openmp-commits mailing list