LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
serialize_matrices.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_SERIALIZE_MATRICES_HPP_
28 #define LBANN_UTILS_SERIALIZATION_SERIALIZE_MATRICES_HPP_
29 
30 #include "cereal_utils.hpp"
32 
34 
35 #include <El.hpp>
36 #include <stdexcept>
37 
38 // These really belong in Elemental; let's just extend that.
39 namespace El {
40 
63 template <typename ArchiveT, typename T>
64 void save(ArchiveT& ar, ::El::AbstractMatrix<T> const& mat);
65 
66 template <typename ArchiveT,
67  typename T,
68  ::El::Device D,
70 void save(ArchiveT& ar, ::El::Matrix<T, D> const& mat);
71 
72 namespace details {
89 template <typename ArchiveT,
90  typename T,
92 void do_save(ArchiveT& ar, ::El::Matrix<T, ::El::Device::CPU> const& mat);
93 
94 #ifdef LBANN_HAS_GPU
95 template <typename ArchiveT,
96  typename T,
97  lbann::utils::WhenNotTextArchive<ArchiveT> = 1>
98 void do_save(ArchiveT& ar, ::El::Matrix<T, ::El::Device::GPU> const& mat);
99 #endif // LBANN_HAS_GPU
100 } // namespace details
101 
103 template <typename ArchiveT,
104  typename T,
105  ::El::Device D,
107 void save(ArchiveT& ar, ::El::Matrix<T, D> const& mat);
108 
109 // Special treatment for the rooted archive.
110 template <typename ArchiveT, typename T, ::El::Device D>
112  ::El::Matrix<T, D> const& mat);
113 
114 template <typename ArchiveT,
115  typename T,
116  lbann::utils::WhenNotTextArchive<ArchiveT> = 1>
117 void load(ArchiveT& archive, ::El::AbstractMatrix<T>& mat);
118 
137 template <typename ArchiveT,
138  typename T,
139  ::El::Device D,
140  lbann::utils::WhenTextArchive<ArchiveT> = 1>
141 void load(ArchiveT& archive, ::El::Matrix<T, D>& mat);
142 
152 template <typename ArchiveT,
153  typename T,
154  lbann::utils::WhenNotTextArchive<ArchiveT> = 1>
155 void load(ArchiveT& archive, ::El::Matrix<T, ::El::Device::CPU>& mat);
156 
157 #if defined LBANN_HAS_GPU
158 
167 template <typename ArchiveT,
168  typename T,
169  lbann::utils::WhenNotTextArchive<ArchiveT> = 1>
170 void load(ArchiveT& archive, ::El::Matrix<T, ::El::Device::GPU>& mat);
171 #endif // defined LBANN_HAS_GPU
172 
173 template <typename ArchiveT, typename T, ::El::Device D>
175  ::El::Matrix<T, D>& mat);
176 
177 // DistMatrix
178 
201 template <typename ArchiveT,
202  typename T,
203  lbann::utils::WhenTextArchive<ArchiveT> = 1>
204 void save(ArchiveT& ar, ::El::AbstractDistMatrix<T> const& mat);
205 
215 template <typename ArchiveT,
216  typename T,
217  lbann::utils::WhenTextArchive<ArchiveT> = 1>
218 void load(ArchiveT& ar, ::El::AbstractDistMatrix<T>& mat);
219 
232 template <typename ArchiveT,
233  typename T,
234  lbann::utils::WhenNotTextArchive<ArchiveT> = 1>
235 void save(ArchiveT& ar, ::El::AbstractDistMatrix<T> const& mat);
236 
246 template <typename ArchiveT,
247  typename T,
248  lbann::utils::WhenNotTextArchive<ArchiveT> = 1>
249 void load(ArchiveT& ar, ::El::AbstractDistMatrix<T>& mat);
250 
251 template <typename ArchiveT,
252  typename T,
253  lbann::utils::WhenTextArchive<ArchiveT> = 1>
255  ::El::AbstractDistMatrix<T> const& mat);
256 
257 template <typename ArchiveT,
258  typename T,
259  lbann::utils::WhenTextArchive<ArchiveT> = 1>
261  ::El::AbstractDistMatrix<T>& mat);
262 
263 template <typename ArchiveT,
264  typename T,
265  lbann::utils::WhenNotTextArchive<ArchiveT> = 1>
267  ::El::AbstractDistMatrix<T> const& mat);
268 
269 template <typename ArchiveT,
270  typename T,
271  lbann::utils::WhenNotTextArchive<ArchiveT> = 1>
273  ::El::AbstractDistMatrix<T>& mat);
274 
275 template <typename ArchiveT,
276  typename T,
277  lbann::utils::WhenNotTextArchive<ArchiveT> = 1>
279  ::El::DistMatrix<T, ::El::CIRC, ::El::CIRC> const& mat);
280 
281 template <typename ArchiveT,
282  typename T,
283  lbann::utils::WhenNotTextArchive<ArchiveT> = 1>
285  ::El::DistMatrix<T, ::El::CIRC, ::El::CIRC>& mat);
286 
287 } // namespace El
288 
289 // Dealing with smart pointers and object construction
290 
291 namespace lbann {
292 namespace utils {
293 
301 {
302  grid_manager(Grid const& g);
303  ~grid_manager();
304 };
305 
311 Grid const& get_current_grid() noexcept;
312 
313 lbann_comm& get_current_comm() noexcept;
314 } // namespace utils
315 } // namespace lbann
316 
317 namespace cereal {
318 
320 template <typename DataT,
321  ::El::Dist CDist,
322  ::El::Dist RDist,
323  ::El::DistWrap Wrap,
324  ::El::Device D>
325 struct LoadAndConstruct<::El::DistMatrix<DataT, CDist, RDist, Wrap, D>>
326 {
327  using DistMatrixType = ::El::DistMatrix<DataT, CDist, RDist, Wrap, D>;
328  using CircMatrixType =
329  ::El::DistMatrix<DataT, ::El::CIRC, ::El::CIRC, Wrap, ::El::Device::CPU>;
330 
331  template <
332  typename ArchiveT,
333  ::h2::meta::EnableWhen<::lbann::utils::IsBuiltinArchive<ArchiveT>, int> = 0>
334  static void load_and_construct(ArchiveT& ar,
335  cereal::construct<DistMatrixType>& construct);
336 
337  template <typename ArchiveT>
338  static void load_and_construct(lbann::RootedInputArchiveAdaptor<ArchiveT>& ar,
339  cereal::construct<DistMatrixType>& construct);
340 }; // struct LoadAndConstruct<::El::DistMatrix<DataT, CDist, RDist, Wrap, D>>
341 } // namespace cereal
342 
343 #endif // LBANN_UTILS_SERIALIZATION_SERIALIZE_MATRICES_HPP_
El::Grid Grid
Definition: base.hpp:126
EnableWhen<!IsTextArchive< ArchiveT > &&IsBuiltinArchive< ArchiveT >, ResultT > WhenNotTextArchive
SFINAE helper for splitting text-based and non-text-based serialization functions.
Grid const & get_current_grid() noexcept
Get the current grid being used for deserialization.
constexpr El::Device Device
lbann_comm & get_current_comm() noexcept
::El::DistMatrix< DataT, ::El::CIRC, ::El::CIRC, Wrap, ::El::Device::CPU > CircMatrixType
void load(ArchiveT &archive, ::El::AbstractMatrix< T > &mat)
void save(ArchiveT &ar, ::El::AbstractMatrix< T > const &mat)
Save a matrix to a text-based archive.
EnableWhen< IsTextArchive< ArchiveT > &&IsBuiltinArchive< ArchiveT >, ResultT > WhenTextArchive
SFINAE helper for splitting text-based and non-text-based serialization functions.
void do_save(ArchiveT &ar, ::El::Matrix< T, ::El::Device::CPU > const &mat)
Save a CPU matrix to a non-text-based archive.
::distconv::tensor::Distribution Dist