alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Assert.hpp
Go to the documentation of this file.
1/* Copyright 2022 Axel Huebl, Benjamin Worpitz, Matthias Werner, Jan Stephan, Bernhard Manfred Gruber
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
8
9#include <cassert>
10#include <type_traits>
11
12//! The assert can be explicit disabled by defining NDEBUG
13#define ALPAKA_ASSERT(...) assert(__VA_ARGS__)
14
15//! Macro which expands to a noop.
16//! Macro enforces an semicolon after the call.
17#define ALPAKA_NOOP(...) \
18 do \
19 { \
20 } while(false)
21
22//! ALPAKA_ASSERT_ACC_IMPL is an assert-like macro.
23//! It can be disabled setting the ALPAKA_DISABLE_ASSERT_ACC preprocessor symbol or the NDEBUG preprocessor symbol.
24#if !defined(ALPAKA_DISABLE_ASSERT_ACC)
25# define ALPAKA_ASSERT_ACC_IMPL(...) ALPAKA_ASSERT(__VA_ARGS__)
26#else
27# define ALPAKA_ASSERT_ACC_IMPL(...) ALPAKA_NOOP(__VA_ARGS__)
28#endif
29
30//! ALPAKA_ASSERT_ACC is an assert-like macro.
31//!
32//! In device code for a GPU or SYCL backend it can be disabled setting the ALPAKA_DISABLE_ASSERT_ACC preprocessor
33//! symbol or the NDEBUG preprocessor symbol. In device code for a native C++ CPU backend and in host code, it is
34//! equivalent to ALPAKA_ASSERT, and can be disabled setting the NDEBUG preprocessor symbol.
35#if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) && defined(__CUDA_ARCH__)
36// CUDA device code
37# define ALPAKA_ASSERT_ACC(...) ALPAKA_ASSERT_ACC_IMPL(__VA_ARGS__)
38#elif defined(ALPAKA_ACC_GPU_HIP_ENABLED) && defined(__HIP_DEVICE_COMPILE__)
39// HIP/ROCm device code
40# define ALPAKA_ASSERT_ACC(...) ALPAKA_ASSERT_ACC_IMPL(__VA_ARGS__)
41#elif defined(ALPAKA_ACC_SYCL_ENABLED) && defined(__SYCL_DEVICE_ONLY__)
42// SYCL/oneAPI device code
43# if defined(SYCL_EXT_ONEAPI_ASSERT)
44# define ALPAKA_ASSERT_ACC(...) ALPAKA_ASSERT_ACC_IMPL(__VA_ARGS__)
45# else
46# define ALPAKA_ASSERT_ACC(...) ALPAKA_NOOP(__VA_ARGS__)
47# endif
48// add here any other #elif conditions for non-CPU backends
49// ...
50#else
51// CPU backend, or host code
52# define ALPAKA_ASSERT_ACC(...) ALPAKA_ASSERT(__VA_ARGS__)
53#endif
54
55namespace alpaka::core
56{
57 namespace detail
58 {
59 template<typename TArg>
61 {
63 [[maybe_unused]] TArg const& arg)
64 {
65 if constexpr(std::is_signed_v<TArg>)
66 ALPAKA_ASSERT_ACC(arg >= 0);
67
68 // Nothing to do for unsigned types.
69 }
70 };
71 } // namespace detail
72
73 //! This method checks integral values if they are greater or equal zero.
74 //! The implementation prevents warnings for checking this for unsigned types.
76 template<typename TArg>
81
82 namespace detail
83 {
84 template<typename TLhs, typename TRhs>
86 {
88 [[maybe_unused]] TRhs const& rhs)
89 {
90 if constexpr(std::is_signed_v<TRhs> || (TLhs::value != 0u))
91 ALPAKA_ASSERT_ACC(TLhs::value > rhs);
92
93 // Nothing to do for unsigned types comparing to zero.
94 }
95 };
96 } // namespace detail
97
98 //! This function asserts that the integral value TLhs is greater than TRhs.
100 template<typename TLhs, typename TRhs>
105} // namespace alpaka::core
#define ALPAKA_ASSERT_ACC(...)
ALPAKA_ASSERT_ACC is an assert-like macro.
Definition Assert.hpp:52
#define ALPAKA_FN_HOST_ACC
Definition Common.hpp:39
#define ALPAKA_NO_HOST_ACC_WARNING
Disable nvcc warning: 'calling a host function from host device function.' Usage: ALPAKA_NO_HOST_ACC_...
Definition Common.hpp:82
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC constexpr auto assertValueUnsigned(TArg const &arg) -> void
This method checks integral values if they are greater or equal zero. The implementation prevents war...
Definition Assert.hpp:77
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC constexpr auto assertGreaterThan(TRhs const &rhs) -> void
This function asserts that the integral value TLhs is greater than TRhs.
Definition Assert.hpp:101
auto clipCast(V const &val) -> T
Definition ClipCast.hpp:16
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC constexpr auto assertGreaterThan(TRhs const &rhs)
Definition Assert.hpp:87
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC constexpr auto assertValueUnsigned(TArg const &arg)
Definition Assert.hpp:62