testCallRecord.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------1------------------------------------------- */
11 
22 #include <gtsam/base/Matrix.h>
23 #include <gtsam/base/Testable.h>
24 
26 #include <iostream>
27 
28 using namespace std;
29 using namespace gtsam;
30 
31 /* ************************************************************************* */
32 static const int Cols = 3;
33 
34 
37  return Eigen::Dynamic;
38  }
39  else return i;
40 }
41 struct CallConfig {
46  CallConfig(int rows, int cols):
47  compTimeRows(dynamicIfAboveMax(rows)),
48  compTimeCols(cols),
49  runTimeRows(rows),
50  runTimeCols(cols)
51  {
52  }
53  CallConfig(int compTimeRows, int compTimeCols, int runTimeRows, int runTimeCols):
54  compTimeRows(compTimeRows),
55  compTimeCols(compTimeCols),
56  runTimeRows(runTimeRows),
57  runTimeCols(runTimeCols)
58  {
59  }
60 
61  bool equals(const CallConfig & c, double /*tol*/) const {
62  return
63  this->compTimeRows == c.compTimeRows &&
64  this->compTimeCols == c.compTimeCols &&
65  this->runTimeRows == c.runTimeRows &&
66  this->runTimeCols == c.runTimeCols;
67  }
68  void print(const std::string & prefix) const {
69  std::cout << prefix << "{" << compTimeRows << ", " << compTimeCols << ", " << runTimeRows << ", " << runTimeCols << "}\n" ;
70  }
71 };
72 
74 namespace gtsam {
75 template<> struct traits<CallConfig> : public Testable<CallConfig> {};
76 }
77 
78 struct Record: public internal::CallRecordImplementor<Record, Cols> {
79  Record() : cc(0, 0) {}
80  ~Record() override {
81  }
82  void print(const std::string& indent) const {
83  }
84  void startReverseAD4(internal::JacobianMap& jacobians) const {
85  }
86 
87  mutable CallConfig cc;
88  private:
89  template<typename SomeMatrix>
90  void reverseAD4(const SomeMatrix & dFdT, internal::JacobianMap& jacobians) const {
91  cc.compTimeRows = SomeMatrix::RowsAtCompileTime;
92  cc.compTimeCols = SomeMatrix::ColsAtCompileTime;
93  cc.runTimeRows = dFdT.rows();
94  cc.runTimeCols = dFdT.cols();
95  }
96 
97  template<typename Derived, int Rows>
98  friend struct internal::CallRecordImplementor;
99 };
100 
101 internal::JacobianMap* NJM_ptr = static_cast<internal::JacobianMap *>(nullptr);
102 internal::JacobianMap & NJM = *NJM_ptr;
103 
104 /* ************************************************************************* */
106 
107 TEST(CallRecord, virtualReverseAdDispatching) {
108  Record record;
109  {
110  const int Rows = 1;
111  record.CallRecord::reverseAD2(Eigen::Matrix<double, Rows, Cols>(), NJM);
112  EXPECT((assert_equal(record.cc, CallConfig(Rows, Cols))));
113  record.CallRecord::reverseAD2(DynRowMat(Rows, Cols), NJM);
114  EXPECT((assert_equal(record.cc, CallConfig(Eigen::Dynamic, Cols, Rows, Cols))));
115  record.CallRecord::reverseAD2(Eigen::MatrixXd(Rows, Cols), NJM);
117  }
118  {
119  const int Rows = 2;
120  record.CallRecord::reverseAD2(Eigen::Matrix<double, Rows, Cols>(), NJM);
121  EXPECT((assert_equal(record.cc, CallConfig(Rows, Cols))));
122  record.CallRecord::reverseAD2(DynRowMat(Rows, Cols), NJM);
123  EXPECT((assert_equal(record.cc, CallConfig(Eigen::Dynamic, Cols, Rows, Cols))));
124  record.CallRecord::reverseAD2(Eigen::MatrixXd(Rows, Cols), NJM);
126  }
127  {
128  const int Rows = 3;
129  record.CallRecord::reverseAD2(Eigen::Matrix<double, Rows, Cols>(), NJM);
130  EXPECT((assert_equal(record.cc, CallConfig(Rows, Cols))));
131  record.CallRecord::reverseAD2(DynRowMat(Rows, Cols), NJM);
132  EXPECT((assert_equal(record.cc, CallConfig(Eigen::Dynamic, Cols, Rows, Cols))));
133  record.CallRecord::reverseAD2(Eigen::MatrixXd(Rows, Cols), NJM);
135  }
136  {
137  const int Rows = 4;
138  record.CallRecord::reverseAD2(Eigen::Matrix<double, Rows, Cols>(), NJM);
139  EXPECT((assert_equal(record.cc, CallConfig(Rows, Cols))));
140  record.CallRecord::reverseAD2(DynRowMat(Rows, Cols), NJM);
141  EXPECT((assert_equal(record.cc, CallConfig(Eigen::Dynamic, Cols, Rows, Cols))));
142  record.CallRecord::reverseAD2(Eigen::MatrixXd(Rows, Cols), NJM);
144  }
145  {
146  const int Rows = 5;
147  record.CallRecord::reverseAD2(Eigen::Matrix<double, Rows, Cols>(), NJM);
148  EXPECT((assert_equal(record.cc, CallConfig(Rows, Cols))));
149  record.CallRecord::reverseAD2(DynRowMat(Rows, Cols), NJM);
150  EXPECT((assert_equal(record.cc, CallConfig(Eigen::Dynamic, Cols, Rows, Cols))));
151  record.CallRecord::reverseAD2(Eigen::MatrixXd(Rows, Cols), NJM);
153  }
154  {
155  const int Rows = 6;
156  record.CallRecord::reverseAD2(Eigen::Matrix<double, Rows, Cols>(), NJM);
157  EXPECT((assert_equal(record.cc, CallConfig(Rows, Cols))));
158  record.CallRecord::reverseAD2(DynRowMat(Rows, Cols), NJM);
159  EXPECT((assert_equal(record.cc, CallConfig(Eigen::Dynamic, Cols, Rows, Cols))));
160  record.CallRecord::reverseAD2(Eigen::MatrixXd(Rows, Cols), NJM);
162  }
163 }
164 
165 /* ************************************************************************* */
166 int main() {
167  TestResult tr;
168  return TestRegistry::runAllTests(tr);
169 }
170 /* ************************************************************************* */
171 
internal::JacobianMap & NJM
void startReverseAD4(internal::JacobianMap &jacobians) const
~Record() override
void print(const std::string &prefix) const
Concept check for values that can be used in unit tests.
static int runAllTests(TestResult &result)
int dynamicIfAboveMax(int i)
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
Definition: Half.h:150
void reverseAD4(const SomeMatrix &dFdT, internal::JacobianMap &jacobians) const
Eigen::Matrix< double, Eigen::Dynamic, Cols > DynRowMat
CallConfig(int rows, int cols)
CallConfig(int compTimeRows, int compTimeCols, int runTimeRows, int runTimeCols)
bool equals(const CallConfig &c, double) const
#define EXPECT(condition)
Definition: Test.h:151
Internals for Expression.h, not for general consumption.
static const int Cols
traits
Definition: chartTesting.h:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
int main()
CallConfig cc
const int Dynamic
Definition: Constants.h:21
The matrix class, also used for vectors and row-vectors.
internal::JacobianMap * NJM_ptr
const int CallRecordMaxVirtualStaticRows
Definition: CallRecord.h:133
void print(const std::string &indent) const
TEST(CallRecord, virtualReverseAdDispatching)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:21