-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b978a5c
commit ac03900
Showing
9 changed files
with
211 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ Suggests: | |
crul, | ||
Rarr, | ||
vcr (>= 0.6.0), | ||
foreach, | ||
doParallel, | ||
pbapply, | ||
parallel, | ||
future, | ||
bench |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
--- | ||
title: "Read and write in parallel" | ||
output: rmarkdown::html_vignette | ||
vignette: > | ||
%\VignetteIndexEntry{Read and write in parallel} | ||
%\VignetteEngine{knitr::rmarkdown} | ||
%\VignetteEncoding{UTF-8} | ||
--- | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>", | ||
out.width = "100%" | ||
) | ||
devtools::load_all() | ||
``` | ||
|
||
By default, reads and writes are performed sequentially (i.e., not in parallel). | ||
Users can opt-in to parallel read/write functionality via `options`. | ||
|
||
## Simulate slow operations | ||
|
||
```{r} | ||
SlowDirectoryStore <- R6::R6Class("SlowDirectoryStore", | ||
inherit = DirectoryStore, | ||
public = list( | ||
get_item = function(key) { | ||
Sys.sleep(0.5) # Simulate a slow read. | ||
return(super$get_item(key)) | ||
}, | ||
set_item = function(key, value) { | ||
Sys.sleep(0.5) # Simulate a slow write. | ||
return(super$set_item(key, value)) | ||
} | ||
) | ||
) | ||
``` | ||
|
||
## Read in parallel | ||
|
||
Provide an integer >= 2 to the option to use forking-based parallelism. | ||
This value will be passed to the `cl` parameter of `pbapply::pblapply`. | ||
|
||
```{r} | ||
options(pizzarr.parallel_read_enabled = 4) | ||
root <- pizzarr_sample("dog.ome.zarr") | ||
store <- SlowDirectoryStore$new(root) | ||
zarr_arr <- zarr_open(store = store, path = "/0") | ||
arr <- zarr_arr$get_item("...")$data | ||
sum(arr) | ||
``` | ||
|
||
## Write in parallel | ||
|
||
```{r} | ||
options(pizzarr.parallel_write_enabled = 4) | ||
root <- pizzarr_sample("dog.ome.zarr") | ||
store <- SlowDirectoryStore$new(root) | ||
zarr_arr <- zarr_open(store = store, path = "/0") | ||
arr <- zarr_arr$get_item("...")$data | ||
zarr_arr$set_item("...", arr * 2.0) | ||
doubled_arr <- zarr_arr$get_item("...")$data | ||
sum(doubled_arr) | ||
``` | ||
|
||
## Parallel operations with future backend | ||
|
||
To use the `future` backend for `pbapply`, set the value of the option to the string `"future"`. | ||
|
||
Cluster-based: | ||
|
||
```{r} | ||
options(pizzarr.parallel_read_enabled = "future") | ||
cl <- parallel::makeCluster(2) | ||
future::plan(future::cluster, workers = cl) | ||
root <- pizzarr_sample("dog.ome.zarr") | ||
store <- SlowDirectoryStore$new(root) | ||
zarr_arr <- zarr_open(store = store, path = "/0") | ||
arr <- zarr_arr$get_item("...")$data | ||
sum(arr) | ||
parallel::stopCluster(cl) | ||
``` | ||
|
||
Multisession-based: | ||
|
||
```{r} | ||
options(pizzarr.parallel_read_enabled = "future") | ||
future::plan(future::multisession, workers = 4) | ||
root <- pizzarr_sample("dog.ome.zarr") | ||
store <- SlowDirectoryStore$new(root) | ||
zarr_arr <- zarr_open(store = store, path = "/0") | ||
arr <- zarr_arr$get_item("...")$data | ||
sum(arr) | ||
``` | ||
|
||
## Sequential operations | ||
|
||
To return to sequential mode, run: | ||
|
||
```{r} | ||
options( | ||
pizzarr.parallel_read_enabled = FALSE, | ||
pizzarr.parallel_write_enabled = FALSE | ||
) | ||
``` | ||
|
||
## Disable progress bar | ||
|
||
Parallel operations are implemented with `pbapply`. | ||
To disable the progress bar, run: | ||
|
||
```{r} | ||
pbapply::pboptions(type = "none") | ||
``` | ||
|
||
To re-enable, run: | ||
|
||
```{r} | ||
pbapply::pboptions(type = "timer") | ||
``` |