Skip to content

Conversation

@nicolaloi
Copy link
Contributor

Type

  • Bug fix (non-breaking change which fixes an issue): Fixes Segmentation Fault for extract_voxel_grid #6712
  • New feature (non-breaking change which adds functionality). Resolves #
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) Resolves #

Motivation and Context

When calling UniformTSDFVolume::ExtractVoxelGrid, a Segmentation Fault occurs, or the program hangs indefinitely.

Checklist:

  • I have run python util/check_style.py --apply to apply Open3D code style
    to my code.
  • This PR changes Open3D behavior or adds new functionality.
    • Both C++ (Doxygen) and Python (Sphinx / Google style) documentation is
      updated accordingly.
    • I have added or updated C++ and / or Python unit tests OR included test
      results
      (e.g. screenshots or numbers) here.
  • I will follow up and update the code if CI fails.
  • For fork PRs, I have selected Allow edits from maintainers.

Description

The issue is caused by accessing a std::unordered_map (voxel_grid->voxels_) which is not thread-safe, within an OpenMP for loop:

voxel_grid->voxels_[index] = geometry::Voxel(index, color);

Fix of this PR: create per thread std::vector<geometry::Voxel> in parallel with OpenMP, and then insert the data into the std::unordered_map outside of the parallel section.

@ssheorey ssheorey requested a review from Copilot August 4, 2025 18:32
Copy link

Copilot AI left a 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() and GetThreadNum() 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

Copy link
Member

@ssheorey ssheorey left a 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) {
Copy link
Member

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.

Copy link
Contributor Author

@nicolaloi nicolaloi Aug 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ssheorey voxel_grid->voxels_ is not a vector, but an unordered_map, and thread_vector is a vector of voxels (values without a key), so I cannot directly use insert. In my new commit 1753862, I have modified thread_vector to also contain the keys to be able to use insert, if that's okay with you.

@nicolaloi nicolaloi force-pushed the nicolaloi/fix-extract-voxel-grid branch from 4927dc8 to 1753862 Compare August 24, 2025 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segmentation Fault for extract_voxel_grid

2 participants