cuda_helper.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef REMOTEAR3_CUDA_HELPER_H
  2. #define REMOTEAR3_CUDA_HELPER_H
  3. #include "utility.hpp"
  4. #include <cuda.h>
  5. #include <cuda_runtime.h>
  6. #include <nppdefs.h>
  7. #include <spdlog/spdlog.h>
  8. inline bool check_cuda_api_call(CUresult api_ret, unsigned int line_number,
  9. const char *file_name, const char *api_call_str) {
  10. if (api_ret == CUDA_SUCCESS) [[likely]] return true;
  11. const char *error_name, *error_str;
  12. auto ret = cuGetErrorName(api_ret, &error_name);
  13. if (ret != CUDA_SUCCESS) [[unlikely]] error_name = "Unknown";
  14. ret = cuGetErrorString(api_ret, &error_str);
  15. if (ret != CUDA_SUCCESS) [[unlikely]] error_str = "Unknown";
  16. SPDLOG_ERROR("CUDA runtime api call {} failed at {}:{} with error 0x{:x}:{}, {}.",
  17. api_call_str, file_name, line_number,
  18. (int) api_ret, error_name, error_str);
  19. RET_ERROR_B;
  20. }
  21. inline bool check_cuda_api_call(cudaError api_ret, unsigned int line_number,
  22. const char *file_name, const char *api_call_str) {
  23. if (api_ret == cudaSuccess) [[likely]] return true;
  24. SPDLOG_ERROR("CUDA driver api call {} failed at {}:{} with error 0x{:x}.",
  25. api_call_str, file_name, line_number, (int) api_ret);
  26. RET_ERROR_B;
  27. }
  28. inline bool check_cuda_api_call(NppStatus api_ret, unsigned int line_number,
  29. const char *file_name, const char *api_call_str) {
  30. if (api_ret == NPP_SUCCESS) [[likely]] return true;
  31. SPDLOG_ERROR("NPP api call {} failed at {}:{} with error 0x{:x}.",
  32. api_call_str, file_name, line_number, (int) api_ret);
  33. RET_ERROR_B;
  34. }
  35. #define CUDA_API_CHECK(api_call) \
  36. check_cuda_api_call( \
  37. api_call, __LINE__, __FILE__, #api_call)
  38. #define CUDA_API_CHECK_P(api_call) \
  39. if (!check_cuda_api_call( \
  40. api_call, __LINE__, __FILE__, #api_call)) [[unlikely]] \
  41. return nullptr
  42. #endif //REMOTEAR3_CUDA_HELPER_H