From 39cca42091a090c9875dbf9f60737e844ce5afdc Mon Sep 17 00:00:00 2001 From: prdm0 Date: Tue, 23 Apr 2024 10:46:02 -0300 Subject: [PATCH] Update accept_reject.r --- .github/workflows/test-parallel-windows.yaml | 2 +- NAMESPACE | 1 + R/accept_reject.r | 13 +++++-------- tests/testthat/test-time-parallel.R | 4 ++-- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/.github/workflows/test-parallel-windows.yaml b/.github/workflows/test-parallel-windows.yaml index 31e63a4..03a6e90 100644 --- a/.github/workflows/test-parallel-windows.yaml +++ b/.github/workflows/test-parallel-windows.yaml @@ -23,7 +23,7 @@ jobs: - name: Install dependencies run: | - install.packages(c('remotes', 'testthat')) + install remotes::install_github('prdm0/AcceptReject') remotes::install_deps(dependencies = TRUE) shell: Rscript {0} diff --git a/NAMESPACE b/NAMESPACE index 8405e4a..71fec65 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,6 +32,7 @@ importFrom(graphics,hist) importFrom(lbfgs,lbfgs) importFrom(numDeriv,grad) importFrom(parallel,detectCores) +importFrom(parallel,makeCluster) importFrom(pbmcapply,pbmclapply) importFrom(purrr,map_dbl) importFrom(purrr,partial) diff --git a/R/accept_reject.r b/R/accept_reject.r index 3e4419d..e71c8ca 100644 --- a/R/accept_reject.r +++ b/R/accept_reject.r @@ -105,14 +105,14 @@ #' @import rlang #' @importFrom lbfgs lbfgs #' @importFrom purrr partial map_dbl -#' @importFrom pbmcapply pbmclapply #' @importFrom numDeriv grad -#' @importFrom parallel detectCores +#' @importFrom parallel detectCores makeCluster #' @importFrom stats dunif runif dweibull #' @importFrom utils capture.output #' @importFrom assertthat assert_that #' @importFrom cli cli_alert_danger cli_alert_warning #' @importFrom glue glue +#' @importFrom pbmcapply pbmclapply #' @export accept_reject <- function(n = 1L, @@ -238,23 +238,20 @@ accept_reject <- rep(n_per_core, n_cores - remainder), rep(n_per_core + 1L, remainder) ) - capture.output( r <- unlist(pbmcapply::pbmclapply( X = n_each_core, FUN = one_step, mc.cores = n_cores ))) - } else if (parallel && .Platform$OS.type == "windows") { - n_cores <- parallel::detectCores() - cl <- parallel::makeCluster(n_cores) + } else if(parallel && .Platform$OS.type == "windows"){ + cl <- makeCluster(detectCores()) capture.output( r <- unlist(parallel::parLapply( - cl = cl, X = n_each_core, fun = one_step ))) - parallel::stopCluster(cl) + stopCluster(cl) } else { r <- one_step(n) } diff --git a/tests/testthat/test-time-parallel.R b/tests/testthat/test-time-parallel.R index ea76e76..40794c8 100644 --- a/tests/testthat/test-time-parallel.R +++ b/tests/testthat/test-time-parallel.R @@ -26,6 +26,6 @@ simulation <- function(n, parallel = FALSE){ # Retornar o resultado return(x) } -simulation(n = 3e6L, parallel = FALSE) +simulation(n = 3e6, parallel = FALSE) -simulation(n = 3e6L, parallel = TRUE) +simulation(n = 3e6, parallel = TRUE)