英文:
Why is OpenMP reduction failing with error message 'user defined reduction not found for 'avgs'?'
问题
在这段代码中,为什么对reduction(减少)的操作会引发错误:
用户定义的reduction(减少)未找到 'avgs'
我预期这段代码会使avgs
的值在每个并行线程中私有化,然后在for循环执行结束时将它们全部相加。
英文:
Why is the reduction in this code throwing an error:
user defined reduction not found for 'avgs'
void parallel_avg_pixel(long img[DIM_ROW][DIM_COL][DIM_RGB], long *avgs) {
int row, col, pixel;
long count = 0;
#pragma omp parallel for reduction(+:avgs)
for (row = 0; row < DIM_ROW; row++) {
for (col = 0; col < DIM_COL; col++) {
for (pixel = 0; pixel < DIM_RGB; pixel++){
avgs[pixel] += img[row][col][pixel];
count++;
}
}
}
count /= 3;
for (pixel = 0; pixel < DIM_RGB; pixel++) {
avgs[pixel] /= count;
}
}
I expected this code to make the value of avgs
private for each thread in parallel and then sum them all together at the end of execution of the for loops.
答案1
得分: 3
你的变量 avgs
是一个指针。你不能对指针进行缩减操作。OpenMP 告诉你,如果你想对指针进行缩减操作,你需要为它们定义一个缩减规则。
好的,你把它当作数组来使用,但问题是 OpenMP 不知道它的长度是多少。
请使用以下语法:
#pragma omp reduction(+:avgs[0:DIM_RGB])
英文:
Your variable avgs
is a pointer. You can not reduce on pointers. OpenMP is telling you that if you want to reduce pointers, you need to define a reduction for them.
Ok, you're using it as an array but then the problem is OpenMP doesn't know how long it is.
Use the following syntax:
#pragma omp reduction(+:avgs[0:DIM_RGB])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论