|
46 | 46 |
|
47 | 47 | namespace pcl { |
48 | 48 |
|
| 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 | + |
49 | 71 | template <typename PointSource, typename PointTarget, typename FeatureT> |
50 | 72 | void |
51 | 73 | SampleConsensusInitialAlignment<PointSource, PointTarget, FeatureT>::setSourceFeatures( |
@@ -172,12 +194,16 @@ SampleConsensusInitialAlignment<PointSource, PointTarget, FeatureT>::computeErro |
172 | 194 | std::vector<float> nn_distance(1); |
173 | 195 |
|
174 | 196 | const ErrorFunctor& compute_error = *error_functor_; |
| 197 | + const auto& tree = tree_; |
175 | 198 | float error = 0; |
176 | 199 |
|
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)]; |
178 | 204 | // Find the distance between point and its nearest neighbor in the target point |
179 | 205 | // cloud |
180 | | - tree_->nearestKSearch(point, 1, nn_index, nn_distance); |
| 206 | + tree->nearestKSearch(point, 1, nn_index, nn_distance); |
181 | 207 |
|
182 | 208 | // Compute the error |
183 | 209 | error += compute_error(nn_distance[0]); |
|
0 commit comments