OpenMP - for while循环中的循环

时间:2018-04-10 20:03:37

标签: c parallel-processing openmp

我正在尝试并行化我编写的一些串行代码,但我对如何处理while循环感到困惑。该代码解决了时间积分,计算机制问题。

(编辑)首先,我使用了许多结构来存储数据,包括矩阵/矢量库Meschach,以及数据结构:

typedef struct {
  unsigned int dim, max_dim;
  double * ve;
} VEC;

typedef struct {
  unsigned int dim, max_dim;
  double * ve;
} MAT;

一些用户定义的结构

typedef struct SCNI{
  MAT * B;
  VEC * Fsub ; 
  VEC * index;
     .
     . 
} SCNI;

我正在尝试并行化一个循环,但我似乎无法得到一个不依赖于所用线程数的答案。

// 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 < 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 < 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
..................................

我不太确定你如何分享数据结构,以及我的问题是否来自我正在使用的矩阵库。

任何帮助/提示/技巧都会非常受欢迎,因为我对OpenMP很陌生,而且一般来说都是多线程编程。

0 个答案:

没有答案