Skip to content

Commit

Permalink
Remove export of with_progress and format vignette.
Browse files Browse the repository at this point in the history
  • Loading branch information
astamm committed Jan 6, 2025
1 parent b4c5289 commit 3df3189
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Description: A flexible permutation framework for making
data or density-valued data.
License: GPL (>= 3)
Encoding: UTF-8
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
Imports:
cli,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,5 @@ importFrom(furrr,future_map)
importFrom(furrr,future_map_dbl)
importFrom(magrittr,"%>%")
importFrom(progressr,progressor)
importFrom(progressr,with_progress)
importFrom(tibble,tibble)
useDynLib(flipr, .registration = TRUE)
2 changes: 1 addition & 1 deletion R/flipr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' @useDynLib flipr, .registration = TRUE
#' @importFrom furrr future_map future_map_dbl
#' @import ggplot2
#' @importFrom progressr with_progress progressor
#' @importFrom progressr progressor
#' @importFrom R6 R6Class
#' @importFrom Rcpp sourceCpp
#' @import rlang
Expand Down
1 change: 1 addition & 0 deletions flipr.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: bf8dffc9-fb15-4cc1-b2cb-51260a920c79

RestoreWorkspace: No
SaveWorkspace: No
Expand Down
1 change: 0 additions & 1 deletion vignettes/exactness.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ stat_assignments <- list(delta = 1)
nperms <- 20
alpha <- 0.05
progressr::with_progress({
p <- progressr::progressor(steps = length(sim) / 10) # progress bar set up
ii <- 1
Expand Down
50 changes: 38 additions & 12 deletions vignettes/parallelization.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,28 @@ time_without_parallelization <- df_parallelization$time_without_par
time_with_parallelization <- df_parallelization$time_par
```

The [**flipr**](https://lmjl-alea.github.io/flipr/) package uses functions contained in the [**furrr**](https://future.futureverse.org/index.html) package for parallel processing. The setting of parallelization has to be done on the user side. We illustrate here how to achieve asynchronous evaluation. We use the [**future**](https://future.futureverse.org/index.html) package to set the plan, the **parallel** package to define a default cluster, and the [**progressr**](https://progressr.futureverse.org/index.html) package to report progress updates.

## Computation without parallel processing

To show the benefit of parallel processing, we compare here the processing times necessary to evaluate a grid with a plausibility function. First, here is the computation without parallelization.
The [**flipr**](https://lmjl-alea.github.io/flipr/) package uses functions
contained in the [**furrr**](https://future.futureverse.org/index.html) package
for parallel processing. The setting of parallelization has to be done on the
user side. We illustrate here how to achieve asynchronous evaluation. We use the
[**future**](https://future.futureverse.org/index.html) package to set the plan,
the **parallel** package to define a default cluster, and the
[**progressr**](https://progressr.futureverse.org/index.html) package to report
progress updates.

By setting the desired number of cores, we define the number of background R
sessions that will be used to evaluate expressions in parallel. This number is
used to set the multisession plan with the function `future::plan()` and to
define a default cluster with `parallel::setDefaultCluster()`. Then, to enable
the visualization of evaluation progress, we can put the code in the
`progressr::with_progress()` function, or more simply set it for all the
following code with the `progressr::handlers()` function. After these settings,
[**flipr**](https://lmjl-alea.github.io/flipr/) functions can be used, as shown
in this example.

To show the benefit of parallel processing, we compare here the processing times
necessary to evaluate a grid with a plausibility function. First, here is the
computation without parallelization.

```{r, eval=FALSE}
set.seed(1234)
Expand Down Expand Up @@ -63,7 +80,15 @@ time_without_parallelization

## Computation with parallel processing

By setting the desired number of cores, we define the number of background R sessions that will be used to evaluate expressions in parallel. This number is used to set the multisession plan with the function `future::plan()` and to define a default cluster with `parallel::setDefaultCluster()`. Then, to enable the visualization of evaluation progress, we can put the code in the `progressr::with_progress()` function, or more simply set it for all the following code with the `progressr::handlers()` function. After these settings, [**flipr**](https://lmjl-alea.github.io/flipr/) functions can be used, as shown in this example.
By setting the desired number of cores, we define the number of background R
sessions that will be used to evaluate expressions in parallel. This number is
used to set the multisession plan with the function `future::plan()` and to
define a default cluster with `parallel::setDefaultCluster()`. Then, to enable
the visualization of evaluation progress, we can put the code in the
`progressr::with_progress()` function, or more simply set it for all the
following code with the `progressr::handlers()` function. After these settings,
[**flipr**](https://lmjl-alea.github.io/flipr/) functions can be used, as shown
in this example.

```{r, eval=FALSE}
ncores <- 4
Expand Down Expand Up @@ -106,21 +131,22 @@ time_with_parallelization <- tictoc::toc()
parallel::stopCluster(cl)
```

It is good practice to shut down the workers with the `parallel::stopCluster()` function at the end of the code.
It is good practice to shut down the workers with the `parallel::stopCluster()`
function at the end of the code.

```{r}
time_with_parallelization
```

This experiment proves that we can save a lot of computation time when using parallel processing, as we gained approximately 33 seconds in this example to evaluate the plausibility function.
This experiment proves that we can save a lot of computation time when using
parallel processing, as we gained approximately 33 seconds in this example to
evaluate the plausibility function.

Finally, to return to a sequential plan with no progress updates, the following code can be used.
Finally, to return to a sequential plan with no progress updates, the following
code can be used.

```{r, eval=FALSE}
future::plan(sequential)
parallel::setDefaultCluster(NULL)
progressr::handlers(global = FALSE)
```



0 comments on commit 3df3189

Please sign in to comment.