-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Fix thread safety of UniformTSDFVolume::ExtractVoxelGrid #7315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix thread safety of UniformTSDFVolume::ExtractVoxelGrid #7315
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a thread safety issue in UniformTSDFVolume::ExtractVoxelGrid that was causing segmentation faults or infinite hangs when multiple threads attempted to access the same std::unordered_map concurrently.
- Introduces new utility functions
GetNumThreads()andGetThreadNum()to support thread-local storage - Restructures the parallel loop to use per-thread vectors instead of direct map access
- Consolidates voxel data into the final map outside the parallel region
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| cpp/open3d/utility/Parallel.h | Adds declarations for thread utility functions |
| cpp/open3d/utility/Parallel.cpp | Implements thread utility functions with OpenMP support |
| cpp/open3d/pipelines/integration/UniformTSDFVolume.cpp | Refactors voxel extraction to use thread-safe approach |
| CHANGELOG.md | Documents the bug fix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @nicolaloi for fixing this bug. Some comments below.
| } | ||
|
|
||
| for (const auto &thread_vector : per_thread_voxels) { | ||
| for (const auto &voxel : thread_vector) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use insert(start, end) to insert the entire vector at once.
Use reserve to prevent repeated re-allocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4927dc8 to
1753862
Compare
Type
Motivation and Context
When calling
UniformTSDFVolume::ExtractVoxelGrid, a Segmentation Fault occurs, or the program hangs indefinitely.Checklist:
python util/check_style.py --applyto apply Open3D code styleto my code.
updated accordingly.
results (e.g. screenshots or numbers) here.
Description
The issue is caused by accessing a
std::unordered_map(voxel_grid->voxels_) which is not thread-safe, within an OpenMP for loop:Open3D/cpp/open3d/pipelines/integration/UniformTSDFVolume.cpp
Line 277 in 1c48fcd
Fix of this PR: create per thread
std::vector<geometry::Voxel>in parallel with OpenMP, and then insert the data into thestd::unordered_mapoutside of the parallel section.