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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
message("Testing rng")
test_rng_lapply <- function()
{
.rng_get_generator <- BiocParallel:::.rng_get_generator
.rng_reset_generator <- BiocParallel:::.rng_reset_generator
.workerLapply <- BiocParallel:::.workerLapply
.RNGstream <- BiocParallel:::.RNGstream
.rng_next_substream <- BiocParallel:::.rng_next_substream
OPTIONS <- BiocParallel:::.workerOptions()
state <- .rng_get_generator()
on.exit(.rng_reset_generator(state$kind, state$seed))
SEED <- .RNGstream(bpstart(SerialParam()))
checkIdentical(
## same sequence of random number streams
.workerLapply(1:2, function(i) rnorm(1), NULL, OPTIONS, SEED),
.workerLapply(1:2, function(i) rnorm(1), NULL, OPTIONS, SEED)
)
SEED1 <- .RNGstream(bpstart(SerialParam()))
SEED2 <- .rng_next_substream(SEED1)
target <- .workerLapply(1:2, function(i) rnorm(2), NULL, OPTIONS, SEED1)
obs <- c(
.workerLapply(1, function(i) rnorm(2), NULL, OPTIONS, SEED1),
.workerLapply(1, function(i) rnorm(2), NULL, OPTIONS, SEED2)
)
checkIdentical(target, obs)
checkTrue(identical(state, .rng_get_generator()))
}
test_rng_bplapply <- function()
{
.rng_get_generator <- BiocParallel:::.rng_get_generator
.rng_reset_generator <- BiocParallel:::.rng_reset_generator
state <- .rng_get_generator()
on.exit(.rng_reset_generator(state$kind, state$seed))
p1 <- SerialParam(RNGseed = 123)
p2 <- SnowParam(3, RNGseed = 123)
p3 <- SnowParam(5, RNGseed = 123)
FUN <- function(i) rnorm(2)
## SerialParam / SnowParam same results
target <- bplapply(1:11, FUN, BPPARAM = p1)
checkIdentical(bplapply(1:11, FUN, BPPARAM = p2), target)
## SerialParam / SnowParam same results, different number of workers
checkIdentical(bplapply(1:11, FUN, BPPARAM = p3), target)
if (identical(.Platform$OS.type, "unix")) {
## SerialParam / TransientMulticoreParam same results
p4a <- MulticoreParam(5, RNGseed = 123)
checkIdentical(bplapply(1:11, FUN, BPPARAM = p4a), target)
## SerialParam / MulticoreParam same results
p4b <- bpstart(MulticoreParam(5, RNGseed = 123))
checkIdentical(bplapply(1:11, FUN, BPPARAM = p4b), target)
bpstop(p4b)
}
## single worker coerced to SerialParam
p5 <- SnowParam(1, RNGseed = 123)
checkIdentical(bplapply(1:11, FUN, BPPARAM = p5), target, "p5")
checkIdentical(state$kind, .rng_get_generator()$kind)
}
test_rng_bpiterate <- function()
{
.rng_get_generator <- BiocParallel:::.rng_get_generator
.rng_reset_generator <- BiocParallel:::.rng_reset_generator
state <- .rng_get_generator()
on.exit(.rng_reset_generator(state$kind, state$seed))
FUN <- function(i) rnorm(2)
ITER_factory <- function() {
x <- 1:11
i <- 0L
function() {
i <<- i + 1L
if (i > length(x))
return(NULL)
x[[i]]
}
}
p1 <- SerialParam(RNGseed = 123)
p2 <- SnowParam(3, RNGseed = 123)
p3 <- SnowParam(5, RNGseed = 123)
target <- bplapply(1:11, FUN, BPPARAM = p1)
checkIdentical(target, bpiterate(ITER_factory(), FUN, BPPARAM = p1), "p1")
checkIdentical(target, bpiterate(ITER_factory(), FUN, BPPARAM = p2), "p2")
checkIdentical(target, bpiterate(ITER_factory(), FUN, BPPARAM = p3), "p3")
if (identical(.Platform$OS.type, "unix")) {
## SerialParam / TransientMulticoreParam same results
p4a <- MulticoreParam(5, RNGseed = 123)
checkIdentical(
target, bpiterate(ITER_factory(), FUN, BPPARAM = p4a),
"p4a"
)
## SerialParam / MulticoreParam same results
p4b <- bpstart(MulticoreParam(5, RNGseed = 123))
checkIdentical(
target, bpiterate(ITER_factory(), FUN, BPPARAM = p4b),
"p4b"
)
bpstop(p4b)
}
checkIdentical(state$kind, .rng_get_generator()$kind)
}
test_rng_bpstart <- function()
{
.rng_get_generator <- BiocParallel:::.rng_get_generator
.rng_reset_generator <- BiocParallel:::.rng_reset_generator
state <- .rng_get_generator()
FUN <- function(i) rnorm(2)
ITER_factory <- function() {
x <- 1:11
i <- 0L
function() {
i <<- i + 1L
if (i > length(x))
return(NULL)
x[[i]]
}
}
## bplapply
p0 <- bpstart(SerialParam()) # random seed
result1 <- unlist(bplapply(1:11, FUN, BPPARAM = p0))
result2 <- unlist(bplapply(1:11, FUN, BPPARAM = p0))
checkTrue(!any(result1 %in% result2))
bpstart(bpstop(p0)) # different random seed
result3 <- unlist(bplapply(1:11, FUN, BPPARAM = p0))
checkTrue(!any(result3 %in% result1))
p0 <- bpstart(SerialParam(RNGseed = 123)) # set seed
result1 <- unlist(bplapply(1:11, FUN, BPPARAM = p0))
result2 <- unlist(bplapply(1:11, FUN, BPPARAM = p0))
checkTrue(!any(result1 %in% result2))
bpstart(bpstop(p0)) # reset seed, same stream
result3 <- unlist(bplapply(1:11, FUN, BPPARAM = p0))
result4 <- unlist(bplapply(1:11, FUN, BPPARAM = p0))
checkIdentical(result3, result1)
checkIdentical(result4, result2)
## bpiterate
p0 <- bpstart(SerialParam()) # random seed
result1 <- unlist(bpiterate(ITER_factory(), FUN, BPPARAM = p0))
result2 <- unlist(bpiterate(ITER_factory(), FUN, BPPARAM = p0))
checkTrue(!any(result1 %in% result2))
bpstart(bpstop(p0)) # different random seed
result3 <- unlist(bpiterate(ITER_factory(), FUN, BPPARAM = p0))
checkTrue(!any(result3 %in% result1))
p0 <- bpstart(SerialParam(RNGseed = 123)) # set seed
result1 <- unlist(bpiterate(ITER_factory(), FUN, BPPARAM = p0))
result2 <- unlist(bpiterate(ITER_factory(), FUN, BPPARAM = p0))
checkTrue(!any(result1 %in% result2))
bpstart(bpstop(p0)) # reset seed, same stream
result3 <- unlist(bpiterate(ITER_factory(), FUN, BPPARAM = p0))
result4 <- unlist(bpiterate(ITER_factory(), FUN, BPPARAM = p0))
checkIdentical(result3, result1)
checkIdentical(result4, result2)
checkIdentical(state$kind, .rng_get_generator()$kind)
}
.test_rng_bpstart_does_not_iterate_rng_seed <- function(param) {
.rng_get_generator <- BiocParallel:::.rng_get_generator
state <- .rng_get_generator()
set.seed(123L)
target <- runif(1L)
## bpstart() should not increment the random number seed
set.seed(123L)
bpstart(param)
checkIdentical(target, runif(1L))
bpstop(param)
## bplapply does not increment stream
set.seed(123)
result <- bplapply(1:3, runif, BPPARAM = param)
checkIdentical(target, runif(1L))
## bplapply uses internal stream
set.seed(123)
result <- bplapply(1:3, runif, BPPARAM = param)
checkTrue(!identical(result, bplapply(1:3, runif, BPPARAM = param)))
checkIdentical(target, runif(1L))
target1 <- lapply(1:3, runif)
checkTrue(!identical(result, target1))
checkIdentical(state$kind, .rng_get_generator()$kind)
}
test_rng_bpstart_does_not_iterate_rng_seed <- function() {
.rng_get_generator <- BiocParallel:::.rng_get_generator
.rng_reset_generator <- BiocParallel:::.rng_reset_generator
TEST_FUN <- .test_rng_bpstart_does_not_iterate_rng_seed
state <- .rng_get_generator()
on.exit(.rng_reset_generator(state$kind, state$seed))
TEST_FUN(SerialParam())
TEST_FUN(SnowParam(2))
if (identical(.Platform$OS.type, "unix"))
TEST_FUN(MulticoreParam(2))
}
.test_rng_global_and_RNGseed_indepenent <- function(param_fun) {
set.seed(123)
target <- bplapply(1:3, runif, BPPARAM = param_fun())
current <- bplapply(1:3, runif, BPPARAM = param_fun(RNGseed = 123))
checkTrue(!identical(target, current))
}
test_rng_global_and_RNGseed_independent <- function() {
.rng_get_generator <- BiocParallel:::.rng_get_generator
.rng_reset_generator <- BiocParallel:::.rng_reset_generator
TEST_FUN <- .test_rng_global_and_RNGseed_indepenent
state <- .rng_get_generator()
on.exit(.rng_reset_generator(state$kind, state$seed))
TEST_FUN(SerialParam)
TEST_FUN(SnowParam)
if (identical(.Platform$OS.type, "unix"))
TEST_FUN(MulticoreParam)
}
.test_rng_lapply_bpredo_impl <- function(param) {
FUN <- function(i) rnorm(1)
target <- unlist(bplapply(1:11, FUN, BPPARAM = param))
FUN0 <- function(i) {
if (identical(i, 7L)) {
stop("i == 7")
} else rnorm(1)
}
result <- bptry(bplapply(1:11, FUN0, BPPARAM = param))
checkIdentical(unlist(result[-7]), target[-7])
checkTrue(inherits(result[[7]], "remote_error"))
FUN1 <- function(i) {
if (identical(i, 7L)) {
## the random number stream should be in the same state as the
## first time through the loop, and rnorm(1) should return
## same result as FUN
rnorm(1)
} else {
## if this branch is used, then we are incorrectly updating
## already calculated elements -- '0' in the output would
## indicate this error
0
}
}
result <- unlist(bplapply(1:11, FUN1, BPREDO = result, BPPARAM = param))
checkIdentical(result, target)
bpstart(param)
target1 <- unlist(bplapply(1:11, FUN, BPPARAM = param))
target2 <- unlist(bplapply(1:11, FUN, BPPARAM = param))
target3 <- unlist(bplapply(1:11, FUN, BPPARAM = param))
bpstop(param)
bpstart(param)
result1 <- bptry(bplapply(1:11, FUN0, BPPARAM = param))
result1_redo1 <- unlist(bplapply(1:11, FUN1, BPREDO = result1, BPPARAM = param))
result2 <- unlist(bplapply(1:11, FUN, BPPARAM = param))
result1_redo2 <- unlist(bplapply(1:11, FUN1, BPREDO = result1, BPPARAM = param))
result3 <- unlist(bplapply(1:11, FUN, BPPARAM = param))
checkIdentical(target1, result1_redo1)
checkIdentical(target1, result1_redo2)
checkIdentical(target2, result2)
checkIdentical(target3, result3)
}
test_rng_lapply_bpredo <- function()
{
.rng_get_generator <- BiocParallel:::.rng_get_generator
.rng_reset_generator <- BiocParallel:::.rng_reset_generator
state <- .rng_get_generator()
on.exit(.rng_reset_generator(state$kind, state$seed))
param <- SerialParam(RNGseed = 123, stop.on.error = FALSE)
.test_rng_lapply_bpredo_impl(param)
if (identical(.Platform$OS.type, "unix")) {
param <- MulticoreParam(3, RNGseed = 123, stop.on.error = FALSE)
.test_rng_lapply_bpredo_impl(param)
}
}
.test_rng_iterate_bpredo_impl <- function(param) {
FUN <- function(i) rnorm(1)
target <- unlist(bplapply(1:11, FUN, BPPARAM = param))
FUN0 <- function(i) {
if (identical(i, 7L)) {
stop("i == 7")
} else rnorm(1)
}
iter_factory <- function(n){
i <- 0L
function() if(i<n) i <<- i + 1L
}
result <- bptry(bpiterate(iter_factory(11), FUN0, BPPARAM = param))
checkIdentical(unlist(result[-7]), target[-7])
checkTrue(is.null(result[[7]]))
checkTrue(inherits(attr(result,"errors")[[1]], "remote_error"))
FUN1 <- function(i) {
if (identical(i, 7L)) {
## the random number stream should be in the same state as the
## first time through the loop, and rnorm(1) should return
## same result as FUN
rnorm(1)
} else {
## if this branch is used, then we are incorrectly updating
## already calculated elements -- '0' in the output would
## indicate this error
0
}
}
result <- unlist(bpiterate(iter_factory(11), FUN1, BPREDO = result, BPPARAM = param))
checkIdentical(result, target)
bpstart(param)
target1 <- unlist(bplapply(1:11, FUN, BPPARAM = param))
target2 <- unlist(bplapply(1:11, FUN, BPPARAM = param))
target3 <- unlist(bplapply(1:11, FUN, BPPARAM = param))
bpstop(param)
bpstart(param)
result1 <- bptry(bpiterate(iter_factory(11), FUN0, BPPARAM = param))
result1_redo1 <- unlist(bpiterate(iter_factory(11), FUN1, BPREDO = result1, BPPARAM = param))
result2 <- unlist(bpiterate(iter_factory(11), FUN, BPPARAM = param))
result1_redo2 <- unlist(bpiterate(iter_factory(11), FUN1, BPREDO = result1, BPPARAM = param))
result3 <- unlist(bpiterate(iter_factory(11), FUN, BPPARAM = param))
bpstop(param)
checkIdentical(target1, result1_redo1)
checkIdentical(target1, result1_redo2)
checkIdentical(target2, result2)
checkIdentical(target3, result3)
}
test_rng_iterate_bpredo <- function()
{
.rng_get_generator <- BiocParallel:::.rng_get_generator
.rng_reset_generator <- BiocParallel:::.rng_reset_generator
state <- .rng_get_generator()
on.exit(.rng_reset_generator(state$kind, state$seed))
param <- SerialParam(RNGseed = 123, stop.on.error = FALSE)
.test_rng_iterate_bpredo_impl(param)
if (identical(.Platform$OS.type, "unix")) {
param <- MulticoreParam(3, RNGseed = 123, stop.on.error = FALSE)
.test_rng_iterate_bpredo_impl(param)
}
}
test_rng_fallback_SerialParam <- function()
{
.rng_get_generator <- BiocParallel:::.rng_get_generator
.rng_reset_generator <- BiocParallel:::.rng_reset_generator
state <- .rng_get_generator()
on.exit(.rng_reset_generator(state$kind, state$seed))
FUN <- function(i) rnorm(1)
param <- SerialParam(RNGseed = 123, stop.on.error = FALSE)
target <- unlist(bplapply(1:2, FUN, BPPARAM = param))
param2 <- SnowParam(RNGseed = 123, workers = 1L)
checkIdentical(unlist(bplapply(1:2, FUN, BPPARAM = param2)), target)
bpstart(param2)
checkIdentical(bplapply(1, FUN, BPPARAM = param2)[[1]], target[1])
checkIdentical(bplapply(1, FUN, BPPARAM = param2)[[1]], target[2])
bpstop(param2)
}
test_rng_reproducibility_across_versions <- function()
{
p <- SerialParam(RNGseed = 123)
bptasks(p) <- 3
## This number should NEVER change across version
res0 <- list(14L, 14L, 6L, 17L, 20L,
5L, 16L, 1L, 4L, 16L,
4L, 20L, 20L, 3L, 3L,
17L, 11L, 19L, 1L, 3L)
fun <- function(x) sample(1:20, 1)
res1 <- bplapply(1:20, fun, BPPARAM = p)
checkIdentical(res1, res0)
iter_factory <- function(n){
i <- 0L
function() if(i<n) i <<- i + 1L
}
res2 <- bpiterate(iter_factory(20), fun, BPPARAM = p)
checkIdentical(res2, res0)
}
test_rng_reset_seed <- function()
{
## the seed stream must be the same
## if the seed is set to the same value
## no matter if the BPPARAM has been started or not
p <- SerialParam()
bpstart(p)
opts <- bpoptions(RNGseed = 123)
res1 <- bplapply(1:3, function(x) runif(1),
BPPARAM = p,
BPOPTIONS = opts)
res2 <- bplapply(1:3, function(x) runif(1),
BPPARAM = p,
BPOPTIONS = opts)
checkIdentical(res1, res2)
}
|