-
Notifications
You must be signed in to change notification settings - Fork 33
[r][cpp] add Bernoulli Sampling #339
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
Open
erliuu
wants to merge
2
commits into
main
Choose a base branch
from
el/bernoulli-sampling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright 2026 BPCells contributors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
| // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
| // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
| // option. This file may not be copied, modified, or distributed | ||
| // except according to those terms. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
|
|
||
| #include "MatrixIterator.h" | ||
|
|
||
| namespace BPCells { | ||
|
|
||
| // Keep each non-zero entry independently with probability `prob`, with the | ||
| // keep/drop decision a deterministic function of (seed, col, row) via splitmix64 and invariant to storage order. | ||
| template <typename T, bool Transpose = false> class BernoulliSample : public MatrixLoaderWrapper<T> { | ||
| private: | ||
| uint64_t seed; | ||
| uint32_t threshold; | ||
| size_t loaded = 0; | ||
|
|
||
| // splitmix64 via Daniel Lemire's testingRNG: | ||
| // https://github.com/lemire/testingRNG/blob/master/source/splitmix64.h | ||
| static constexpr uint64_t GOLDEN_GAMMA = UINT64_C(0x9E3779B97F4A7C15); | ||
|
|
||
| static inline uint64_t splitmix64_r(uint64_t *seed) { | ||
| uint64_t z = (*seed += GOLDEN_GAMMA); | ||
| z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9); | ||
| z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB); | ||
| return z ^ (z >> 31); | ||
| } | ||
|
|
||
| // returns the value of splitmix64 "offset" steps from seed | ||
| static inline uint64_t splitmix64_stateless(uint64_t seed, uint64_t offset) { | ||
| seed += offset * GOLDEN_GAMMA; | ||
| return splitmix64_r(&seed); | ||
| } | ||
|
|
||
| public: | ||
| BernoulliSample(std::unique_ptr<MatrixLoader<T>> &&loader, double prob, uint64_t seed) | ||
| : MatrixLoaderWrapper<T>(std::move(loader)), seed(seed) { | ||
| if (!(prob > 0.0 && prob <= 1.0)) | ||
| throw std::runtime_error("BernoulliSample: prob must be in (0, 1]"); | ||
| double scaled = prob * 4294967296.0; // 2^32 | ||
| threshold = (prob >= 1.0 || scaled >= 4294967295.5) | ||
| ? UINT32_MAX | ||
| : static_cast<uint32_t>(scaled); | ||
| } | ||
|
|
||
| bool load() override { | ||
| loaded = 0; | ||
| const uint64_t nrow = this->loader->rows(); | ||
| const uint64_t ncol = this->loader->cols(); | ||
|
|
||
| while (loaded == 0) { | ||
| if (!this->loader->load()) return false; | ||
| uint32_t *row_data = this->loader->rowData(); | ||
| T *val_data = this->loader->valData(); | ||
| size_t cap = this->loader->capacity(); | ||
| uint32_t col = this->loader->currentCol(); | ||
|
|
||
| for (size_t i = 0; i < cap; i++) { | ||
| uint32_t row = row_data[i]; | ||
| uint64_t offset = !Transpose ? (col * nrow + row) : (row * ncol + col); | ||
| uint32_t h = static_cast<uint32_t>(splitmix64_stateless(seed, offset)); | ||
| row_data[loaded] = row; | ||
| val_data[loaded] = val_data[i]; | ||
| loaded += h < threshold; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| uint32_t capacity() const override { return loaded; } | ||
| }; | ||
|
|
||
| } // end namespace BPCells |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.