gpu_resources.hpp File Reference
#include <cuda.h>
#include <cuda_runtime.h>
#include <opm/simulators/linalg/gpuistl/detail/gpu_safe_call.hpp>
#include <type_traits>
Include dependency graph for gpu_resources.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  Opm
 
namespace  Opm::gpuistl
 

Macros

#define OPM_CREATE_GPU_RESOURCE(name, type, create, destroy, ...)
 Creates an RAII-style GPU resource class. More...
 
#define OPM_CREATE_GPU_RESOURCE_NO_CREATE(name, type, destroy)
 A helper macro for creating a GPU resource wrapper without invoking creation logic. More...
 

Functions

 Opm::gpuistl::OPM_CREATE_GPU_RESOURCE (GPUStream, cudaStream_t, cudaStreamCreate, cudaStreamDestroy)
 Manages a CUDA stream resource. More...
 
 Opm::gpuistl::OPM_CREATE_GPU_RESOURCE (GPUEvent, cudaEvent_t, cudaEventCreate, cudaEventDestroy)
 Manages a CUDA event resource. More...
 
 Opm::gpuistl::OPM_CREATE_GPU_RESOURCE (GPUGraph, cudaGraph_t, cudaGraphCreate, cudaGraphDestroy, 0)
 Manages a CUDA graph resource. More...
 
 Opm::gpuistl::OPM_CREATE_GPU_RESOURCE_NO_CREATE (GPUGraphExec, cudaGraphExec_t, cudaGraphExecDestroy)
 Manages a CUDA graph execution resource. More...
 

Macro Definition Documentation

◆ OPM_CREATE_GPU_RESOURCE

#define OPM_CREATE_GPU_RESOURCE (   name,
  type,
  create,
  destroy,
  ... 
)
Value:
class name \
{ \
public: \
using resource_type = type; \
\
name() \
{ \
OPM_GPU_SAFE_CALL(create(&resource_, ##__VA_ARGS__)); \
} \
name(const name&) = delete; \
name& operator=(const name&) = delete; \
\
~name() \
{ \
OPM_GPU_WARN_IF_ERROR(destroy(resource_)); \
} \
\
const resource_type& get() const \
{ \
return resource_; \
} \
resource_type& get() \
{ \
return resource_; \
} \
\
private: \
resource_type resource_ = nullptr; \
}

Creates an RAII-style GPU resource class.

This macro automatically generates a class that:

  • Defines a resource type and a corresponding instance.
  • Initializes the resource in the constructor via the provided creation function.
  • Cleans up the resource in the destructor via the provided destruction function.
Template Parameters
nameThe class name to be defined.
typeThe underlying resource type to manage.
createThe function used to create or allocate the resource.
destroyThe function used to destroy or deallocate the resource.
Parameters
...Additional arguments forwarded to the creation function.

Usage example:

OPM_CREATE_GPU_RESOURCE(MyResource, ResourceType, createFunction, destroyFunction, args...);
#define OPM_CREATE_GPU_RESOURCE(name, type, create, destroy,...)
Creates an RAII-style GPU resource class.
Definition: gpu_resources.hpp:44

This ensures correct setup and teardown of GPU resources without clutter in user code.

◆ OPM_CREATE_GPU_RESOURCE_NO_CREATE

#define OPM_CREATE_GPU_RESOURCE_NO_CREATE (   name,
  type,
  destroy 
)
Value:
class name \
{ \
public: \
using resource_type = type; \
\
name() = default; \
\
~name() \
{ \
OPM_GPU_WARN_IF_ERROR(destroy(resource_)); \
} \
name(const name&) = delete; \
name& operator=(const name&) = delete; \
\
const resource_type& get() const \
{ \
return resource_; \
} \
resource_type& get() \
{ \
return resource_; \
} \
\
private: \
resource_type resource_; \
}

A helper macro for creating a GPU resource wrapper without invoking creation logic.

This macro, closely related to OPM_CREATE_GPU_RESOURCE, provides a class that manages a GPU resource of the specified type. Upon destruction, it automatically destroys the underlying resource to ensure proper cleanup of GPU memory or handles.

Usage:

  • Construct an instance of this wrapper to store a GPU resource.
  • The resource is destroyed when the wrapper goes out of scope.

Use OPM_CREATE_GPU_RESOURCE for variants that require resource creation logic.

Template Parameters
nameName of the wrapper class to manage the GPU resource.
typeThe type of the GPU resource to manage.
destroyThe function or macro used to destroy the resource.