opm-simulators
is_gpu_pointer.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_DETAIL_IS_GPU_POINTER_HPP
17 #define OPM_SIMULATORS_LINALG_GPUISTL_DETAIL_IS_GPU_POINTER_HPP
18 
19 #include <cuda.h>
20 #include <cuda_runtime.h>
21 #include <memory>
22 #include <opm/simulators/linalg/gpuistl/detail/gpu_safe_call.hpp>
23 
24 namespace Opm::gpuistl::detail
25 {
26 
38 template <class T>
39 inline bool
40 isGPUPointer(const T* ptr)
41 {
42  if (ptr == nullptr) {
43  return false;
44  }
45  cudaPointerAttributes attributes;
46  OPM_GPU_SAFE_CALL(cudaPointerGetAttributes(&attributes, ptr));
47  return attributes.type == cudaMemoryTypeDevice;
48 }
49 
50 
59 template <class T, class D>
60 inline bool
61 isGPUPointer(const std::unique_ptr<T, D>& ptr)
62 {
63  return isGPUPointer(ptr.get());
64 }
65 
73 template <class T>
74 inline bool
75 isGPUPointer(const std::shared_ptr<T>& ptr)
76 {
77  return isGPUPointer(ptr.get());
78 }
79 } // namespace Opm::gpuistl::detail
80 #endif
Contains wrappers to make the CuBLAS library behave as a modern C++ library with function overlading...
Definition: autotuner.hpp:29
bool isGPUPointer(const T *ptr)
Checks whether the given pointer is associated with GPU device memory.
Definition: is_gpu_pointer.hpp:40