LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
cereal_utils.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 #pragma once
27 #ifndef LBANN_UTILS_SERIALIZATION_CEREAL_UTILS_HPP_
28 #define LBANN_UTILS_SERIALIZATION_CEREAL_UTILS_HPP_
29 
30 #include "lbann_config.hpp"
31 
32 #include <cereal/cereal.hpp>
33 
34 #ifdef LBANN_HAS_CEREAL_BINARY_ARCHIVES
35 #include <cereal/archives/binary.hpp>
36 #endif // LBANN_HAS_CEREAL_BINARY_ARCHIVES
37 #ifdef LBANN_HAS_CEREAL_JSON_ARCHIVES // Not yet supported
38 #include <cereal/archives/json.hpp>
39 #endif // LBANN_HAS_CEREAL_JSON_ARCHIVES
40 #ifdef LBANN_HAS_CEREAL_PORTABLE_BINARY_ARCHIVES // Not yet supported
41 #include <cereal/archives/portable_binary.hpp>
42 #endif // LBANN_HAS_CEREAL_PORTABLE_BINARY_ARCHIVES
43 #ifdef LBANN_HAS_CEREAL_XML_ARCHIVES
44 #include <cereal/archives/xml.hpp>
45 #endif // LBANN_HAS_CEREAL_XML_ARCHIVES
46 
47 #include <cereal/details/traits.hpp>
48 
49 #include <cereal/types/base_class.hpp>
50 #include <cereal/types/map.hpp> // Not sure that we need this
51 #include <cereal/types/polymorphic.hpp>
52 #include <cereal/types/set.hpp> // Only used by small number of classes
53 #include <cereal/types/string.hpp>
54 #include <cereal/types/unordered_map.hpp>
55 #include <cereal/types/unordered_set.hpp> // Only used by small number of classes
56 #include <cereal/types/utility.hpp>
57 #include <cereal/types/vector.hpp>
58 
59 #include <h2/meta/Core.hpp>
60 #include <h2/meta/TypeList.hpp>
61 
62 #if !(defined __CUDACC__)
63 namespace lbann {
64 namespace utils {
65 
66 using namespace ::cereal::traits;
67 using namespace ::h2::meta;
68 
69 namespace details {
74 template <typename ArchiveT>
76 
77 #if !(defined DOXYGEN_SHOULD_SKIP_THIS)
78 
79 template <typename ArchiveT>
80 struct IsBuiltinArchiveT : std::false_type
81 {
82 };
83 
84 // Add all the builtin types
85 #ifdef LBANN_HAS_CEREAL_BINARY_ARCHIVES
86 template <>
87 struct IsBuiltinArchiveT<cereal::BinaryInputArchive> : std::true_type
88 {
89 };
90 template <>
91 struct IsBuiltinArchiveT<cereal::BinaryOutputArchive> : std::true_type
92 {
93 };
94 #endif // LBANN_HAS_CEREAL_BINARY_ARCHIVES
95 
96 #ifdef LBANN_HAS_CEREAL_JSON_ARCHIVES
97 template <>
98 struct IsBuiltinArchiveT<cereal::JSONInputArchive> : std::true_type
99 {
100 };
101 template <>
102 struct IsBuiltinArchiveT<cereal::JSONOutputArchive> : std::true_type
103 {
104 };
105 #endif // LBANN_HAS_CEREAL_JSON_ARCHIVES
106 
107 #ifdef LBANN_HAS_CEREAL_PORTABLE_BINARY_ARCHIVES
108 template <>
109 struct IsBuiltinArchiveT<cereal::PortableBinaryInputArchive> : std::true_type
110 {
111 };
112 template <>
113 struct IsBuiltinArchiveT<cereal::PortableBinaryOutputArchive> : std::true_type
114 {
115 };
116 #endif // LBANN_HAS_CEREAL_PORTABLE_BINARY_ARCHIVES
117 
118 #ifdef LBANN_HAS_CEREAL_XML_ARCHIVES
119 template <>
120 struct IsBuiltinArchiveT<cereal::XMLInputArchive> : std::true_type
121 {
122 };
123 template <>
124 struct IsBuiltinArchiveT<cereal::XMLOutputArchive> : std::true_type
125 {
126 };
127 #endif // LBANN_HAS_CEREAL_XML_ARCHIVES
128 #endif // defined DOXYGEN_SHOULD_SKIP_THIS
129 } // namespace details
130 
134 template <typename ArchiveT>
136 
140 template <typename ArchiveT>
141 constexpr bool IsTextArchive = is_text_archive<ArchiveT>::value;
142 
146 template <typename ArchiveT>
147 constexpr bool IsInputArchive =
148  std::is_base_of_v<cereal::detail::InputArchiveBase, ArchiveT>;
149 
153 template <typename ArchiveT>
154 constexpr bool IsOutputArchive =
155  std::is_base_of_v<cereal::detail::OutputArchiveBase, ArchiveT>;
156 
160 template <typename ArchiveT, typename ResultT = int>
161 using WhenTextArchive =
162  EnableWhen<IsTextArchive<ArchiveT> && IsBuiltinArchive<ArchiveT>, ResultT>;
163 
167 template <typename ArchiveT, typename ResultT = int>
168 using WhenNotTextArchive =
169  EnableWhen<!IsTextArchive<ArchiveT> && IsBuiltinArchive<ArchiveT>, ResultT>;
170 
171 } // namespace utils
172 } // namespace lbann
173 #endif // !(defined __CUDACC__ || defined __HIPCC__)
174 #endif // LBANN_UTILS_SERIALIZATION_CEREAL_UTILS_HPP_
constexpr bool IsOutputArchive
Variable template for checking that an archive type is an "Output" archive.
EnableWhen<!IsTextArchive< ArchiveT > &&IsBuiltinArchive< ArchiveT >, ResultT > WhenNotTextArchive
SFINAE helper for splitting text-based and non-text-based serialization functions.
constexpr bool IsBuiltinArchive
Variable template for checking that an archive is a default Cereal archive type.
Predicate for testing if the given type is a built-in Cereal archive.
constexpr bool IsInputArchive
Variable template for checking that an archive type is an "Input" archive.
EnableWhen< IsTextArchive< ArchiveT > &&IsBuiltinArchive< ArchiveT >, ResultT > WhenTextArchive
SFINAE helper for splitting text-based and non-text-based serialization functions.
constexpr bool IsTextArchive
Variable template for checking that an archive type is marked as a text archive in Cereal...