paralelizing for loop with inequality (openmp c++)

paralelizing for loop with inequality (openmp c++)

本文关键字:openmp c++ inequality for loop with paralelizing      更新时间:2023-10-16

我试图并行我的这部分代码,但由于使用不等式!=而出现错误。

 double sum_sin = 0.0, sum_cos = 0.0;
            int count = 0;
    #pragma omp parallel for reduction(+ : count ,sum_sin,sum_cos)
            for (vector<int>::iterator it = box_neighbors[bx[i]].begin(); it != box_neighbors[bx[i]].end(); ++it)
                {
                for (vector<int>::iterator itp = box_particles[*it].begin(); itp != box_particles[*it].end(); ++itp)
                     {
                     if(dist(x[i], y[i], x[*itp], y[*itp], L) < R0_two)
                        {
                        sum_sin+= sin(theta[*itp]);
                        sum_cos+= cos(theta[*itp]);
                        count+=1;  //number of neighbours of i'th particle
                        }
                     }
                }
            sum_sin/= count;
            sum_cos/= count;

如何删除错误?这是错误:

invalid controlling predicate
             for (vector<int>::iterator it = box_neighbors[bx[i]].begin(); it !=

我根据注释将代码固定到

           double sum_sin = 0.0, sum_cos = 0.0;
            int count = 0;
            #pragma omp parallel for reduction(+ : count ,sum_sin,sum_cos)
std::vector<int> v; 
for(std::size_t it=0; it<v.size(); ++it) 
            //for (vector<int>::iterator it = box_neighbors[bx[i]].begin(); it != box_neighbors[bx[i]].end(); ++it)
                {
for(std::size_t itp=0; itp<v.size(); ++itp) 
                //for (vector<int>::iterator itp = box_particles[*it].begin(); itp != box_particles[*it].end(); ++itp)
                     {
                     if(dist(x[i], y[i], x[*itp], y[*itp], L) < R0_two)
                        {
                        sum_sin+= sin(theta[*itp]);
                        sum_cos+= cos(theta[*itp]);
                        count+=1;  //number of neighbours of i'th particle
                        }
                     }
                }

但是出现了新的错误:

    error: for statement expected before ‘std’
     std::vector<int> v; 
     ^
 error: invalid type argument of unary ‘*’ (have ‘std::size_t {aka long unsigned int}’)
                          if(dist(x[i], y[i], x[*itp], y[*itp], L) < R0_two)
                                                 ^
  error: invalid type argument of unary ‘*’ (have ‘std::size_t {aka long unsigned int}’)
                          if(dist(x[i], y[i], x[*itp], y[*itp], L) < R0_two)
                                                          ^
    error: invalid type argument of unary ‘*’ (have ‘std::size_t {aka long unsigned int}’)
                             sum_sin+= sin(theta[*itp]);
                                                  ^
   error: invalid type argument of unary ‘*’ (have ‘std::size_t {aka long unsigned int}’)
                             sum_cos+= cos(theta[*itp]);

只需将循环条件从

it != box_neighbors[bx[i]].end()

it < box_neighbors[bx[i]].end()

自 3.0 版以来,OpenMP 确实支持随机访问迭代器作为循环变量。但是,您仍然必须坚持规范循环形式,不支持!=

只有前for loop,使用迭代器,就在omp pragma下面,是有问题的。使用 OpenMP 时,通常只使用带有计数器的规范循环会更安全。因此,所需的最小更改为:

double sum_sin = 0.0, sum_cos = 0.0;
int count = 0;
#pragma omp parallel for reduction(+ : count, sum_sin, sum_cos)
for (std::size_t it = 0; it < box_neighbors[bx[i]].size(); ++it)
{
  const int star_it = box_neighbors[bx[i]][it];
  for (vector<int>::iterator itp = box_particles[star_it].begin();
       itp != box_particles[star_it].end(); ++itp)
  {
    if (dist(x[i], y[i], x[*itp], y[*itp], L) < R0_two)
    {
      sum_sin += sin(theta[*itp]);
      sum_cos += cos(theta[*itp]);
      count += 1;  // number of neighbours of i'th particle
    }
  }
}
sum_sin /= count;
sum_cos /= count;