LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
MatrixHelpers.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2021, 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_UNIT_TEST_UTILITIES_MATRIX_HELPERS_HPP_INCLUDED
27 #define LBANN_UNIT_TEST_UTILITIES_MATRIX_HELPERS_HPP_INCLUDED
28 
29 #include <lbann/base.hpp>
30 
31 namespace El {
32 
33 template <typename T>
34 bool compare_values(Matrix<T, Device::CPU> const& A,
35  Matrix<T, Device::CPU> const& B)
36 #ifdef HYDROGEN_RELEASE_BUILD
37  noexcept
38 #endif
39 {
40  using IdxT = decltype(A.Height());
41 
42  // Short-circuit for compare-to-self.
43  if (&A == &B)
44  return true;
45 
46  // Short-circuit for dimension mis-match
47  auto const A_height = A.Height(), A_width = A.Width();
48  if ((A_height != B.Height()) || (A_width != B.Width()))
49  return false;
50 
51  for (IdxT col = To<IdxT>(0); col < A_width; ++col)
52  for (IdxT row = To<IdxT>(0); row < A_height; ++row)
53  if (A.CRef(row, col) != B.CRef(row, col))
54  return false;
55  return true;
56 }
57 
58 // The "operator==" equivalence relation for non-distributed matrices
59 // is defined as follows.
60 //
61 // Two matrices, A and B, are considered equivalent if all of the
62 // following conditions are met:
63 //
64 // 1. They represent their data with the same type.
65 // 2. Their memory resides on the same device.
66 // 3. They have the same shape (LDim *NOT* included).
67 // 4. A_ij == B_ij for each i,j. Comparison is `operator==` for the
68 // appropriate data type.
69 
70 template <typename S, typename T>
71 bool operator==(AbstractMatrix<S> const& A, AbstractMatrix<T> const& B) noexcept
72 {
73  return false;
74 }
75 
76 template <typename T>
77 bool operator==(AbstractMatrix<T> const& A, AbstractMatrix<T> const& B)
78 #ifdef HYDROGEN_RELEASE_BUILD
79  noexcept
80 #endif
81 {
82  using Proxy = AbstractMatrixReadDeviceProxy<T, Device::CPU>;
83  if (&A == &B) // Short-circuit when testing reflexively.
84  return true;
85 
86  if ((A.GetDevice() != B.GetDevice()) || (A.Height() != B.Height()) ||
87  (A.Width() != B.Width()))
88  return false;
89 
90  return compare_values(Proxy(A).GetLocked(), Proxy(B).GetLocked());
91 }
92 
93 // Two DistMatrices are equivalent under `operator==` iff their
94 // distributions match and their local matrices are equivalent under
95 // `operator==`.
96 
97 template <typename S, typename T>
98 bool operator==(AbstractDistMatrix<S> const& A,
99  AbstractDistMatrix<T> const& B) noexcept
100 {
101  return false;
102 }
103 
104 template <typename T>
105 bool operator==(AbstractDistMatrix<T> const& A,
106  AbstractDistMatrix<T> const& B) noexcept
107 {
108  return ((A.DistData() == B.DistData()) &&
109  (A.LockedMatrix() == B.LockedMatrix()));
110 }
111 
112 } // namespace El
113 #endif // LBANN_UNIT_TEST_UTILITIES_MATRIX_HELPERS_HPP_INCLUDED
bool operator==(AbstractMatrix< S > const &A, AbstractMatrix< T > const &B) noexcept
bool compare_values(Matrix< T, Device::CPU > const &A, Matrix< T, Device::CPU > const &B)