Paralle computing

Hello everyone,
I am currently working on parallel computing. When I use core=1,2,3,4 it takes almost the same amount of time even though it was supposed to take approximately one-fourth of the time when I used core=4 compared to core=1. But when I use core=5,6,... it takes more time than before. Please note that my 2022 Dell XPS has 10 cores. Can anyone help me out? The sample code is shown below.

library(parallel)
library(doParallel)
library(foreach)

core=3
cl <- parallel::makeCluster(core, setup_strategy = "sequential")
# Activate cluster for foreach library
registerDoParallel(cl)

unregister_dopar <- function() {
  env <- foreach:::.foreachGlobals
  rm(list=ls(name=env), pos=env)
}

result=foreach(i = 1:iter, .packages = c("VSURF", "MASS")) %dopar%{
...
}

Well, if I try your code with this foreach:

bench::mark(
  result=foreach(i = 1:10) %dopar%{
    Sys.sleep(1)
  }, 
  iterations = 1)

it looks like your cluster creation should work (on my computer, it takes 10s with 1 core and 1 second with 10 cores, as expected). Maybe you can try running the same, to determine if the problem is related to your computer, or related to the code in the loop.

Also my experience is that the futureverse does make parallel code easier, maybe you could consider trying with {doFuture}, and see if that helps.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.