Skip to content

Commit

Permalink
Merge pull request #17744 from Rombur/hipcc_debug
Browse files Browse the repository at this point in the history
Replace std::array with Kokkos::Array in Tensor
  • Loading branch information
bangerth authored Nov 1, 2024
2 parents be27d31 + 208981d commit 2bfd2f2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
13 changes: 9 additions & 4 deletions cmake/modules/FindDEAL_II_KOKKOS.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ endif()


if(Kokkos_FOUND)
if (Kokkos_ENABLE_CUDA OR Kokkos_ENABLE_HIP)
# In version older than 3.7.0, Kokkos::Array::operator[] is not constexpr,
# so we use std::array instead.
if (KOKKOS_VERSION VERSION_LESS 3.7.0)
# We are using std::array in device code which calls the host-only function
# __glibcxx_requires_subscript when defining _GLIBCXX_ASSERTIONS
list(REMOVE_ITEM DEAL_II_DEFINITIONS_DEBUG "_GLIBCXX_ASSERTIONS")
endif()
endif()
if(Kokkos_ENABLE_CUDA)
# We need to disable SIMD vectorization for CUDA device code.
# Otherwise, nvcc compilers from version 9 on will emit an error message like:
Expand All @@ -60,10 +69,6 @@ if(Kokkos_FOUND)
# __host__ constexpr functions in device code
KOKKOS_CHECK(OPTIONS CUDA_LAMBDA CUDA_CONSTEXPR)

# We are using std::array in device code which calls the host-only function
# __glibcxx_requires_subscript when defining _GLIBCXX_ASSERTIONS
list(REMOVE_ITEM DEAL_II_DEFINITIONS_DEBUG "_GLIBCXX_ASSERTIONS")

# Disable a bunch of annoying warnings generated by boost, template code,
# and in other random places:
#
Expand Down
45 changes: 25 additions & 20 deletions include/deal.II/base/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,12 @@
#include <deal.II/base/template_constraints.h>
#include <deal.II/base/tensor_accessors.h>

#include <Kokkos_Array.hpp>

#ifdef DEAL_II_WITH_ADOLC
# include <adolc/adouble.h> // Taped double
#endif

// boost::serialization::make_array used to be in array.hpp, but was
// moved to a different file in BOOST 1.64
#include <boost/version.hpp>
#if BOOST_VERSION >= 106400
# include <boost/serialization/array_wrapper.hpp>
#else
# include <boost/serialization/array.hpp>
#endif


#include <array>
#include <cmath>
#include <ostream>
#include <type_traits>
Expand Down Expand Up @@ -848,9 +839,15 @@ class Tensor
* a rank-1 tensor, then we simply need an array of scalars.
* Otherwise, it is an array of tensors one rank lower.
*/
#if KOKKOS_VERSION >= 30700
std::conditional_t<rank_ == 1,
Kokkos::Array<Number, dim>,
Kokkos::Array<Tensor<rank_ - 1, dim, Number>, dim>>
#else
std::conditional_t<rank_ == 1,
std::array<Number, dim>,
std::array<Tensor<rank_ - 1, dim, Number>, dim>>
#endif
values;

/**
Expand Down Expand Up @@ -1262,17 +1259,17 @@ template <int rank_, int dim, typename Number>
constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE
Tensor<rank_, dim, Number>::Tensor()
: values(
// In order to initialize the std::array<Number,dim>, we would need a
// In order to initialize the Kokkos::Array<Number,dim>, we would need a
// brace-enclosed list of length 'dim'. There is no way in C++ to create
// such a list in-place, but we can come up with a lambda function that
// expands such a list via template-pack expansion, and then uses this
// list to initialize a std::array which it then returns.
// list to initialize a Kokkos::Array which it then returns.
//
// The trick to come up with such a lambda function is to have a function
// that takes an argument that depends on a template-pack of integers.
// We will call the function with an integer list of length 'dim', and
// in the function itself expand that pack in a way that it serves as
// a brace-enclosed list of initializers for a std::array.
// a brace-enclosed list of initializers for a Kokkos::Array.
//
// Of course, we do not want to initialize the array with the integers,
// but with zeros. (Or, more correctly, a zero of the element type.)
Expand All @@ -1295,7 +1292,11 @@ Tensor<rank_, dim, Number>::Tensor()
// returning a zero instead.
[]<std::size_t... I>(
const std::index_sequence<I...> &) constexpr -> decltype(values) {
if constexpr (rank_ == 1)
if constexpr (dim == 0)
{
return {};
}
else if constexpr (rank_ == 1)
{
auto get_zero_and_ignore_argument = [](int) {
return internal::NumberType<Number>::value(0.0);
Expand Down Expand Up @@ -1328,7 +1329,11 @@ namespace internal
namespace TensorInitialization
{
template <int rank, int dim, typename Number, std::size_t... I>
# if KOKKOS_VERSION >= 30700
constexpr Kokkos::Array<typename Tensor<rank, dim, Number>::value_type, dim>
# else
constexpr std::array<typename Tensor<rank, dim, Number>::value_type, dim>
# endif
make_zero_array(const std::index_sequence<I...> &)
{
static_assert(sizeof...(I) == dim, "This is bad.");
Expand Down Expand Up @@ -1819,7 +1824,7 @@ Tensor<rank_, dim, Number>::unroll(const Iterator begin,
ExcMessage(
"The provided iterator range must contain at least 'dim' "
"elements."));
std::copy(std::begin(values), std::end(values), begin);
std::copy(values.data(), values.data() + values.size(), begin);
}
}

Expand Down Expand Up @@ -1895,10 +1900,10 @@ template <class Archive>
inline void
Tensor<rank_, dim, Number>::serialize(Archive &ar, const unsigned int)
{
if constexpr (rank_ > 1)
ar &values;
else
ar &boost::serialization::make_array(&values[0], dim);
for (int i = 0; i < dim; ++i)
{
ar &values[i];
}
}


Expand Down
2 changes: 1 addition & 1 deletion tests/serialization/tensor.output
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

DEAL::0 0 0 0 3 0 0 1.00000000000000000e+00 2.00000000000000000e+00 3.00000000000000000e+00 4.00000000000000000e+00 5.00000000000000000e+00 6.00000000000000000e+00 7.00000000000000000e+00 8.00000000000000000e+00 9.00000000000000000e+00
DEAL::0 0 0 0 1.00000000000000000e+00 2.00000000000000000e+00 3.00000000000000000e+00 4.00000000000000000e+00 5.00000000000000000e+00 6.00000000000000000e+00 7.00000000000000000e+00 8.00000000000000000e+00 9.00000000000000000e+00

DEAL::OK

0 comments on commit 2bfd2f2

Please sign in to comment.