Skip to content

Commit

Permalink
Decouple the sitmo package (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
rstub authored Sep 4, 2023
1 parent 5bb89d4 commit ce75f10
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 24 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ inst/doc
*~
vignettes/*.R
vignettes/*.html
^docs$
revdep
include
external/tmp
/revdep
/include
/external/tmp
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: dqrng
Type: Package
Title: Fast Pseudo Random Number Generators
Version: 0.3.1
Version: 0.3.1.1
Authors@R: c(
person("Ralf", "Stubner", email = "[email protected]", role = c("aut", "cre")),
person("daqana GmbH", role = "cph"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# dqrng (unreleased)

* Decoupled the 'sitmo' package. It is now possible to use, e.g., the distribution functions from the header-only library without having an explicit `LinkingTo: sitmo`

# dqrng 0.3.1

* new method `dqrrademacher` for drawing Rademacher weights (Kyle Butts in [#50](https://github.com/daqana/dqrng/pull/50) fixing [#49](https://github.com/daqana/dqrng/pull/49))
Expand Down
8 changes: 0 additions & 8 deletions inst/include/dqrng_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <stdexcept>
#include <xoshiro.h>
#include <pcg_random.hpp>
#include <threefry.h>

namespace dqrng {
using default_64bit_generator = ::dqrng::xoroshiro128plus;
Expand Down Expand Up @@ -172,13 +171,6 @@ inline void random_64bit_wrapper<pcg64>::seed(result_type seed, result_type stre
cache = false;
}

template<>
inline void random_64bit_wrapper<sitmo::threefry_20_64>::seed(result_type seed, result_type stream) {
gen.seed(seed);
gen.set_counter(0, 0, 0, stream);
cache = false;
}


template<typename RNG = default_64bit_generator>
typename std::enable_if<!std::is_base_of<random_64bit_generator, RNG>::value, rng64_t>::type
Expand Down
2 changes: 1 addition & 1 deletion inst/include/dqrng_sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ inline Rcpp::Vector<RTYPE> no_replacement_set(dqrng::rng64_t &rng, INT m, INT n,
}

template<int RTYPE, typename INT>
inline Rcpp::Vector<RTYPE> sample(dqrng::rng64_t &rng, INT m, INT n, bool replace, int offset) {
inline Rcpp::Vector<RTYPE> sample(dqrng::rng64_t &rng, INT m, INT n, bool replace, int offset = 0) {
if (replace || n <= 1) {
return dqrng::sample::replacement<RTYPE, INT>(rng, m, n, offset);
} else {
Expand Down
35 changes: 35 additions & 0 deletions inst/include/dqrng_threefry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2018-2019 Ralf Stubner (daqana GmbH)
// Copyright 2023 Ralf Stubner
//
// 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/>.

#ifndef DQRNG_THREEFRY_H
#define DQRNG_THREEFRY_H 1

#include <threefry.h>
#include <dqrng_generator.h>

namespace dqrng {

template<>
inline void random_64bit_wrapper<sitmo::threefry_20_64>::seed(result_type seed, result_type stream) {
gen.seed(seed);
gen.set_counter(0, 0, 0, stream);
cache = false;
}
} // namespace dqrng

#endif // DQRNG_THREEFRY_H
2 changes: 1 addition & 1 deletion src/dqrng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <dqrng_sample.h>
#include <xoshiro.h>
#include <pcg_random.hpp>
#include <threefry.h>
#include <dqrng_threefry.h>
#include <convert_seed.h>
#include <R_randgen.h>

Expand Down
19 changes: 12 additions & 7 deletions vignettes/dqrng.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,14 @@ sampleCpp(1000)
### Using the header only library

The RNG wrapper and distributions functions can be used from C++ by including `dqrng_generator.h` and `dqrng_distribution.h`.
In order to use these header files, you have to use at least C++11 and link to the `BH` and `sitmo` packages as well.
In order to use these header files, you have to use at least C++11.
You have to link to the `BH` package as well to use dqrng's distribution functions.
For example, you can use the distribution functions from dqrng together with some foreign 64bit RNG.
Here a minimal [SplitMix](https://xoroshiro.di.unimi.it/splitmix64.c) generator is used together with `dqrng::normal_distribution`:

```{r, eval=FALSE, engine='Rcpp'}
#include <Rcpp.h>
// [[Rcpp::depends(dqrng, BH, sitmo)]]
// [[Rcpp::depends(dqrng, BH)]]
#include <dqrng_distribution.h>
// [[Rcpp::plugins(cpp11)]]
#include <cstdint>
Expand Down Expand Up @@ -232,20 +233,23 @@ Rcpp::NumericVector threefry_rnorm(const int n, const double mean = 0.0, const d
std::generate(result.begin(), result.end(), [&dist, &rng](){return dist(*rng);});
return result;
}
/*** R
threefry_rnorm(10)
system.time(threefry_rnorm(1e7))
*/
```

Note that for the (recommended) Threefry engine with 20 rounds some additional integration is provided in the `dqrng_threefry.h` header file.

Alternatively, you could combine the included RNGs together with dqrng's tooling and some other distribution function.
For example, this function generates random numbers according to the normal distribution using the standard library from C++11:

```{r, eval=FALSE, engine='Rcpp'}
#include <random>
#include <Rcpp.h>
// [[Rcpp::depends(dqrng, BH, sitmo)]]
#include <dqrng_distribution.h>
// [[Rcpp::depends(dqrng)]]
#include <dqrng_generator.h>
#include <xoshiro.h>
// [[Rcpp::plugins(cpp11)]]
Expand All @@ -257,6 +261,7 @@ Rcpp::NumericVector std_rnorm(const int n, const double mean = 0.0, const double
std::generate(result.begin(), result.end(), [&dist, &rng](){return dist(*rng);});
return result;
}
/*** R
std_rnorm(10)
system.time(std_rnorm(1e7))
Expand All @@ -271,7 +276,7 @@ For example, the following function uses the 32 bit PCG variant together with Bo

```{r, eval=FALSE, engine='Rcpp'}
#include <Rcpp.h>
// [[Rcpp::depends(dqrng, BH, sitmo)]]
// [[Rcpp::depends(dqrng, BH)]]
#include <pcg_random.hpp>
#include <boost/random/normal_distribution.hpp>
Expand All @@ -297,13 +302,13 @@ The parameters of the distributions are adjusted for every draw using `<distribu

```{r, eval=FALSE, engine='Rcpp'}
#include <Rcpp.h>
// [[Rcpp::depends(dqrng, BH, sitmo)]]
// [[Rcpp::depends(dqrng, BH)]]
#include <boost/random/binomial_distribution.hpp>
#include <dqrng_distribution.h>
#include <xoshiro.h>
// [[Rcpp::plugins(cpp11)]]
// aliases for the used ditributions
// aliases for the used distributions
using binomial = boost::random::binomial_distribution<int>;
using normal = dqrng::normal_distribution;
// [[Rcpp::export]]
Expand Down
4 changes: 2 additions & 2 deletions vignettes/parallel.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Care has been taken to keep the global RNG `rng` usable outside of the parallel

```{r, eval=FALSE, engine='Rcpp'}
#include <Rcpp.h>
// [[Rcpp::depends(dqrng, BH, sitmo)]]
// [[Rcpp::depends(dqrng, BH)]]
#include <xoshiro.h>
#include <dqrng_distribution.h>
// [[Rcpp::plugins(cpp11)]]
Expand Down Expand Up @@ -138,7 +138,7 @@ The resulting correlation matrix should be close to the identity matrix if the d

```{r, eval=FALSE, engine='Rcpp'}
#include <Rcpp.h>
// [[Rcpp::depends(dqrng, BH, sitmo)]]
// [[Rcpp::depends(dqrng, BH)]]
#include <pcg_random.hpp>
#include <dqrng_sample.h>
// [[Rcpp::plugins(cpp11)]]
Expand Down

0 comments on commit ce75f10

Please sign in to comment.