cgal icon indicating copy to clipboard operation
cgal copied to clipboard

Parallelize density weight precomputation in wlop_simplify_and_regularize_point_set when using Parallel_tag

Open SiddheshCodes4554 opened this issue 1 month ago • 3 comments

In the Point_set_processing_3 package, CGAL::wlop_simplify_and_regularize_point_set supports both CGAL::Sequential_tag and CGAL::Parallel_tag concurrency tags.

When require_uniform_sampling is set to true (via named parameters), the algorithm precomputes the initial density weights for all input points against the Kd-tree structure before running the iterative projection loop:

https://github.com/CGAL/cgal/blob/master/Point_set_processing_3/include/CGAL/wlop_simplify_and_regularize_point_set.h#L524-L539

Currently, this step is implemented as a serial loop with an existing TODO:

if (require_uniform_sampling)//default value is false
{
  original_density_weights.reserve(number_of_original);
  //todo: this part could also be parallelized if needed
  for (it = first_original_iter, i = 0; it != points.end() ; ++it, ++i)
  {
    FT density = simplify_and_regularize_internal::
                 compute_density_weight_for_original_point<Kernel, Kd_Tree>
                                       (
                                         get(point_map, *it),
                                         original_kd_tree,
                                         radius);

    original_density_weights.push_back(density);
  }
}

Since original_kd_tree is purely read-only during neighbor search queries, computing compute_density_weight_for_original_point is embarrassingly parallel. For dense point clouds, this serial step creates a noticeable performance bottleneck even when the caller explicitly passes CGAL::Parallel_tag.

Proposed Enhancement:
Resize original_density_weights to number_of_original upfront.
Parallelize the loop using CGAL::for_each<ConcurrencyTag> (matching the pattern used later in the file for sample updates) when CGAL::Parallel_tag is enabled, while preserving the sequential fallback for CGAL::Sequential_tag.
Validate output determinism against existing test suites in Point_set_processing_3/test/Point_set_processing_3/.
I would like to work on this issue and prepare a Pull Request.

Source Code
Algorithm location: 

Point_set_processing_3/include/CGAL/wlop_simplify_and_regularize_point_set.h

Example usage triggering the sequential bottleneck under parallel mode:

cpp
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/wlop_simplify_and_regularize_point_set.h>
#include <vector>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_3 Point_3;
int main()
{
  std::vector<Point_3> points;
  // Fill with a large point set (e.g., 500k+ points)
  std::vector<Point_3> output;
  // Running with Parallel_tag and require_uniform_sampling = true
  CGAL::wlop_simplify_and_regularize_point_set<CGAL::Parallel_tag>(
    points,
    std::back_inserter(output),
    CGAL::parameters::require_uniform_sampling(true)
                     .select_percentage(5)
  );
  return 0;
}
Environment
Operating system (Windows/Mac/Linux, 32/64 bits): All supported OS (Windows / Linux / macOS, 64-bit)
Compiler: GCC / Clang / MSVC (C++17 or C++20)
Release or debug mode: Release
Specific flags used (if any): -DCGAL_ACTIVATE_CONCURRENT_MESH_3 / -DCGAL_LINKED_WITH_TBB
CGAL version: main branch (6.x-dev)
Boost version: 1.74+
Other libraries versions if used (Eigen, TBB, etc.): Intel oneTBB

SiddheshCodes4554 avatar Oct 20 '26 15:10 SiddheshCodes4554

/assign

SiddheshCodes4554 avatar Oct 20 '26 15:10 SiddheshCodes4554

hey i am would like to work on this issue. I have recently submitted a PR to CGAL (#9815). Is this issue still available? i will be happy to take it up and submit a PR.

vedpanvar avatar Nov 04 '26 08:11 vedpanvar

I've opened a pull request for this: https://github.com/CGAL/cgal/pull/9848

It computes the density weights with CGAL::for_each, so Parallel_tag now parallelizes that step. It also builds the Kd-tree explicitly before the loop, because Kd_tree builds lazily on the first query and that build is not thread-safe. I extended wlop_simplify_and_regularize_test, which never ran with require_uniform_sampling(true), to cover both tags with that option and to check the output size and coordinates.

On a 20,000-point sphere (Release build, GCC 15.2, 8 threads), the run with Parallel_tag and require_uniform_sampling(true) went from about 0.9 s to about 0.3 s. That is one input on one machine, so please treat it as indicative only.

The pull request is ready for review.

Diyan4444 avatar Nov 11 '26 10:11 Diyan4444