sourcetracker icon indicating copy to clipboard operation
sourcetracker copied to clipboard

Parallelisation possible?

Open susheelbhanu opened this issue 3 years ago • 1 comments

Hi,

As asked in #3, is it possible to parallelise this on a linux machine?

Thank you, Susheel

susheelbhanu avatar Dec 17 '21 14:12 susheelbhanu

use R packages?

library(doParallel)
library(foreach)

# Set the number of cores for parallel computation
num_cores <- 4
registerDoParallel(cores = num_cores)

# Create an empty list for results
results <- vector("list", nrow(alphas))
rmse <- numeric(nrow(alphas))
rmse.sem <- numeric(nrow(alphas))

# Use foreach loop for parallel processing
foreach(i = 1:nrow(alphas), .combine = "c") %dopar% {
  cat(sprintf('Loop %d of %d, alpha1=%f, alpha2=%f ', i, nrow(alphas), alphas[i, 1], alphas[i, 2]))
  if (verbosity > 2) cat('\n')
  
  # Perform the evaluation and store the results
  results[[i]] <- eval.fit(otus, envs, individual.samples = individual.samples,
                           ntrials = ntrials, rarefaction_depth = rarefaction_depth,
                           alpha1 = alphas[i, 1], alpha2 = alphas[i, 2], beta = beta, verbosity = verbosity - 1, ...)
  rmse[i] <- results[[i]]$rmse
  rmse.sem[i] <- results[[i]]$rmse.sem
  
  if (verbosity > 0) cat(sprintf('RMSE = %.3f +/- %.3f\n', rmse[i], rmse.sem[i]))
}

# Stop parallel computation and unregister the parallel environment
stopImplicitCluster()

Jiangjao avatar Oct 07 '23 02:10 Jiangjao