Skip to content

Commit

Permalink
Reduced duplicate computations with temporary variables. (cms-sw#614)
Browse files Browse the repository at this point in the history
Also removed addition of transposed matrix by directly copying the needed elements.
cms-sw#31722 (comment)
cms-sw#31722 (comment)
  • Loading branch information
ericcano committed May 17, 2021
1 parent 8472e87 commit a598f67
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions RecoPixelVertexing/PixelTrackFitting/interface/BrokenLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,31 +208,42 @@ namespace brokenline {
const riemannFit::VectorNd<n>& varBeta) {
riemannFit::MatrixNd<n> c_uMat = riemannFit::MatrixNd<n>::Zero();
for (u_int i = 0; i < n; i++) {
// Pre compute differences used multiple time. Notation is t<j><k> = sTotal(i + k) - sTotal(i + k - j).
// We pre-compute only when possible/needed
// The compiler with unroll this loop with a known boundary, and no tests should
// be actually executed.
const auto t10 = (i > 0) ? sTotal(i) - sTotal(i - 1) : 0;
const auto t21 = ((i > 0) && (i < (n - 1))) ? sTotal(i + 1) - sTotal(i - 1) : 0;
const auto t11 = (i < (n - 1)) ? sTotal(i + 1) - sTotal(i) : 0;
const auto t12 = (i < (n - 2)) ? sTotal(i + 2) - sTotal(i + 1) : 0;
const auto t22 = (i < (n - 2)) ? sTotal(i + 2) - sTotal(i) : 0;
const auto t11xt10 = t11 * t10;
const auto t12xt11 = t12 * t11;
c_uMat(i, i) = weights(i);
if (i > 1)
c_uMat(i, i) += 1. / (varBeta(i - 1) * riemannFit::sqr(sTotal(i) - sTotal(i - 1)));
c_uMat(i, i) += 1. / (varBeta(i - 1) * riemannFit::sqr(t10));

if (i > 0 && i < n - 1)
c_uMat(i, i) +=
(1. / varBeta(i)) * riemannFit::sqr((sTotal(i + 1) - sTotal(i - 1)) /
((sTotal(i + 1) - sTotal(i)) * (sTotal(i) - sTotal(i - 1))));
c_uMat(i, i) += riemannFit::sqr(t21 / t11xt10) / varBeta(i);

if (i < n - 2)
c_uMat(i, i) += 1. / (varBeta(i + 1) * riemannFit::sqr(sTotal(i + 1) - sTotal(i)));
c_uMat(i, i) += 1. / (varBeta(i + 1) * riemannFit::sqr(t11));

if (i > 0 && i < n - 1)
c_uMat(i, i + 1) =
1. / (varBeta(i) * (sTotal(i + 1) - sTotal(i))) *
(-(sTotal(i + 1) - sTotal(i - 1)) / ((sTotal(i + 1) - sTotal(i)) * (sTotal(i) - sTotal(i - 1))));
if (i < n - 2)
c_uMat(i, i + 1) +=
1. / (varBeta(i + 1) * (sTotal(i + 1) - sTotal(i))) *
(-(sTotal(i + 2) - sTotal(i)) / ((sTotal(i + 2) - sTotal(i + 1)) * (sTotal(i + 1) - sTotal(i))));
c_uMat(i, i + 1) = -t21 / (varBeta(i) * t11 * t11xt10);

if (i < n - 2)
c_uMat(i, i + 2) = 1. / (varBeta(i + 1) * (sTotal(i + 2) - sTotal(i + 1)) * (sTotal(i + 1) - sTotal(i)));
c_uMat(i, i + 1) += -t22 / (varBeta(i + 1) * t11 * t12xt11);

c_uMat(i, i) *= 0.5;
if (i < n - 1) // for 2 previous steps, non diagonal element copy for this symmetric matrix
c_uMat(i + 1, i) = c_uMat(i, i + 1);

if (i < n - 2) {
c_uMat(i, i + 2) = 1. / (varBeta(i + 1) * t12xt11);
c_uMat(i + 2, i) = c_uMat(i, i + 2); // non-diagonal element copy.
}
}
return c_uMat + c_uMat.transpose();
return c_uMat;
}

/*!
Expand Down

0 comments on commit a598f67

Please sign in to comment.