Skip to content

Commit

Permalink
refactor!: Split helper header into multiple files (#2153)
Browse files Browse the repository at this point in the history
In #2151 I discovered that modifying `Helpers.hpp` basically triggers a full rebuild and that we put a bunch of different stuff into it. In this PR I try to improve things by splitting up `Helpers.hpp` into multiple headers
  • Loading branch information
andiwand committed May 27, 2023
1 parent 210824a commit 64ed0b2
Show file tree
Hide file tree
Showing 144 changed files with 575 additions and 618 deletions.
1 change: 0 additions & 1 deletion Core/include/Acts/Detector/detail/KdtSurfacesProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "Acts/Detector/detail/ReferenceGenerators.hpp"
#include "Acts/Surfaces/Surface.hpp"
#include "Acts/Utilities/BinningData.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/KDTree.hpp"

#include <array>
Expand Down
1 change: 0 additions & 1 deletion Core/include/Acts/Detector/detail/SupportHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Surfaces/Surface.hpp"
#include "Acts/Utilities/Helpers.hpp"

#include <array>
#include <vector>
Expand Down
1 change: 1 addition & 0 deletions Core/include/Acts/EventData/MultiTrajectory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Acts/EventData/TrackStatePropMask.hpp"
#include "Acts/EventData/Types.hpp"
#include "Acts/Geometry/GeometryContext.hpp"
#include "Acts/Utilities/AlgebraHelpers.hpp"
#include "Acts/Utilities/HashedString.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/TypeTraits.hpp"
Expand Down
2 changes: 1 addition & 1 deletion Core/include/Acts/EventData/MultiTrajectory.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/AlgebraHelpers.hpp"
#include "Acts/Utilities/TypeTraits.hpp"

#include <bitset>
Expand Down
1 change: 1 addition & 0 deletions Core/include/Acts/EventData/VectorMultiTrajectory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "Acts/EventData/TrackStatePropMask.hpp"
#include "Acts/EventData/detail/DynamicColumn.hpp"
#include "Acts/Utilities/Concepts.hpp"
#include "Acts/Utilities/Helpers.hpp"

#include <unordered_map>

Expand Down
1 change: 0 additions & 1 deletion Core/include/Acts/Geometry/CylinderVolumeBounds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "Acts/Geometry/Volume.hpp"
#include "Acts/Geometry/VolumeBounds.hpp"
#include "Acts/Utilities/BinningType.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/detail/periodic.hpp"

#include <array>
Expand Down
3 changes: 2 additions & 1 deletion Core/include/Acts/Geometry/GeometryObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "Acts/Geometry/GeometryIdentifier.hpp"
#include "Acts/Geometry/Polyhedron.hpp"
#include "Acts/Utilities/BinningType.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/VectorHelpers.hpp"

namespace Acts {

Expand Down Expand Up @@ -90,4 +90,5 @@ inline double GeometryObject::binningPositionValue(const GeometryContext& gctx,
BinningValue bValue) const {
return VectorHelpers::cast(binningPosition(gctx, bValue), bValue);
}

} // namespace Acts
1 change: 0 additions & 1 deletion Core/include/Acts/Geometry/SurfaceArrayCreator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "Acts/Surfaces/Surface.hpp"
#include "Acts/Surfaces/SurfaceArray.hpp"
#include "Acts/Utilities/BinningType.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "Acts/Utilities/detail/AxisFwd.hpp"

Expand Down
2 changes: 1 addition & 1 deletion Core/include/Acts/Navigation/NavigationStateUpdators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "Acts/Navigation/NavigationDelegates.hpp"
#include "Acts/Utilities/BinningType.hpp"
#include "Acts/Utilities/Enumerate.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/IAxis.hpp"
#include "Acts/Utilities/VectorHelpers.hpp"

#include <array>
#include <memory>
Expand Down
1 change: 1 addition & 0 deletions Core/include/Acts/Propagator/Navigator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "Acts/Propagator/ConstrainedStep.hpp"
#include "Acts/Surfaces/Surface.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "Acts/Utilities/StringHelpers.hpp"

#include <iomanip>
#include <iterator>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

#pragma once

#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Definitions/TrackParametrization.hpp"
#include "Acts/Utilities/VectorHelpers.hpp"

#include <array>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "Acts/MagneticField/MagneticFieldContext.hpp"
#include "Acts/Material/Interactions.hpp"
#include "Acts/Propagator/Propagator.hpp"
#include "Acts/Utilities/Helpers.hpp"

#include <array>
#include <cmath>
Expand Down
1 change: 0 additions & 1 deletion Core/include/Acts/Propagator/detail/LoopProtection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#pragma once

#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/Logger.hpp"

namespace Acts {
Expand Down
1 change: 0 additions & 1 deletion Core/include/Acts/Surfaces/DiscSurface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "Acts/Surfaces/DiscBounds.hpp"
#include "Acts/Surfaces/InfiniteBounds.hpp"
#include "Acts/Surfaces/Surface.hpp"
#include "Acts/Utilities/Helpers.hpp"

namespace Acts {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "Acts/TrackFitting/KalmanFitter.hpp"
#include "Acts/TrackFitting/detail/VoidKalmanComponents.hpp"
#include "Acts/Utilities/CalibrationContext.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "Acts/Utilities/Result.hpp"
#include "Acts/Utilities/Zip.hpp"
Expand Down
199 changes: 199 additions & 0 deletions Core/include/Acts/Utilities/AlgebraHelpers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// This file is part of the Acts project.
//
// Copyright (C) 2016-2023 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#pragma once

#include "Acts/Definitions/Algebra.hpp"

#include <bitset>
#include <optional>

#include "Eigen/Dense"

namespace Acts {

/// Convert a bitset to a matrix of integers, with each element set to the bit
/// value.
/// @note How the bits are assigned to matrix elements depends on the storage
/// type of the matrix being converted (row-major or col-major)
/// @tparam MatrixType Matrix type that is produced
/// @param bs The bitset to convert
/// @return A matrix with the integer values of the bits from @p bs
template <typename MatrixType>
MatrixType bitsetToMatrix(const std::bitset<MatrixType::RowsAtCompileTime *
MatrixType::ColsAtCompileTime>
bs) {
constexpr int rows = MatrixType::RowsAtCompileTime;
constexpr int cols = MatrixType::ColsAtCompileTime;

static_assert(rows != -1 && cols != -1,
"bitsetToMatrix does not support dynamic matrices");

MatrixType m;
auto* p = m.data();
for (size_t i = 0; i < rows * cols; i++) {
p[i] = bs[rows * cols - 1 - i];
}
return m;
}

/// Convert an integer matrix to a bitset.
/// @note How the bits are ordered depends on the storage type of the matrix
/// being converted (row-major or col-major)
/// @tparam Derived Eigen base concrete type
/// @param m Matrix that is converted
/// @return The converted bitset.
template <typename Derived>
auto matrixToBitset(const Eigen::PlainObjectBase<Derived>& m) {
using MatrixType = Eigen::PlainObjectBase<Derived>;
constexpr size_t rows = MatrixType::RowsAtCompileTime;
constexpr size_t cols = MatrixType::ColsAtCompileTime;

std::bitset<rows * cols> res;

auto* p = m.data();
for (size_t i = 0; i < rows * cols; i++) {
res[rows * cols - 1 - i] = static_cast<bool>(p[i]);
}

return res;
}

/// @brief Perform a blocked matrix multiplication, avoiding Eigen GEMM
/// methods.
///
/// @tparam A The type of the first matrix, which should be MxN
/// @tparam B The type of the second matrix, which should be NxP
///
/// @param[in] a An MxN matrix of type A
/// @param[in] b An NxP matrix of type P
///
/// @returns The product ab
template <typename A, typename B>
inline ActsMatrix<A::RowsAtCompileTime, B::ColsAtCompileTime> blockedMult(
const A& a, const B& b) {
// Extract the sizes of the matrix types that we receive as template
// parameters.
constexpr int M = A::RowsAtCompileTime;
constexpr int N = A::ColsAtCompileTime;
constexpr int P = B::ColsAtCompileTime;

// Ensure that the second dimension of our first matrix equals the first
// dimension of the second matrix, otherwise we cannot multiply.
static_assert(N == B::RowsAtCompileTime);

if constexpr (M <= 4 && N <= 4 && P <= 4) {
// In cases where the matrices being multiplied are small, we can rely on
// Eigen do to a good job, and we don't really need to do any blocking.
return a * b;
} else {
// Here, we want to calculate the expression: C = AB, Eigen, natively,
// doesn't do a great job at this if the matrices A and B are large
// (roughly M >= 8, N >= 8, or P >= 8), and applies a slow GEMM operation.
// We apply a blocked matrix multiplication operation to decompose the
// multiplication into smaller operations, relying on the fact that:
//
// ┌ ┐ ┌ ┐ ┌ ┐
// │ C₁₁ C₁₂ │ = │ A₁₁ A₁₂ │ │ B₁₁ B₁₂ │
// │ C₂₁ C₂₂ │ = │ A₂₁ A₂₂ │ │ B₂₁ B₂₂ │
// └ ┘ └ ┘ └ ┘
//
// where:
//
// C₁₁ = A₁₁ * B₁₁ + A₁₂ * B₂₁
// C₁₂ = A₁₁ * B₁₂ + A₁₂ * B₂₂
// C₂₁ = A₂₁ * B₁₁ + A₂₂ * B₂₁
// C₂₂ = A₂₁ * B₁₂ + A₂₂ * B₂₂
//
// The sizes of these submatrices are roughly half (in each dimension) that
// of the parent matrix. If the size of the parent matrix is even, we can
// divide it exactly, If the size of the parent matrix is odd, then some
// of the submatrices will be one larger than the others. In general, for
// any matrix Q, the sizes of the submatrices are (where / denotes integer
// division):
//
// Q₁₁ : M / 2 × P / 2
// Q₁₂ : M / 2 × (P + 1) / 2
// Q₂₁ : (M + 1) / 2 × P / 2
// Q₂₂ : (M + 1) / 2 × (P + 1) / 2
//
// See https://csapp.cs.cmu.edu/public/waside/waside-blocking.pdf for a
// more in-depth explanation of blocked matrix multiplication.
constexpr int M1 = M / 2;
constexpr int M2 = (M + 1) / 2;
constexpr int N1 = N / 2;
constexpr int N2 = (N + 1) / 2;
constexpr int P1 = P / 2;
constexpr int P2 = (P + 1) / 2;

// Construct the end result in this matrix, which destroys a few of Eigen's
// built-in optimization techniques, but sadly this is necessary.
ActsMatrix<M, P> r;

// C₁₁ = A₁₁ * B₁₁ + A₁₂ * B₂₁
r.template topLeftCorner<M1, P1>().noalias() =
a.template topLeftCorner<M1, N1>() *
b.template topLeftCorner<N1, P1>() +
a.template topRightCorner<M1, N2>() *
b.template bottomLeftCorner<N2, P1>();

// C₁₂ = A₁₁ * B₁₂ + A₁₂ * B₂₂
r.template topRightCorner<M1, P2>().noalias() =
a.template topLeftCorner<M1, N1>() *
b.template topRightCorner<N1, P2>() +
a.template topRightCorner<M1, N2>() *
b.template bottomRightCorner<N2, P2>();

// C₂₁ = A₂₁ * B₁₁ + A₂₂ * B₂₁
r.template bottomLeftCorner<M2, P1>().noalias() =
a.template bottomLeftCorner<M2, N1>() *
b.template topLeftCorner<N1, P1>() +
a.template bottomRightCorner<M2, N2>() *
b.template bottomLeftCorner<N2, P1>();

// C₂₂ = A₂₁ * B₁₂ + A₂₂ * B₂₂
r.template bottomRightCorner<M2, P2>().noalias() =
a.template bottomLeftCorner<M2, N1>() *
b.template topRightCorner<N1, P2>() +
a.template bottomRightCorner<M2, N2>() *
b.template bottomRightCorner<N2, P2>();

return r;
}
}

/// Calculate the inverse of an Eigen matrix after checking if it can be
/// numerically inverted. This allows to catch potential FPEs before they occur.
///
/// Our main motivation for this is that users might have a strict FPE policy
/// which would flag every single occurrence as a failure and then sombody has
/// to investigate. Since we are processing a high number of events and floating
/// point numbers sometimes work in mysterious ways the caller of this function
/// might want to hide FPEs and handle them in a more controlled way.
///
/// @tparam Derived Eigen derived concrete type
/// @tparam Result Eigen result type defaulted to input type
///
/// @param m Eigen matrix to invert
///
/// @return The theta value
template <typename MatrixType, typename ResultType = MatrixType>
std::optional<ResultType> safeInverse(const MatrixType& m) noexcept {
ResultType result;
bool invertible = false;

m.computeInverseWithCheck(result, invertible);

if (invertible) {
return result;
}

return std::nullopt;
}

} // namespace Acts
3 changes: 2 additions & 1 deletion Core/include/Acts/Utilities/BinningData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#pragma once

#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Utilities/BinningType.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/ThrowAssert.hpp"
#include "Acts/Utilities/VectorHelpers.hpp"

#include <algorithm>
#include <cmath>
Expand Down
6 changes: 1 addition & 5 deletions Core/include/Acts/Utilities/BoundingBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@

#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Utilities/Frustum.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/Ray.hpp"
#include "Acts/Visualization/IVisualization3D.hpp"

#include <algorithm>
#include <array>
#include <iostream>
#include <limits>
#include <ostream>
#include <memory>
#include <tuple>
#include <vector>

Expand Down
2 changes: 0 additions & 2 deletions Core/include/Acts/Utilities/FpeMonitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

#pragma once

#include "Acts/Utilities/Helpers.hpp"

namespace Acts {

class FpeMonitor {
Expand Down
2 changes: 1 addition & 1 deletion Core/include/Acts/Utilities/Frustum.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/VectorHelpers.hpp"

template <typename value_t, size_t DIM, size_t SIDES>
template <size_t D, std::enable_if_t<D == 2, int>>
Expand Down

0 comments on commit 64ed0b2

Please sign in to comment.