Taskflow  2.4-master-branch
cuda_error.hpp
1 #pragma once
2 
3 #include <cuda.h>
4 #include <iostream>
5 #include <sstream>
6 #include <exception>
7 
8 #include "../utility/stringify.hpp"
9 
10 #define TF_CUDA_REMOVE_FIRST_HELPER(N, ...) __VA_ARGS__
11 #define TF_CUDA_REMOVE_FIRST(...) TF_CUDA_REMOVE_FIRST_HELPER(__VA_ARGS__)
12 #define TF_CUDA_GET_FIRST_HELPER(N, ...) N
13 #define TF_CUDA_GET_FIRST(...) TF_CUDA_GET_FIRST_HELPER(__VA_ARGS__)
14 
15 #define TF_CHECK_CUDA(...) \
16 if(TF_CUDA_GET_FIRST(__VA_ARGS__) != cudaSuccess) { \
17  std::ostringstream oss; \
18  auto ev = TF_CUDA_GET_FIRST(__VA_ARGS__); \
19  auto unknown_str = "unknown error"; \
20  auto unknown_name = "cudaErrorUnknown"; \
21  auto error_str = ::cudaGetErrorString(ev); \
22  auto error_name = ::cudaGetErrorName(ev); \
23  oss << "[" << __FILE__ << ":" << __LINE__ << "] " \
24  << (error_str ? error_str : unknown_str) \
25  << " (" \
26  << (error_name ? error_name : unknown_name) \
27  << ") - "; \
28  tf::ostreamize(oss, TF_CUDA_REMOVE_FIRST(__VA_ARGS__)); \
29  throw std::runtime_error(oss.str()); \
30 }
31