From 08de8b5dd911063d3095946dc9e3c832bf98eebe Mon Sep 17 00:00:00 2001 From: Jeremy Coyle Date: Mon, 29 Apr 2024 09:32:25 -0700 Subject: [PATCH 1/2] make RNG match non-delayed version when delayed is type sequential or future plan sequential is in effect --- CRAN-SUBMISSION | 4 ++-- R/Job.R | 2 +- R/Scheduler.R | 11 +++++++++-- tests/testthat/test_Delayed.R | 2 +- tests/testthat/test_benchmark.R | 2 +- tests/testthat/test_seed.R | 29 ++++++++++++++++++----------- 6 files changed, 32 insertions(+), 18 deletions(-) diff --git a/CRAN-SUBMISSION b/CRAN-SUBMISSION index 36845b3..505bf81 100644 --- a/CRAN-SUBMISSION +++ b/CRAN-SUBMISSION @@ -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 diff --git a/R/Job.R b/R/Job.R index 513c5d0..5992092 100644 --- a/R/Job.R +++ b/R/Job.R @@ -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) }) diff --git a/R/Scheduler.R b/R/Scheduler.R index a4cd5db..4465f61 100644 --- a/R/Scheduler.R +++ b/R/Scheduler.R @@ -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]]) @@ -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 { diff --git a/tests/testthat/test_Delayed.R b/tests/testthat/test_Delayed.R index afb2d62..1e12828 100644 --- a/tests/testthat/test_Delayed.R +++ b/tests/testthat/test_Delayed.R @@ -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) }) diff --git a/tests/testthat/test_benchmark.R b/tests/testthat/test_benchmark.R index a14caea..d6d1dc3 100644 --- a/tests/testthat/test_benchmark.R +++ b/tests/testthat/test_benchmark.R @@ -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({ diff --git a/tests/testthat/test_seed.R b/tests/testthat/test_seed.R index c8936dd..5689d03 100644 --- a/tests/testthat/test_seed.R +++ b/tests/testthat/test_seed.R @@ -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) ) ) ) From 5c8acba7117b32fe276f49bcd6a40262e963bf8c Mon Sep 17 00:00:00 2001 From: Jeremy Coyle Date: Mon, 29 Apr 2024 09:39:03 -0700 Subject: [PATCH 2/2] fix workflow --- .github/workflows/R-CMD-check.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/R-CMD-check.yml b/.github/workflows/R-CMD-check.yml index 5f2d726..fcce7e5 100644 --- a/.github/workflows/R-CMD-check.yml +++ b/.github/workflows/R-CMD-check.yml @@ -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'