Skip to content
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

Fix vector initialization in NormalEstimationOMP #3614

Conversation

flowvision
Copy link
Contributor

I created a custom pcl::Search class which I then used to do a normal estimation using NormalEstimationOMP. In the implementation of nearestKSearch in my custom class I assumed that k_indices and k_sqr_distances are initialized to the correct size because it says so in the documentation. When I then did the normal estimation the application would crash because I tried to index into an empty vector.

The problem was that the vectors nn_indices and nn_dists in NormalEstimationOMP::computeFeatures must be initialized to the correct size. They are initialized at the beginning of the function but the OpenMP parallel for loop basically generates a new pair of vectors for each thread. These vectors are initialized with the default constructor which means that they have zero size.
To fix this I used 'firstprivate' for the vectors instead of 'private' this causes that the copy constructor to be called instead of the default constructor.

The vectors nn_indices and nn_dists in
NormalEstimationOMP::computeFeatures must be initialized to the correct
size. They are initialized at the beginning of the function but the
OpenMP parallel for loop basically generates a new pair of vectors for
each thread. These vectors are initialized with the default constructor
which means that they have zero size.
To fix this I used 'firstprivate' for the vectors instead of 'private'
this causes that the copy constructor is called instead of the default
constructor.
@kunaltyagi
Copy link
Member

kunaltyagi commented Jan 31, 2020

👍 for firstprivate

As before, it'd be great if you can create a test which fails on OpenMP. I don't know if these issues are tested on the CI, but a test will be a good reason to look into enabling OpenMP on CI in some form

@flowvision
Copy link
Contributor Author

I added the test.
If OpenMP is enabled the test will fail without the fix.
If OpenMP is not enabled the code falls back to a regular for loop so that it works the same way as in NormalEstimation. Because of that the test would also succeed even without the fix.

Copy link
Member

@kunaltyagi kunaltyagi left a comment

Choose a reason for hiding this comment

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

Just a few minor aesthetic changes. LGTM otherwise

test/features/test_normal_estimation.cpp Outdated Show resolved Hide resolved
test/features/test_normal_estimation.cpp Show resolved Hide resolved
@flowvision
Copy link
Contributor Author

I also fixed the build for Ubuntu 16.04 where there was an error because of some unused variables.

Why is the unused parameters warning only treated as an error in Ubuntu 16.04 but not in other builds?

@kunaltyagi
Copy link
Member

unused parameters warning only treated as an error in Ubuntu 16.04 but not in other builds?

16.04 has -Werror enabled because it has an older compiler which doesn't detect all warnings. We are working on removing warnings in PCL to enable -Werror on Mac and Ubuntu 19.04 builds.

Though I fear windows will be tough to enable the similar flag since the CI spews so much warnings. We might be missing some crucial flag there, due to MSVC's idiosyncrasies.

@kunaltyagi
Copy link
Member

All tests pass. Please reorganize the commits (squash, rebase as needed) and it's merge time 🚀

@flowvision flowvision force-pushed the vector_initialization_NormalEstimationOMP branch from 2107629 to 5e0d70c Compare February 3, 2020 10:59
@kunaltyagi kunaltyagi merged commit 16bd23e into PointCloudLibrary:master Feb 5, 2020
@flowvision flowvision deleted the vector_initialization_NormalEstimationOMP branch February 5, 2020 08:00
@taketwo taketwo added the changelog: fix Meta-information for changelog generation label Mar 18, 2020
@taketwo taketwo changed the title Fix vector initialization in NormalEstimationOMP Fix vector initialization in NormalEstimationOMP Mar 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: fix Meta-information for changelog generation module: features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants