0

I wanted to do a permutation test and this is the structure of the code(with dummy data). The permutations will run in parallel and want to count how many times the generated matrix fails the test. (see code block 2). But this is slow in the block 2 because it works in a single processor. I want to write a .combine function to go with the foreach() function but I don't know how to give the input parameters(cc and a matrices)

library(foreach)
library(parallel)

#matrix to be populated
cc<-matrix(0,nrow = 10,ncol = 10)

#fixed matrix
a<-matrix(runif(100), ncol=10)

iters<-1e3
cl<-makeCluster(8)
registerDoParallel(cl)


ls<-foreach(icount(iters)) %dopar% {
#generated matrix
  b<-matrix(runif(100), ncol=10)
  b
 }


stopCluster(cl)

This part is the problem. I want to count how many times each element of matrix b is greater than the fixed matrix a and add the count for each element to cc matrix. If i can define a .combine function this should execute when each matrix is generated.

for(b in ls){
  for(i in 1:dim(a)[1]) {
    for(j in 1:dim(a)[2]) {
      if(a[i,j] < b[i,j]) cc[i,j]=cc[i,j] + 1

     }
   }
}

cc
ChathuraG
  • 160
  • 2
  • 11

1 Answers1

1

Regarding, "I don't know how to give the input parameters(cc and a matrices)"

I think .export can resolve your problem as document mentioned.

BTW, any variables in the for loop will be automatically exported to slave processors so in your example, the matrix a, b and cc can be operated as serial code under %dopar%.

.export
character vector of variables to export. This can be useful when accessing a variable that isn't defined in the current environment. The default value in NULL.

Another example of foreach can be found in here.


Regarding, "If i can define a .combine function this should execute when each matrix is generated."

Yes, you can define a function as normal R function and pass to .combine=your_func which will be called after the slave process back.

An example in here.

Community
  • 1
  • 1
Patric
  • 2,063
  • 17
  • 18