简体   繁体   English

使用不等式并行化for循环(openmp c ++)

[英]paralelizing for loop with inequality (openmp c++)

I tried to paralleling this part of my code, but there is an error due to using inequality != . 我试图并行处理我的代码的这一部分,但是由于使用不等式!=导致出现错误。

 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;

How can I remove the error? 如何清除错误? This is the error: 这是错误:

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

I canged the code according to the comments to 我根据评论修改了代码

           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
                        }
                     }
                }

But new errors occure: 但是出现了新的错误:

    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]);

Simply change the loop condition from 只需更改以下条件即可

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

to

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

OpenMP does support random access iterators as loop variables since version 3.0. 从3.0版开始,OpenMP确实支持将随机访问迭代器作为循环变量。 However, you still have to adhere to the canonical loop form with does not support != . 但是,您仍然必须坚持不支持!=的规范循环形式。

Only the first for loop , using iterators, just below the omp pragma , is problematic. 只有第一个for loop (使用迭代器)位于omp pragma正下方,才是有问题的。 When using OpenMP it is generally safer to only use canonical loops with a counter. 使用OpenMP时,通常仅使用带有计数器的规范循环更为安全。 Thus the required minimal change is: 因此,所需的最小更改为:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM