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