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

Use random generator from C++11 instead of from Boost #2956

Merged
merged 6 commits into from
Apr 11, 2019

Conversation

SunBlack
Copy link
Contributor

@SunBlack SunBlack commented Mar 29, 2019

There are still three occurences of Boost random generator: #2954 (metslib) #2955 (OutofcoreOctreeDiskContainer), #2988 (RangeLikelihood)

Solves partially #2803

/////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
pcl::common::UniformGenerator<T>::UniformGenerator(T min, T max, pcl::uint32_t seed)
: distribution_ (min, max)
, generator_ (rng_, distribution_)
{
parameters_ = Parameters (min, max, seed);
if(parameters_.seed != -1)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just saw a compiler warning related to this:

common/include/pcl/common/impl/random.hpp:49:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(parameters_.seed != -1)

Even I didn't touched here anything: Should we fix this warning in this PR (and how?) or in a separate?

Copy link
Member

Choose a reason for hiding this comment

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

We can fix it here. Just static_cast<pcl::uint32_t>(-1).

Copy link
Contributor Author

@SunBlack SunBlack Apr 5, 2019

Choose a reason for hiding this comment

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

Just wondering about this:

struct Parameters
{
Parameters (T _mean = 0, T _sigma = 1, pcl::uint32_t _seed = 1)
: mean (_mean)
, sigma (_sigma)
, seed (_seed)
{}
T mean;
T sigma;
pcl::uint32_t seed;
};
/** Constructor
* \param[in] mean normal mean
* \param[in] sigma normal variation
* \param[in] seed seeding value
*/
NormalGenerator(T mean = 0, T sigma = 1, pcl::uint32_t seed = -1);
/** Constructor
* \param parameters normal distribution parameters and seed
*/
NormalGenerator(const Parameters& parameters);

One constructor of NormalGenerator has as default seed -1 during the other one (via Parameters) has 1 as default seed.

Copy link
Member

Choose a reason for hiding this comment

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

I think -1 should be the default everywhere. However, thinking more about this, wouldn't it be better if -1 meant "seed with random device" instead of "not seed at all".

Copy link
Member

Choose a reason for hiding this comment

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

I think -1 should be the default everywhere. However, thinking more about this, wouldn't it be better if -1 meant "seed with random device" instead of "not seed at all".

It's a valid point, but I'm not sure if it's a good idea to change the behavior already on this PR. Once the review is complete, I would be open to give it a try on a separate commit or PR just to see if the change is harmless or if it causes unit tests to start exploding like last time... Then we can decide based on the result.

Edit: I started the review before Heiko replied and wrote the comment then. Just leaving it for the extra info on my stance. I'm supportive of addressing it on a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved discussion to #2985. I think it is better, if this behavior changes are not part of this PR.

@SunBlack SunBlack force-pushed the random_generator branch 3 times, most recently from 77f65ba to 49d051a Compare April 1, 2019 15:59
@SunBlack
Copy link
Contributor Author

SunBlack commented Apr 1, 2019

Tests seems to fail, because std random number generator produce other random numbers as boost random number generator with same seed. But not sure if this is always the reason - I even can't reproduce all test failures (Ubuntu just has 3, during Mac has 5). So it would be nice, if another can check this too.

@SergioRAgostinho
Copy link
Member

Seems like you opened another can of worms :) This is a very big PR, so I'm in favor of splitting it up after we agree on good splitting points. After quickly skimming through the PR and getting acquainted with the new semantics behind <random> the use of std::random_device caught my eye.

One source [1] claims the method might actually throw on some implementations, source [2] only focuses on the aspect that it might still be pseudo random if there's no hardware support. I don't have experience with other platforms other than desktops so I'm not sure if the concern in [1] is applicable to us. I'm tempted to say that no. Opinions?

Regarding the PR split, I feel the per module approach is worthy once more. I'm more interested in revising the core modules first and then progressing towards apps, examples and tools.

[1] - http://www.cplusplus.com/reference/random/random_device/
[2] - https://en.cppreference.com/w/cpp/numeric/random/random_device

@SunBlack
Copy link
Contributor Author

SunBlack commented Apr 3, 2019

Seems like you opened another can of worms :)

Indeed, I wasn't expected this issues at the time I started :(

This is a very big PR, so I'm in favor of splitting it up after we agree on good splitting points.

I was already thinking about it. My idea was to split everthing out, which causes tests to fail.

One source [1] claims the method might actually throw on some implementations, source [2] only focuses on the aspect that it might still be pseudo random if there's no hardware support. I don't have experience with other platforms other than desktops so I'm not sure if the concern in [1] is applicable to us. I'm tempted to say that no. Opinions?

Well, I don't think [1] is an issue for us. I believe all modern platforms (mobile and desktop) can handle this. Maybe some specific platforms doesn't have it, but I'm nearly sure they are not part of the target group of the PCL.

Regarding the PR split, I feel the per module approach is worthy once more. I'm more interested in revising the core modules first and then progressing towards apps, examples and tools.

