Using doParallel
will allow you to combine the results using rbind
:
library(doParallel)
N=100000
nworkers <- detectCores()
cl <- makePSOCKcluster(nworkers)
registerDoParallel(cl)
step <- ceiling(N/nworkers)
BestLoops <- foreach(start_from=seq(1, N, step),
.combine='rbind') %dopar% {
mat <- matrix(0,step,2)
i <- 1
for(j in start_from:(start_from+step-1)) {
mat[i,]=c(j,3)
i <- i +1
}
mat
}
You just perform the code on each of the workers but with each using a smaller number of iterations.