我使用openmp将我的c ++代码并行化,如下所示:
....//some code
double S_theta = 0.0, S_x = 0.0, S_y = 0.0;
#pragma omp parallel for private(dx,dy,theta_new) reduction(+ : S_x,S_y,S_theta)
for(int j = 0; j < N; j++)
{
if (j==i) continue;
double delta_x = x[i]-x[j],
for(int ky = -1; ky<= 1; ky++)
{
for(int kx = -1; kx<= 1; kx++)
{
if (r_ij_square > l0_two)
{
double r_ij = sqrt(r_ij_square);
r_x_ij/= r_ij;
r_y_ij/= r_ij;
double rdote = r_x_ij * e_x[j] + r_y_ij * e_y[j];
S_theta += e_dot_e * ( e_cross_e - rdote * (e_x[i] * r_y_ij - e_y[i] * r_x_ij) ) / (r_ij_square * r_ij);
double S = rdote /r_ij_square;
S_x += r_x_ij * S;
S_y += r_y_ij * S;
}
}
}
double zeta = -eta / 2.0 ;
theta_new[i] += zeta + dt * dlthet;
}
....//some code
但每次运行时结果都会改变!当我删除这一行
#pragma omp parallel for reduction(+ : S_x,S_y,S_theta)
,结果在不同的运行中是相同的! 我的代码出了什么问题?
答案 0 :(得分:0)
您的变量S
在循环外声明,并将在运行循环的所有线程之间共享。鉴于它是如何使用的,它可以在使用它的循环中声明为double S = rdote / r_ij_square;
。