Loading...
Searching...
No Matches
gpu_resources.hpp
/*
Copyright 2025 Equinor ASA
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_SIMULATORS_LINALG_GPUISTL_GPU_RESOURCES_HPP
#define OPM_SIMULATORS_LINALG_GPUISTL_GPU_RESOURCES_HPP
#include <cuda.h>
#include <cuda_runtime.h>
#include <opm/simulators/linalg/gpuistl/detail/gpu_safe_call.hpp>
#include <type_traits>
#define OPM_CREATE_GPU_RESOURCE(name, type, create, destroy, ...) \
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; \
}
#define OPM_CREATE_GPU_RESOURCE_NO_CREATE(name, type, destroy) \
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_; \
}
namespace Opm::gpuistl
{
OPM_CREATE_GPU_RESOURCE(GPUStream, cudaStream_t, cudaStreamCreate, cudaStreamDestroy);
OPM_CREATE_GPU_RESOURCE(GPUEvent, cudaEvent_t, cudaEventCreate, cudaEventDestroy);
OPM_CREATE_GPU_RESOURCE(
GPUGraph, cudaGraph_t, cudaGraphCreate, cudaGraphDestroy, 0); // Per documentation, needs 0 as last argument
OPM_CREATE_GPU_RESOURCE_NO_CREATE(GPUGraphExec, cudaGraphExec_t, cudaGraphExecDestroy);
} // namespace Opm::gpuistl
#endif