Skip to content

Commit

Permalink
Use faster version of in-place add
Browse files Browse the repository at this point in the history
  • Loading branch information
ProfFan committed Jun 2, 2020
1 parent 50ffeb7 commit 15fda68
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions gtsam/linear/JacobianFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,18 +552,27 @@ VectorValues JacobianFactor::hessianDiagonal() const {
void JacobianFactor::hessianDiagonalAdd(VectorValues& d) const {
for (size_t pos = 0; pos < size(); ++pos) {
Key j = keys_[pos];
size_t nj = Ab_(pos).cols();
Vector dj(nj);
for (size_t k = 0; k < nj; ++k) {
Vector column_k = Ab_(pos).col(k);
if (model_)
model_->whitenInPlace(column_k);
dj(k) = dot(column_k, column_k);
}
auto item = d.find(j);

if(item != d.end()) {
item->second += dj;
size_t nj = Ab_(pos).cols();

This comment has been minimized.

Copy link
@dellaert

dellaert Jun 2, 2020

Member

Same comments as other one. Use emplace - and avoid having two copies of almost identical code :-) Sorry for going so deep, but we will be the only ones visiting this code for a while to come.

This comment has been minimized.

Copy link
@ProfFan

ProfFan Jun 2, 2020

Author Collaborator

I think the current way is better - otherwise we need to move the if inside the loop I think.

This comment has been minimized.

Copy link
@dellaert

dellaert Jun 2, 2020

Member

Maybe you’re not getting what I mean. Do emplace first. If you get new memory, init with zeros. Way cheaper than re-traversing. Then have one += loop.

Vector& dj = item->second;
for (size_t k = 0; k < nj; ++k) {
Vector column_k = Ab_(pos).col(k);
if (model_)
model_->whitenInPlace(column_k);
dj(k) += dot(column_k, column_k);
}
} else {
size_t nj = Ab_(pos).cols();
Vector dj(nj);
for (size_t k = 0; k < nj; ++k) {
Vector column_k = Ab_(pos).col(k);
if (model_)
model_->whitenInPlace(column_k);
dj(k) = dot(column_k, column_k);
}

d.emplace(j, dj);
}
}
Expand Down

0 comments on commit 15fda68

Please sign in to comment.