LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
dim_helpers.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 #ifndef LBANN_UTILS_DIM_HELPERS_HPP_
27 #define LBANN_UTILS_DIM_HELPERS_HPP_
28 
29 #include <functional>
31 #include <numeric>
32 
33 namespace lbann {
34 
40 template <typename Out, typename In>
41 auto get_linear_size_as(std::vector<In> const& dims)
42 {
43  return (dims.size() ? std::accumulate(cbegin(dims),
44  cend(dims),
45  Out{1},
46  std::multiplies<Out>())
47  : Out{0});
48 }
49 
50 template <typename Out, typename In>
51 auto get_linear_size_as(size_t ndims, In const* dims)
52 {
53  return (
54  ndims ? std::accumulate(dims, dims + ndims, Out{1}, std::multiplies<Out>())
55  : Out{0});
56 }
57 
58 template <typename T>
59 auto get_linear_size(std::vector<T> const& dims)
60 {
61  return get_linear_size_as<T>(dims);
62 }
63 
64 template <typename T>
65 auto get_linear_size(size_t ndims, T const* dims)
66 {
67  return get_linear_size_as<T>(ndims, dims);
68 }
69 
70 template <typename T>
71 auto get_strides(size_t ndims, T const* dims, T const& lowest_stride)
72 {
73  std::vector<T> strides(ndims, lowest_stride);
74  if (ndims > 0) {
75  for (size_t ii = ndims - 1; ii != 0; --ii) {
76  if (dims[ii] == T(0))
77  LBANN_ERROR("Zero-sized dimension not allowed. Dims[", ii, "] = 0.");
78  strides[ii - 1] = strides[ii] * dims[ii];
79  }
80  }
81  return strides;
82 }
83 
84 template <typename T>
85 auto get_strides(std::vector<T> const& dims, T const& lowest_stride)
86 {
87  return get_strides(dims.size(), dims.data(), lowest_stride);
88 }
89 
90 template <typename T>
91 auto get_packed_strides(size_t ndims, T const* dims)
92 {
93  return get_strides(ndims, dims, T(1));
94 }
95 
96 template <typename T>
97 auto get_packed_strides(std::vector<T> const& dims)
98 {
99  return get_strides(dims.size(), dims.data(), T(1));
100 }
101 
102 template <typename To, typename From>
103 auto vector_cast(std::vector<From> const& from)
104 {
105  return std::vector<To>{from.cbegin(), from.cend()};
106 }
107 
108 namespace details {
109 
110 template <typename T, typename... ArgTs>
111 std::enable_if_t<std::is_integral_v<T>>
112 accumulate_dims(std::vector<size_t>& acc, T const& x, ArgTs&&... rest);
113 
114 template <typename T, typename... ArgTs>
115 std::enable_if_t<std::is_integral_v<T>>
116 accumulate_dims(std::vector<size_t>& acc,
117  std::vector<T> const& x,
118  ArgTs&&... rest);
119 
120 inline void accumulate_dims(std::vector<size_t>&) {}
121 
122 template <typename T, typename... ArgTs>
123 std::enable_if_t<std::is_integral_v<T>>
124 accumulate_dims(std::vector<size_t>& acc, T const& x, ArgTs&&... rest)
125 {
126  acc.push_back(x);
127  accumulate_dims(acc, std::forward<ArgTs>(rest)...);
128 }
129 
130 template <typename T, typename... ArgTs>
131 std::enable_if_t<std::is_integral_v<T>>
132 accumulate_dims(std::vector<size_t>& acc,
133  std::vector<T> const& x,
134  ArgTs&&... rest)
135 {
136  acc.insert(end(acc), cbegin(x), cend(x));
137  accumulate_dims(acc, std::forward<ArgTs>(rest)...);
138 }
139 } // namespace details
140 
141 template <typename... ArgTs>
142 std::vector<size_t> splice_dims(ArgTs&&... args)
143 {
144  std::vector<size_t> dims;
145  details::accumulate_dims(dims, std::forward<ArgTs>(args)...);
146  return dims;
147 }
148 
149 } // namespace lbann
150 #endif // LBANN_UTILS_DIM_HELPERS_HPP_
auto get_strides(ColMajorDims< DimT > const &dims)
Compute packed strides of the given dimensions.
auto get_linear_size_as(std::vector< In > const &dims)
Compute the linear size of the given dimensions with a specific type.
Definition: dim_helpers.hpp:41
std::vector< size_t > splice_dims(ArgTs &&... args)
auto get_linear_size(std::vector< T > const &dims)
Definition: dim_helpers.hpp:59
#define LBANN_ERROR(...)
Definition: exception.hpp:37
auto vector_cast(std::vector< From > const &from)
std::enable_if_t< std::is_integral_v< T > > accumulate_dims(std::vector< size_t > &acc, T const &x, ArgTs &&... rest)
auto get_packed_strides(size_t ndims, T const *dims)
Definition: dim_helpers.hpp:91