Loading...
Searching...
No Matches
cuda_error.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * CUDA error handling utilities.
5 *
6 * WARNING: This file includes <cuda_runtime.h> and <cublas_v2.h>.
7 * It MUST NOT be included from shared (vendor-neutral) headers.
8 * Only CUDA-specific translation units (cuda.cpp, cuda.cu) should
9 * include this file. This enforces AGENTS.md ยง6.1: serial baseline
10 * files must compile without CUDA toolchain.
11 *
12 * Error logic is in inline functions; macros exist solely to capture
13 * __FILE__ and __LINE__ for diagnostic messages.
14 */
15
17
18#include <cublas_v2.h>
19#include <cuda_runtime.h>
20
21#include <stdexcept>
22#include <string>
23
24namespace q3 {
25namespace execution {
26namespace gpu {
27namespace cuda {
28
29/**
30 * Check a CUDA runtime API call and throw on error.
31 *
32 * @param error Return value from a cuda* function.
33 * @param file Source file name (from __FILE__).
34 * @param line Source line number (from __LINE__).
35 * @throws std::runtime_error If error != cudaSuccess.
36 */
37inline void check_error(cudaError_t error, const char* file, int line) {
38 if (error != cudaSuccess) {
39 q3::log::error("CUDA error at {}:{}: {} (code {})",
40 file,
41 line,
42 cudaGetErrorString(error),
43 static_cast<int>(error));
44 throw std::runtime_error(std::string("CUDA error: ") + cudaGetErrorString(error));
45 }
46}
47
48/**
49 * Check a cuBLAS API call and throw on error.
50 *
51 * @param status Return value from a cublas* function.
52 * @param file Source file name (from __FILE__).
53 * @param line Source line number (from __LINE__).
54 * @throws std::runtime_error If status != CUBLAS_STATUS_SUCCESS.
55 */
56inline void check_cublas_error(cublasStatus_t status, const char* file, int line) {
57 if (status != CUBLAS_STATUS_SUCCESS) {
58 q3::log::error("cuBLAS error at {}:{}: code {}", file, line, static_cast<int>(status));
59 throw std::runtime_error("cuBLAS error");
60 }
61}
62
63} // namespace cuda
64} // namespace gpu
65} // namespace execution
66} // namespace q3
67
68/**
69 * Check a CUDA runtime API call, logging file and line on error.
70 *
71 * Usage:
72 * Q3_CUDA_CHECK(cudaMalloc(&pointer, size));
73 */
74#define Q3_CUDA_CHECK(call) q3::execution::gpu::cuda::check_error((call), __FILE__, __LINE__)
75
76/**
77 * Check a cuBLAS API call, logging file and line on error.
78 *
79 * Usage:
80 * Q3_CUBLAS_CHECK(cublasCreate(&handle));
81 */
82#define Q3_CUBLAS_CHECK(call) \
83 q3::execution::gpu::cuda::check_cublas_error((call), __FILE__, __LINE__)
void check_error(cudaError_t error, const char *file, int line)
void check_cublas_error(cublasStatus_t status, const char *file, int line)
void error(spdlog::format_string_t< Args... > fmt, Args &&... args)
Definition logger.hpp:192