-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<random>: Implement Lemire's fast integer generation
#3012
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
Merged
StephanTLavavej
merged 12 commits into
microsoft:main
from
MattStephanson:gh_178_uniform_int
Sep 22, 2022
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0156b8d
Lemire's fast integer generation
MattStephanson 3c8de09
review feedback
MattStephanson d4cd6f3
Merge branch 'main' into gh_176_uniform_int
MattStephanson 3970473
Merge branch 'main' into gh_178_uniform_int
MattStephanson 771edfd
add benchmark code to the repo
strega-nil 6c5657b
Add banner.
StephanTLavavej 7e02578
Include <cstdint>, qualify typedefs.
StephanTLavavej 2f00b93
Rename max to maximum.
StephanTLavavej ff90fe8
review feedback
MattStephanson 2191c90
Merge branch 'main' into gh_178_uniform_int
MattStephanson cc4dcea
Episode V: The Review Feedback Strikes Back
MattStephanson 7401f4e
Merge branch 'main' into gh_178_uniform_int
StephanTLavavej 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <benchmark/benchmark.h> | ||
| #include <cstdint> | ||
| #include <random> | ||
|
|
||
| /// Test URBGs alone | ||
|
|
||
| static void BM_mt19937(benchmark::State& state) { | ||
| std::mt19937 gen; | ||
| for (auto _ : state) { | ||
| benchmark::DoNotOptimize(gen()); | ||
| } | ||
| } | ||
| BENCHMARK(BM_mt19937); | ||
|
|
||
| static void BM_mt19937_64(benchmark::State& state) { | ||
| std::mt19937_64 gen; | ||
| for (auto _ : state) { | ||
| benchmark::DoNotOptimize(gen()); | ||
| } | ||
| } | ||
| BENCHMARK(BM_mt19937_64); | ||
|
|
||
| static void BM_lcg(benchmark::State& state) { | ||
| std::minstd_rand gen; | ||
| for (auto _ : state) { | ||
| benchmark::DoNotOptimize(gen()); | ||
| } | ||
| } | ||
| BENCHMARK(BM_lcg); | ||
|
|
||
| std::uint32_t GetMax() { | ||
| std::random_device gen; | ||
| std::uniform_int_distribution<std::uint32_t> dist(10'000'000, 20'000'000); | ||
| return dist(gen); | ||
| } | ||
|
|
||
| static const std::uint32_t maximum = GetMax(); // random divisor to prevent strength reduction | ||
|
|
||
| /// Test mt19937 | ||
|
|
||
| static void BM_raw_mt19937_old(benchmark::State& state) { | ||
| std::mt19937 gen; | ||
| std::_Rng_from_urng<std::uint32_t, decltype(gen)> rng(gen); | ||
| for (auto _ : state) { | ||
| benchmark::DoNotOptimize(rng(maximum)); | ||
| } | ||
| } | ||
| BENCHMARK(BM_raw_mt19937_old); | ||
|
|
||
| static void BM_raw_mt19937_new(benchmark::State& state) { | ||
| std::mt19937 gen; | ||
| std::_Rng_from_urng_v2<std::uint32_t, decltype(gen)> rng(gen); | ||
| for (auto _ : state) { | ||
| benchmark::DoNotOptimize(rng(maximum)); | ||
| } | ||
| } | ||
| BENCHMARK(BM_raw_mt19937_new); | ||
|
|
||
| /// Test mt19937_64 | ||
|
|
||
| static void BM_raw_mt19937_64_old(benchmark::State& state) { | ||
| std::mt19937_64 gen; | ||
| std::_Rng_from_urng<std::uint64_t, decltype(gen)> rng(gen); | ||
| for (auto _ : state) { | ||
| benchmark::DoNotOptimize(rng(maximum)); | ||
| } | ||
| } | ||
| BENCHMARK(BM_raw_mt19937_64_old); | ||
|
|
||
| static void BM_raw_mt19937_64_new(benchmark::State& state) { | ||
| std::mt19937_64 gen; | ||
| std::_Rng_from_urng_v2<std::uint64_t, decltype(gen)> rng(gen); | ||
| for (auto _ : state) { | ||
| benchmark::DoNotOptimize(rng(maximum)); | ||
| } | ||
| } | ||
| BENCHMARK(BM_raw_mt19937_64_new); | ||
|
|
||
| /// Test minstd_rand | ||
|
|
||
| static void BM_raw_lcg_old(benchmark::State& state) { | ||
| std::minstd_rand gen; | ||
| std::_Rng_from_urng<std::uint32_t, decltype(gen)> rng(gen); | ||
| for (auto _ : state) { | ||
| benchmark::DoNotOptimize(rng(maximum)); | ||
| } | ||
| } | ||
| BENCHMARK(BM_raw_lcg_old); | ||
|
|
||
| static void BM_raw_lcg_new(benchmark::State& state) { | ||
| std::minstd_rand gen; | ||
| std::_Rng_from_urng_v2<std::uint32_t, decltype(gen)> rng(gen); | ||
| for (auto _ : state) { | ||
| benchmark::DoNotOptimize(rng(maximum)); | ||
| } | ||
| } | ||
| BENCHMARK(BM_raw_lcg_new); | ||
|
|
||
| BENCHMARK_MAIN(); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_matrix.lst |
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.