opm-simulators
gpu_resources.hpp
1 /*
2  Copyright 2025 Equinor ASA
3  This file is part of the Open Porous Media project (OPM).
4  OPM is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8  OPM is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  You should have received a copy of the GNU General Public License
13  along with OPM. If not, see <http://www.gnu.org/licenses/>.
14 */
15 
16 #ifndef OPM_SIMULATORS_LINALG_GPUISTL_GPU_RESOURCES_HPP
17 #define OPM_SIMULATORS_LINALG_GPUISTL_GPU_RESOURCES_HPP
18 #include <cuda.h>
19 #include <cuda_runtime.h>
20 #include <opm/simulators/linalg/gpuistl/detail/gpu_safe_call.hpp>
21 #include <type_traits>
22 
44 #define OPM_CREATE_GPU_RESOURCE(name, type, create, destroy, ...) \
45  class name \
46  { \
47  public: \
48  using resource_type = type; \
49  \
50  name() \
51  { \
52  OPM_GPU_SAFE_CALL(create(&resource_, ##__VA_ARGS__)); \
53  } \
54  name(const name&) = delete; \
55  name& operator=(const name&) = delete; \
56  \
57  ~name() \
58  { \
59  OPM_GPU_WARN_IF_ERROR(destroy(resource_)); \
60  } \
61  \
62  const resource_type& get() const \
63  { \
64  return resource_; \
65  } \
66  resource_type& get() \
67  { \
68  return resource_; \
69  } \
70  \
71  private: \
72  resource_type resource_ = nullptr; \
73  }
74 
92 #define OPM_CREATE_GPU_RESOURCE_NO_CREATE(name, type, destroy) \
93  class name \
94  { \
95  public: \
96  using resource_type = type; \
97  \
98  name() = default; \
99  \
100  ~name() \
101  { \
102  OPM_GPU_WARN_IF_ERROR(destroy(resource_)); \
103  } \
104  name(const name&) = delete; \
105  name& operator=(const name&) = delete; \
106  \
107  const resource_type& get() const \
108  { \
109  return resource_; \
110  } \
111  resource_type& get() \
112  { \
113  return resource_; \
114  } \
115  \
116  private: \
117  resource_type resource_; \
118  }
119 
120 namespace Opm::gpuistl
121 {
130 OPM_CREATE_GPU_RESOURCE(GPUStream, cudaStream_t, cudaStreamCreate, cudaStreamDestroy);
131 
140 OPM_CREATE_GPU_RESOURCE(GPUEvent, cudaEvent_t, cudaEventCreate, cudaEventDestroy);
141 
151  GPUGraph, cudaGraph_t, cudaGraphCreate, cudaGraphDestroy, 0); // Per documentation, needs 0 as last argument
152 
161 OPM_CREATE_GPU_RESOURCE_NO_CREATE(GPUGraphExec, cudaGraphExec_t, cudaGraphExecDestroy);
162 } // namespace Opm::gpuistl
163 #endif
OPM_CREATE_GPU_RESOURCE(GPUStream, cudaStream_t, cudaStreamCreate, cudaStreamDestroy)
Manages a CUDA stream resource.
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUD...
Definition: AmgxInterface.hpp:37
OPM_CREATE_GPU_RESOURCE_NO_CREATE(GPUGraphExec, cudaGraphExec_t, cudaGraphExecDestroy)
Manages a CUDA graph execution resource.