title: doParallel performance on a tensor in R
body: <p>I need to perform some operations on a tensor and I would like make this parallel.
  Consider the following example:</p>
  
  <pre><code># first part without doParallel
  
  N = 8192
  M = 128
  F = 64
  
  ma &lt;- function(x,n=5){filter(x,rep(1/n,n), sides=2)}
  
  
  m &lt;- array(rexp(N*M*F), dim=c(N,M,F))
  
  new_m &lt;- array(0, dim=c(N,M,F))
  
  system.time ( for(i in 1:N) {
          for(j in 1:F) {
              ma_r &lt;- ma(m[i,,j],2)
              ma_r &lt;- c(ma_r[-length(ma_r)], ma_r[(length(ma_r)-1)])
              new_m[i,,j] &lt;- ma_r       
          }
      }
  )
  </code></pre>
  
  <p>This takes around 38 seconds in my laptop.
  The following is with doParallel:</p>
  
  <pre><code># second part with doParallel
  
  library(doParallel)  
  no_cores &lt;- detectCores() - 1  
  cl &lt;- makeCluster(no_cores, type="FORK")  
  registerDoParallel(cl)
  
  
  calcMat &lt;- function(x){
  
      n &lt;- dim(x)[1]
      m &lt;- dim(x)[2]
  
      new_x &lt;- matrix(0, nrow=n, ncol=m)
  
      for(j in 1:ncol(x)) {
          ma_r &lt;- ma(x[,j],2)
          ma_r &lt;- c(ma_r[-length(ma_r)], ma_r[(length(ma_r)-1)])
          new_x[,j] &lt;- ma_r       
      }
  
      return(new_x)
  
  }
  
  
  system.time ( a_list &lt;- foreach(i=1:N) %dopar% {
      m_m &lt;- m[i,,]
      new_m_m &lt;- calcMat(m_m)
   }
  )
  
  
  Y &lt;- array(unlist(a_list), dim = c(nrow(a_list[[1]]), ncol(a_list[[1]]), length(a_list)))
  Y &lt;- aperm(Y, c(3,1,2))
  
  
  stopCluster(cl) 
  </code></pre>
  
  <p>This second one takes around 36 seconds.
  So I do not see any improvement in terms of time.
  Does anyone know what is the reason for that?</p>