Skip to content

Commit

Permalink
Replace assert with runtime error (#122)
Browse files Browse the repository at this point in the history
* replace assert with std::runtime_error

* Add corresponding throw tests

* use check_precondition in get_shift function

* fix conflicts

* fix conflicts

* fix conflicts

---------

Co-authored-by: Yuuichi Asahi <[email protected]>
  • Loading branch information
yasahi-hpc and Yuuichi Asahi authored Jul 30, 2024
1 parent 72c7c8e commit 0b1ab3d
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 9 deletions.
8 changes: 6 additions & 2 deletions common/src/KokkosFFT_Helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ auto get_shift(const ViewType& inout, axis_type<DIM> _axes, int direction = 1) {

// Assert if the elements are overlapped
constexpr int rank = ViewType::rank();
assert(!KokkosFFT::Impl::has_duplicate_values(axes));
assert(!KokkosFFT::Impl::is_out_of_range_value_included(axes, rank));
KOKKOSFFT_EXPECTS(!KokkosFFT::Impl::has_duplicate_values(axes),
"Axes overlap");
KOKKOSFFT_EXPECTS(
!KokkosFFT::Impl::is_out_of_range_value_included(axes, rank),
"Axes include an out-of-range index."
"Axes must be in the range of [-rank, rank-1].");

axis_type<rank> shift = {0};
for (int i = 0; i < static_cast<int>(DIM); i++) {
Expand Down
14 changes: 10 additions & 4 deletions common/src/KokkosFFT_layouts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ auto get_extents(const InViewType& in, const OutViewType& out,
if (is_real_v<in_value_type>) {
// Then R2C
if (is_complex_v<out_value_type>) {
assert(_out_extents.at(inner_most_axis) ==
_in_extents.at(inner_most_axis) / 2 + 1);
KOKKOSFFT_EXPECTS(
_out_extents.at(inner_most_axis) ==
_in_extents.at(inner_most_axis) / 2 + 1,
"For R2C, the 'output extent' of transform must be equal to "
"'input extent'/2 + 1");
} else {
throw std::runtime_error(
"If the input type is real, the output type should be complex");
Expand All @@ -78,8 +81,11 @@ auto get_extents(const InViewType& in, const OutViewType& out,
if (is_real_v<out_value_type>) {
// Then C2R
if (is_complex_v<in_value_type>) {
assert(_in_extents.at(inner_most_axis) ==
_out_extents.at(inner_most_axis) / 2 + 1);
KOKKOSFFT_EXPECTS(
_in_extents.at(inner_most_axis) ==
_out_extents.at(inner_most_axis) / 2 + 1,
"For C2R, the 'input extent' of transform must be equal to "
"'output extent' / 2 + 1");
} else {
throw std::runtime_error(
"If the output type is real, the input type should be complex");
Expand Down
8 changes: 6 additions & 2 deletions common/src/KokkosFFT_padding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ auto get_modified_shape(const InViewType in, const OutViewType /* out */,
}

// Assert if the elements are overlapped
assert(!KokkosFFT::Impl::has_duplicate_values(positive_axes));
assert(!KokkosFFT::Impl::is_out_of_range_value_included(positive_axes, rank));
KOKKOSFFT_EXPECTS(!KokkosFFT::Impl::has_duplicate_values(positive_axes),
"Axes overlap");
KOKKOSFFT_EXPECTS(
!KokkosFFT::Impl::is_out_of_range_value_included(positive_axes, rank),
"Axes include an out-of-range index."
"Axes must be in the range of [-rank, rank-1].");

using full_shape_type = shape_type<rank>;
full_shape_type modified_shape;
Expand Down
35 changes: 34 additions & 1 deletion common/src/KokkosFFT_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,52 @@

#if defined(KOKKOS_ENABLE_CXX17)
#include <cstdlib>
#define KOKKOSFFT_EXPECTS(expression, msg) \
KokkosFFT::Impl::check_precondition((expression), msg, __FILE__, __LINE__, \
__FUNCTION__)
#else
#include <source_location>
#define KOKKOSFFT_EXPECTS(expression, msg) \
KokkosFFT::Impl::check_precondition( \
(expression), msg, std::source_location::current().file_name(), \
std::source_location::current().line(), \
std::source_location::current().function_name(), \
std::source_location::current().column())
#endif

namespace KokkosFFT {
namespace Impl {

inline void check_precondition(const bool expression,
[[maybe_unused]] const std::string& msg,
[[maybe_unused]] const char* file_name, int line,
[[maybe_unused]] const char* function_name,
[[maybe_unused]] const int column = -1) {
// Quick return if possible
if (expression) return;

std::stringstream ss("file: ");
if (column == -1) {
// For C++ 17
ss << file_name << '(' << line << ") `" << function_name << "`: " << msg
<< '\n';
} else {
// For C++ 20 and later
ss << file_name << '(' << line << ':' << column << ") `" << function_name
<< "`: " << msg << '\n';
}
throw std::runtime_error(ss.str());
}

template <typename ViewType>
auto convert_negative_axis(ViewType, int _axis = -1) {
static_assert(Kokkos::is_view<ViewType>::value,
"convert_negative_axis: ViewType is not a Kokkos::View.");
int rank = static_cast<int>(ViewType::rank());
assert(_axis >= -rank && _axis < rank); // axis should be in [-rank, rank-1]

KOKKOSFFT_EXPECTS(_axis >= -rank && _axis < rank,
"Axis must be in [-rank, rank-1]");

int axis = _axis < 0 ? rank + _axis : _axis;
return axis;
}
Expand Down
Loading

0 comments on commit 0b1ab3d

Please sign in to comment.