Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
- Exposed `get_plotly_fig` and modified `draw_plotly` to return the `Figure` it creates. (PR #7258)
- Fix build with librealsense v2.44.0 and upcoming VS 2022 17.13 (PR #7074)
- Fix `deprecated-declarations` warnings when compiling code with C++20 standard (PR #7303)
- Fix thread safety of UniformTSDFVolume::ExtractVoxelGrid (PR #7315)

## 0.13

Expand Down
47 changes: 32 additions & 15 deletions cpp/open3d/pipelines/integration/UniformTSDFVolume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,28 +257,45 @@ std::shared_ptr<geometry::VoxelGrid> UniformTSDFVolume::ExtractVoxelGrid()
voxel_grid->voxel_size_ = voxel_length_;
voxel_grid->origin_ = origin_;

// Create a vector to hold voxels for each thread in the parallel region,
// since access to voxel_grid->voxels_ (std::unordered_map) is not
// thread-safe.
std::vector<std::vector<geometry::Voxel>> per_thread_voxels;

#pragma omp parallel num_threads(utility::EstimateMaxThreads())
{
#pragma omp single
{ per_thread_voxels.resize(utility::GetNumThreads()); }
int thread_id = utility::GetThreadNum();

#ifdef _WIN32
#pragma omp parallel for schedule(static) \
num_threads(utility::EstimateMaxThreads())
#pragma omp for schedule(static)
#else
#pragma omp parallel for collapse(2) schedule(static) \
num_threads(utility::EstimateMaxThreads())
#pragma omp for collapse(2) schedule(static)
#endif
for (int x = 0; x < resolution_; x++) {
for (int y = 0; y < resolution_; y++) {
for (int z = 0; z < resolution_; z++) {
const int ind = IndexOf(x, y, z);
const float w = voxels_[ind].weight_;
const float f = voxels_[ind].tsdf_;
if (w != 0.0f && f < 0.98f && f >= -0.98f) {
double c = (f + 1.0) * 0.5;
Eigen::Vector3d color = Eigen::Vector3d(c, c, c);
Eigen::Vector3i index = Eigen::Vector3i(x, y, z);
voxel_grid->voxels_[index] = geometry::Voxel(index, color);
for (int x = 0; x < resolution_; x++) {
for (int y = 0; y < resolution_; y++) {
for (int z = 0; z < resolution_; z++) {
const int ind = IndexOf(x, y, z);
const float w = voxels_[ind].weight_;
const float f = voxels_[ind].tsdf_;
if (w != 0.0f && f < 0.98f && f >= -0.98f) {
double c = (f + 1.0) * 0.5;
Eigen::Vector3d color(c, c, c);
Eigen::Vector3i index(x, y, z);
per_thread_voxels[thread_id].emplace_back(index, color);
}
}
}
}
}

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.

voxel_grid->voxels_[voxel.grid_index_] = voxel;
}
}

return voxel_grid;
}

Expand Down
16 changes: 16 additions & 0 deletions cpp/open3d/utility/Parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ int EstimateMaxThreads() {
#endif
}

int GetNumThreads() {
#ifdef _OPENMP
return omp_get_num_threads();
#else
return 1; // No parallelism available.
#endif
}

int GetThreadNum() {
#ifdef _OPENMP
return omp_get_thread_num();
#else
return 0; // No parallelism available, so thread ID is always 0.
#endif
}

bool InParallel() {
// TODO: when we add TBB/Parallel STL support to ParallelFor, update this.
#ifdef _OPENMP
Expand Down
6 changes: 6 additions & 0 deletions cpp/open3d/utility/Parallel.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ namespace utility {
/// Estimate the maximum number of threads to be used in a parallel region.
int EstimateMaxThreads();

/// Returns the number of threads in the current parallel region.
int GetNumThreads();

/// Returns the thread ID in the current parallel region.
int GetThreadNum();

/// Returns true if in an parallel section.
bool InParallel();

Expand Down
Loading