HeyHoLetsGo
HeyHoLetsGo

Reputation: 147

Parallelize custom function

I've created a loop containing a matrix that works fine. The function tkas a matrix and as imput, calcualte correlation between the amtrix columns and then saves the output in the specified folde.r

mtx <- is a matrix file
out <- output folder
methods <- c("pearson","spearman","kendall")
for(method in methods){
drugCorrelations(methods,mtx,out.folder)
}

But I've not been able to parallelize it, this is my best try

cl <- parallel::makeCluster(3)
doParallel::registerDoParallel(cl)

foreach(i=1:3) %dopar% {
  drugCorrelations(methods[i],mtx,out.folder)
}
parallel::stopCluster(cl)

Any suggestion on how to parelleize this. I would prefer to keep the function as it is and being able to execute this function three times with the different methods.

Thanks

Upvotes: 0

Views: 163

Answers (1)

alan ocallaghan
alan ocallaghan

Reputation: 3038

What has not worked in your attempt so far? See the code below for a working example using mclapply:

library(parallel)
mclapply(
  methods,
  function(method) cor(rnorm(100), rnorm(100), method=method),
  mc.cores = 3
)

Upvotes: 1

Related Questions