Skip to content

Commit

Permalink
Fix a bug when using realloc on views of non-default constructible el…
Browse files Browse the repository at this point in the history
…ement types (kokkos#6993)

* Add few missing constexpr for alloc_prop_input

Co-authored-by: Daniel Arndt <arndtd@ornl.gov>

* Update tests

* Fix DualView

* Address review comments

* Add missing decorators

* Move NoDefaultConstructor out of function

---------

Co-authored-by: Daniel Arndt <arndtd@ornl.gov>
  • Loading branch information
aprokop and masterleinad committed May 8, 2024
1 parent e4cc686 commit d61d75a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
8 changes: 4 additions & 4 deletions containers/src/Kokkos_DualView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,13 +944,13 @@ class DualView : public ViewTraits<DataType, Properties...> {

if (sizeMismatch) {
::Kokkos::realloc(arg_prop, d_view, n0, n1, n2, n3, n4, n5, n6, n7);
if (alloc_prop_input::initialize) {
if constexpr (alloc_prop_input::initialize) {
h_view = create_mirror_view(typename t_host::memory_space(), d_view);
} else {
h_view = create_mirror_view(Kokkos::WithoutInitializing,
typename t_host::memory_space(), d_view);
}
} else if (alloc_prop_input::initialize) {
} else if constexpr (alloc_prop_input::initialize) {
if constexpr (alloc_prop_input::has_execution_space) {
const auto& exec_space =
Impl::get_property<Impl::ExecutionSpaceTag>(arg_prop);
Expand Down Expand Up @@ -1038,7 +1038,7 @@ class DualView : public ViewTraits<DataType, Properties...> {
/* Resize on Device */
if (sizeMismatch) {
::Kokkos::resize(properties, d_view, n0, n1, n2, n3, n4, n5, n6, n7);
if (alloc_prop_input::initialize) {
if constexpr (alloc_prop_input::initialize) {
h_view = create_mirror_view(typename t_host::memory_space(), d_view);
} else {
h_view = create_mirror_view(Kokkos::WithoutInitializing,
Expand All @@ -1054,7 +1054,7 @@ class DualView : public ViewTraits<DataType, Properties...> {
/* Resize on Host */
if (sizeMismatch) {
::Kokkos::resize(properties, h_view, n0, n1, n2, n3, n4, n5, n6, n7);
if (alloc_prop_input::initialize) {
if constexpr (alloc_prop_input::initialize) {
d_view = create_mirror_view(typename t_dev::memory_space(), h_view);

} else {
Expand Down
10 changes: 8 additions & 2 deletions core/src/Kokkos_CopyViews.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3235,7 +3235,10 @@ impl_realloc(Kokkos::View<T, P...>& v, const size_t n0, const size_t n1,
v = view_type(); // Best effort to deallocate in case no other view refers
// to the shared allocation
v = view_type(arg_prop_copy, n0, n1, n2, n3, n4, n5, n6, n7);
} else if (alloc_prop_input::initialize) {
return;
}

if constexpr (alloc_prop_input::initialize) {
if constexpr (alloc_prop_input::has_execution_space) {
const auto& exec_space =
Impl::get_property<Impl::ExecutionSpaceTag>(arg_prop);
Expand Down Expand Up @@ -3330,7 +3333,10 @@ impl_realloc(Kokkos::View<T, P...>& v,
if (v.layout() != layout) {
v = view_type(); // Deallocate first, if the only view to allocation
v = view_type(arg_prop, layout);
} else if (alloc_prop_input::initialize) {
return;
}

if constexpr (alloc_prop_input::initialize) {
if constexpr (alloc_prop_input::has_execution_space) {
const auto& exec_space =
Impl::get_property<Impl::ExecutionSpaceTag>(arg_prop);
Expand Down
13 changes: 13 additions & 0 deletions core/unit_test/TestRealloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ void impl_testRealloc() {
EXPECT_EQ(oldPointer, newPointer);
}
}
struct NoDefaultConstructor {
int value;
KOKKOS_FUNCTION
NoDefaultConstructor(int x) : value(x) {}
};

template <class DeviceType>
void testRealloc() {
Expand All @@ -154,6 +159,14 @@ void testRealloc() {
impl_testRealloc<DeviceType,
WithoutInitializing>(); // without data initialization
}
// Check #6992 fix (no default initialization in realloc without initializing)
{
using view_type = Kokkos::View<NoDefaultConstructor*, DeviceType>;
view_type view_1d_no_default(
Kokkos::view_alloc(Kokkos::WithoutInitializing, "view_1d_no_default"),
5);
realloc_dispatch(WithoutInitializing{}, view_1d_no_default, 3);
}
}

} // namespace TestViewRealloc
Expand Down
13 changes: 13 additions & 0 deletions core/unit_test/TestResize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ void impl_testResize() {
}
}

struct NoDefaultConstructor {
int value;
KOKKOS_FUNCTION
NoDefaultConstructor(int x) : value(x) {}
};

template <class DeviceType>
void testResize() {
{
Expand All @@ -367,6 +373,13 @@ void testResize() {
impl_testResize<DeviceType,
WithoutInitializing>(); // without data initialization
}
{
using view_type = Kokkos::View<NoDefaultConstructor*, DeviceType>;
view_type view_1d_no_default(
Kokkos::view_alloc(Kokkos::WithoutInitializing, "view_1d_no_default"),
5);
resize_dispatch(WithoutInitializing{}, view_1d_no_default, 3);
}
}

} // namespace TestViewResize
Expand Down

0 comments on commit d61d75a

Please sign in to comment.