gpu_pointer_attributes.hpp
Go to the documentation of this file.
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_GPU_POINTER_ATTRIBUTES_HPP
17#define OPM_SIMULATORS_LINALG_GPUISTL_DETAIL_GPU_POINTER_ATTRIBUTES_HPP
18
20
21#include <opm/common/ErrorMacros.hpp>
22
23#include <cuda.h>
24#include <cuda_runtime.h>
25
26#include <format>
27#include <memory>
28#include <source_location>
29#include <stdexcept>
30#include <string_view>
31
33{
34
46template <class T>
47inline bool
48isGPUPointer(const T* ptr)
49{
50 if (ptr == nullptr) {
51 return false;
52 }
53 cudaPointerAttributes attributes {};
54 OPM_GPU_SAFE_CALL(cudaPointerGetAttributes(&attributes, ptr));
55
56 return attributes.type == cudaMemoryTypeDevice;
57}
58
59
68template <class T, class D>
69inline bool
70isGPUPointer(const std::unique_ptr<T, D>& ptr)
71{
72 return isGPUPointer(ptr.get());
73}
74
82template <class T>
83inline bool
84isGPUPointer(const std::shared_ptr<T>& ptr)
85{
86 return isGPUPointer(ptr.get());
87}
88
89
90// Check if the pointer is a CPU pointer -----------------------------------------
91
108template <class T>
109inline bool
110isCPUPointer(const T* ptr)
111{
112 if (ptr == nullptr) {
113 return false;
114 }
115
116 cudaPointerAttributes attributes {};
117 OPM_GPU_SAFE_CALL(cudaPointerGetAttributes(&attributes, ptr));
118
119 // Enumerator value 0 == Unregistered on CUDA 11+ / modern HIP.
120 // Do not name cudaMemoryTypeUnregistered: hipify-perl will not translate it.
121 const auto pointerTypeUnregistered = static_cast<cudaMemoryType>(0);
122 return attributes.type == cudaMemoryTypeHost || attributes.type == pointerTypeUnregistered;
123}
124
125
134template <class T, class D>
135inline bool
136isCPUPointer(const std::unique_ptr<T, D>& ptr)
137{
138 return isCPUPointer(ptr.get());
139}
140
148template <class T>
149inline bool
150isCPUPointer(const std::shared_ptr<T>& ptr)
151{
152 return isCPUPointer(ptr.get());
153}
154
161inline void
162assertHostPointer([[maybe_unused]] const void* ptr,
163 [[maybe_unused]] std::string_view name,
164 [[maybe_unused]] const std::source_location location
165 = std::source_location::current())
166{
167#ifndef NDEBUG
168 if (!isCPUPointer(ptr)) {
169 OPM_THROW(std::invalid_argument,
170 std::format("{} is not a CPU pointer\n"
171 " file: {}\n"
172 " line: {}\n"
173 " column: {}\n"
174 " function: {}",
175 name,
176 location.file_name(),
177 location.line(),
178 location.column(),
179 location.function_name()));
180 }
181#endif
182}
183
190inline void
191assertDevicePointer([[maybe_unused]] const void* ptr,
192 [[maybe_unused]] std::string_view name,
193 [[maybe_unused]] const std::source_location location
194 = std::source_location::current())
195{
196#ifndef NDEBUG
197 if (!isGPUPointer(ptr)) {
198 OPM_THROW(std::invalid_argument,
199 std::format("{} is not a device/GPU pointer\n"
200 " file: {}\n"
201 " line: {}\n"
202 " column: {}\n"
203 " function: {}",
204 name,
205 location.file_name(),
206 location.line(),
207 location.column(),
208 location.function_name()));
209 }
210#endif
211}
212
213} // namespace Opm::gpuistl::detail
214
218#define OPM_GPUISTL_DETAIL_ASSERT_HOST_POINTER(x) ::Opm::gpuistl::detail::assertHostPointer((x), #x)
219
223#define OPM_GPUISTL_DETAIL_ASSERT_DEVICE_POINTER(x) \
224 ::Opm::gpuistl::detail::assertDevicePointer((x), #x)
225
226#endif
#define OPM_GPU_SAFE_CALL(expression)
OPM_GPU_SAFE_CALL checks the return type of the GPU expression (function call) and throws an exceptio...
Definition: gpu_safe_call.hpp:164
Definition: autotuner.hpp:30
bool isGPUPointer(const T *ptr)
Checks whether the given pointer is associated with GPU device memory.
Definition: gpu_pointer_attributes.hpp:48
void assertDevicePointer(const void *ptr, std::string_view name, const std::source_location location=std::source_location::current())
Debug-only check that ptr is device/GPU memory.
Definition: gpu_pointer_attributes.hpp:191
void assertHostPointer(const void *ptr, std::string_view name, const std::source_location location=std::source_location::current())
Debug-only check that ptr is host/CPU memory.
Definition: gpu_pointer_attributes.hpp:162
bool isCPUPointer(const T *ptr)
Checks whether the given pointer is associated with CPU host memory.
Definition: gpu_pointer_attributes.hpp:110