将c顺序转换为OpenMP问题

时间:2016-11-28 22:11:21

标签: c loops openmp

我正在开发一个n-body赋值,它使用OpenMP将顺序程序转换为并行程序。我有下面的函数与OpenMP关闭工作正常,但产生不正确的结果。我已经锁定了循环内未更新的唯一变量,这些变量未在循环中声明。我已经盯着它看了很长时间,我不确定我做错了什么。任何提示将不胜感激。

void update() {
  int i, j;
  Body *tmp;

  omp_lock_t lock;
  omp_init_lock(&lock);

  for (i=0; i<numBodies; i++) {
    double x = bodies[i].x;
    double y = bodies[i].y;
    double vx = bodies[i].vx;
    double vy = bodies[i].vy;
    double ax = 0;
    double ay = 0;

    #pragma omp parallel for num_threads(4)
    for (j=0; j<numBodies; j++) {
      double r, mass, dx, dy, r_squared, acceleration;

      if (j==i) continue;
      dx = bodies[j].x - x;
      dy = bodies[j].y - y;
      mass = bodies[j].mass;
      r_squared = dx*dx + dy*dy;
      if (r_squared != 0) {
        r = sqrt(r_squared);
        if (r != 0) {
          acceleration = K*mass/(r_squared);
          omp_set_lock(&lock);
          ax += acceleration*dx/r;
          ay += acceleration*dy/r;
          omp_unset_lock(&lock);
        }
      }
    }
    x += vx;
    y += vy;
    if (x>=x_max || x<x_min) x=x+(ceil((x_max-x)/univ_x)-1)*univ_x;
    if (y>=y_max || y<y_min) y=y+(ceil((y_max-y)/univ_y)-1)*univ_y;
    vx += ax;
    vy += ay;
    assert(!(isnan(x) || isnan(y)));
    assert(!(isnan(vx) || isnan(vy)));
    bodies_new[i].x = x;
    bodies_new[i].y = y;
    bodies_new[i].vx = vx;
    bodies_new[i].vy = vy;
  }
  tmp = bodies;
  bodies = bodies_new;
  bodies_new = tmp;
  omp_destroy_lock(&lock);
}

1 个答案:

答案 0 :(得分:0)

将pragma移动到外部循环,删除缩减并将I和j添加为私有。