forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for overflow during backend initialization (Cuda, HIP, SYCL) (k…
…okkos#6159) * Fix potential overflow * Rewrite update condition to avoid overflow * Avoid overflow when setting scratch flags * Restrict overflow check to unsigned integral types * Rework `multiply_overflow` * Use `||` instead of `or` (MSVC fix) * Fix SYCL build - use correct namespace - only used overflow detection when necessary * Clean up unit tests * Use correct variables and consistent formatting * Add a FIXME for SYCL sizeScratchGrain Co-authored-by: Daniel Arndt <arndtd@ornl.gov> * Add `multiply_overflow_abort` helper --------- Co-authored-by: Daniel Arndt <arndtd@ornl.gov>
- Loading branch information
1 parent
4d1c6c3
commit ced2451
Showing
6 changed files
with
185 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//@HEADER | ||
// ************************************************************************ | ||
// | ||
// Kokkos v. 4.0 | ||
// Copyright (2022) National Technology & Engineering | ||
// Solutions of Sandia, LLC (NTESS). | ||
// | ||
// Under the terms of Contract DE-NA0003525 with NTESS, | ||
// the U.S. Government retains certain rights in this software. | ||
// | ||
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://kokkos.org/LICENSE for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//@HEADER | ||
|
||
#ifndef KOKKOS_CHECKED_INTEGER_OPS_HPP | ||
#define KOKKOS_CHECKED_INTEGER_OPS_HPP | ||
|
||
#include <type_traits> | ||
|
||
#include <impl/Kokkos_Error.hpp> | ||
|
||
namespace Kokkos { | ||
namespace Impl { | ||
|
||
template <typename T> | ||
std::enable_if_t<std::is_integral_v<T>, bool> constexpr multiply_overflow( | ||
T a, T b, T& res) { | ||
static_assert(std::is_unsigned_v<T>, | ||
"Operation not implemented for signed integers."); | ||
auto product = a * b; | ||
if ((a == 0) || (b == 0) || (a == product / b)) { | ||
res = product; | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
template <typename T> | ||
T multiply_overflow_abort(T a, T b) { | ||
T result; | ||
if (multiply_overflow(a, b, result)) | ||
Kokkos::abort("Arithmetic overflow detected."); | ||
|
||
return result; | ||
} | ||
|
||
} // namespace Impl | ||
} // namespace Kokkos | ||
|
||
#endif // KOKKOS_CHECKED_INTEGER_OPS_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//@HEADER | ||
// ************************************************************************ | ||
// | ||
// Kokkos v. 4.0 | ||
// Copyright (2022) National Technology & Engineering | ||
// Solutions of Sandia, LLC (NTESS). | ||
// | ||
// Under the terms of Contract DE-NA0003525 with NTESS, | ||
// the U.S. Government retains certain rights in this software. | ||
// | ||
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://kokkos.org/LICENSE for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//@HEADER | ||
|
||
#include <gtest/gtest.h> | ||
#include <impl/Kokkos_CheckedIntegerOps.hpp> | ||
#include <limits> | ||
|
||
namespace { | ||
|
||
TEST(TEST_CATEGORY, checked_integer_operations_multiply_overflow) { | ||
{ | ||
auto result = 1u; | ||
auto is_overflow = Kokkos::Impl::multiply_overflow(1u, 2u, result); | ||
EXPECT_EQ(result, 2u); | ||
EXPECT_FALSE(is_overflow); | ||
} | ||
{ | ||
auto result = 1u; | ||
auto is_overflow = Kokkos::Impl::multiply_overflow( | ||
std::numeric_limits<unsigned>::max(), 2u, result); | ||
EXPECT_TRUE(is_overflow); | ||
} | ||
} | ||
|
||
TEST(TEST_CATEGORY, checked_integer_operations_multiply_overflow_abort) { | ||
{ | ||
auto result = Kokkos::Impl::multiply_overflow_abort(1u, 2u); | ||
EXPECT_EQ(result, 2u); | ||
} | ||
{ | ||
ASSERT_DEATH(Kokkos::Impl::multiply_overflow_abort( | ||
std::numeric_limits<unsigned>::max(), 2u), | ||
"Arithmetic overflow detected."); | ||
} | ||
} | ||
|
||
} // namespace |