-
Notifications
You must be signed in to change notification settings - Fork 1
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
Helpers for using rngs from other packages #333
Merged
Merged
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
d44fadd
Start work on a sensible API
richfitz 7bbbec7
Start on a nice interface for pointers
richfitz 872eb55
Gracefully cope with pointer serialisation
richfitz e6f9d7f
Add some encapsulation
richfitz 4e858cb
Start adding docs
richfitz 0b99e97
Tidy up examples
richfitz ec82dcf
Tidy up interface
richfitz bf69eb4
Bump version and add news
richfitz db72276
Expose the number of streams
richfitz 27a12f1
Expand example
richfitz 394b0ee
Restore important error check
richfitz 4c5cd33
Minor cleanup
richfitz 8c58863
Move code into its own file
richfitz be595a3
Check that all pointer types can be synced
richfitz e3b687b
Check we can hit all branches in pointer code
richfitz 34cd062
Tidy up headers
richfitz 827ca85
Test pointer handling
richfitz 3e68479
Make vignettes buildable
richfitz 0c43a24
Tidy vignette
richfitz 73920e9
Copy files from right place
richfitz de32262
Spelling
richfitz 30da86a
Avoid windows pain
richfitz 57688d3
Better default behaviour of state()
richfitz 2c1935b
Documentation improvement from review
richfitz b4bf97e
Fix docs
richfitz 369fc29
Regenerate vignette
richfitz 621e602
Add test that state() syncs rng state
richfitz 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 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 |
---|---|---|
|
@@ -16,7 +16,7 @@ inst/doc | |
pkgdown | ||
inst/include/cub | ||
*.gcov | ||
vignettes_src/gpu.md | ||
vignettes_src/*.md | ||
|
||
.vscode/ | ||
|
||
|
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,6 +1,6 @@ | ||
Package: dust | ||
Title: Iterate Multiple Realisations of Stochastic Models | ||
Version: 0.11.7 | ||
Version: 0.11.8 | ||
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"), | ||
email = "[email protected]"), | ||
person("John", "Lees", role = "aut"), | ||
|
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
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,76 @@ | ||
##' @title Create pointer to random number generator stream | ||
##' | ||
##' @description This function exists to support use from other | ||
##' packages that wish to use dust's random number support, and | ||
##' creates an opaque pointer to a set of random number streams. It | ||
##' is described more fully in `vignette("rng_package.Rmd")` | ||
##' | ||
##' @export | ||
##' @examples | ||
##' dust::dust_rng_pointer$new() | ||
dust_rng_pointer <- R6::R6Class( | ||
"dust_rng_pointer", | ||
cloneable = FALSE, | ||
|
||
private = list( | ||
ptr_ = NULL, | ||
state_ = NULL, | ||
is_current_ = NULL | ||
), | ||
|
||
public = list( | ||
##' @field algorithm The name of the generator algorithm used (read-only) | ||
algorithm = NULL, | ||
|
||
##' @field n_streams The number of streams of random numbers provided | ||
##' (read-only) | ||
n_streams = NULL, | ||
|
||
##' @description Create a new `dust_rng_pointer` object | ||
##' | ||
##' @param seed The random number seed to use (see [dust::dust_rng] | ||
##' for details) | ||
##' | ||
##' @param n_streams The number of independent random number streams to | ||
##' create | ||
##' | ||
##' @param algorithm The random number algorithm to use. The default is | ||
##' `xoshiro256plus` which is a good general choice | ||
initialize = function(seed = NULL, n_streams = 1L, | ||
algorithm = "xoshiro256plus") { | ||
dat <- dust_rng_pointer_init(n_streams, seed, algorithm) | ||
private$ptr_ <- dat[[1L]] | ||
private$state_ <- dat[[2L]] | ||
private$is_current_ <- TRUE | ||
|
||
self$algorithm <- algorithm | ||
self$n_streams <- n_streams | ||
lockBinding("algorithm", self) | ||
lockBinding("n_streams", self) | ||
}, | ||
|
||
##' @description Synchronise the R copy of the random number state. | ||
##' Typically this is only needed before serialisation if you have | ||
##' ever used the object. | ||
sync = function() { | ||
dust_rng_pointer_sync(private, self$algorithm) | ||
}, | ||
|
||
##' @description Return a raw vector of state. This can be used to | ||
##' create other generators with the same state. | ||
state = function() { | ||
if (!private$is_current_) { | ||
self$sync() | ||
} | ||
private$state_ | ||
}, | ||
|
||
##' @description Return a logical, indicating if the random number | ||
##' state that would be returned by `state()` is "current" (i.e., the | ||
##' same as the copy held in the pointer) or not. This is `TRUE` on | ||
##' creation or immediately after calling `$sync()` or `$state()` | ||
##' and `FALSE` after any use of the pointer. | ||
is_current = function() { | ||
private$is_current_ | ||
} | ||
)) |
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ Marsaglia | |
Mersenne | ||
OMP | ||
OpenMP | ||
OpenMP's | ||
Perez | ||
Poisson | ||
R's | ||
|
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible useful reference for #271. If we wanted to add this and support saving and loading, see the serialisation examples:
https://pybind11.readthedocs.io/en/stable/advanced/classes.html#pickling-support
https://github.com/pybind/pybind11/blob/master/tests/test_pickling.cpp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The R serialisation has no hooking functionality, it's super annoying