Skip to content

Update sample methods#2137

Merged
dellaert merged 8 commits intodevelopfrom
update-rng
May 19, 2025
Merged

Update sample methods#2137
dellaert merged 8 commits intodevelopfrom
update-rng

Conversation

@varunagrawal
Copy link
Copy Markdown
Contributor

Using lessons learned in #2136, I've updated the sampling methods to use default parameters for the PRNG. This not only reduces code duplication, but is also fully backwards-compatible.

I use nullptr as the default parameter since that is idiomatic in C++.

@varunagrawal varunagrawal requested a review from dellaert May 16, 2025 14:04
@varunagrawal varunagrawal self-assigned this May 16, 2025
Copy link
Copy Markdown
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

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

An improvement, but maybe inconsistent now between various files? I think it should all be nullptr default or all something else, no? And we should clarify if the default will then be a file-specific local rng or a global rng.

* @return HybridValues
*/
HybridValues sample(const HybridValues &given, std::mt19937_64 *rng) const;
HybridValues sample(const HybridValues &given,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What happens when nullptr? Seems different from yesterday's PR where we pass in a kSomething

Copy link
Copy Markdown
Contributor Author

@varunagrawal varunagrawal May 17, 2025

Choose a reason for hiding this comment

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

Passing in a static variable address as a default argument is

  1. Not idiomatic C++ which recommends using a nullptr as the default for a raw pointer.
  2. Did not play well with the python wrapper due to memory allocation.

Whenever a sample method gets a nullptr for the rng, it keeps passing it along until we actually use the rng (e.g. in Sample::sample). At that point, I have a simple check:

rng = (rng == nullptr) ? &kRandomNumberGenerator : rng;

This makes the code really clean since we don't have to worry about the rng pointer until we actually need it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok. And yesterday’s changes are now also defaulting to null ptr?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I needed to do that to get the CI passing.

Key key = firstFrontalKey();

// Check if rng is nullptr, then assign default
rng = (rng == nullptr) ? &kRandomNumberGenerator : rng;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this a local or a global default rng?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's not a global default. There is one definition in GaussianConditional.cpp and one in DiscreteConditional.h. I think we should make it a global default so code like ShonanAveraging can also leverage it (they all use the same seed of 42).

@dellaert
Copy link
Copy Markdown
Member

Rather than renaming, (i don’t like the new name so much) you can put each one in a cpp file in an anonymous namespace

@dellaert dellaert merged commit 01409fc into develop May 19, 2025
36 checks passed
@dellaert dellaert deleted the update-rng branch May 19, 2025 02:50
@varunagrawal
Copy link
Copy Markdown
Contributor Author

I was hoping to follow your suggestion, perhaps in another PR.

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.

2 participants