Skip to content

Commit 4367090

Browse files
committed
Implement parallel PF weight updates using TBB
1 parent 65f8452 commit 4367090

File tree

11 files changed

+75
-11
lines changed

11 files changed

+75
-11
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ include(cmakemodules/script_SIMD.cmake REQUIRED) # SSE2/SSE3/... optimiza
308308
include(cmakemodules/script_simpleini.cmake REQUIRED) # SimpleINI lib
309309
include(cmakemodules/script_suitesparse.cmake REQUIRED) # SuiteSparse libs
310310
include(cmakemodules/script_swissrange.cmake REQUIRED) # Support for SWISSRANGE 3D camera:
311+
include(cmakemodules/script_tbb.cmake REQUIRED) # TBB
311312
include(cmakemodules/script_tinyxml2.cmake REQUIRED) # tinyxml2 lib
312313
include(cmakemodules/script_triclops.cmake REQUIRED) # Check for PointGreyResearch (PGR) Triclops library
313314
include(cmakemodules/script_videre_svs.cmake REQUIRED) # Support for Videre Design stereo camera:

apps/ro-localization/CPosePDFParticlesExtended.h

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ class TExtendedCPose2D
3030
mrpt::math::CVectorDouble state;
3131
};
3232

33-
#define DUMMY_LINKAGE
34-
3533
/** Declares a class that represents a Probability Distribution
3634
* function (PDF) of a 2D pose (x,y,phi).
3735
* This class implements that PDF using a set of particles

cmakemodules/script_show_final_summary.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ SHOW_CONFIG_LINE_SYSTEM("OpenGL GLES " CMAKE_MRPT_HAS_GL
143143
SHOW_CONFIG_LINE_SYSTEM("GLUT " CMAKE_MRPT_HAS_GLUT)
144144
SHOW_CONFIG_LINE_SYSTEM("PCAP (Wireshark logs for Velodyne) " CMAKE_MRPT_HAS_LIBPCAP)
145145
SHOW_CONFIG_LINE_SYSTEM("SuiteSparse " CMAKE_MRPT_HAS_SUITESPARSE "[Version: ${SuiteSparse_VERSION}]")
146+
SHOW_CONFIG_LINE_SYSTEM("TBB " CMAKE_MRPT_HAS_TBB "[Version: ${TBB_VERSION}]")
146147
SHOW_CONFIG_LINE_SYSTEM("tinyxml2 " CMAKE_MRPT_HAS_TINYXML2)
147148
SHOW_CONFIG_LINE_SYSTEM("wxWidgets " CMAKE_MRPT_HAS_WXWIDGETS "[Version: ${wxWidgets_VERSION_STRING} ${CMAKE_WXWIDGETS_TOOLKIT_NAME}]")
148149
message(STATUS "")

cmakemodules/script_tbb.cmake

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
set(CMAKE_MRPT_HAS_TBB 0)
3+
set(CMAKE_MRPT_HAS_TBB_SYSTEM 0)
4+
5+
option(DISABLE_TBB "Force not using TBB" "OFF")
6+
mark_as_advanced(DISABLE_TBB)
7+
if(DISABLE_TBB)
8+
return()
9+
endif()
10+
11+
find_package(TBB QUIET)
12+
13+
if(TBB_FOUND)
14+
set(CMAKE_MRPT_HAS_TBB 1)
15+
set(CMAKE_MRPT_HAS_TBB_SYSTEM 1)
16+
endif()

doc/source/doxygen-docs/changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# Version 2.14.0: UNRELEASED
44
- Changes in libraries:
5+
- \ref mrpt_slam_grp:
6+
- Particle filtering algorithm pfStandardProposal now uses TBB (if present) for automatically running weight updates in parallel.
57
- \ref mrpt_rtti_grp:
68
- mrpt::rtti::CObject::GetRuntimeClassIdStatic() no longer depends on static variables, but on constexpr. This totally removes the possibility of initialization order fiasco while registering classes.
79
- **IMPORTANT CHANGE**: To make the change above possible, these macros have changed:

libs/nav/src/tpspace/CPTG_DiffDrive_CollisionGridBased.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include "nav-precomp.h" // Precomp header
1111
//
12+
#include <mrpt/core/WorkerThreadsPool.h>
13+
#include <mrpt/core/get_env.h>
1214
#include <mrpt/io/CFileGZInputStream.h>
1315
#include <mrpt/io/CFileGZOutputStream.h>
1416
#include <mrpt/kinematics/CVehicleVelCmd_DiffDriven.h>
@@ -785,7 +787,7 @@ void CPTG_DiffDrive_CollisionGridBased::internal_initialize(
785787
}
786788
} // k
787789

788-
if (verbose) cout << format("Done! [%.03f sec]\n", tictac.Tac());
790+
if (verbose) cout << format("Done! [%.03f sec]", tictac.Tac()) << std::endl;
789791

790792
// save it to the cache file for the next run:
791793
saveColGridsToFile(cacheFilename, m_robotShape);

libs/obs/include/mrpt/obs/CObservation2DRangeScan.h

+8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
+------------------------------------------------------------------------+ */
99
#pragma once
1010

