Parallelization in R

RaukR 2023 • Advanced R for Bioinformatics

Sebastian DiLorenzo

27-Jun-2023

Overview

  • parallelization
  • future
  • plans
    • sequential
    • multisession/multicluster
    • cluster

parallelization



  • Save time by doing atomic tasks in parallel


  • Divide tasks or datasets into smaller pieces


  • Can bottleneck if tasks are directly dependent

R-package: future

Other packages decide the parallelization method during development. With future the code is the same and the USER decides parallelization method.

  • Very simple
  • Uniform code, no matter the strategy
  • User defined parallelization
  • Unblocked R process during resolving of futures process
  • Works well on multiple architectures


Building block: variable %<-% {expression(s)}

Plans

plan(sequential)


Building block: variable %<-% {expression(s)}

future::plan(sequential)

a %<-% {
  Sys.sleep(3)
  a <- 1
}
b %<-% {
  Sys.sleep(3)
  b <- 2
}

a + b
[1] 3
   user  system elapsed 
  0.061   0.004   6.076 

plan(multisession) & plan(multicore)

plan(multicore)

a %<-% {
  Sys.sleep(3)
  a <- 1
}
b %<-% {
  Sys.sleep(3)
  b <- 2
}

a + b
[1] 3
   user  system elapsed 
  0.129   0.072   3.154 
availableCores()
system 
     6 

plan(multisession) & plan(multicore)

plan(multicore)

a %<-% {
  Sys.sleep(3)
  a <- 1
}
b %<-% {
  Sys.sleep(3)
  b <- 2
}
c %<-% {
  Sys.sleep(3)
  c <- 3
}
...
k %<-% {
  Sys.sleep(3)
  e <- 5
}

a + b + c + d + e + f + g + h + i + j + k
[1] 60
   user  system elapsed 
  0.701   0.298   6.392 

plan(cluster)

  • To some degree a wrapper around parallel::makeCluster()
  • For example:
    • 3 connected nodes (computers) named n1:n3
    • Each with 16 CPUs
plan(cluster, workers = c("n1", "n2", "n3"))

Specialized R package for interfacing with common HPC job schedulers exists: future.batchtools

Thank you! Questions?

         _                  
platform x86_64-pc-linux-gnu
os       linux-gnu          
major    4                  
minor    2.3                

2023 • SciLifeLabNBISRaukR