-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add header file for parallel variate generation * Make clone(0) work also for PCG * Move seeding from R into dqrng::generator<RNG>() * Rework Parallel RNG vignette * use dqrng::generator to create the (main) RNG and rng->clone() for the local versions * remove the first two examples that make use of the global RNG * add a section on global RNG usage which discusses dqrng_extra/parallel_generate.h
- Loading branch information
Showing
11 changed files
with
276 additions
and
185 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,16 +1,17 @@ | ||
Package: dqrng | ||
Type: Package | ||
Title: Fast Pseudo Random Number Generators | ||
Version: 0.3.2.5 | ||
Version: 0.3.2.6 | ||
Authors@R: c( | ||
person("Ralf", "Stubner", email = "[email protected]", role = c("aut", "cre")), | ||
person("Ralf", "Stubner", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0009-0009-1908-106X")), | ||
person("daqana GmbH", role = "cph"), | ||
person("David Blackman", role = "cph", comment = "Xoroshiro / Xoshiro family"), | ||
person("Melissa O'Neill", email = "[email protected]", role = "cph", comment = "PCG family"), | ||
person("Sebastiano Vigna", email = "[email protected]", role = "cph", comment = "Xoroshiro / Xoshiro family"), | ||
person("Aaron", "Lun", role="ctb"), | ||
person("Kyle", "Butts", role = "ctb", email = "[email protected]"), | ||
person("Henrik", "Sloot", role = "ctb") | ||
person("Henrik", "Sloot", role = "ctb"), | ||
person("Philippe", "Grosjean", role = c("ctb"), comment = c(ORCID = "0000-0002-2694-9471")) | ||
) | ||
Description: Several fast random number generators are provided as C++ | ||
header only libraries: The PCG family by O'Neill (2014 | ||
|
This file contains 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 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 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 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,81 @@ | ||
// Copyright 2024 Ralf Stubner | ||
// Copyright 2024 Philippe Grosjean | ||
// | ||
// This file is part of dqrng. | ||
// | ||
// dqrng is free software: you can redistribute it and/or modify it | ||
// under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// dqrng is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with dqrng. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#include <sstream> | ||
#include <dqrng.h> | ||
#include <RcppParallel/RVector.h> | ||
#include <omp.h> | ||
|
||
namespace dqrng { | ||
namespace extra { | ||
template<typename Dist, typename... Params> | ||
Rcpp::NumericVector parallel_generate(std::size_t n, | ||
std::size_t threads, | ||
std::size_t streams, | ||
Params&&... params) { | ||
if (n < streams) | ||
streams = n; | ||
std::size_t stream_size = n / streams; | ||
std::size_t remainder = n % streams; | ||
|
||
// use RcppParallel::RVector as thread safe accessor | ||
Rcpp::NumericVector res(Rcpp::no_init(n)); | ||
RcppParallel::RVector<double> work(res); | ||
|
||
// use global RNG from dqrng | ||
dqrng::random_64bit_accessor rng{}; | ||
std::stringstream buffer; | ||
|
||
#ifdef _OPENMP | ||
std::size_t maxthreads = omp_get_num_procs(); | ||
if (threads > maxthreads) | ||
threads = maxthreads; | ||
// No need for more threads than there are streams | ||
if (threads > streams) | ||
threads = streams; | ||
#endif | ||
|
||
#pragma omp parallel num_threads(threads) | ||
{ | ||
std::size_t start,end; | ||
|
||
#pragma omp for schedule(static,1) | ||
for (std::size_t i = 0; i < streams; ++i) { | ||
if (i < remainder) { | ||
start = i * stream_size + i; | ||
end = start + stream_size + 1; | ||
} else { | ||
start = i * stream_size + remainder; | ||
end = start + stream_size; | ||
} | ||
// private RNG in each stream; RNG with i == 0 is identical to global RNG | ||
auto prng = rng.clone(i); | ||
prng->generate<Dist>(std::begin(work) + start, std::begin(work) + end, | ||
std::forward<Params>(params)...); | ||
if (i == 0) {// Save the state of the global RNG's clone | ||
buffer << *prng; | ||
} | ||
} | ||
} | ||
// Make sure that the global RNG advances as well by applying the state | ||
// of the global RNG's clone to the global RNG | ||
buffer >> rng; | ||
return res; | ||
} | ||
} // namespace extra | ||
} // namespace dqrng |
This file contains 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.
This file contains 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 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 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.