title: Filling Armadillo sparse matrix in parallel with OpenMP
body: <p>I need to represent a very large block-bidiagonal matrix as a sparse matrix in Armadillo. I have all the values of all the blocks into two <code>arma::field&lt;arma::mat&gt;</code> lists, so I only need to cast them into a <code>arma::sp_mat</code> object.</p>
  
  <p>I can do it:</p>
  
  <ul>
  <li>sequentially (slower)</li>
  <li>in parallel by using <code>arma::mat</code> and then converting the dense matrix into a sparse matrix (faster) </li>
  </ul>
  
  <p>I would like to fill the <code>arma::sp_mat</code> directly in parallel, which should be the fastest route. I tried with code similar to the snippet below (using Rcpp), but it crashes and I'm guessing it's because of the way sparse matrices are grown in Armadillo. Is there a way to make it work? </p>
  
  <pre><code>#include &lt;RcppArmadillo.h&gt;
  #include &lt;omp.h&gt;
  
  arma::sp_mat test_ll(int blocks=10, int rowcol_per_block=250){
    arma::mat mat1 = arma::randn() * arma::ones(rowcol_per_block, rowcol_per_block);
  
    arma::sp_mat tester(blocks * rowcol_per_block, 
                        blocks * rowcol_per_block);
  
  #pragma omp parallel for
    for(int i=0; i&lt;blocks; i++){
      tester.submat( i*rowcol_per_block, i*rowcol_per_block, 
                     (i+1)*rowcol_per_block-1, (i+1)*rowcol_per_block-1) = mat1;
  
    }
    return tester;
  }
  </code></pre>
  
  <p>I tried pre-filling all the blocks of a appropriately-sized <code>sp_mat</code> and then <em>changing</em> them in parallel, but that also crashes.</p>
  
  <p><strong>Update</strong> 
  No difference in using the <a href="http://arma.sourceforge.net/docs.html#SpMat" rel="nofollow noreferrer">#2 constructor</a> to initialize the <code>sp_mat</code> (providing non-zero locations and filling them with 1, and then changing them in parallel). That also fails. </p>