From c356310020b50fee000a0d10cc7ccf9fb89c703d Mon Sep 17 00:00:00 2001 From: Nicolas Morales Date: Mon, 23 Sep 2024 11:04:33 -0400 Subject: [PATCH 1/5] #365: initial Kokkos::Array serialization support and test --- src/CMakeLists.txt | 2 +- src/checkpoint/checkpoint.h | 1 + src/checkpoint/container/kokkos_array.h | 73 ++++++++++++++++++++++ tests/unit/test_footprinter.cc | 3 + tests/unit/test_kokkos_serialize_array.cc | 74 +++++++++++++++++++++++ tests/unit/test_kokkos_serialize_pair.cc | 2 + 6 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/checkpoint/container/kokkos_array.h create mode 100644 tests/unit/test_kokkos_serialize_array.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8fc5d102..da3757f9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -95,11 +95,11 @@ if (${Kokkos_DIR_FOUND}) message(STATUS "Magistrate: Kokkos kernels disabled") set(KERNELS 0) endif() + message(STATUS "Magistrate: Kokkos (Version ${Kokkos_VERSION}) enabled") target_compile_definitions( ${MAGISTRATE_LIBRARY} PUBLIC MAGISTRATE_KOKKOS_ENABLED=1 MAGISTRATE_KOKKOS_KERNELS_ENABLED=${KERNELS} ) - message(STATUS "Magistrate: Kokkos enabled") target_link_libraries(${MAGISTRATE_LIBRARY} PUBLIC Kokkos::kokkos) endif() diff --git a/src/checkpoint/checkpoint.h b/src/checkpoint/checkpoint.h index 06d9504d..8ce173fe 100644 --- a/src/checkpoint/checkpoint.h +++ b/src/checkpoint/checkpoint.h @@ -69,6 +69,7 @@ #include "checkpoint/container/kokkos_unordered_map_serialize.h" #include "checkpoint/container/kokkos_pair_serialize.h" +#include "checkpoint/container/kokkos_array.h" #include "checkpoint/container/kokkos_complex_serialize.h" #include "checkpoint/checkpoint_api.h" diff --git a/src/checkpoint/container/kokkos_array.h b/src/checkpoint/container/kokkos_array.h new file mode 100644 index 00000000..93a6c654 --- /dev/null +++ b/src/checkpoint/container/kokkos_array.h @@ -0,0 +1,73 @@ +/* +//@HEADER +// ***************************************************************************** +// +// array_serialize.h +// DARMA/checkpoint => Serialization Library +// +// Copyright 2019 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. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from this +// software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Questions? Contact darma@sandia.gov +// +// ***************************************************************************** +//@HEADER +*/ + +#if !defined INCLUDED_CHECKPOINT_CONTAINER_KOKKOS_ARRAY_H +#define INCLUDED_CHECKPOINT_CONTAINER_KOKKOS_ARRAY_H + +#include "checkpoint/common.h" +#include "checkpoint/serializers/serializers_headers.h" + +#if MAGISTRATE_KOKKOS_ENABLED + +#include + +namespace checkpoint { + +#if KOKKOS_VERSION_LESS(4, 4, 0) +template +void serialize(Serializer& s, Kokkos::Array& array) { + static_assert(std::is_void_v< Proxy >, "Magistrate does not support serializing Kokkos Arrays with proxies"); + dispatch::serializeArray(s, &array[0], array.size()); +} +#else +template +void serialize(Serializer& s, Kokkos::Array& array) { + dispatch::serializeArray(s, &array[0], array.size()); +} +#endif + +} /* end namespace checkpoint */ + +#endif /*MAGISTRATE_KOKKOS_ENABLED*/ + +#endif /*INCLUDED_CHECKPOINT_CONTAINER_KOKKOS_ARRAY_H*/ diff --git a/tests/unit/test_footprinter.cc b/tests/unit/test_footprinter.cc index 993b3fb4..f3f12ea3 100644 --- a/tests/unit/test_footprinter.cc +++ b/tests/unit/test_footprinter.cc @@ -665,12 +665,15 @@ TEST_F(TestFootprinter, test_kokkos_pair) { EXPECT_EQ(checkpoint::getMemoryFootprint(pairIntInt), expected_size); } // 'pair' without second element + // This is deprecated in Kokkos >= 4.4 +#if KOKKOS_VERSION_LESS(4, 4, 0) { auto pairIntVoid = Kokkos::pair(10); auto expected_size = sizeof(pairIntVoid.first); EXPECT_EQ(checkpoint::getMemoryFootprint(pairIntVoid), expected_size); } +#endif // KOKKOS_VERSION_LESS(4, 4, 0) } TEST_F(TestFootprinter, test_kokkos_complex) { diff --git a/tests/unit/test_kokkos_serialize_array.cc b/tests/unit/test_kokkos_serialize_array.cc new file mode 100644 index 00000000..4d6aa967 --- /dev/null +++ b/tests/unit/test_kokkos_serialize_array.cc @@ -0,0 +1,74 @@ +/* +//@HEADER +// ***************************************************************************** +// +// test_kokkos_serialize_pair.cc +// DARMA/checkpoint => Serialization Library +// +// Copyright 2019 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. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from this +// software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Questions? Contact darma@sandia.gov +// +// ***************************************************************************** +//@HEADER +*/ + +#include +#if MAGISTRATE_KOKKOS_ENABLED + +#include "test_commons.h" + +namespace checkpoint { namespace tests { namespace unit { + +struct KokkosArrayTest : virtual testing::Test { }; + +template +static void test_kokkos_array(Kokkos::Array& refArray) { + using array_type = Kokkos::Array; + + auto serialized = checkpoint::serialize(refArray); + auto deserialized = checkpoint::deserialize(serialized->getBuffer()); + auto& outArray = *deserialized; + + for (size_t i = 0; i < N; ++i ) + ASSERT_EQ(refArray[i], outArray[i]); +} + +TEST_F(KokkosArrayTest, test_kokkos_array) { + using namespace ::checkpoint; + + auto arr1 = Kokkos::Array{ 1, 2, 3, 4, 5 }; + test_kokkos_array(arr1); +} + +}}} // namespace checkpoint::tests::unit + +#endif /*MAGISTRATE_KOKKOS_ENABLED*/ diff --git a/tests/unit/test_kokkos_serialize_pair.cc b/tests/unit/test_kokkos_serialize_pair.cc index 528ce679..dbaa28ff 100644 --- a/tests/unit/test_kokkos_serialize_pair.cc +++ b/tests/unit/test_kokkos_serialize_pair.cc @@ -73,8 +73,10 @@ TEST_F(KokkosPairTest, test_kokkos_pair) { auto pairUnsignedVector = Kokkos::pair>{30, {2, 3, 4 ,5}}; test_kokkos_pair(pairUnsignedVector); +#if KOKKOS_VERSION_LESS(4, 4, 0) auto pairIntVoid = Kokkos::pair(100); test_kokkos_pair(pairIntVoid); +#endif } }}} // namespace checkpoint::tests::unit From bdf2cde90d4e9e9aebba9714c93e34e282af2b10 Mon Sep 17 00:00:00 2001 From: Nicolas Morales Date: Wed, 25 Sep 2024 14:00:02 -0400 Subject: [PATCH 2/5] #365: add more tests and properly deal with size 0 arrays --- src/checkpoint/container/array_serialize.h | 2 +- src/checkpoint/container/kokkos_array.h | 2 +- tests/unit/test_kokkos_serialize_array.cc | 14 +++++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/checkpoint/container/array_serialize.h b/src/checkpoint/container/array_serialize.h index d5c0851d..99f57fad 100644 --- a/src/checkpoint/container/array_serialize.h +++ b/src/checkpoint/container/array_serialize.h @@ -53,7 +53,7 @@ namespace checkpoint { template void serialize(Serializer& s, std::array& array) { - dispatch::serializeArray(s, &array[0], array.size()); + dispatch::serializeArray(s, array.data(), array.size()); } } /* end namespace checkpoint */ diff --git a/src/checkpoint/container/kokkos_array.h b/src/checkpoint/container/kokkos_array.h index 93a6c654..10302c55 100644 --- a/src/checkpoint/container/kokkos_array.h +++ b/src/checkpoint/container/kokkos_array.h @@ -62,7 +62,7 @@ void serialize(Serializer& s, Kokkos::Array& array) { #else template void serialize(Serializer& s, Kokkos::Array& array) { - dispatch::serializeArray(s, &array[0], array.size()); + dispatch::serializeArray(s, array.data(), array.size()); } #endif diff --git a/tests/unit/test_kokkos_serialize_array.cc b/tests/unit/test_kokkos_serialize_array.cc index 4d6aa967..4dfb16a7 100644 --- a/tests/unit/test_kokkos_serialize_array.cc +++ b/tests/unit/test_kokkos_serialize_array.cc @@ -65,8 +65,20 @@ static void test_kokkos_array(Kokkos::Array& refArray) { TEST_F(KokkosArrayTest, test_kokkos_array) { using namespace ::checkpoint; - auto arr1 = Kokkos::Array{ 1, 2, 3, 4, 5 }; + auto arr1 = Kokkos::Array< int, 5 >{ 1, 2, 3, 4, 5 }; test_kokkos_array(arr1); + + auto arr2 = Kokkos::Array< float, 3 >{ 3.14f, 2.71f, 365.242f }; + test_kokkos_array(arr2); + + auto arr3 = Kokkos::Array< double, 2 >{ 3.14, 2.71 }; + test_kokkos_array(arr3); + + auto arr4 = Kokkos::Array< int, 1 >{ 3 }; + test_kokkos_array(arr4); + + auto empty_arr = Kokkos::Array< double, 0 >{}; + test_kokkos_array(empty_arr); } }}} // namespace checkpoint::tests::unit From 75c41710012aacc2d73ec72a6313d418f8f01f73 Mon Sep 17 00:00:00 2001 From: Nicolas Morales Date: Wed, 25 Sep 2024 14:18:57 -0400 Subject: [PATCH 3/5] #365: fix size 0 kokkos arrays in Kokkos < 4.4 case --- src/checkpoint/container/kokkos_array.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/checkpoint/container/kokkos_array.h b/src/checkpoint/container/kokkos_array.h index 10302c55..36ed1061 100644 --- a/src/checkpoint/container/kokkos_array.h +++ b/src/checkpoint/container/kokkos_array.h @@ -57,7 +57,7 @@ namespace checkpoint { template void serialize(Serializer& s, Kokkos::Array& array) { static_assert(std::is_void_v< Proxy >, "Magistrate does not support serializing Kokkos Arrays with proxies"); - dispatch::serializeArray(s, &array[0], array.size()); + dispatch::serializeArray(s, array.data(), array.size()); } #else template From f83becb5e2606b26e54f28f5f364d2f1142cd426 Mon Sep 17 00:00:00 2001 From: Nicolas Morales Date: Wed, 25 Sep 2024 15:13:26 -0400 Subject: [PATCH 4/5] #365: fix headers --- src/checkpoint/container/kokkos_array.h | 2 +- tests/unit/test_kokkos_serialize_array.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/checkpoint/container/kokkos_array.h b/src/checkpoint/container/kokkos_array.h index 36ed1061..637c0038 100644 --- a/src/checkpoint/container/kokkos_array.h +++ b/src/checkpoint/container/kokkos_array.h @@ -2,7 +2,7 @@ //@HEADER // ***************************************************************************** // -// array_serialize.h +// kokkos_array.h // DARMA/checkpoint => Serialization Library // // Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC diff --git a/tests/unit/test_kokkos_serialize_array.cc b/tests/unit/test_kokkos_serialize_array.cc index 4dfb16a7..9581194a 100644 --- a/tests/unit/test_kokkos_serialize_array.cc +++ b/tests/unit/test_kokkos_serialize_array.cc @@ -2,7 +2,7 @@ //@HEADER // ***************************************************************************** // -// test_kokkos_serialize_pair.cc +// test_kokkos_serialize_array.cc // DARMA/checkpoint => Serialization Library // // Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC From fd3ddc4f0bdff1abc622dc3ae3307b3f44f3934a Mon Sep 17 00:00:00 2001 From: Nicolas Morales Date: Thu, 26 Sep 2024 17:08:32 -0400 Subject: [PATCH 5/5] #365: fix headers again --- src/checkpoint/container/kokkos_array.h | 8 ++++---- tests/unit/test_kokkos_serialize_array.cc | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/checkpoint/container/kokkos_array.h b/src/checkpoint/container/kokkos_array.h index 637c0038..d40762b2 100644 --- a/src/checkpoint/container/kokkos_array.h +++ b/src/checkpoint/container/kokkos_array.h @@ -3,7 +3,7 @@ // ***************************************************************************** // // kokkos_array.h -// DARMA/checkpoint => Serialization Library +// DARMA/magistrate => Serialization Library // // Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. @@ -41,8 +41,8 @@ //@HEADER */ -#if !defined INCLUDED_CHECKPOINT_CONTAINER_KOKKOS_ARRAY_H -#define INCLUDED_CHECKPOINT_CONTAINER_KOKKOS_ARRAY_H +#if !defined INCLUDED_SRC_CHECKPOINT_CONTAINER_KOKKOS_ARRAY_H +#define INCLUDED_SRC_CHECKPOINT_CONTAINER_KOKKOS_ARRAY_H #include "checkpoint/common.h" #include "checkpoint/serializers/serializers_headers.h" @@ -70,4 +70,4 @@ void serialize(Serializer& s, Kokkos::Array& array) { #endif /*MAGISTRATE_KOKKOS_ENABLED*/ -#endif /*INCLUDED_CHECKPOINT_CONTAINER_KOKKOS_ARRAY_H*/ +#endif /*INCLUDED_SRC_CHECKPOINT_CONTAINER_KOKKOS_ARRAY_H*/ diff --git a/tests/unit/test_kokkos_serialize_array.cc b/tests/unit/test_kokkos_serialize_array.cc index 9581194a..78ebc01b 100644 --- a/tests/unit/test_kokkos_serialize_array.cc +++ b/tests/unit/test_kokkos_serialize_array.cc @@ -3,7 +3,7 @@ // ***************************************************************************** // // test_kokkos_serialize_array.cc -// DARMA/checkpoint => Serialization Library +// DARMA/magistrate => Serialization Library // // Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.