11+
#include <mrpt/containers/NonCopiableData.h>
1112
#include <mrpt/core/aligned_std_vector.h>
13+
#include <mrpt/core/lock_helper.h>
1214
#include <mrpt/maps/CMetricMap.h>
1315
#include <mrpt/math/CPolygon.h>
1416
#include <mrpt/obs/CObservation.h>
1517
#include <mrpt/obs/T2DScanProperties.h>
1618
#include <mrpt/poses/CPose3D.h>
1719
#include <mrpt/serialization/CSerializable.h>
1820

21+
#include <mutex>
22+
1923
// Add for declaration of mexplus::from template specialization
2024
DECLARE_MEXPLUS_FROM(mrpt::obs::CObservation2DRangeScan)
2125

@@ -166,6 +170,7 @@ class CObservation2DRangeScan : public CObservation
166170
* It's a generic smart pointer to avoid depending here in the library
167171
* mrpt-obs on classes on other libraries.
168172
*/
173+
mutable mrpt::containers::NonCopiableData<std::recursive_mutex> m_cachedMapMtx;
169174
mutable mrpt::maps::CMetricMap::Ptr m_cachedMap;
170175
/** Internal method, used from buildAuxPointsMap() */
171176
void internal_buildAuxPointsMap(const void* options = nullptr) const;
@@ -183,6 +188,7 @@ class CObservation2DRangeScan : public CObservation
183188
template <class POINTSMAP>
184189
inline const POINTSMAP* getAuxPointsMap() const
185190
{
191+
auto lck = mrpt::lockHelper(m_cachedMapMtx.data);
186192
return static_cast<const POINTSMAP*>(m_cachedMap.get());
187193
}
188194

@@ -201,6 +207,8 @@ class CObservation2DRangeScan : public CObservation
201207
template <class POINTSMAP>
202208
inline const POINTSMAP* buildAuxPointsMap(const void* options = nullptr) const
203209
{
210+
auto lck = mrpt::lockHelper(m_cachedMapMtx.data);
211+
204212
if (!m_cachedMap) internal_buildAuxPointsMap(options);
205213
return static_cast<const POINTSMAP*>(m_cachedMap.get());
206214
}

libs/obs/src/CObservation2DRangeScan.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ void CObservation2DRangeScan::serializeFrom(mrpt::serialization::CArchive& in, u
178178
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
179179
};
180180

181+
auto lck = mrpt::lockHelper(m_cachedMapMtx.data);
181182
m_cachedMap.reset();
182183
}
183184

