Skip to content

Commit 45f77c7

Browse files
authored
Add OpenMP threading to search (#6284)
* Add OpenMP threading to search * switch to ptrdiff_t --------- Co-authored-by: jmackay2 <jmackay2>
1 parent 9933344 commit 45f77c7

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

benchmarks/search/radius_search.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ static void
4646
BM_KdTreeAll(benchmark::State& state,
4747
const pcl::PointCloud<pcl::PointXYZ>::Ptr cloudIn,
4848
const double searchRadius,
49-
const size_t neighborLimit)
49+
const size_t neighborLimit,
50+
const bool threaded)
5051
{
5152
pcl::search::KdTree<pcl::PointXYZ> kdtree(false);
5253
kdtree.setInputCloud(cloudIn);
54+
if (threaded) {
55+
kdtree.setNumberOfThreads(0);
56+
}
5357

5458
// Leaving indices empty to have it search through all points
5559
pcl::Indices indices;
@@ -123,7 +127,17 @@ main(int argc, char** argv)
123127
->Unit(benchmark::kMicrosecond);
124128

125129
benchmark::RegisterBenchmark(
126-
"KdTreeAll", &BM_KdTreeAll, cloudFiltered, searchRadius, neighborLimit)
130+
"KdTreeAll", &BM_KdTreeAll, cloudFiltered, searchRadius, neighborLimit, false)
131+
->Unit(benchmark::kMicrosecond)
132+
->UseManualTime()
133+
->Iterations(1);
134+
135+
benchmark::RegisterBenchmark("KdTreeAllThreaded",
136+
&BM_KdTreeAll,
137+
cloudFiltered,
138+
searchRadius,
139+
neighborLimit,
140+
true)
127141
->Unit(benchmark::kMicrosecond)
128142
->UseManualTime()
129143
->Iterations(1);

search/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set(SUBSYS_DESC "Point cloud generic search library")
33
set(SUBSYS_DEPS common kdtree octree)
44

55
PCL_SUBSYS_OPTION(build "${SUBSYS_NAME}" "${SUBSYS_DESC}" ON)
6-
PCL_SUBSYS_DEPEND(build NAME ${SUBSYS_NAME} DEPS ${SUBSYS_DEPS} EXT_DEPS flann)
6+
PCL_SUBSYS_DEPEND(build NAME ${SUBSYS_NAME} DEPS ${SUBSYS_DEPS} OPT_DEPS OpenMP EXT_DEPS flann)
77

88
PCL_ADD_DOC("${SUBSYS_NAME}")
99

search/include/pcl/search/impl/search.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,16 @@ pcl::search::Search<PointT>::nearestKSearch (
120120
{
121121
k_indices.resize (cloud.size ());
122122
k_sqr_distances.resize (cloud.size ());
123-
for (std::size_t i = 0; i < cloud.size (); i++)
123+
#pragma omp parallel for num_threads(num_threads_) default(none) shared(cloud, k, k_indices, k_sqr_distances)
124+
for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(cloud.size ()); i++)
124125
nearestKSearch (cloud, static_cast<index_t> (i), k, k_indices[i], k_sqr_distances[i]);
125126
}
126127
else
127128
{
128129
k_indices.resize (indices.size ());
129130
k_sqr_distances.resize (indices.size ());
130-
for (std::size_t i = 0; i < indices.size (); i++)
131+
#pragma omp parallel for num_threads(num_threads_) default(none) shared(cloud, indices, k, k_indices, k_sqr_distances)
132+
for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(indices.size ()); i++)
131133
nearestKSearch (cloud, indices[i], k, k_indices[i], k_sqr_distances[i]);
132134
}
133135
}
@@ -172,14 +174,16 @@ pcl::search::Search<PointT>::radiusSearch (
172174
{
173175
k_indices.resize (cloud.size ());
174176
k_sqr_distances.resize (cloud.size ());
175-
for (std::size_t i = 0; i < cloud.size (); i++)
177+
#pragma omp parallel for num_threads(num_threads_) default(none) shared(cloud, radius, k_indices, k_sqr_distances, max_nn)
178+
for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(cloud.size ()); i++)
176179
radiusSearch (cloud, static_cast<index_t> (i), radius,k_indices[i], k_sqr_distances[i], max_nn);
177180
}
178181
else
179182
{
180183
k_indices.resize (indices.size ());
181184
k_sqr_distances.resize (indices.size ());
182-
for (std::size_t i = 0; i < indices.size (); i++)
185+
#pragma omp parallel for num_threads(num_threads_) default(none) shared(cloud, indices, radius, k_indices, k_sqr_distances, max_nn)
186+
for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(indices.size ()); i++)
183187
radiusSearch (cloud,indices[i],radius,k_indices[i],k_sqr_distances[i], max_nn);
184188
}
185189
}

search/include/pcl/search/search.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ namespace pcl
384384
pc.resize (cloud.size ());
385385
for (std::size_t i = 0; i < cloud.size (); ++i)
386386
pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (cloud[i], pc[i]));
387-
radiusSearch (pc, Indices (), radius, k_indices, k_sqr_distances, max_nn);
387+
radiusSearch (pc, Indices(), radius, k_indices, k_sqr_distances, max_nn);
388388
}
389389
else
390390
{
@@ -395,6 +395,19 @@ namespace pcl
395395
}
396396
}
397397

398+
/** \brief Set the number of threads to use for searching over multiple points or indices
399+
* \param[in] nr_threads the number of threads to use (0 automatically sets the threads based on the hardware)
400+
*/
401+
void setNumberOfThreads(unsigned int nr_threads) {
402+
#ifdef _OPENMP
403+
num_threads_ = nr_threads != 0 ? nr_threads : omp_get_num_procs();
404+
#else
405+
if (nr_threads != 1) {
406+
PCL_WARN("OpenMP is not available. Keeping number of threads unchanged at 1\n");
407+
}
408+
#endif
409+
}
410+
398411
protected:
399412
void
400413
sortResults (Indices& indices, std::vector<float>& distances) const;
@@ -403,6 +416,9 @@ namespace pcl
403416
IndicesConstPtr indices_;
404417
bool sorted_results_;
405418
std::string name_;
419+
420+
/** \brief The number of threads to use when searching over multiple points or indices */
421+
unsigned int num_threads_{1};
406422

407423
private:
408424
struct Compare

0 commit comments

Comments
 (0)