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

Random seed Open3D globally #5247

Merged
merged 3 commits into from
Jun 25, 2022
Merged

Random seed Open3D globally #5247

merged 3 commits into from
Jun 25, 2022

Conversation

yxlao
Copy link
Collaborator

@yxlao yxlao commented Jun 24, 2022

Summary

This PR allows all of Open3D's random number generator and random samplers to be globally seeded. If you plan to add a new random number or random sampling feature to Open3D, you should always use one of these helper functions/classes, instead of seeding your own random numbers.

Changes

  • Global singleton and mutex for random number generation.
  • Several helper functions and helper classes to generate random numbers or use the random number engine.
  • Adapt all code to use the new random number mechanism, and removed all other code for seeding.

Usage

#include <algorithm>
#include <iostream>

#include "open3d/utility/Random.h"

int main() {
    using namespace open3d;

    // Globally seed.
    utility::random::Seed(0);

    // Simply get a random number.
    std::cout << utility::random::RandUint32() << std::endl;

    // Using the random engine.
    std::vector<int> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    {
        std::lock_guard<std::mutex> lock(*utility::random::GetMutex());
        std::shuffle(vals.begin(), vals.end(), *utility::random::GetEngine());
    }

    // Create a generator: integer in [0, 10).
    utility::random::UniformIntGenerator gen_int(0, 10);
    for (int i = 0; i < 5; i++) {
        std::cout << gen_int() << std::endl;
    }

    // Create a generator: double in [-1.0, 1.0).
    utility::random::UniformDoubleGenerator gen_double(-1.0, 1.0);
    for (int i = 0; i < 5; i++) {
        std::cout << gen_double() << std::endl;
    }

    return 0;
}
import open3d as o3d

o3d.utility.random.seed(xxx);

Known limitations

  • The random number is not platform-independent.
    • std::mt19937 is platform-independent.
    • std::uniform_int_distribution is not platform-independent.
    • uniform_real_distribution is not platform-independent.
  • No CUDA support yet.

Future PR

  1. Port or write our own uniform_int_distribution and uniform_real_distribution, such that everything becomes OS/compiler independent.
  2. Add CUDA support. We don't require CUDA to generate the same random number as the CPU.
  3. Add Tenosr rand operation to generate randomized Tensor.

This change is Reviewable

@update-docs
Copy link

update-docs bot commented Jun 24, 2022

Thanks for submitting this pull request! The maintainers of this repository would appreciate if you could update the CHANGELOG.md based on your changes.

@yxlao yxlao force-pushed the yixing/random-singleton branch from b912538 to bcb0386 Compare June 24, 2022 08:22
@yxlao yxlao changed the title Global singleton for seeding random numbers Random seed Open3D globally Jun 24, 2022
@yxlao yxlao force-pushed the yixing/random-singleton branch 5 times, most recently from df1d313 to 0cbd4b4 Compare June 24, 2022 10:21
@yxlao yxlao force-pushed the yixing/random-singleton branch from 0cbd4b4 to c0dd3dd Compare June 24, 2022 12:36
@yxlao yxlao force-pushed the yixing/random-singleton branch from f94dcd1 to 6187090 Compare June 24, 2022 16:59
@yxlao yxlao marked this pull request as ready for review June 24, 2022 17:51
@yxlao yxlao requested review from yuecideng and ssheorey June 24, 2022 17:51
/* = RANSACConvergenceCriteria()*/
utility::optional<unsigned int> seed /* = utility::nullopt*/) {
&checkers,
const RANSACConvergenceCriteria &criteria) {
Copy link
Member

@ssheorey ssheorey Jun 24, 2022

Choose a reason for hiding this comment

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

Keep comments indicating default values?

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, this looks very useful!

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.

3 participants