Skip to content

Commit

Permalink
Update accept_reject.r
Browse files Browse the repository at this point in the history
  • Loading branch information
prdm0 committed Apr 23, 2024
1 parent c2f6f3c commit 39cca42
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-parallel-windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 5 additions & 8 deletions R/accept_reject.r
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-time-parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 39cca42

Please sign in to comment.