@@ -423,6 +424,8 @@ void internal_set_build_points_map_from_scan2D(scan2pts_functor fn)
423424
---------------------------------------------------------------*/
424425
void CObservation2DRangeScan::internal_buildAuxPointsMap(const void* options) const
425426
{
427+
auto lck = mrpt::lockHelper(m_cachedMapMtx.data);
428+
426429
if (!ptr_internal_build_points_map_from_scan2D)
427430
throw std::runtime_error(
428431
"[CObservation2DRangeScan::buildAuxPointsMap] ERROR: This function "

libs/slam/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ list(APPEND slam_EXTRA_SRCS
1010
list(APPEND slam_EXTRA_SRCS_NAME
1111
"slam" "slam" "slam-headers")
1212

13+
if(CMAKE_MRPT_HAS_TBB)
14+
set(tbb_dep TBB)
15+
else()
16+
set(tbb_dep "")
17+
endif()
18+
1319
#---------------------------------------------
1420
# Macro declared in "DeclareMRPTLib.cmake":
1521
#---------------------------------------------
@@ -19,8 +25,13 @@ define_mrpt_lib(
1925
# Dependencies
2026
mrpt-vision
2127
mrpt-maps
28+
# Other imported targets:
29+
${tbb_dep} # find_package() lib name
2230
)
2331

2432
if(BUILD_mrpt-slam)
2533

34+
if (CMAKE_MRPT_HAS_TBB)
35+
target_link_libraries(slam PUBLIC TBB::tbb)
36+
endif()
2637
endif()

libs/slam/include/mrpt/slam/PF_implementations.h

+28-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <mrpt/bayes/CParticleFilterCapable.h>
1313
#include <mrpt/bayes/CParticleFilterData.h>
14+
#include <mrpt/config.h>
1415
#include <mrpt/math/data_utils.h> // averageLogLikelihood()
1516
#include <mrpt/math/distributions.h> // chi2inv
1617
#include <mrpt/obs/CActionCollection.h>
@@ -21,6 +22,11 @@
2122
#include <mrpt/slam/TKLDParams.h>
2223

2324
#include <cmath>
25+
#include <set>
26+
27+
#if MRPT_HAS_TBB
28+
#include <tbb/tbb.h>
29+
#endif
2430

2531
/** \file PF_implementations.h
2632
* This file contains the implementations of the template members declared in
@@ -334,17 +340,31 @@ void PF_implementation<PARTICLE_TYPE, MYSELF, STORAGE>::PF_SLAM_implementation_p
334340
// UPDATE STAGE
335341
// ----------------------------------------------------------------------
336342
// Compute all the likelihood values & update particles weight:
343+
#if MRPT_HAS_TBB
344+
tbb::parallel_for(
345+
tbb::blocked_range<size_t>(0, M),
346+
[&](const tbb::blocked_range<size_t>& r)
347+
{
348+
for (size_t i = r.begin(); i != r.end(); ++i)
349+
{
350+
#else
337351
for (size_t i = 0; i < M; i++)
338352
{
339-
bool pose_is_valid;
340-
const mrpt::math::TPose3D partPose =
341-
getLastPose(i, pose_is_valid); // Take the particle data:
342-
auto partPose2 = mrpt::poses::CPose3D(partPose);
343-
const double obs_log_lik =
344-
PF_SLAM_computeObservationLikelihoodForParticle(PF_options, i, *sf, partPose2);
345-
ASSERT_(!std::isnan(obs_log_lik) && std::isfinite(obs_log_lik));
346-
me->m_particles[i].log_w += obs_log_lik * PF_options.powFactor;
353+
#endif
354+
bool pose_is_valid;
355+
const mrpt::math::TPose3D partPose =
356+
getLastPose(i, pose_is_valid); // Take the particle data:
357+
auto partPose2 = mrpt::poses::CPose3D(partPose);
358+
const double obs_log_lik =
359+
PF_SLAM_computeObservationLikelihoodForParticle(PF_options, i, *sf, partPose2);
360+
ASSERT_(!std::isnan(obs_log_lik) && std::isfinite(obs_log_lik));
361+
me->m_particles[i].log_w += obs_log_lik * PF_options.powFactor;
362+
#if MRPT_HAS_TBB
363+
} // for each particle "i"
364+
});
365+
#else
347366
} // for each particle "i"
367+
#endif
348368

349369
// Normalization of weights is done outside of this method
350370
// automatically.

parse-files/config.h.in

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ ${CMAKE_MRPT_BUILD_SHARED_LIB}
112112
#define MRPT_HAS_SIMPLEINI ${CMAKE_MRPT_HAS_SIMPLEINI}
113113
#define MRPT_HAS_SIMPLEINI_SYSTEM ${CMAKE_MRPT_HAS_SIMPLEINI_SYSTEM}
114114

115+
#define MRPT_HAS_TBB ${CMAKE_MRPT_HAS_TBB}
116+
115117
#cmakedefine MRPT_ROS_VERSION ${MRPT_ROS_VERSION}
116118

117119
/** These two values are detected in Eigen when building MRPT, so we have

0 commit comments

Comments
 (0)