rsnt_translations
56,426
edits
No edit summary |
(Marked this version for translation) |
||
Line 221: | Line 221: | ||
For more on submitting jobs, see the [[Running jobs]] page. | For more on submitting jobs, see the [[Running jobs]] page. | ||
=== doParallel and foreach === | === doParallel and foreach === <!--T:39--> | ||
====Usage==== | ====Usage==== | ||
Foreach can be considered as a unified interface for all backends (i.e. doMC, doMPI, doParallel, doRedis, etc.). It works on all platforms, assuming that the backend works. doParallel acts as an interface between foreach and the parallel package and can be loaded alone. There are some known efficiency issues when using foreach to run a very large number of very small tasks. Therefore, keep in mind that the following code is not the best example of an optimized use of the foreach() call but rather that the function chosen was kept at a minimum for demonstration purposes. | Foreach can be considered as a unified interface for all backends (i.e. doMC, doMPI, doParallel, doRedis, etc.). It works on all platforms, assuming that the backend works. doParallel acts as an interface between foreach and the parallel package and can be loaded alone. There are some known efficiency issues when using foreach to run a very large number of very small tasks. Therefore, keep in mind that the following code is not the best example of an optimized use of the foreach() call but rather that the function chosen was kept at a minimum for demonstration purposes. | ||
<!--T:48--> | |||
You must register the backend by feeding it the number of cores available. If the backend is not registered, foreach will assume that the number of cores is 1 and will proceed to go through the iterations serially. | You must register the backend by feeding it the number of cores available. If the backend is not registered, foreach will assume that the number of cores is 1 and will proceed to go through the iterations serially. | ||
Line 232: | Line 232: | ||
# to register the backend; | # to register the backend; | ||
# to call foreach() by keeping it on the same line as the %do% (serial) or %dopar% operator. | # to call foreach() by keeping it on the same line as the %do% (serial) or %dopar% operator. | ||
====Running==== <!--T:40--> | |||
<!--T:49--> | |||
1. Place your R code in a script file, in this case the file is called ''test_foreach.R''. | 1. Place your R code in a script file, in this case the file is called ''test_foreach.R''. | ||
Line 245: | Line 245: | ||
library(doParallel) # | library(doParallel) # | ||
<!--T:50--> | |||
# a very simple function | # a very simple function | ||
test_func <- function(var1, var2) { | test_func <- function(var1, var2) { | ||
Line 250: | Line 251: | ||
} | } | ||
<!--T:51--> | |||
# we will iterate over two sets of values, you can modify this to explore the mechanism of foreach | # we will iterate over two sets of values, you can modify this to explore the mechanism of foreach | ||
var1.v = c(1:8) | var1.v = c(1:8) | ||
var2.v = seq(0.1, 1, length.out = 8) | var2.v = seq(0.1, 1, length.out = 8) | ||
<!--T:52--> | |||
# Use the environment variable SLURM_NTASKS to set the number of cores. | # Use the environment variable SLURM_NTASKS to set the number of cores. | ||
# This is for SLURM. Replace SLURM_NTASKS by the proper variable for your system. | # This is for SLURM. Replace SLURM_NTASKS by the proper variable for your system. | ||
Line 259: | Line 262: | ||
ncores = Sys.getenv("SLURM_NTASKS") | ncores = Sys.getenv("SLURM_NTASKS") | ||
<!--T:53--> | |||
registerDoParallel(cores=ncores)# Shows the number of Parallel Workers to be used | registerDoParallel(cores=ncores)# Shows the number of Parallel Workers to be used | ||
print(ncores) # this how many cores are available, and how many you have requested. | print(ncores) # this how many cores are available, and how many you have requested. | ||
getDoParWorkers()# you can compare with the number of actual workers | getDoParWorkers()# you can compare with the number of actual workers | ||
<!--T:54--> | |||
# be careful! foreach() and %dopar% must be on the same line! | # be careful! foreach() and %dopar% must be on the same line! | ||
foreach(var1=var1.v, .combine=rbind) %:% foreach(var2=var2.v, .combine=rbind) %dopar% {test_func(var1=var1, var2=var2)} | foreach(var1=var1.v, .combine=rbind) %:% foreach(var2=var2.v, .combine=rbind) %dopar% {test_func(var1=var1, var2=var2)} | ||
Line 283: | Line 288: | ||
#SBATCH --mail-type=ALL # send an email in all cases (job started, job ended, job aborted) | #SBATCH --mail-type=ALL # send an email in all cases (job started, job ended, job aborted) | ||
<!--T:55--> | |||
module load r/3.4.3 | module load r/3.4.3 | ||
export R_LIBS=~/local/R_libs/ | export R_LIBS=~/local/R_libs/ |