rforeachdoparallelsnow

How to make foreach %doparalell% execute all iterations?


I am setting up a foreach loop with %dopar%. The function inside should be executed n times. However no matter what the function is the function is executed only 6 times. There are no errors. How do I make foreach execute all iterations?

I tried changing the number inside of makePSOCKcluster() and results are the same. Currently I am running makePSOCKcluster(nworkers) with nworkers = 7 ( parallel::detectCores() returns 8 ). I was originally running a complex function inside my loop but I tried substituting the function for something simple like sqrt() and I get the same result. I am working on a mac: R version 3.5.1 (2018-07-02) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS 10.14.2

Using package versions: doParallel_1.0.11 iterators_1.0.10 foreach_1.4.4

suppressPackageStartupMessages( library(foreach) )
suppressPackageStartupMessages( library(doParallel) )

nworkers <- parallel::detectCores() -1 
cl <- makePSOCKcluster(nworkers)
registerDoParallel(cl)

epkgs = c("lubridate","dplyr","tidyr","doParallel","foreach", "Rcpp")
efuns = ls(globalenv())

foreach( i= 1:31, packages = epkgs, .export = efuns) %dopar% { sqrt(i)} 

The result I get is :

[[1]]
[1] 1

[[2]]
[1] 1.414214

[[3]]
[1] 1.732051

[[4]]
[1] 2

[[5]]
[1] 2.236068

[[6]]
[1] 2.44949

While the functions should have returned 31 elements.


Solution

  • Update I was missing a "." in front of packages so it was assuming that was an iterator. 6 packages 6 results.