Skip to content

Commit

Permalink
Merge pull request #27 from tlverse/fix-random
Browse files Browse the repository at this point in the history
make RNG match non-delayed version
  • Loading branch information
jeremyrcoyle authored Apr 29, 2024
2 parents acdd758 + 5c8acba commit 98dfc74
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 21 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/R-CMD-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ jobs:
uses: actions/checkout@v2

- name: Setup R
uses: r-lib/actions/setup-r@master
uses: r-lib/actions/setup-r@v2-branch
with:
r-version: ${{ matrix.config.r }}

- name: Install pandoc
uses: r-lib/actions/setup-pandoc@v1
uses: r-lib/actions/setup-pandoc@v2-branch

- name: Install tinyTeX
uses: r-lib/actions/setup-tinytex@v1
uses: r-lib/actions/setup-tinytex@v2-branch

- name: Install system dependencies
if: runner.os == 'Linux'
Expand Down
4 changes: 2 additions & 2 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 0.4.0
Date: 2022-10-19 21:05:01 UTC
SHA: 33570a373e61917feeb60a122681c22ec7747cb9
Date: 2022-10-19 22:12:19 UTC
SHA: acdd758d9a62ee5168d0da0ceeff0d9c4e8bc394
2 changes: 1 addition & 1 deletion R/Job.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ SequentialJob <- R6Class(
start_time <-proc.time()

private$.result <- try({
set.seed(delayed_object$seed)
# set.seed(delayed_object$seed)
eval_delayed(to_eval, delayed_object$timeout)
})

Expand Down
11 changes: 9 additions & 2 deletions R/Scheduler.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ Scheduler <- R6Class(
dependent_uuid = NULL) {
state <- delayed_object$update_state
uuid <- delayed_object$uuid
delayed_object$seed <- runif(1,0,1e6)

if (!(delayed_object$sequential || ("sequential"%in%attr(plan(),"class")))) {
# if we're using a parallel future plan,
# we should set seeds for reproducibility
delayed_object$seed <- runif(1,0,1e6)
}


private$.n_tasks <- private$.n_tasks + 1
delayed_object$task_order <- private$.n_tasks
assign(uuid, delayed_object, envir = private$.task_lists[[state]])
Expand Down Expand Up @@ -127,7 +134,7 @@ Scheduler <- R6Class(
if (!is.null(current_task)) {
job_type <- private$.job_type

if (current_task$sequential) {
if (current_task$sequential || ("sequential"%in%attr(plan(),"class"))) {
SequentialJob$new(current_task)
self$update_task(current_task, "ready", "running")
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_Delayed.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ test_that("progress bar", {
x
}
delayed_ident <- delayed_fun(ident_fun)
d_list <- lapply(1:1e2, delayed_ident)
d_list <- lapply(1:1e1, delayed_ident)
d_bundle <- bundle_delayed(d_list)
res <- d_bundle$compute(progress = TRUE)
})
2 changes: 1 addition & 1 deletion tests/testthat/test_benchmark.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ make_adder_list <- function() {
# big_adder$compute()
# plot(big_adder)


plan(sequential)
big_adder <- make_adder_list()

time_sequential <- system.time({
Expand Down
29 changes: 18 additions & 11 deletions tests/testthat/test_seed.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,49 @@ library(future)
gen_rand <- function(x) {
# cat(sprintf("generating for x=%d\n", x))
x <- runif(1)
Sys.sleep(x)
Sys.sleep(runif(1,0,2))
x
}

dgr <- delayed_fun(gen_rand)

# generate random numbers
set.seed(1234)
non_delayed1 <- unlist(lapply(1:5, gen_rand))
non_delayed_1 <- unlist(lapply(1:5, gen_rand))

set.seed(1234)
non_delayed2 <- unlist(lapply(1:5, gen_rand))
non_delayed_2 <- unlist(lapply(1:5, gen_rand))

plan(sequential)

set.seed(1234)
delayed_obj <- bundle_delayed(lapply(1:5, dgr))
delayed1 <- unlist(delayed_obj$compute())
delayed_sequential <- unlist(delayed_obj$compute())


plan(multicore, workers = 2)

set.seed(1234)
delayed_obj <- bundle_delayed(lapply(1:5, dgr))
delayed2 <- unlist(delayed_obj$compute())
delayed_mc_1 <- unlist(delayed_obj$compute())

set.seed(1234)
delayed_obj <- bundle_delayed(lapply(1:5, dgr))
delayed_mc_2 <- unlist(delayed_obj$compute())

print(non_delayed1)
print(non_delayed2)
print(delayed1)
print(delayed2)
# print(non_delayed_1)
# print(non_delayed_2)
# print(delayed_sequential)
# print(delayed_mc_1)
# print(delayed_mc_2)

test_that(
"delayed preserves ordering (needed for RNG reproducibility)",
expect_true(
all(
all.equal(non_delayed1, non_delayed2),
all.equal(delayed1, delayed2)
all.equal(non_delayed_1, non_delayed_2),
all.equal(non_delayed_1, delayed_sequential),
all.equal(delayed_mc_1, delayed_mc_2)
)
)
)

0 comments on commit 98dfc74

Please sign in to comment.