[Openmp-commits] [PATCH] D156245: [OpenMP][libomptarget] Process resources when getting/returning from managers

Kevin Sala via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Tue Jul 25 08:27:06 PDT 2023


kevinsala created this revision.
kevinsala added reviewers: jdoerfert, jhuber6, tianshilei1992, mhalk.
kevinsala added a project: OpenMP.
Herald added subscribers: sunshaoce, guansong, yaxunl.
Herald added a project: All.
kevinsala requested review of this revision.
Herald added subscribers: openmp-commits, jplehr, sstefan1.

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
while the resource manager mutex is acquired.

This patch can be useful to implement D154523 <https://reviews.llvm.org/D154523>

Depends on D155629 <https://reviews.llvm.org/D155629>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156245

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


Index: openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h
===================================================================
--- openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h
+++ openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h
@@ -1176,15 +1176,44 @@
     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 getResourceImpl(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 a resource from the pool or create new ones. If the function succeeds,
+  /// the handle to the resource is saved in \p Handle. Also process each of the
+  /// obtained resource with \p Processor.
+  template <typename FuncTy>
+  Error getResourceImpl(ResourceHandleTy &Handle, FuncTy Processor) {
+    return getResourcesImpl(1, &Handle, Processor);
+  }
+
+  /// 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() &&
@@ -1200,15 +1229,25 @@
     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;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156245.543968.patch
Type: text/x-patch
Size: 3441 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20230725/65f90d27/attachment.bin>


More information about the Openmp-commits mailing list