Skip to content

Commit

Permalink
Zero additional allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
tizianoGuadagnino committed Dec 4, 2024
1 parent 0395784 commit 1fdd09b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
23 changes: 6 additions & 17 deletions cpp/kiss_icp/core/Registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
#include "Registration.hpp"

#include <tbb/blocked_range.h>
#include <tbb/concurrent_vector.h>
#include <tbb/global_control.h>
#include <tbb/info.h>
#include <tbb/parallel_for.h>
#include <tbb/parallel_reduce.h>
#include <tbb/task_arena.h>

Expand All @@ -44,7 +46,7 @@ using Matrix3_6d = Eigen::Matrix<double, 3, 6>;
using Vector6d = Eigen::Matrix<double, 6, 1>;
} // namespace Eigen

using Correspondences = std::vector<std::pair<Eigen::Vector3d, Eigen::Vector3d>>;
using Correspondences = tbb::concurrent_vector<std::pair<Eigen::Vector3d, Eigen::Vector3d>>;
using LinearSystem = std::pair<Eigen::Matrix6d, Eigen::Vector6d>;

namespace {
Expand All @@ -61,30 +63,17 @@ Correspondences DataAssociation(const std::vector<Eigen::Vector3d> &points,
using points_iterator = std::vector<Eigen::Vector3d>::const_iterator;
Correspondences correspondences;
correspondences.reserve(points.size());
correspondences = tbb::parallel_reduce(
tbb::parallel_for(
// Range
tbb::blocked_range<points_iterator>{points.cbegin(), points.cend()},
// Identity
correspondences,
// 1st lambda: Parallel computation
[&](const tbb::blocked_range<points_iterator> &r, Correspondences res) -> Correspondences {
res.reserve(r.size());
[&](const tbb::blocked_range<points_iterator> &r) {
std::for_each(r.begin(), r.end(), [&](const auto &point) {
const auto &[closest_neighbor, distance] = voxel_map.GetClosestNeighbor(point);
if (distance < max_correspondance_distance) {
res.emplace_back(point, closest_neighbor);
correspondences.emplace_back(point, closest_neighbor);
}
});
return res;
},
// 2nd lambda: Parallel reduction
[](Correspondences a, const Correspondences &b) -> Correspondences {
a.insert(a.end(), //
std::make_move_iterator(b.cbegin()), //
std::make_move_iterator(b.cend()));
return a;
});

return correspondences;
}

Expand Down
1 change: 1 addition & 0 deletions cpp/kiss_icp/core/VoxelHashMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ using kiss_icp::Voxel;

std::vector<Voxel> GetAdjacentVoxels(const Voxel &voxel, int adjacent_voxels = 1) {
std::vector<Voxel> voxel_neighborhood;
voxel_neighborhood.reserve(27);
for (int i = voxel.x() - adjacent_voxels; i < voxel.x() + adjacent_voxels + 1; ++i) {
for (int j = voxel.y() - adjacent_voxels; j < voxel.y() + adjacent_voxels + 1; ++j) {
for (int k = voxel.z() - adjacent_voxels; k < voxel.z() + adjacent_voxels + 1; ++k) {
Expand Down

0 comments on commit 1fdd09b

Please sign in to comment.