gpu_stream.hpp
Go to the documentation of this file.
1/*
2 Copyright 2026 Equinor ASA
3
4 This file is part of the Open Porous Media project (OPM).
5 OPM is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 OPM is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with OPM. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#ifndef OPM_GPUISTL_DETAIL_GPU_STREAM_HPP
20#define OPM_GPUISTL_DETAIL_GPU_STREAM_HPP
21
22#include <opm/common/ErrorMacros.hpp>
23
24#include <cuda_runtime.h>
25
26#include <format>
27#include <source_location>
28#include <stdexcept>
29#include <string_view>
30
32{
33
40inline void
41assertGPUStream([[maybe_unused]] cudaStream_t stream,
42 [[maybe_unused]] std::string_view name,
43 [[maybe_unused]] const std::source_location location
44 = std::source_location::current())
45{
46#ifndef NDEBUG
47 const cudaError_t err = cudaStreamQuery(stream);
48 if (err == cudaSuccess || err == cudaErrorNotReady) {
49 return;
50 }
51
52 if (err != cudaErrorInvalidResourceHandle) {
53 OPM_THROW(std::runtime_error,
54 std::format("GPU stream query for {} failed\n"
55 " file: {}\n"
56 " line: {}\n"
57 " column: {}\n"
58 " function: {}\n"
59 " GPU stream error: {}",
60 name,
61 location.file_name(),
62 location.line(),
63 location.column(),
64 location.function_name(),
65 cudaGetErrorString(err)));
66 }
67
68 OPM_THROW(std::invalid_argument,
69 std::format("{} is not a valid GPU stream\n"
70 " file: {}\n"
71 " line: {}\n"
72 " column: {}\n"
73 " function: {}\n"
74 " GPU stream error: {} (invalid stream handle)",
75 name,
76 location.file_name(),
77 location.line(),
78 location.column(),
79 location.function_name(),
80 cudaGetErrorString(err)));
81#endif
82}
83
84} // namespace Opm::gpuistl::detail
85
89#define OPM_GPUISTL_DETAIL_ASSERT_GPU_STREAM(x) ::Opm::gpuistl::detail::assertGPUStream((x), #x)
90
91#endif // OPM_GPUISTL_DETAIL_GPU_STREAM_HPP
Definition: autotuner.hpp:30
void assertGPUStream(cudaStream_t stream, std::string_view name, const std::source_location location=std::source_location::current())
Debug-only check that stream is a valid GPU stream handle.
Definition: gpu_stream.hpp:41