BioCMAMC-ST
has_serialize.hpp
1#ifndef __COMMON_HAS_SERIALIZE_HPP__
2#define __COMMON_HAS_SERIALIZE_HPP__
3
4#include "biocma_cst_config.hpp"
5#include <Kokkos_Core.hpp>
6#include <type_traits>
7#include <vector>
8
9#define IF_ENABLE_CEREAL_SERDE \
10 typename std::enable_if<AutoGenerated::FlagCompileTime::use_cereal_serde>::type
11namespace
12{
13 template <typename, typename T> struct __has_serialize
14 {
15 static_assert(std::integral_constant<T, false>::value,
16 "Second template parameter needs to be of function type.");
17 };
18
19 // specialization that does the checking
20
21 template <typename C, typename Ret, typename... Args> struct __has_serialize<C, Ret(Args...)>
22 {
23 private:
24 template <typename T>
25 static constexpr auto check(T*) ->
26 typename std::is_same<decltype(std::declval<T>().serialize(std::declval<Args>()...)),
27 Ret>::type;
28
29 template <typename> static constexpr std::false_type check(...);
30
31 using type = decltype(check<C>(0));
32
33 public:
34 static constexpr bool value = type::value;
35 };
36} // namespace
37template <class C, typename T> consteval bool has_serialize()
38{
39 return __has_serialize<C, void(T&)>::value;
40}
41
42template <class ViewType, typename Enable = void> struct ViewSerialization;
43
44// Specialization for 1D Kokkos::View
45template <class ViewType>
47 ViewType,
48 typename std::enable_if<ViewType::rank == 1 &&
49 AutoGenerated::FlagCompileTime::use_cereal_serde>::type>
50{
51 template <class Archive>
52 static void serialize(Archive& ar, const ViewType& view, std::size_t n_used_elements)
53 {
54 using T = typename ViewType::value_type;
55 auto temp_host = Kokkos::create_mirror_view_and_copy(Kokkos::DefaultHostExecutionSpace(), view);
56 std::vector<T> data_vector(temp_host.data(), temp_host.data() + n_used_elements);
57 ar(data_vector);
58 }
59
60 template <class Archive> static void deserialize(Archive& ar, ViewType& view)
61 {
62 using T = typename ViewType::value_type;
63
64 std::vector<T> data_vector;
65 ar(data_vector);
66
67 view = ViewType("deser", data_vector.size());
68
69 auto tmpdata = ViewType(data_vector.data(), data_vector.size());
70 Kokkos::deep_copy(view, tmpdata);
71 }
72};
73
74// Specialization for 2D Kokkos::View
75template <class ViewType>
77 ViewType,
78 typename std::enable_if<ViewType::rank == 2 &&
79 AutoGenerated::FlagCompileTime::use_cereal_serde>::type>
80{
81 template <class Archive>
82 static void serialize(Archive& ar, const ViewType& view, std::size_t n_used_elements)
83 {
84 (void)n_used_elements;
85 using T = typename ViewType::value_type;
86 auto temp_host = Kokkos::create_mirror_view_and_copy(Kokkos::DefaultHostExecutionSpace(), view);
87 const auto rows = view.extent(0);
88 const auto cols = view.extent(1);
89 ar(rows);
90 ar(cols);
91 std::vector<T> data_vector(temp_host.data(), temp_host.data() + rows * cols);
92 ar(data_vector);
93 }
94
95 template <class Archive> static void deserialize(Archive& ar, ViewType& view)
96 {
97 using T = typename ViewType::value_type;
98 using L = typename ViewType::array_layout;
99 std::size_t rows{};
100 std::size_t cols{};
101 std::vector<T> data_vector;
102 ar(rows, cols);
103 ar(data_vector);
104 view = ViewType("deser", rows, cols);
105 auto tmpdata = ViewType(data_vector.data(), L(rows, cols));
106
107 Kokkos::deep_copy(view, tmpdata);
108 }
109};
110
111template <class Archive, class ViewType> void serialize_view(Archive& archive, const ViewType& view)
112{
113 // using T = typename ViewType::value_type;
114 ViewSerialization<ViewType>::serialize(archive, view, view.extent(0));
115}
116
117template <class Archive, class ViewType> void deserialize_view(Archive& archive, ViewType& view)
118{
120}
121
122#endif
static void serialize(Archive &ar, const ViewType &view, std::size_t n_used_elements)
Definition has_serialize.hpp:52
static void serialize(Archive &ar, const ViewType &view, std::size_t n_used_elements)
Definition has_serialize.hpp:82
Definition has_serialize.hpp:42