Adjusting app, example and tools is easy, because they have no tests which depends on the result of generated values by the random generator, so they are straight forward during core components could be difficult to adjust.

Nevertheless we should discuss about another thing: Is it really necessary to generate random data within core classes? Usually you have separate factory classes in case you need random content. And btw: It is really ugly to have test which expect specific values of random generator.

@taketwo
Copy link
Member

taketwo commented Apr 4, 2019

It is really ugly to have test which expect specific values of random generator.

Fully agree. We can drop such tests, but then we may be left without any tests at all :D

@SergioRAgostinho
Copy link
Member

Adjusting app, example and tools is easy, because they have no tests which depends on the result of generated values by the random generator, so they are straight forward during core components could be difficult to adjust.

Let's go with the easy cases then first 👍 and progress to the more challenging problems as we progress.

Is it really necessary to generate random data within core classes?

Likely not. But correcting this behaviour is not really mandatory at the moment.

It is really ugly to have test which expect specific values of random generator.

Fully agree. We can drop such tests, but then we may be left without any tests at all :D

😅

@SergioRAgostinho SergioRAgostinho added changelog: enhancement Meta-information for changelog generation changelog: ABI break Meta-information for changelog generation labels Apr 9, 2019
/////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
pcl::common::UniformGenerator<T>::UniformGenerator(T min, T max, pcl::uint32_t seed)
: distribution_ (min, max)
, generator_ (rng_, distribution_)
{
parameters_ = Parameters (min, max, seed);
if(parameters_.seed != -1)
Copy link
Member

Choose a reason for hiding this comment

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

I think -1 should be the default everywhere. However, thinking more about this, wouldn't it be better if -1 meant "seed with random device" instead of "not seed at all".

It's a valid point, but I'm not sure if it's a good idea to change the behavior already on this PR. Once the review is complete, I would be open to give it a try on a separate commit or PR just to see if the change is harmless or if it causes unit tests to start exploding like last time... Then we can decide based on the result.

Edit: I started the review before Heiko replied and wrote the comment then. Just leaving it for the extra info on my stance. I'm supportive of addressing it on a separate PR.

common/include/pcl/common/random.h Show resolved Hide resolved
simulation/src/range_likelihood.cpp Show resolved Hide resolved
test/registration/test_correspondence_rejectors.cpp Outdated Show resolved Hide resolved
indices.push_back (idx);

boost::variate_generator< boost::mt19937, boost::uniform_int<> > rand_indices(boost::mt19937 (), boost::uniform_int<> (0, static_cast<int> (indices.size ()) - 1));
std::uniform_int_distribution<> rand_indices(0, static_cast<int> (indices.size ()) - 1);
Copy link
Member

Choose a reason for hiding this comment

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

I don't see the point behind this static_cast<int>. Just remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

indices.size () is a size_t, so we have an unsigned type here. In case size() returns 0 we got with static_cast<int> (indices.size ()) - 1 -1 as value, during with without this cast we got std::numeric_limits<size_t>::max() => out of integer range => 0. Because this is test code it doesn't maybe matters. Just want to say we change behavior with it.

test/surface/test_convex_hull.cpp Outdated Show resolved Hide resolved
test/surface/test_convex_hull.cpp Outdated Show resolved Hide resolved
# pragma GCC system_header
#endif

#include <boost/random.hpp>
Copy link
Member

Choose a reason for hiding this comment

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

We might need to keep this file around for deprecation purposes, open for discussion. Since it is a header just emit pragma warning.

tracking/include/pcl/tracking/boost.h Outdated Show resolved Hide resolved
tracking/include/pcl/tracking/boost.h Outdated Show resolved Hide resolved
# pragma GCC system_header
#endif

#include <boost/random.hpp>
Copy link
Member

Choose a reason for hiding this comment

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

Hmmm... I'm starting to notice a shortcoming on my request. I wanted to keep pcl/tracking/boost.h around, so it exists as a file, but I didn't want to include any boost headers inside it. If anyone was using this header for some reason it was just because of boost/random.hpp, then removing this line will still break their code, which means we need to keep it around. :/

@taketwo taketwo added the c++14 label Apr 10, 2019
@SergioRAgostinho
Copy link
Member

The test are "legitimately" failing on windows. Shall we pull out/revert the the failing case and associated class to a new PR so that we can address the unit test correction there?

@SunBlack
Copy link
Contributor Author

I reverted changes to this test as I have no idea why they are failing. As we have already splitted other changes to other PRs from this PR because of failing tests, we could do this here too.

@SergioRAgostinho
Copy link
Member

As we have already splitted other changes to other PRs from this PR because of failing tests, we could do this here too.

Let's definitely do that. That way we can look at it from a finer scope.

@SergioRAgostinho SergioRAgostinho merged commit 8866006 into PointCloudLibrary:master Apr 11, 2019
@SunBlack SunBlack deleted the random_generator branch April 11, 2019 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: ABI break Meta-information for changelog generation changelog: enhancement Meta-information for changelog generation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants