blob: ea360cc2ae788f2685b818b44d555ec025b89d29 [file] [log] [blame]
Austin Schuh8bd96322020-02-13 21:18:22 -08001#include "aos/events/logging/logger.h"
2
3#include "Eigen/Dense"
4
Austin Schuh2f8fd752020-09-01 22:38:28 -07005#include "third_party/gmp/gmpxx.h"
6
Austin Schuh8bd96322020-02-13 21:18:22 -08007namespace aos {
8namespace logger {
9
Austin Schuh2f8fd752020-09-01 22:38:28 -070010namespace {
11Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> ToDouble(
12 Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic> in) {
13 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> result =
14 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(in.rows(),
15 in.cols());
16 for (int i = 0; i < in.rows(); ++i) {
17 for (int j = 0; j < in.cols(); ++j) {
18 result(i, j) = in(i, j).get_d();
19 }
20 }
21 return result;
22}
23
24std::tuple<Eigen::Matrix<double, Eigen::Dynamic, 1>,
25 Eigen::Matrix<double, Eigen::Dynamic, 1>>
26Solve(const Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic> &mpq_map,
27 const Eigen::Matrix<mpq_class, Eigen::Dynamic, 1> &mpq_offsets) {
28 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
29 // Least squares solve for the slopes and offsets.
30 const Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic> inv =
31 (mpq_map.transpose() * mpq_map).inverse() * mpq_map.transpose();
32 aos::monotonic_clock::time_point end_time = aos::monotonic_clock::now();
33
34 VLOG(1) << "Took "
35 << std::chrono::duration<double>(end_time - start_time).count()
36 << " seconds to invert";
37
38 Eigen::Matrix<mpq_class, Eigen::Dynamic, 1> mpq_solution_slope =
39 inv.block(0, 0, inv.rows(), 1);
40 Eigen::Matrix<mpq_class, Eigen::Dynamic, 1> mpq_solution_offset =
41 inv.block(0, 1, inv.rows(), inv.cols() - 1) *
42 mpq_offsets.block(1, 0, inv.rows() - 1, 1);
43
44 mpq_solution_offset *= mpq_class(1, 1000000000);
45
46 return std::make_tuple(ToDouble(mpq_solution_slope),
47 ToDouble(mpq_solution_offset));
48}
49} // namespace
50
Austin Schuh8bd96322020-02-13 21:18:22 -080051// This is slow to compile, so we put it in a separate file. More parallelism
52// and less change.
Austin Schuh2f8fd752020-09-01 22:38:28 -070053std::tuple<Eigen::Matrix<double, Eigen::Dynamic, 1>,
54 Eigen::Matrix<double, Eigen::Dynamic, 1>>
55LogReader::SolveOffsets() {
56 // TODO(austin): Split this out and unit tests a bit better. When we do
57 // partial node subsets and also try to optimize this again would be a good
58 // time.
59 //
60 // TODO(austin): CHECK that the number doesn't change over time. We can freak
61 // out if that happens.
62
63 // Start by counting how many node pairs we have an offset estimated for.
64 int nonzero_offset_count = 1;
65 for (int i = 1; i < valid_matrix_.rows(); ++i) {
66 if (valid_matrix_(i) != 0) {
67 ++nonzero_offset_count;
68 }
69 }
70
71 Eigen::IOFormat HeavyFmt(Eigen::FullPrecision, 0, ", ", ";\n", "[", "]", "[",
72 "]");
73
74 // If there are missing rows, we can't solve the original problem and instead
75 // need to filter the matrix to remove the missing rows and solve a simplified
76 // problem. What this means practically is that we might have pairs of nodes
77 // which are communicating, but we don't have timestamps between. But we can
78 // have multiple paths in our graph between 2 nodes, so we can still solve
79 // time without the missing timestamp.
80 //
81 // In the following example, we can drop any of the last 3 rows, and still
82 // solve.
83 //
84 // [1/3 1/3 1/3 ] [ta] [t_distributed]
85 // [ 1 -1-m1 0 ] [tb] = [oab]
86 // [ 1 0 -1-m2 ] [tc] [oac]
87 // [ 0 1 -1-m2 ] [obc]
88 if (nonzero_offset_count != offset_matrix_.rows()) {
89 Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic> mpq_map =
90 Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic>::Zero(
91 nonzero_offset_count, map_matrix_.cols());
92 Eigen::Matrix<mpq_class, Eigen::Dynamic, 1> mpq_offsets =
93 Eigen::Matrix<mpq_class, Eigen::Dynamic, 1>::Zero(nonzero_offset_count);
94
95 std::vector<bool> valid_nodes(nodes_count(), false);
96
97 size_t destination_row = 0;
98 for (int j = 0; j < map_matrix_.cols(); ++j) {
99 mpq_map(0, j) = mpq_class(1, map_matrix_.cols());
100 }
101 mpq_offsets(0) = mpq_class(0);
102 ++destination_row;
103
104 for (int i = 1; i < offset_matrix_.rows(); ++i) {
105 // Copy over the first row, i.e. the row which says that all times average
106 // to the distributed time. And then copy over all valid rows.
107 if (valid_matrix_(i)) {
108 mpq_offsets(destination_row) = mpq_class(offset_matrix_(i));
109
110 for (int j = 0; j < map_matrix_.cols(); ++j) {
111 mpq_map(destination_row, j) = map_matrix_(i, j) + slope_matrix_(i, j);
112 if (mpq_map(destination_row, j) != 0) {
113 valid_nodes[j] = true;
114 }
115 }
116
117 ++destination_row;
118 }
119 }
120
121 VLOG(1) << "Filtered map " << ToDouble(mpq_map).format(HeavyFmt);
122 VLOG(1) << "Filtered offsets " << ToDouble(mpq_offsets).format(HeavyFmt);
123
124 // Compute (and cache) the current connectivity. If we have N nodes
125 // configured, but logs only from one of them, we want to assume that the
126 // rest of the nodes match the distributed clock exactly.
127 //
128 // If data shows up later for them, we will CHECK when time jumps.
129 //
130 // TODO(austin): Once we have more info on what cases are reasonable, we can
131 // open up the restrictions.
132 if (valid_matrix_ != last_valid_matrix_) {
133 Eigen::FullPivLU<Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic>>
134 full_piv(mpq_map);
135 const size_t connected_nodes = full_piv.rank();
136
137 size_t valid_node_count = 0;
138 for (size_t i = 0; i < valid_nodes.size(); ++i) {
139 const bool valid_node = valid_nodes[i];
140 if (valid_node) {
141 ++valid_node_count;
142 } else {
143 LOG(WARNING)
144 << "Node "
145 << logged_configuration()->nodes()->Get(i)->name()->string_view()
146 << " has no observations, setting to distributed clock.";
147 }
148 }
149
150 // Confirm that the set of nodes we have connected matches the rank.
151 // Otherwise a<->b and c<->d would count as 4 but really is 3.
152 CHECK_EQ(std::max(static_cast<size_t>(1u), valid_node_count),
153 connected_nodes)
154 << ": Ambiguous nodes.";
155
156 last_valid_matrix_ = valid_matrix_;
157 cached_valid_node_count_ = valid_node_count;
158 }
159
160 // There are 2 cases. Either all the nodes are connected with each other by
161 // actual data, or we have isolated nodes. We want to force the isolated
162 // nodes to match the distributed clock exactly, and to solve for the other
163 // nodes.
164 if (cached_valid_node_count_ == 0) {
165 // Cheat. If there are no valid nodes, the slopes are 1, and offset is 0,
166 // ie, just be the distributed clock.
167 return std::make_tuple(
168 Eigen::Matrix<double, Eigen::Dynamic, 1>::Ones(nodes_count()),
169 Eigen::Matrix<double, Eigen::Dynamic, 1>::Zero(nodes_count()));
Austin Schuh1179e7f2020-10-03 00:06:04 -0700170 } else if (cached_valid_node_count_ == nodes_count()) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700171 return Solve(mpq_map, mpq_offsets);
Austin Schuh1179e7f2020-10-03 00:06:04 -0700172 } else {
173 // Strip out any columns (nodes) which aren't relevant. Solve the
174 // simplified problem, then set any nodes which were missing back to slope
175 // 1, offset 0 (ie the distributed clock).
176 Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic>
177 valid_node_mpq_map =
178 Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic>::Zero(
179 nonzero_offset_count, cached_valid_node_count_);
180
181 {
182 // Only copy over the columns with valid nodes in them.
183 size_t column = 0;
184 for (size_t i = 0; i < valid_nodes.size(); ++i) {
185 if (valid_nodes[i]) {
186 valid_node_mpq_map.col(column) = mpq_map.col(i);
187
188 ++column;
189 }
190 }
191 // The 1/n needs to be based on the number of nodes being solved.
192 // Recreate it here.
193 for (int j = 0; j < valid_node_mpq_map.cols(); ++j) {
194 valid_node_mpq_map(0, j) = mpq_class(1, cached_valid_node_count_);
195 }
196 }
197
198 VLOG(1) << "Reduced node filtered map "
199 << ToDouble(valid_node_mpq_map).format(HeavyFmt);
200 VLOG(1) << "Reduced node filtered offsets "
201 << ToDouble(mpq_offsets).format(HeavyFmt);
202
203 // Solve the simplified problem now.
204 std::tuple<Eigen::Matrix<double, Eigen::Dynamic, 1>,
205 Eigen::Matrix<double, Eigen::Dynamic, 1>>
206 valid_result = Solve(valid_node_mpq_map, mpq_offsets);
207
208 // And expand the results back into a solution matrix.
209 std::tuple<Eigen::Matrix<double, Eigen::Dynamic, 1>,
210 Eigen::Matrix<double, Eigen::Dynamic, 1>>
211 result = std::make_tuple(
212 Eigen::Matrix<double, Eigen::Dynamic, 1>::Ones(nodes_count()),
213 Eigen::Matrix<double, Eigen::Dynamic, 1>::Zero(nodes_count()));
214
215 {
216 size_t column = 0;
217 for (size_t i = 0; i < valid_nodes.size(); ++i) {
218 if (valid_nodes[i]) {
219 std::get<0>(result)(i) = std::get<0>(valid_result)(column);
220 std::get<1>(result)(i) = std::get<1>(valid_result)(column);
221
222 ++column;
223 }
224 }
225 }
226
227 return result;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700228 }
229 } else {
230 const Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic> mpq_map =
231 map_matrix_ + slope_matrix_;
232 VLOG(1) << "map " << (map_matrix_ + slope_matrix_).format(HeavyFmt);
233 VLOG(1) << "offsets " << offset_matrix_.format(HeavyFmt);
234
235 return Solve(mpq_map, offset_matrix_);
236 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800237}
238
239} // namespace logger
240} // namespace aos