Skip to content

Commit

Permalink
Guard [MD]RangePolicy precondition check for deprecated code 4 (kok…
Browse files Browse the repository at this point in the history
…kos#6726)

* Guard [MD]RangePolicy precondition check for deprecated code 4

* No good reason to use a raw string literal for the warning msg

* Drop pointless inline specifier

* Restore original behavior when deprecated code 4 is enabled

* first blush

* Fixup obviously wont display both message when aborting

* Trust me, it's not badly written. It's just way above your head.

* Add assertions for the old behavior

* Fiddling with string comparison

* Attempt to resolve death tests error msg matching issues

* Fix that stupid regex
  • Loading branch information
dalg24 committed Jan 20, 2024
1 parent 8a91490 commit 6912b39
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 19 deletions.
6 changes: 5 additions & 1 deletion core/src/KokkosExp_MDRangePolicy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,12 @@ struct MDRangePolicy : public Kokkos::Impl::PolicyTraits<Properties...> {
"Kokkos::MDRangePolicy bounds error: The lower bound (" +
std::to_string(m_lower[i]) + ") is greater than its upper bound (" +
std::to_string(m_upper[i]) + ") in dimension " + std::to_string(i) +
".";
".\n";
#if !defined(KOKKOS_ENABLE_DEPRECATED_CODE_4)
Kokkos::abort(msg.c_str());
#elif defined(KOKKOS_ENABLE_DEPRECATION_WARNINGS)
Kokkos::Impl::log_warning(msg);
#endif
}

if (m_tile[i] <= 0) {
Expand Down
12 changes: 9 additions & 3 deletions core/src/Kokkos_ExecPolicy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ class RangePolicy : public Impl::PolicyTraits<Properties...> {
/** \brief Total range */
inline RangePolicy(const member_type work_begin, const member_type work_end)
: RangePolicy(typename traits::execution_space(), work_begin, work_end) {
check_bounds_validity();
set_auto_chunk_size();
}

Expand Down Expand Up @@ -222,13 +221,20 @@ class RangePolicy : public Impl::PolicyTraits<Properties...> {
m_granularity_mask = m_granularity - 1;
}

inline void check_bounds_validity() {
void check_bounds_validity() {
if (m_end < m_begin) {
std::string msg = "Kokkos::RangePolicy bounds error: The lower bound (" +
std::to_string(m_begin) +
") is greater than the upper bound (" +
std::to_string(m_end) + ").";
std::to_string(m_end) + ").\n";
#ifndef KOKKOS_ENABLE_DEPRECATED_CODE_4
Kokkos::abort(msg.c_str());
#endif
m_begin = 0;
m_end = 0;
#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
Kokkos::Impl::log_warning(msg);
#endif
}
}

Expand Down
12 changes: 10 additions & 2 deletions core/src/impl/Kokkos_Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
#include <cstring>
#include <cstdlib>

#include <ostream>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdexcept>
#include <Kokkos_Core.hpp> // show_warnings
#include <impl/Kokkos_Error.hpp>
#include <Cuda/Kokkos_Cuda_Error.hpp>

Expand All @@ -38,6 +39,12 @@ void throw_runtime_exception(const std::string &msg) {
throw std::runtime_error(msg);
}

void log_warning(const std::string &msg) {
if (show_warnings()) {
std::cerr << msg << std::flush;
}
}

std::string human_memory_size(size_t arg_bytes) {
double bytes = arg_bytes;
const double K = 1024;
Expand All @@ -64,7 +71,8 @@ std::string human_memory_size(size_t arg_bytes) {

void Experimental::RawMemoryAllocationFailure::print_error_message(
std::ostream &o) const {
o << "Allocation of size " << Impl::human_memory_size(m_attempted_size);
o << "Allocation of size "
<< ::Kokkos::Impl::human_memory_size(m_attempted_size);
o << " failed";
switch (m_failure_mode) {
case FailureMode::OutOfMemoryError:
Expand Down
2 changes: 2 additions & 0 deletions core/src/impl/Kokkos_Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace Impl {

[[noreturn]] void throw_runtime_exception(const std::string &msg);

void log_warning(const std::string &msg);

std::string human_memory_size(size_t arg_bytes);

} // namespace Impl
Expand Down
43 changes: 35 additions & 8 deletions core/unit_test/TestMDRangePolicyConstructors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <Kokkos_Core.hpp>

#include <regex>

namespace {

template <class IndexType>
Expand Down Expand Up @@ -99,15 +101,40 @@ TEST(TEST_CATEGORY_DEATH, policy_invalid_bounds) {

::testing::FLAGS_gtest_death_test_style = "threadsafe";

auto dim = (Policy::inner_direction == Kokkos::Iterate::Right) ? 1 : 0;
auto [dim0, dim1] = (Policy::inner_direction == Kokkos::Iterate::Right)
? std::make_pair(1, 0)
: std::make_pair(0, 1);
std::string msg1 =
"Kokkos::MDRangePolicy bounds error: The lower bound (100) is greater "
"than its upper bound (90) in dimension " +
std::to_string(dim0) + ".\n";

ASSERT_DEATH(
{
(void)Policy({100, 100}, {90, 90});
},
"Kokkos::MDRangePolicy bounds error: The lower bound \\(100\\) is "
"greater than its upper bound \\(90\\) in dimension " +
std::to_string(dim) + "\\.");
std::string msg2 =
"Kokkos::MDRangePolicy bounds error: The lower bound (100) is greater "
"than its upper bound (90) in dimension " +
std::to_string(dim1) + ".\n";

#if !defined(KOKKOS_ENABLE_DEPRECATED_CODE_4)
// escape the parentheses in the regex to match the error message
msg1 = std::regex_replace(msg1, std::regex("\\(|\\)"), "\\$&");
(void)msg2;
ASSERT_DEATH({ (void)Policy({100, 100}, {90, 90}); }, msg1);
#else
if (!Kokkos::show_warnings()) {
GTEST_SKIP() << "Kokkos warning messages are disabled";
}

::testing::internal::CaptureStderr();
(void)Policy({100, 100}, {90, 90});
#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
ASSERT_EQ(::testing::internal::GetCapturedStderr(), msg1 + msg2);
#else
ASSERT_TRUE(::testing::internal::GetCapturedStderr().empty());
(void)msg1;
(void)msg2;
#endif

#endif
}
#endif

Expand Down
48 changes: 43 additions & 5 deletions core/unit_test/TestRangePolicyConstructors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <Kokkos_Core.hpp>

#include <regex>

namespace {

TEST(TEST_CATEGORY, range_policy_runtime_parameters) {
Expand Down Expand Up @@ -74,13 +76,49 @@ TEST(TEST_CATEGORY_DEATH, range_policy_invalid_bounds) {
using Policy = Kokkos::RangePolicy<TEST_EXECSPACE>;
using ChunkSize = Kokkos::ChunkSize;

ASSERT_DEATH({ (void)Policy(100, 90); },
"Kokkos::RangePolicy bounds error: The lower bound \\(100\\) is "
"greater than the upper bound \\(90\\)\\.");
std::string msg =
"Kokkos::RangePolicy bounds error: The lower bound (100) is greater than "
"the upper bound (90).\n";
#ifndef KOKKOS_ENABLE_DEPRECATED_CODE_4
// escape the parentheses in the regex to match the error message
msg = std::regex_replace(msg, std::regex("\\(|\\)"), "\\$&");
ASSERT_DEATH({ (void)Policy(100, 90); }, msg);

ASSERT_DEATH({ (void)Policy(TEST_EXECSPACE(), 100, 90, ChunkSize(10)); },
"Kokkos::RangePolicy bounds error: The lower bound \\(100\\) is "
"greater than the upper bound \\(90\\)\\.");
msg);
#else

if (!Kokkos::show_warnings()) {
GTEST_SKIP() << "Kokkos warning messages are disabled";
}

{
::testing::internal::CaptureStderr();
Policy policy(100, 90);
ASSERT_EQ((int)policy.begin(), 0);
ASSERT_EQ((int)policy.end(), 0);
#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
ASSERT_EQ(::testing::internal::GetCapturedStderr(), msg);
#else
ASSERT_TRUE(::testing::internal::GetCapturedStderr().empty());
(void)msg;
#endif
}

{
::testing::internal::CaptureStderr();
Policy policy(TEST_EXECSPACE(), 100, 90, ChunkSize(10));
ASSERT_EQ((int)policy.begin(), 0);
ASSERT_EQ((int)policy.end(), 0);
#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
ASSERT_EQ(::testing::internal::GetCapturedStderr(), msg);
#else
ASSERT_TRUE(::testing::internal::GetCapturedStderr().empty());
(void)msg;
#endif
}

#endif
}

} // namespace

0 comments on commit 6912b39

Please sign in to comment.