alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
UniformCudaHip.hpp
Go to the documentation of this file.
1/* Copyright 2022 Axel Huebl, Benjamin Worpitz, Matthias Werner, René Widera, Jan Stephan, Andrea Bocci, Bernhard
2 * Manfred Gruber
3 * SPDX-License-Identifier: MPL-2.0
4 */
5
6#pragma once
7
10
11#include <algorithm>
12#include <initializer_list>
13#include <stdexcept>
14#include <string>
15#include <tuple>
16#include <type_traits>
17
18#if ALPAKA_LANG_CUDA || ALPAKA_LANG_HIP
19
20namespace alpaka::uniform_cuda_hip::detail
21{
22 //! CUDA/HIP runtime API error checking with log and exception, ignoring specific error values
23 template<typename TApi, bool TThrow>
24 ALPAKA_FN_HOST inline void rtCheck(
25 typename TApi::Error_t const& error,
26 char const* desc,
27 char const* file,
28 int const& line) noexcept(!TThrow)
29 {
30 if(error != TApi::success)
31 {
32 // reset the last error to allow user side error handling. Using std::ignore to discard unneeded
33 // return values is suggested by the C++ core guidelines.
34 std::ignore = TApi::getLastError();
35
36 if constexpr(TThrow)
37 {
38 auto const sError = std::string{
39 std::string(file) + "(" + std::to_string(line) + ") " + std::string(desc) + " : '"
40 + TApi::getErrorName(error) + "': '" + std::string(TApi::getErrorString(error)) + "'!"};
41
42 throw std::runtime_error(sError);
43 }
44 }
45 }
46
47 //! CUDA/HIP runtime API error checking with log and exception, ignoring specific error values
48 template<typename TApi, bool TThrow>
49 ALPAKA_FN_HOST inline void rtCheckIgnore(
50 typename TApi::Error_t const& error,
51 char const* cmd,
52 char const* file,
53 int const& line,
54 std::initializer_list<typename TApi::Error_t> ignoredErrorCodes) noexcept(!TThrow)
55 {
56 if(error != TApi::success)
57 {
58 // If the error code is not one of the ignored ones.
59 if(std::find(std::cbegin(ignoredErrorCodes), std::cend(ignoredErrorCodes), error)
60 == std::cend(ignoredErrorCodes))
61 {
62 using namespace std::literals;
63 rtCheck<TApi, TThrow>(error, ("'"s + std::string(cmd) + "' returned error "s).c_str(), file, line);
64 }
65 else
66 {
67 // reset the last error to avoid propagation to the next CUDA/HIP API call. Using std::ignore
68 // to discard unneeded return values is recommended by the C++ core guidelines.
69 std::ignore = TApi::getLastError();
70 }
71 }
72 }
73
74 //! CUDA/HIP runtime API last error checking with log and exception.
75 template<typename TApi, bool TThrow>
76 ALPAKA_FN_HOST inline void rtCheckLastError(char const* desc, char const* file, int const& line) noexcept(!TThrow)
77 {
78 typename TApi::Error_t const error(TApi::getLastError());
79 rtCheck<TApi, TThrow>(error, desc, file, line);
80 }
81} // namespace alpaka::uniform_cuda_hip::detail
82
83# define ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK_IMPL(ApiInterfaceType, cmd, throw, ...) \
84 do \
85 { \
86 ::alpaka::uniform_cuda_hip::detail::rtCheckLastError<ApiInterfaceType, throw>( \
87 "'" #cmd "' A previous API call (not this one) set the error ", \
88 __FILE__, \
89 __LINE__); \
90 ::alpaka::uniform_cuda_hip::detail::rtCheckIgnore<ApiInterfaceType, throw>( \
91 cmd, \
92 #cmd, \
93 __FILE__, \
94 __LINE__, \
95 {__VA_ARGS__}); \
96 } while(0)
97
98//! CUDA/HIP runtime error checking with log and exception, ignoring specific error values
99# define ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK_IGNORE(ApiInterfaceType, cmd, ...) \
100 ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK_IMPL(ApiInterfaceType, cmd, true, __VA_ARGS__)
101
102//! CUDA/HIP runtime error checking with log and exception.
103# define ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(ApiInterfaceType, cmd) \
104 ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK_IMPL(ApiInterfaceType, cmd, true, )
105
106//! CUDA/HIP runtime error checking with log and exception, ignoring specific error values
107# define ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK_IGNORE_NOEXCEPT(ApiInterfaceType, cmd, ...) \
108 ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK_IMPL(ApiInterfaceType, cmd, false, __VA_ARGS__)
109
110//! CUDA/HIP runtime error checking with log.
111# define ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK_NOEXCEPT(ApiInterfaceType, cmd) \
112 ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK_IMPL(ApiInterfaceType, cmd, false, )
113#endif
#define ALPAKA_FN_HOST
All functions that can be used on an accelerator have to be attributed with ALPAKA_FN_ACC or ALPAKA_F...
Definition common.hpp:32