Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#365: add support for Kokkos::Array #366

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions src/checkpoint/checkpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/container/array_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace checkpoint {

template <typename Serializer, typename T, size_t N>
void serialize(Serializer& s, std::array<T, N>& array) {
dispatch::serializeArray(s, &array[0], array.size());
dispatch::serializeArray(s, array.data(), array.size());
}

} /* end namespace checkpoint */
Expand Down
73 changes: 73 additions & 0 deletions src/checkpoint/container/kokkos_array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
//@HEADER
// *****************************************************************************
//
// kokkos_array.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 [email protected]
//
// *****************************************************************************
//@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 <Kokkos_Array.hpp>

namespace checkpoint {

#if KOKKOS_VERSION_LESS(4, 4, 0)
template <typename Serializer, typename T, size_t N, class Proxy>
void serialize(Serializer& s, Kokkos::Array<T, N, Proxy>& array) {
static_assert(std::is_void_v< Proxy >, "Magistrate does not support serializing Kokkos Arrays with proxies");
dispatch::serializeArray(s, array.data(), array.size());
}
#else
template <typename Serializer, typename T, size_t N>
void serialize(Serializer& s, Kokkos::Array<T, N>& array) {
dispatch::serializeArray(s, array.data(), array.size());
}
#endif

} /* end namespace checkpoint */

#endif /*MAGISTRATE_KOKKOS_ENABLED*/

#endif /*INCLUDED_CHECKPOINT_CONTAINER_KOKKOS_ARRAY_H*/
3 changes: 3 additions & 0 deletions tests/unit/test_footprinter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, void>(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) {
Expand Down
86 changes: 86 additions & 0 deletions tests/unit/test_kokkos_serialize_array.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
//@HEADER
// *****************************************************************************
//
// test_kokkos_serialize_array.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 [email protected]
//
// *****************************************************************************
//@HEADER
*/

#include <Kokkos_Array.hpp>
#if MAGISTRATE_KOKKOS_ENABLED

#include "test_commons.h"

namespace checkpoint { namespace tests { namespace unit {

struct KokkosArrayTest : virtual testing::Test { };

template <typename T, size_t N>
static void test_kokkos_array(Kokkos::Array<T, N>& refArray) {
using array_type = Kokkos::Array<T, N>;

auto serialized = checkpoint::serialize<array_type>(refArray);
auto deserialized = checkpoint::deserialize<array_type>(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< 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

#endif /*MAGISTRATE_KOKKOS_ENABLED*/
2 changes: 2 additions & 0 deletions tests/unit/test_kokkos_serialize_pair.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ TEST_F(KokkosPairTest, test_kokkos_pair) {
auto pairUnsignedVector = Kokkos::pair<unsigned, std::vector<int>>{30, {2, 3, 4 ,5}};
test_kokkos_pair(pairUnsignedVector);

#if KOKKOS_VERSION_LESS(4, 4, 0)
auto pairIntVoid = Kokkos::pair<int, void>(100);
test_kokkos_pair(pairIntVoid);
#endif
}

}}} // namespace checkpoint::tests::unit
Expand Down
Loading