###########################################################
require(foreach)
require(multicore)
require(doMC)

Ncore <- 6
registerDoMC(cores=Ncore)
Ncol <- 1200
Nrow <- 1200

do.par.test.called.func <- function(Mat1, Mat2){
  Mat3 <- Mat1 + Mat2
  for(i in 1:nrow(Mat3)){
    for (j in 1:ncol(Mat3)){
      Mat3[i,j] <- sqrt(abs(Mat3[i,j]))
    }
  }
  Mat3
}

do.par.test.func <- function(Nrow, Ncol, Ncore){
  x.mat <- matrix(rnorm(Ncol * Nrow), Nrow, Ncol)
  y.mat <- matrix(rnorm(Ncol * Nrow), Nrow, Ncol)
  N <- ceiling(Nrow/Ncore)
  foreach(i = 0:(Ncore-1), .combine='rbind') %dopar%
    do.par.test.called.func(x.mat[(i * N + 1):((i+1) * N),], y.mat[(i *
    N + 1):((i+1) * N),])
}

#
for(i in 1:10){print(system.time(do.par.out <- do.par.test.func(Nrow, Ncol, Ncore)))}
############################################
