1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
---
title: "Random Numbers in _BiocParallel_"
author:
- name: Martin Morgan
affiliation: Roswell Park Comprehensive Cancer Center, Buffalo, NY
email: Martin.Morgan@RoswellPark.org
date: "Edited: 7 September, 2021; Compiled: `r format(Sys.time(), '%B %d, %Y')`"
vignette: >
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{4. Random Numbers in BiocParallel}
%\VignetteEncoding{UTF-8}
output:
BiocStyle::html_document:
number_sections: yes
toc: yes
toc_depth: 4
---
[RPCI]: https://www.roswellpark.org/martin-morgan
# Scope
`r Biocpkg("BiocParallel")` enables use of random number streams in a
reproducible manner. This document applies to the following
`*Param()`:
* `SerialParam()`: sequential evaluation in a single R process.
* `SnowParam()`: parallel evaluation in multiple independent R
processes.
* `MulticoreParam())`: parallel evaluation in R sessions running in
forked threads. Not available on Windows.
The `*Param()` can be used for evaluation with:
* `bplapply()`: `lapply()`-like application of a user-supplied
function `FUN` to a vector or list of elements `X`.
* `bpiterate()`: apply a user-supplied function `FUN` to an unknown
number of elements resulting from successive calls to a
user-supplied function `ITER.`
The reproducible random number implementation also supports:
* `bptry()` and the `BPREDO=` argument, for re-evaluation of elements
that fail (e.g., because of a bug in `FUN`).
# Essentials
## Use of `bplapply()` and `RNGseed=`
Attach `r Biocpkg("BiocParallel")` and ensure that the version is
greater than 1.27.5
```{r}
library(BiocParallel)
stopifnot(
packageVersion("BiocParallel") > "1.27.5"
)
```
For reproducible calculation, use the `RNGseed=` argument in any of the
`*Param()`constructors.
```{r}
result1 <- bplapply(1:3, runif, BPPARAM = SerialParam(RNGseed = 100))
result1
```
Repeating the calculation with the same value for `RNGseed=` results
in the same result; a different random number seed results in
different results.
```{r}
result2 <- bplapply(1:3, runif, BPPARAM = SerialParam(RNGseed = 100))
stopifnot(
identical(result1, result2)
)
result3 <- bplapply(1:3, runif, BPPARAM = SerialParam(RNGseed = 200))
result3
stopifnot(
!identical(result1, result3)
)
```
Results are invariant across `*Param()`
```{r}
result4 <- bplapply(1:3, runif, BPPARAM = SnowParam(RNGseed = 100))
stopifnot(
identical(result1, result4)
)
if (!identical(.Platform$OS.type, "windows")) {
result5 <- bplapply(1:3, runif, BPPARAM = MulticoreParam(RNGseed = 100))
stopifnot(
identical(result1, result5)
)
}
```
Parallel backends can adjust the number of `workers` (processes
performing the evaluation) and `tasks` (how elements of `X` are
distributed between workers). Results are invariant to these
parameters. This is illustrated with `SnowParam()`, but applies also
to `MulticoreParam()`.
```{r}
result6 <- bplapply(1:3, runif, BPPARAM = SnowParam(workers = 2, RNGseed = 100))
result7 <- bplapply(1:3, runif, BPPARAM = SnowParam(workers = 3, RNGseed = 100))
result8 <- bplapply(
1:3, runif,
BPPARAM = SnowParam(workers = 2, tasks = 3, RNGseed = 100)
)
stopifnot(
identical(result1, result6),
identical(result1, result7),
identical(result1, result8)
)
```
Subsequent sections illustrate results with `SerialParam()`, but identical
results are obtained with `SnowParam()` and `MulticoreParam()`.
## Use with `bpiterate()`
`bpiterate()` allows parallel processing of a ’stream’ of data as a
series of tasks, with a task consisting of a portion of the overall
data. It is useful when the data size is not known or easily
partitioned into elements of a vector or list. A real use case might
involve iterating through a BAM file, where a task represents
successive records (perhaps 100,000 per task) in the file. Here we
illustrate with a simple example – iterating through a vector `x =
1:3`
```{r}
ITER_FUN_FACTORY <- function() {
x <- 1:3
i <- 0L
function() {
i <<- i + 1L
if (i > length(x))
return(NULL)
x[[i]]
}
}
```
`ITER_FUN_FACTORY()` is used to create a function that, on each invocation,
returns the next task (here, an element of `x`; in a real example, perhaps
100000 records from a BAM file). When there are no more tasks, the function
returns `NULL`
```{r, collapse = TRUE}
ITER <- ITER_FUN_FACTORY()
ITER()
ITER()
ITER()
ITER()
```
In our simple example, `bpiterate()` is performing the same
computations as `bplapply()` so the results, including the random
number streams used by each task in `bpiterate()`, are the same
```{r}
result9 <- bpiterate(
ITER_FUN_FACTORY(), runif,
BPPARAM = SerialParam(RNGseed = 100)
)
stopifnot(
identical(result1, result9)
)
```
## Use with `bptry()`
`bptry()` in conjunction with the `BPREDO=` argument to `bplapply()`
or `bpiterate()` allows for graceful recovery from errors. Here a
buggy `FUN1()` produces an error for the second element. `bptry()`
allows evaluation to continue for other elements of `X`, despite the
error. This is shown in the result.
```{r}
FUN1 <- function(i) {
if (identical(i, 2L)) {
## error when evaluating the second element
stop("i == 2")
} else runif(i)
}
result10 <- bptry(bplapply(
1:3, FUN1,
BPPARAM = SerialParam(RNGseed = 100, stop.on.error = FALSE)
))
result10
```
`FUN2()` illustrates the flexibility of `bptry()` by fixing the bug
when `i == 2`, but also generating incorrect results if invoked for
previously correct values. The identity of the result to the original
computation shows that only the error task is re-computed, and that
the random number stream used by the task is identical to the original
stream.
```{r}
FUN2 <- function(i) {
if (identical(i, 2L)) {
## the random number stream should be in the same state as the
## first time through the loop, and rnorm(i) should return
## same result as FUN
runif(i)
} else {
## if this branch is used, then we are incorrectly updating
## already calculated elements -- '0' in the output would
## indicate this error
0
}
}
result11 <- bplapply(
1:3, FUN2,
BPREDO = result10,
BPPARAM = SerialParam(RNGseed = 100, stop.on.error = FALSE)
)
stopifnot(
identical(result1, result11)
)
```
## Relationship between` RNGseed=` and `set.seed()`
The global random number stream (influenced by `set.seed()`) is
ignored by `r Biocpkg("BiocParallel")`, and `r Biocpkg("BiocParallel")` does
NOT increment the global stream.
```{r}
set.seed(200)
value <- runif(1)
set.seed(200)
result12 <- bplapply(1:3, runif, BPPARAM = SerialParam(RNGseed = 100))
stopifnot(
identical(result1, result12),
identical(value, runif(1))
)
```
When `RNGseed=` is not used, an internal stream (not accessible to the
user) is used and `r Biocpkg("BiocParallel")` does NOT increment the
global stream.
```{r}
set.seed(100)
value <- runif(1)
set.seed(100)
result13 <- bplapply(1:3, runif, BPPARAM = SerialParam())
stopifnot(
!identical(result1, result13),
identical(value, runif(1))
)
```
## `bpstart()` and random number streams
In all of the examples so far `*Param()` objects are passed to
`bplapply()` or `bpiterate()` in the ’stopped’ state. Internally,
`bplapply()` and `bpiterate()` invoke `bpstart()` to establish the
computational environment (e.g., starting workers for
`SnowParam()`). `bpstart()` can be called explicitly, e.g., to allow
workers to be used across calls to `bplapply()`.
The cluster random number stream is initiated with `bpstart()`. Thus
```{r}
param <- bpstart(SerialParam(RNGseed = 100))
result16 <- bplapply(1:3, runif, BPPARAM = param)
bpstop(param)
stopifnot(
identical(result1, result16)
)
```
This allows a second call to `bplapply` to represent a continuation of
a random number computation – the second call to `bplapply()` results
in different random number streams for each element of `X`.
```{r}
param <- bpstart(SerialParam(RNGseed = 100))
result16 <- bplapply(1:3, runif, BPPARAM = param)
result17 <- bplapply(1:3, runif, BPPARAM = param)
bpstop(param)
stopifnot(
identical(result1, result16),
!identical(result1, result17)
)
```
The results from `bplapply()` are different from the results from
`lapply()`, even with the same random number seed. This is because
correctly implemented parallel random streams require use of a
particular random number generator invoked in specific ways for each
element of `X`, as outlined in the Implementation notes section.
## Relationship between `bplapply()` and `lapply()`
The results from `bplapply()` are different from the results from
`lapply()`, even with the same random number seed. This is because
correctly implemented parallel random streams require use of a
particular random number generator invoked in specific ways for each
element of `X`, as outlined in the Implementation notes section.
```{r}
set.seed(100)
result20 <- lapply(1:3, runif)
stopifnot(
!identical(result1, result20)
)
```
# Implementation notes
The implementation uses the L’Ecuyer-CMRG random number generator (see
`?RNGkind` and `?parallel::clusterSetRNGStream` for additional
details). This random number generates independent streams and
substreams of random numbers. In `r Biocpkg("BiocParallel")`, each
call to `bp start()` creates a new stream from the L’Ecuyer-CMRG
generator. Each element in `bplap` `ply()` or `bpiterate()` creates a
new substream. Each application of `FUN` is therefore using the
L’Ecuyer-CMRG random number generator, with a substream that is
independent of the substreams of all other elements.
Within the user-supplied `FUN` of `bplapply()` or `bpiterate()`, it is
a mistake to use `RNGkind()` to set a different random number
generator, or to use `set.seed()`. This would in principle compromise
the independence of the streams across elements.
# `sessionInfo()`
```{r, echo = FALSE}
sessionInfo()
```
|