0

As a disclaimer, this is my first post, and would I welcome any constructive criticism on appropriate postings in the future.

The input to my function is an array. When I input the array using the %do% option of foreach, I get the desired output array. I have searched through the various foreach threads, and have not found a similar problem. However, when I run the %dopar% option, I only get the input array back in return. Here is the basic outline of my code; I can post the full script if that would be best, but it is quite long. Ultimately, I will be scaling the array to a size where calculations will have to be done in parallel for the sake of time.

#libraries for constructing a parallel for loop
library(foreach)
library(doParallel)
cores <- detectCores()-1
#establish cluster
cl <- makeCluster(cores)
#to register the doparallel backend
registerDoParallel(cl,cores=7)

#input array
Q <- matrix(0,20,20)
Q[1:5,1:5] <- 2

#function in question....
dyn_rec_proto20_parallel <- function(vegmat){
distance <- matrix(0,length(vegmat[1,]),length(vegmat[1,]))
recruitment_prob_mat <- matrix(0,length(vegmat[1,]),length(vegmat[1,]))
foreach(i=1:length(vegmat[1,]),.export = c('IDW_scoring','roll_for_rec','Q','ageing20_parallel' ),)%dopar%{
for(j in 1:length(vegmat[1,])){
  if(vegmat[i,j]==0){ #do a bunch of updates to vegmat
  }
  }
return(vegmat)
}
  • How do you envisage the individual threads combining their changes to your object in parallel? – Jonathan Carroll Jul 18 '17 at 01:26
  • Maybe I do not understand the parallel option; none of the rows of the array which are being assessed in parallel rely on the updated elements of the array. I had though that when each element of i had been assessed it would update that row immediately, then move onto another available index/row. – DeLorean Jul 18 '17 at 02:01
  • You don't understand foreach. You treat it like a for loop but it's actually more similar to lapply, i.e., it requires you to follow principles of functional programming. You should reread the excellent package vignettes. – Roland Jul 18 '17 at 03:52
  • I see, I think I just made a bad assumption there; I figured it worked like a for loop based on its namesake and a quick read through of the function. As there are already excellent apply functions such as parLapply, when is it ideal to use foreach over parLapply? – DeLorean Jul 18 '17 at 05:01
  • You can plug quite a few different parallel backends into foreach. Whether its preferable to parLapply depends on your specific setup and problem. For instance, if you need to generate reproducible pseudo-random numbers you should look into doRNG. – Roland Jul 18 '17 at 06:26
  • I think you could easy do what you want. But, can you provide a small reproducible example? Some minimal data, code and output. – F. Privé Jul 18 '17 at 09:44

0 Answers0