gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:54:56 25 4
gpt4 key购买 nike

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

 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 确实支持将随机访问迭代器作为循环变量。但是,您仍然必须遵守不支持 != 的规范循环形式。

关于c++ - 用不等式并行化 for 循环 (openmp c++​​),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47381400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com