getting-started-parfor.Rmd
# Basic usage - single arguments
misc::parfor(function(x) x^2, 1:10)
## | | | 0% | |======= | 10% | |============== | 20% | |===================== | 30% | |============================ | 40% | |=================================== | 50% | |========================================== | 60% | |================================================= | 70% | |======================================================== | 80% | |=============================================================== | 90% | |======================================================================| 100%
## [1] 1 4 9 16 25 36 49 64 81 100
# Parallel with 2 cores
misc::parfor(function(x) x^2, 1:10, cl = 2)
## | | | 0% | |======= | 10% | |============== | 20% | |===================== | 30% | |============================ | 40% | |=================================== | 50% | |========================================== | 60% | |================================================= | 70% | |======================================================== | 80% | |=============================================================== | 90% | |======================================================================| 100%
## [1] 1 4 9 16 25 36 49 64 81 100
# Multiple arguments using do.call style
args_list <- list(list(x=1, y=2), list(x=3, y=4), list(x=5, y=6))
misc::parfor(function(x, y) x + y, args_list, use_do_call = TRUE)
## | | | 0% | |======================= | 33% | |=============================================== | 67% | |======================================================================| 100%
## [1] 3 7 11
# With additional fixed arguments
misc::parfor(function(x, power) x^power, 1:5, power = 3)
## | | | 0% | |============== | 20% | |============================ | 40% | |========================================== | 60% | |======================================================== | 80% | |======================================================================| 100%
## [1] 1 8 27 64 125
# With error handling
misc::parfor(function(x) if(x==3) stop("error") else x^2, 1:5,
errorhandling = "remove")
## | | | 0% | |============== | 20% | |============================ | 40% | |========================================== | 60% | |======================================================== | 80% | |======================================================================| 100%
## [1] 1 4 16 25