title: Is armadillo solve() thread safe?
body: <p>In my code I have loop in which I construct and over determined linear system and try to solve it:</p>
  
  <pre><code>#pragma omp parallel for
  for (int i = 0; i &lt; n[0]+1; i++) {
      for (int j = 0; j &lt; n[1]+1; j++) {
          for (int k = 0; k &lt; n[2]+1; k++) {
              arma::mat A(max_points, 2);
              arma::mat y(max_points, 1);
              // initialize A and y
  
              arma::vec solution = solve(A,y);
          }
      }
  }
  </code></pre>
  
  <p>Sometimes, quite randomly the program hangs or the results in the solution vector are NaN. And if I put do this:</p>
  
  <pre><code>arma::vec solution;
  #pragma omp critical 
  {
      solution = solve(weights*A,weights*y);
  }
  </code></pre>
  
  <p>then these problem don't seem to happen anymore.</p>
  
  <p>When it hangs, it does so because some threads are waiting at the OpenMP barrier:</p>
  
  <pre><code>Thread 2 (Thread 0x7fe4325a5700 (LWP 39839)):
  #0  0x00007fe44d3c2084 in gomp_team_barrier_wait_end () from /usr/lib64/gcc-4.9.2/lib64/gcc/x86_64-redhat-linux-gnu/4.9.2/libgomp.so.1
  #1  0x00007fe44d3bf8c2 in gomp_thread_start () at ../.././libgomp/team.c:118
  #2  0x0000003f64607851 in start_thread () from /lib64/libpthread.so.0
  #3  0x0000003f642e890d in clone () from /lib64/libc.so.6
  </code></pre>
  
  <p>And the other threads are stuck inside Armadillo: </p>
  
  <pre><code>Thread 1 (Thread 0x7fe44afe2e60 (LWP 39800)):
  #0  0x0000003ee541f748 in dscal_ () from /usr/lib64/libblas.so.3
  #1  0x00007fe44c0d3666 in dlarfp_ () from /usr/lib64/atlas/liblapack.so.3
  #2  0x00007fe44c058736 in dgelq2_ () from /usr/lib64/atlas/liblapack.so.3
  #3  0x00007fe44c058ad9 in dgelqf_ () from /usr/lib64/atlas/liblapack.so.3
  #4  0x00007fe44c059a32 in dgels_ () from /usr/lib64/atlas/liblapack.so.3
  #5  0x00007fe44f09fb3d in bool arma::auxlib::solve_ud&lt;double, arma::Glue&lt;arma::Mat&lt;double&gt;, arma::Mat&lt;double&gt;, arma::glue_times&gt; &gt;(arma::Mat&lt;double&gt;&amp;, arma::Mat&lt;double&gt;&amp;, arma::Base&lt;double, arma::Glue&lt;arma::Mat&lt;double&gt;, arma::Mat&lt;double&gt;, arma::glue_times&gt; &gt; const&amp;) () at /usr/include/armadillo_bits/lapack_wrapper.hpp:677
  #6  0x00007fe44f0a0f87 in arma::Col&lt;double&gt;::Col&lt;arma::Glue&lt;arma::Glue&lt;arma::Mat&lt;double&gt;, arma::Mat&lt;double&gt;, arma::glue_times&gt;, arma::Glue&lt;arma::Mat&lt;double&gt;, arma::Mat&lt;double&gt;, arma::glue_times&gt;, arma::glue_solve&gt; &gt;(arma::Base&lt;double, arma::Glue&lt;arma::Glue&lt;arma::Mat&lt;double&gt;, arma::Mat&lt;double&gt;, arma::glue_times&gt;, arma::Glue&lt;arma::Mat&lt;double&gt;, arma::Mat&lt;double&gt;, arma::glue_times&gt;, arma::glue_solve&gt; &gt; const&amp;) ()
  at /usr/include/armadillo_bits/glue_solve_meat.hpp:39
  </code></pre>
  
  <p>As you can see from the stacktrace my version of Armadillo uses atlas. And according to this documentation atlas seems to be thread safe: <a href="ftp://lsec.cc.ac.cn/netlib/atlas/faq.html#tsafe">ftp://lsec.cc.ac.cn/netlib/atlas/faq.html#tsafe</a></p>
  
  <p><strong>Update 9/11/2015</strong></p>
  
  <p>I finally got some time to run more tests, based on the suggestions of Vladimir F.</p>
  
  <p>When I compile armadillo with ATLAS's BLAS, I'm still able to reproduce then hangs and the NaNs. When it hangs, the only thing that changes in the stacktrace is the call to BLAS:</p>
  
  <pre><code>#0  0x0000003fa8054718 in ATL_dscal_xp1yp0aXbX@plt () from /usr/lib64/atlas/libatlas.so.3
  #1  0x0000003fb05e7666 in dlarfp_ () from /usr/lib64/atlas/liblapack.so.3
  #2  0x0000003fb0576a61 in dgeqr2_ () from /usr/lib64/atlas/liblapack.so.3
  #3  0x0000003fb0576e06 in dgeqrf_ () from /usr/lib64/atlas/liblapack.so.3
  #4  0x0000003fb056d7d1 in dgels_ () from /usr/lib64/atlas/liblapack.so.3
  #5  0x00007ff8f3de4c34 in void arma::lapack::gels&lt;double&gt;(char*, int*, int*, int*, double*, int*, double*, int*, double*, int*, int*) () at /usr/include/armadillo_bits/lapack_wrapper.hpp:677
  #6  0x00007ff8f3de1787 in bool arma::auxlib::solve_od&lt;double, arma::Glue&lt;arma::Mat&lt;double&gt;, arma::Mat&lt;double&gt;, arma::glue_times&gt; &gt;(arma::Mat&lt;double&gt;&amp;, arma::Mat&lt;double&gt;&amp;, arma::Base&lt;double, arma::Glue&lt;arma::Mat&lt;double&gt;, arma::Mat&lt;double&gt;, arma::glue_times&gt; &gt; const&amp;) () at /usr/include/armadillo_bits/auxlib_meat.hpp:3434
  </code></pre>
  
  <p>Compiling without ATLAS, only with netlib BLAS and LAPACK, I was able to reproduce the NaNs but not the hangs.</p>
  
  <p>In both cases, surrounding <code>solve()</code> with <code>#pragma</code> omp critical I have no problems at all</p>