From 0874905f9e2d6d888344dc6a2c1c05bfdeb1e066 Mon Sep 17 00:00:00 2001 From: July Pilowsky Date: Thu, 21 Mar 2024 15:26:16 -0400 Subject: [PATCH] epizootic now runs on foreach I was having problems with future and furrr in this package, so the parallelization engine has switched to doParallel/foreach instead. --- .github/workflows/R-CMD-check.yaml | 2 +- DESCRIPTION | 6 +-- NAMESPACE | 9 ++-- R/SimulationHandler.R | 32 ++++++------- renv.lock | 77 +----------------------------- 5 files changed, 23 insertions(+), 103 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index b321fd9..b3a8064 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -40,7 +40,7 @@ jobs: # setup-r-dependencies is removed ! # instead, we're handling dependencies with renv - name: Cache packages - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ${{ env.RENV_PATHS_ROOT }} key: ${{ runner.os }}-renv-${{ hashFiles('**/renv.lock') }} diff --git a/DESCRIPTION b/DESCRIPTION index 2f65737..a2cb015 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,11 +20,11 @@ Imports: tibble (>= 3.2.1), R6 (>= 2.5.1), cli (>= 3.6.1), - furrr (>= 0.3.1), - future (>= 1.33.0), raster (>= 3.6), qs (>= 0.25.7), - poems (>= 1.1.0) + poems (>= 1.1.0), + doParallel (>= 1.0.16), + foreach (>= 1.5.1) Suggests: testthat (>= 3.0.0), geosphere (>= 1.5), diff --git a/NAMESPACE b/NAMESPACE index 1304cb7..aff63e9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -18,11 +18,10 @@ import(poems) import(purrr) import(tibble) importFrom(R6,R6Class) -importFrom(furrr,furrr_options) -importFrom(furrr,future_map) -importFrom(future,multisession) -importFrom(future,plan) -importFrom(future,sequential) +importFrom(doParallel,registerDoParallel) +importFrom(doParallel,stopImplicitCluster) +importFrom(foreach,"%dopar%") +importFrom(foreach,foreach) importFrom(poems,SimulationModel) importFrom(qs,qsave) importFrom(raster,nlayers) diff --git a/R/SimulationHandler.R b/R/SimulationHandler.R index a17ae0c..5295757 100644 --- a/R/SimulationHandler.R +++ b/R/SimulationHandler.R @@ -8,11 +8,10 @@ #' for parallelization, handles errors differently, and has a different default #' data format (\code{.qs}). #' -#' @importFrom future plan -#' @importFrom future sequential -#' @importFrom future multisession -#' @importFrom furrr furrr_options -#' @importFrom furrr future_map +#' @importFrom foreach foreach +#' @importFrom foreach %dopar% +#' @importFrom doParallel registerDoParallel +#' @importFrom doParallel stopImplicitCluster #' @importFrom qs qsave #' @importFrom R6 R6Class #' @export SimulationHandler @@ -265,34 +264,30 @@ SimulationHandler <- R6Class("SimulationHandler", } model <- NULL # release from memory - # Parallelize and simulate - if (self$parallel_cores == 1) { - plan(sequential) - } else { - plan(multisession, workers = self$parallel_cores) - } - - simulation_log <- future_map(1:nrow(self$sample_data), \(j) { + doParallel::registerDoParallel(cores = self$parallel_cores) + simulation_log <- foreach(i = 1:nrow(self$sample_data), + .packages = c("raster"), + .errorhandling = c("pass")) %dopar% { # Clone the model model <- self$nested_model$clone() # Set the model sample attributes - self$set_model_sample(model, j) + self$set_model_sample(model, i) if (length(model$error_messages)) { return(list(successful = FALSE, message = self$get_message_sample("Error(s) setting model %s sample attributes", i), errors = model$error_messages)) } # Create and run the simulator - simulator <- self$model_simulator$new_clone(simulation_model = model, sample_id = j) + simulator <- self$model_simulator$new_clone(simulation_model = model, sample_id = i) simulator_run_status <- simulator$run() # Substitute sample details into the simulator run status message - simulator_run_status$message <- self$get_message_sample(simulator_run_status$message, j) + simulator_run_status$message <- self$get_message_sample(simulator_run_status$message, i) # Save results if (!is.null(simulator$results)) { - results_file <- file.path(self$results_dir, paste0(self$get_results_filename(j), self$results_ext)) + results_file <- file.path(self$results_dir, paste0(self$get_results_filename(i), self$results_ext)) suppressWarnings(try( if (self$results_ext == ".qs") { qsave(simulator$results, results_file) @@ -309,7 +304,8 @@ SimulationHandler <- R6Class("SimulationHandler", } return(simulator_run_status) - }, .options = furrr_options(seed = TRUE, conditions = character())) + } + doParallel::stopImplicitCluster() # Summarize and write log to a file simulation_log <- self$log_simulation(simulation_log) diff --git a/renv.lock b/renv.lock index e983a5e..776c25b 100644 --- a/renv.lock +++ b/renv.lock @@ -1,6 +1,6 @@ { "R": { - "Version": "4.3.2", + "Version": "4.3.3", "Repositories": [ { "Name": "CRAN", @@ -172,17 +172,6 @@ ], "Hash": "5a295d7d963cc5035284dcdbaf334f4e" }, - "digest": { - "Package": "digest", - "Version": "0.6.34", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "utils" - ], - "Hash": "7ede2ee9ea8d3edbf1ca84c1e333ad1a" - }, "doParallel": { "Package": "doParallel", "Version": "1.0.17", @@ -256,37 +245,6 @@ ], "Hash": "618609b42c9406731ead03adf5379850" }, - "furrr": { - "Package": "furrr", - "Version": "0.3.1", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "future", - "globals", - "lifecycle", - "purrr", - "rlang", - "vctrs" - ], - "Hash": "da7a4c32196cb2262a41dd5a25d486ff" - }, - "future": { - "Package": "future", - "Version": "1.33.1", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "digest", - "globals", - "listenv", - "parallel", - "parallelly", - "utils" - ], - "Hash": "e57e292737f7a4efa9d8a91c5908222c" - }, "gdistance": { "Package": "gdistance", "Version": "1.6.4", @@ -327,17 +285,6 @@ ], "Hash": "45aa4def70a402e7d20efde490c13ecb" }, - "globals": { - "Package": "globals", - "Version": "0.16.2", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "codetools" - ], - "Hash": "baa9585ab4ce47a9f4618e671778cc6f" - }, "glue": { "Package": "glue", "Version": "1.7.0", @@ -421,16 +368,6 @@ ], "Hash": "b8552d117e1b808b09a832f589b79035" }, - "listenv": { - "Package": "listenv", - "Version": "0.9.1", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R" - ], - "Hash": "e2fca3e12e4db979dccc6e519b10a7ee" - }, "locfit": { "Package": "locfit", "Version": "1.5-9.8", @@ -490,18 +427,6 @@ ], "Hash": "df58958f293b166e4ab885ebcad90e02" }, - "parallelly": { - "Package": "parallelly", - "Version": "1.36.0", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "parallel", - "tools", - "utils" - ], - "Hash": "bca377e1c87ec89ebed77bba00635b2e" - }, "pillar": { "Package": "pillar", "Version": "1.9.0",