Skip to content

Commit

Permalink
boost test compliance and renaming pre-conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlag31 committed Sep 15, 2023
1 parent d3b6e8d commit 0618fcf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,42 @@ namespace ArborX::Interpolation::Details
{

template <typename Matrix>
KOKKOS_INLINE_FUNCTION void isSquareMatrix(Matrix const &mat)
KOKKOS_INLINE_FUNCTION void ensureIsSquareMatrix(Matrix const &mat)
{
static_assert(Kokkos::is_view_v<Matrix>, "Matrix must be a view");
static_assert(Matrix::rank == 2, "Matrix must be 2D");
KOKKOS_ASSERT(mat.extent(0) == mat.extent(1));
}

template <typename Matrix>
KOKKOS_INLINE_FUNCTION void isSquareSymmetricMatrix(Matrix const &mat)
KOKKOS_INLINE_FUNCTION void ensureIsSquareSymmetricMatrix(Matrix const &mat)
{
isSquareMatrix(mat);
ensureIsSquareMatrix(mat);
using value_t = typename Matrix::non_const_value_type;
auto isSymmetric = [&]() {
int size = mat.extent(0);
for (int i = 0; i < size; i++)
for (int j = i + 1; j < size; j++)
{
auto const val = Kokkos::abs(mat(i, j) - mat(j, i));
auto const ref = Kokkos::abs(mat(i, j));
static constexpr value_t epsilon =
Kokkos::Experimental::epsilon_v<float>;
if (ref == value_t(0) && val > epsilon)
return false;
if (ref != value_t(0) && val / ref > epsilon)
return false;
}
return true;
};
KOKKOS_ASSERT(isSymmetric());

bool is_symmetric = true;
int size = mat.extent(0);
for (int i = 0; i < size; i++)
for (int j = i + 1; j < size; j++)
{
auto const val = Kokkos::abs(mat(i, j) - mat(j, i));
auto const ref = Kokkos::abs(mat(i, j));
static constexpr value_t epsilon =
Kokkos::Experimental::epsilon_v<float>;
if (ref == value_t(0) && val > epsilon)
is_symmetric = false;
if (ref != value_t(0) && val / ref > epsilon)
is_symmetric = false;
}

KOKKOS_ASSERT(is_symmetric);
}

// Gets the argmax from the upper triangle part of a matrix
template <typename Matrix>
KOKKOS_FUNCTION auto argmaxUpperTriangle(Matrix const &mat)
{
isSquareMatrix(mat);
ensureIsSquareMatrix(mat);
using value_t = typename Matrix::non_const_value_type;
int const size = mat.extent(0);

Expand Down Expand Up @@ -91,13 +91,13 @@ template <typename AMatrix, typename ESMatrix, typename UMatrix>
KOKKOS_FUNCTION void
symmetricPseudoInverseSVDSerialKernel(AMatrix &A, ESMatrix &ES, UMatrix &U)
{
isSquareSymmetricMatrix(A);
ensureIsSquareSymmetricMatrix(A);
static_assert(!std::is_const_v<typename AMatrix::value_type>,
"A must be writable");
isSquareMatrix(ES);
ensureIsSquareMatrix(ES);
static_assert(!std::is_const_v<typename ESMatrix::value_type>,
"ES must be writable");
isSquareMatrix(U);
ensureIsSquareMatrix(U);
static_assert(!std::is_const_v<typename UMatrix::value_type>,
"U must be writable");
static_assert(std::is_same_v<typename AMatrix::value_type,
Expand Down
24 changes: 18 additions & 6 deletions test/ArborX_EnableViewComparison.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@
#include <boost/test/unit_test.hpp>
#include <boost/test/utils/is_forward_iterable.hpp>

template <typename U, typename V>
using CommonValueType =
typename boost::common_type<typename U::non_const_value_type,
typename V::non_const_value_type>::type;

template <typename U, typename V>
void arborxViewCheck(U const &u, V const &v, std::string const &u_name,
std::string const &v_name, double tol = 0.)
std::string const &v_name, CommonValueType<U, V> tol = 0)
{
static constexpr int rank = U::rank;

bool same_dim_size = true;
for (int i = 0; i < rank; i++)
{
int ui = u.extent(i), vi = v.extent(i);
BOOST_TEST(ui == vi, u_name << " == " << v_name << " dimension " << i
<< " sizes"
<< boost::test_tools::tolerance(0.));
BOOST_TEST(ui == vi, "check " << u_name << " == " << v_name
<< " failed at dimension " << i << " size ["
<< ui << " != " << vi << "]");
same_dim_size = (ui == vi) && same_dim_size;
}

Expand All @@ -58,8 +63,15 @@ void arborxViewCheck(U const &u, V const &v, std::string const &u_name,
index[5], index[6], index[7]);
std::string index_str = make_index();

BOOST_TEST(uval == vval, u_name << " == " << v_name << " at " << index_str
<< boost::test_tools::tolerance(tol));
// Can "tol" be used as a tolerance value? If not, go back to regular
// comparison
if constexpr (boost::math::fpc::tolerance_based<decltype(tol)>::value)
BOOST_TEST(uval == vval, u_name << " == " << v_name << " at " << index_str
<< boost::test_tools::tolerance(tol));
else
BOOST_TEST(uval == vval, "check " << u_name << " == " << v_name
<< " failed at " << index_str << " ["
<< uval << " != " << vval << "]");

index[rank - 1]++;
for (int i = rank - 1; i > 0; i--)
Expand Down
1 change: 0 additions & 1 deletion test/tstInterpDetailsSVD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "BoostTest_CUDA_clang_workarounds.hpp"
#include <boost/test/unit_test.hpp>

namespace tt = boost::test_tools;
namespace axid = ArborX::Interpolation::Details;

template <typename ES, typename U, typename V>
Expand Down

0 comments on commit 0618fcf

Please sign in to comment.