title: `stopCluster` in `R snow` freeze
body: <p>I was running a Monte Carlo simulation in cluster computers with <code>snow</code> and <code>R</code>. All went well until <code>R</code> hit the line of <code>stopCluster</code>, at which <code>R</code> freeze and finally exceeded the wall time. I do not see the problem of <code>stopCluster</code>.</p>
  
  <p>The following is a simplified version my <code>R</code> script.</p>
  
  <pre><code>simu &lt;- function(rep_worker, n_used) {
    theta_simu &lt;- c()
    for (i in 1 : rep_worker) {
      theta_simu[i] &lt;- mean(rnorm(n_used))
    }
    theta_simu
  }
  library(Rmpi)
  library(snow)
  np &lt;- mpi.universe.size() - 1
  cl &lt;- makeCluster(np, type = "MPI")
  ## go go go
  n_used &lt;- 1e4
  rep_worker_list &lt;- rep(1, np) # each worker do one `simu`
  theta_cluster &lt;- clusterApply(cl, rep_worker_list, simu, n_used)
  theta_cluster
  stopCluster(cl)
  mpi.exit()
  </code></pre>
  
  <p>The above script was saved as <code>test_stack.R</code> under the directory <code>monte-carlo/R</code>. The <code>pbs</code> script I sent to the server is the following.</p>
  
  <pre><code>#!/bin/bash
  
  #PBS -N test
  #PBS -l walltime=00:30:00
  #PBS -l nodes=3:ppn=8
  #PBS -l pvmem=8gb
  
  module load R/3.3.1
  module load openmpi/gcc/2.0.0
  cd monte-carlo/R
  
  # For snow jobs, use 'mpiexec -n 1'
  mpiexec -n 1 R CMD BATCH test_stack.R
  </code></pre>
  
  <p>A part of the <code>Rout</code> file was listed below. It stop at <code>stopCluster()</code>.</p>
  
  <pre><code>&gt; simu &lt;- function(rep_worker, n_used) {
  +   theta_simu &lt;- c()
  +   for (i in 1 : rep_worker) {
  +     theta_simu[i] &lt;- mean(rnorm(n_used))
  +   }
  +   theta_simu
  + }
  &gt; library(Rmpi)
  &gt; library(snow)
  &gt; np &lt;- mpi.universe.size() - 1
  &gt; cl &lt;- makeCluster(np, type = "MPI")
      23 slaves are spawned successfully. 0 failed.
  &gt; ## go go go
  &gt; n_used &lt;- 1e4
  &gt; rep_worker_list &lt;- rep(1, np) # each worker do one `simu`
  &gt; theta_cluster &lt;- clusterApply(cl, rep_worker_list, simu, n_used)
  &gt; theta_cluster
  [[1]]
  [1] 5.54539e-05
  
  [[2]]
  [1] 0.0009270881
  
  ... (I deleted the rest to save space)
  
  &gt; stopCluster(cl)
  </code></pre>