Skip to content

Commit 63f25ce

Browse files
authored
registration: parallelize SAC-IA error metric with OpenMP (#6431)
* registration: parallelize SAC-IA error metric with OpenMP * registration: add OpenMP thread control to SAC-IA
1 parent 9489406 commit 63f25ce

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

registration/include/pcl/registration/ia_ransac.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class SampleConsensusInitialAlignment : public Registration<PointSource, PointTa
150150
corr_dist_threshold_ = 100.0f;
151151
transformation_estimation_.reset(
152152
new pcl::registration::TransformationEstimationSVD<PointSource, PointTarget>);
153+
setNumberOfThreads();
153154
};
154155

155156
/** \brief Provide a shared pointer to the source point cloud's feature descriptors
@@ -230,6 +231,13 @@ class SampleConsensusInitialAlignment : public Registration<PointSource, PointTa
230231
return (k_correspondences_);
231232
}
232233

234+
/** \brief Initialize the scheduler and set the number of threads to use.
235+
* \param nr_threads the number of hardware threads to use (0 sets the value back to
236+
* automatic)
237+
*/
238+
void
239+
setNumberOfThreads(unsigned int nr_threads = 0);
240+
233241
/** \brief Specify the error function to minimize
234242
* \note This call is optional. TruncatedError will be used by default
235243
* \param[in] error_functor a shared pointer to a subclass of
@@ -316,6 +324,9 @@ class SampleConsensusInitialAlignment : public Registration<PointSource, PointTa
316324
* correspondence. */
317325
int k_correspondences_{10};
318326

327+
/** \brief The number of threads the scheduler should use. */
328+
unsigned int threads_{1};
329+
319330
/** \brief The KdTree used to compare feature descriptors. */
320331
FeatureKdTreePtr feature_tree_;
321332

registration/include/pcl/registration/impl/ia_ransac.hpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@
4646

4747
namespace pcl {
4848

49+
template <typename PointSource, typename PointTarget, typename FeatureT>
50+
void
51+
SampleConsensusInitialAlignment<PointSource, PointTarget, FeatureT>::setNumberOfThreads(
52+
unsigned int nr_threads)
53+
{
54+
#ifdef _OPENMP
55+
if (nr_threads == 0)
56+
threads_ = omp_get_num_procs();
57+
else
58+
threads_ = nr_threads;
59+
PCL_DEBUG("[pcl::%s::setNumberOfThreads] Setting number of threads to %u.\n",
60+
getClassName().c_str(),
61+
threads_);
62+
#else
63+
threads_ = 1;
64+
if (nr_threads != 1)
65+
PCL_WARN("[pcl::%s::setNumberOfThreads] Parallelization is requested, but OpenMP "
66+
"is not available! Continuing without parallelization.\n",
67+
getClassName().c_str());
68+
#endif // _OPENMP
69+
}
70+
4971
template <typename PointSource, typename PointTarget, typename FeatureT>
5072
void
5173
SampleConsensusInitialAlignment<PointSource, PointTarget, FeatureT>::setSourceFeatures(
@@ -172,12 +194,16 @@ SampleConsensusInitialAlignment<PointSource, PointTarget, FeatureT>::computeErro
172194
std::vector<float> nn_distance(1);
173195

174196
const ErrorFunctor& compute_error = *error_functor_;
197+
const auto& tree = tree_;
175198
float error = 0;
176199

177-
for (const auto& point : cloud) {
200+
#pragma omp parallel for default(none) shared(cloud, tree, compute_error) \
201+
firstprivate(nn_index, nn_distance) reduction(+ : error) num_threads(threads_)
202+
for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(cloud.size()); ++i) {
203+
const auto& point = cloud[static_cast<std::size_t>(i)];
178204
// Find the distance between point and its nearest neighbor in the target point
179205
// cloud
180-
tree_->nearestKSearch(point, 1, nn_index, nn_distance);
206+
tree->nearestKSearch(point, 1, nn_index, nn_distance);
181207

182208
// Compute the error
183209
error += compute_error(nn_distance[0]);

0 commit comments

Comments
 (0)