title: #pragma omp parallel for schedule crashes my program
body: <p>I am building a plugin for autodesk maya 2013 in c++. I have to solve a set of optimization problems as fast as i can. I am using open MP for this task. the problem is I don't have very much experience with parallel computing. I tried to use:</p>
  
  <pre><code>#pragma omp parallel for schedule (static)
  </code></pre>
  
  <p>on my for loops (without enough understanding of how it's supposed to work) and it worked very well for some of my code, but crashed another portion of my code.</p>
  
  <p>Here is an example of a function that crashes because of the omp directive:</p>
  
  <pre><code>void PlanarizationConstraint::fillSparseMatrix(const Optimizer&amp; opt, vector&lt;T&gt;&amp; elements, double mu)
  {
      int size = 3;
      #pragma omp parallel for schedule (static)
      for(int i = 0; i &lt; opt.FVIc.outerSize(); i++)
      {
          int index = 3*i;
          Eigen::Matrix&lt;double,3,3&gt; Qxyz = Eigen::Matrix&lt;double,3,3&gt;::Zero();
          for(SpMat::InnerIterator it(opt.FVIc,i); it; ++it)
          {
              int face = it.row();
              for(int n = 0; n &lt; size; n++)
              {
                  Qxyz.row(n) += N(face,n)*N.row(face);
                  elements.push_back(T(index+n,offset+face,(1 - mu)*N(face,n)));
              }
          }
  
          for(int n = 0; n &lt; size; n++)
          {
              for(int k = 0; k &lt; size; k++)
              {
                  elements.push_back(T(index+n,index+k,(1-mu)*Qxyz(n,k)));
              }
          }
      }
  
      #pragma omp parallel for schedule (static)
      for(int j = 0; j &lt; opt.VFIc.outerSize(); j++)
      {
          elements.push_back(T(offset+j,offset+j,opt.fvi[j]));
          for(SpMat::InnerIterator it(opt.VFIc,j); it; ++it)
          {
              int index = 3*it.row();
              for(int n = 0; n &lt; size; n++)
              {
                  elements.push_back(T(offset+j,index+n,N(j,n)));
              }
          }
      }
  }
  </code></pre>
  
  <p>And here is an example of code that works very well with those directives (and is faster because of it)</p>
  
  <pre><code>Eigen::MatrixXd Optimizer::OptimizeLLGeneral()
  {
      ConstraintsManager manager;
      SurfaceConstraint surface(1,true);
      PlanarizationConstraint planarization(1,true,3^Nv,Nf);
      manager.addConstraint(&amp;surface);
      manager.addConstraint(&amp;planarization);
      double mu = mu0;
      for(int k = 0; k &lt; iterations; k++)
      {
          #pragma omp parallel for schedule (static)
          for(int j = 0; j &lt; VFIc.outerSize(); j++)
          {
              manager.calcVariableMatrix(*this,j);
          }
          #pragma omp parallel for schedule (static)
          for(int i = 0; i &lt; FVIc.outerSize(); i++)
          {
              Eigen::MatrixXd A = Eigen::Matrix&lt;double, 3, 3&gt;::Zero();
              Eigen::MatrixXd b = Eigen::Matrix&lt;double, 1, 3&gt;::Zero();
              manager.addLocalMatrixComponent(*this,i,A,b,mu);
              Eigen::VectorXd temp = b.transpose();
              Q.row(i) = A.colPivHouseholderQr().solve(temp);
          }
          mu = r*mu;
      }
      return Q;
  }
  </code></pre>
  
  <p>My question is what makes one function work so well with the omp directive and what makes the other function crash? what is the difference that makes the omp directive act differently?</p>