Skip to content

Commit

Permalink
define precondition macro and violation handler
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlee committed Feb 12, 2024
1 parent 2b274dd commit 22258de
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 13 deletions.
4 changes: 2 additions & 2 deletions include/experimental/__p0009_bits/layout_left.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ class layout_left::mapping {
* TODO: check precondition
* other.required_span_size() is a representable value of type index_type
*/
#if !defined(_MDSPAN_HAS_CUDA) && !defined(_MDSPAN_HAS_HIP) && !defined(NDEBUG)
detail::terminate_if_invalid_strides(detail::with_rank<extents_type::rank()>{}, layout_left{}, __extents, other);
#if !defined(_MDSPAN_HAS_CUDA) && !defined(_MDSPAN_HAS_HIP)
detail::validate_strides(detail::with_rank<extents_type::rank()>{}, layout_left{}, __extents, other);
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions include/experimental/__p0009_bits/layout_right.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ class layout_right::mapping {
* TODO: check precondition
* other.required_span_size() is a representable value of type index_type
*/
#if !defined(_MDSPAN_HAS_CUDA) && !defined(_MDSPAN_HAS_HIP) && !defined(NDEBUG)
detail::terminate_if_invalid_strides(detail::with_rank<extents_type::rank()>{}, layout_right{}, __extents, other);
#if !defined(_MDSPAN_HAS_CUDA) && !defined(_MDSPAN_HAS_HIP)
detail::validate_strides(detail::with_rank<extents_type::rank()>{}, layout_right{}, __extents, other);
#endif
}

Expand Down
16 changes: 7 additions & 9 deletions include/experimental/__p0009_bits/layout_stride.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
# include "no_unique_address.hpp"
#endif

#include <algorithm>
#include <array>
#include <cstdlib>
#include <numeric>
#include <type_traits>
#include <utility>

#ifdef __cpp_lib_span
#include <span>
Expand Down Expand Up @@ -578,11 +577,11 @@ struct layout_stride {
namespace detail {

template <class Layout, class Extents, class Mapping>
constexpr void terminate_if_invalid_strides(with_rank<0>, Layout, const Extents&, const Mapping&)
constexpr void validate_strides(with_rank<0>, Layout, const Extents&, const Mapping&)
{}

template <std::size_t N, class Layout, class Extents, class Mapping>
constexpr void terminate_if_invalid_strides(with_rank<N>, Layout, const Extents& ext, const Mapping& other)
constexpr void validate_strides(with_rank<N>, Layout, const Extents& ext, const Mapping& other)
{
static_assert(std::is_same<typename Mapping::layout_type, layout_stride>::value and
(std::is_same<Layout, layout_left>::value or
Expand All @@ -597,10 +596,9 @@ constexpr void terminate_if_invalid_strides(with_rank<N>, Layout, const Extents&
for (std::size_t r = 0; r < N; r++) {
const std::size_t s = is_left ? r : N - 1 - r;

if (not common_integral_compare(stride, other.stride(s))) {
// assigning to layout_{left,right} with invalid strides
std::abort();
}
MDSPAN_PRECONDITION(common_integral_compare(stride, other.stride(s))
and "invalid strides for layout_{left,right}");

stride *= ext.extent(s);
}
}
Expand Down
48 changes: 48 additions & 0 deletions include/experimental/__p0009_bits/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "config.hpp"

#include <cstdio>
#include <cstdlib>
#include <type_traits> // std::is_void

#ifndef _MDSPAN_HOST_DEVICE
Expand Down Expand Up @@ -101,6 +103,52 @@
#define MDSPAN_IMPL_STANDARD_NAMESPACE_STRING MDSPAN_PP_STRINGIFY(MDSPAN_IMPL_STANDARD_NAMESPACE)
#define MDSPAN_IMPL_PROPOSED_NAMESPACE_STRING MDSPAN_PP_STRINGIFY(MDSPAN_IMPL_STANDARD_NAMESPACE) "::" MDSPAN_PP_STRINGIFY(MDSPAN_IMPL_PROPOSED_NAMESPACE)

namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
namespace detail {

inline void default_precondition_violation_handler(const char* cond, const char* file, unsigned line)
{
(void)std::fprintf(::stderr, "%s:%u: precondition failure: `%s`\n", file, line, cond);
std::abort();
}

} // namespace detail
} // namespace MDSPAN_IMPL_STANDARD_NAMESPACE

#ifndef MDSPAN_PRECONDITION_VIOLATION_HANDLER
#define MDSPAN_PRECONDITION_VIOLATION_HANDLER(cond, file, line) \
MDSPAN_IMPL_STANDARD_NAMESPACE::detail::default_precondition_violation_handler(cond, file, line)
#endif

#ifndef MDSPAN_SKIP_PRECONDITION_VIOLATION_HANDLER
#ifndef NDEBUG
#define MDSPAN_SKIP_PRECONDITION_VIOLATION_HANDLER 0
#else
#define MDSPAN_SKIP_PRECONDITION_VIOLATION_HANDLER 1
#endif
#endif

namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
namespace detail {

template <bool skip = MDSPAN_SKIP_PRECONDITION_VIOLATION_HANDLER>
constexpr void precondition(const char* cond, const char* file, unsigned line)
{
if (skip) { return; }

MDSPAN_PRECONDITION_VIOLATION_HANDLER(cond, file, line);
}

} // namespace detail
} // namespace MDSPAN_IMPL_STANDARD_NAMESPACE

#define MDSPAN_PRECONDITION(...) \
do { \
if (not (__VA_ARGS__)) { \
MDSPAN_IMPL_STANDARD_NAMESPACE::detail::precondition(#__VA_ARGS__, __FILE__, __LINE__); \
} \
} while (0)

// </editor-fold> end Preprocessor helpers }}}1
//==============================================================================

Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ macro(mdspan_add_test name)
target_link_libraries(${name} mdspan gtest_main)
add_test(${name} ${name})
set_property(TARGET ${name} PROPERTY COMPILE_WARNING_AS_ERROR ON)
target_compile_definitions(${name} PUBLIC MDSPAN_SKIP_PRECONDITION_VIOLATION_HANDLER=0)
endmacro()

if(MDSPAN_USE_SYSTEM_GTEST)
Expand Down Expand Up @@ -49,6 +50,8 @@ else()
)
endif()

mdspan_add_test(test_alternate_precondition_violation_handler)
mdspan_add_test(test_macros)
mdspan_add_test(test_extents)
mdspan_add_test(test_mdspan_ctors)
mdspan_add_test(test_mdspan_swap)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_alternate_precondition_violation_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdexcept>

#define MDSPAN_PRECONDITION_VIOLATION_HANDLER(cond, file, line) \
do { \
throw std::logic_error{"precondition failure"}; \
} while (0);

#include <mdspan/mdspan.hpp>
#include <gtest/gtest.h>

TEST(mdspan_macros, alternate_precondition_violation_handler)
{
ASSERT_THROW(MDSPAN_PRECONDITION(false), std::logic_error);
}

TEST(mdspan_macros, alternate_precondition_check_constexpr_invocable)
{
struct fn
{
constexpr auto operator()() const
{
MDSPAN_PRECONDITION(1 + 1 == 2);
return 42;
}
};

static constexpr auto x = fn{}();
(void)x;
}
9 changes: 9 additions & 0 deletions tests/test_layout_ctors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ TEST(TestLayoutLeftListInitialization, test_layout_left_extent_initialization) {
ASSERT_TRUE(m.is_exhaustive());
}

TEST(TestConvertingConstructionFromLayoutStride, precondition_failure) {
using E = Kokkos::extents<size_t, 2, 2>;

const auto stride = Kokkos::layout_stride::mapping<E>{E{}, std::array<size_t, 2>{2, 2}};

ASSERT_DEATH(Kokkos::layout_left::mapping<E>{stride}, "invalid strides");
ASSERT_DEATH(Kokkos::layout_right::mapping<E>{stride}, "invalid strides");
}

// FIXME: CUDA NVCC including 12.0 does not like CTAD on nested classes
#if defined(_MDSPAN_USE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION) && !defined(__NVCC__)
TEST(TestLayoutLeftCTAD, test_layout_left_ctad) {
Expand Down
24 changes: 24 additions & 0 deletions tests/test_macros.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <mdspan/mdspan.hpp>
#include <gtest/gtest.h>

TEST(mdspan_macros, precondition_violation)
{
constexpr auto msg = "hello, world!";

ASSERT_DEATH(MDSPAN_PRECONDITION(false and "hello, world!"), msg);
}

TEST(mdspan_macros, precondition_check_constexpr_invocable)
{
struct fn
{
constexpr auto operator()() const
{
MDSPAN_PRECONDITION(1 + 1 == 2);
return 42;
}
};

static constexpr auto x = fn{}();
(void)x;
}

0 comments on commit 22258de

Please sign in to comment.