title: OpenMP - For loop within a while loop
body: <p>I am trying to parallelise some serial code that I have written, but I am confused on how I can deal with a while loop. The code solves a time integration, computational mechanics problem. </p>
  
  <p><strong>( EDIT )</strong> Firstly I am using a number of structures to store data including a matrix/vector library Meschach, with data stuctures:</p>
  
  <pre><code>typedef struct {
    unsigned int dim, max_dim;
    double * ve;
  } VEC;
  
  typedef struct {
    unsigned int dim, max_dim;
    double * ve;
  } MAT;
  </code></pre>
  
  <p>And some user defined structures</p>
  
  <pre><code>typedef struct SCNI{
    MAT * B;
    VEC * Fsub ; 
    VEC * index;
       .
       . 
  } SCNI;
  </code></pre>
  
  <p>I am trying to parallelize a loop but I can't seem to get an answer that doesn't depend on the number of threads used. </p>
  
  <pre><code>// variables
  int numNode = 600;
  int dim = 2;
  double * Fint = malloc( dim * numNode * sizeof(double) );
  VEC * stress ;
  MAT * defGrad ; 
  VEC * displacement = v_get(dim*numNode) ; // allocate memory for vector
  VEC * matParameters = v_get(5);
  VEC * mRadius = v_get(numNode); 
  double intFactor;
  
  // each point has its own SCNI structure
  SCNI * scni = malloc( numnode * sizeof(SCNI) );
  
  ..................................
  while ( time &lt; t_max )
  // obtain displacement at this time increment
  // displacement = {a} + {b}*delta_t - where a and b are vectors
    v_mltadd(a,b,delta_t,displacement);
  {
  
  #pragma omp parallel default(none) 
  \ shared(displacement,scni,matParameters,mRadius,numnode) 
  \ private(i,intFactor,stressVoigt,defGrad) reduction(+:Fint[0:2*numnode]) 
  {
    defGrad = m_get(dim+1,dim+1) // allocate memory for each defGrad;
    stress = v_get(5) // allocate memory for each private stress;
    memset(Fint,0,numnode*dim*sizeof(double)); // set each Fint to zero initially
  // Internal force loop
  #pragma omp parallel for
    for ( i = 0 ; i &lt; numNode ; i++){
         // function call to find defGrad, using the B matrix from SCNI
         // and the vector displacement.
            calcDefGrad(defGrad,scni[i].B,displacement)
         // find stress using defGrad and the vector material parameters
            calcStress(stress,defGrad,matParamters)
         // find sub forces Fsub = scni[i].B*stress
            scni[i].Fsub = vm_mlt(scni[i].B,stress,scni[i])
         // assemble sub forces into global internal force array Fint
            assemble(Fint,scni[i].Fsub,scni[i].index,intFactor)
    }
  
  }
  ....................................
  }// end while loop
  ..................................
  </code></pre>
  
  <p>I'm not overly sure on how you share the data structures, and whether my issues stem from the matrix library I am using.</p>
  
  <p>Any help/hints/tips would be much appreciated as I am very new to OpenMP, and well multi-threaded programming in general.  </p>