LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
matrix_builder.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 
27 #ifndef NON_PUBLIC_LBANN_SRC_LAYERS_MATRIX_BUILDER_INCLUDED
28 #define NON_PUBLIC_LBANN_SRC_LAYERS_MATRIX_BUILDER_INCLUDED
29 
30 #include "lbann/base.hpp"
31 #include "lbann/utils/memory.hpp"
32 
33 #include <memory>
34 #include <type_traits>
35 
36 namespace lbann {
37 namespace details {
38 namespace meta {
39 // Quick, forced if-then-else
40 template <bool B, typename T, typename F>
41 using IfThenElse = typename std::conditional<B, T, F>::type;
42 } // namespace meta
43 
44 template <typename T>
46 {
47 public:
48  using size_type = El::Int;
49  using data_type = T;
50  using matrix_type = El::AbstractDistMatrix<T>;
51  using matrix_ptr_type = std::unique_ptr<matrix_type>;
52 
53 public:
54  virtual ~MatrixBuilder() = default;
55  virtual matrix_ptr_type MakeEmpty(El::Grid const& g, El::Int root) const = 0;
56  virtual matrix_ptr_type MakeWithSize(El::Grid const& g,
57  El::Int root,
58  size_type height,
59  size_type width) const = 0;
60 
61 }; // class MatrixBuilder
62 
63 // Uses memory mode = 1 for CPU (pinned memory) and for GPU (CUB memory).
64 template <typename T, data_layout L, El::Device D>
66 {
68  using concrete_matrix_type =
70  El::DistMatrix<T, El::STAR, El::VC, El::ELEMENT, D>,
71  El::DistMatrix<T, El::MC, El::MR, El::ELEMENT, D>>;
72 
73 #if defined(HYDROGEN_HAVE_GPU) && defined(HYDROGEN_HAVE_CUB)
74  // Pinned host memory; memory-pooled device memory
75  static constexpr unsigned memory_mode_ = 1U;
76 #elif defined(HYDROGEN_HAVE_GPU)
77  // Pinned host memory; directly-allocated device memory
78  static constexpr unsigned memory_mode_ = (D == El::Device::CPU ? 1U : 0U);
79 #else
80  // Default memory
81  static constexpr unsigned memory_mode_ =
82  El::DefaultMemoryMode<El::Device::CPU>();
83 #endif // defined(HYDROGEN_HAVE_GPU) && defined(HYDROGEN_HAVE_CUB)
84 
85 public:
86  using size_type = typename base_type::size_type;
88 
89 public:
90  matrix_ptr_type MakeEmpty(El::Grid const& g, El::Int root) const final
91  {
92  auto ret = std::make_unique<concrete_matrix_type>(g, root);
93  ret->Matrix().SetMemoryMode(memory_mode_);
94  return ret;
95  }
96 
98  El::Int root,
99  size_type height,
100  size_type width) const final
101  {
102  auto ret = this->MakeEmpty(g, root);
103  ret->Resize(height, width);
104  return ret;
105  }
106 
107 }; // class DefaultMemoryMatrixBuilder
108 
109 } // namespace details
110 } // namespace lbann
111 #endif // NON_PUBLIC_LBANN_SRC_LAYERS_MATRIX_BUILDER_INCLUDED
meta::IfThenElse< L==data_layout::DATA_PARALLEL, El::DistMatrix< T, El::STAR, El::VC, El::ELEMENT, D >, El::DistMatrix< T, El::MC, El::MR, El::ELEMENT, D > > concrete_matrix_type
std::unique_ptr< matrix_type > matrix_ptr_type
El::Grid Grid
Definition: base.hpp:126
matrix_ptr_type MakeEmpty(El::Grid const &g, El::Int root) const final
typename std::conditional< B, T, F >::type IfThenElse
matrix_ptr_type MakeWithSize(El::Grid const &g, El::Int root, size_type height, size_type width) const final
El::AbstractDistMatrix< T > matrix_type