paralelizing for loop with inequality (openmp c++)
paralelizing for loop with inequality (openmp c++)
我试图并行我的这部分代码,但由于使用不等式!=
而出现错误。
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;
相关文章:
- OpenMP阵列性能较差
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- OpenMP:并行更新数组总是需要减少数组吗
- 如何使用OpenMP并行这两个循环
- 从python调用openMP共享库时,未定义opnMP函数
- 如何使用OpenMP并行化此矩阵时间矢量运算
- 如何使用OpenMP使这个循环并行
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 查找最近配对时的OpenMP竞赛条件
- 使用输入打破 OpenMP 中的循环
- 为什么 openmp 的并行不适用于矢量化色彩空间转换?
- 在 openmp 中,omp_get_thread_num是否绑定到物理线程?
- 在C++中使用并行化的预期速度是多少(不是 OpenMp,而是 <thread>)
- OpenMP 加上unordered_map<字符串、双字符串的缩减>
- OpenMP 与有序和关键指令并行
- 我使用 OpenMP 的线程越多,执行时间就越长,这是怎么回事?
- OpenMP for 循环并行性问题
- 两个连续的 OpenMP 并行区域会相互减慢速度
- 读取文件时无法使用 OpenMP 获得加速