是否可以使用 openmp 减少数组? [英] Is it possible to do a reduction on an array with openmp?

查看:36
本文介绍了是否可以使用 openmp 减少数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenMP 本身是否支持减少表示数组的变量?

Does OpenMP natively support reduction of a variable that represents an array?

这会像下面这样工作......

This would work something like the following...

float* a = (float*) calloc(4*sizeof(float));
omp_set_num_threads(13);
#pragma omp parallel reduction(+:a)
for(i=0;i<4;i++){
   a[i] += 1;  // Thread-local copy of a incremented by something interesting
}
// a now contains [13 13 13 13]

理想情况下,omp 并行会有类似的东西,如果您有足够多的线程使其有意义,那么累积将通过二叉树发生.

Ideally, there would be something similar for an omp parallel for, and if you have a large enough number of threads for it to make sense, the accumulation would happen via binary tree.

推荐答案

仅适用于 OpenMP 3.0 中的 Fortran,并且可能仅适用于某些编译器.

Only in Fortran in OpenMP 3.0, and probably only with certain compilers.

参见上一个示例(示例 3):

See the last example (Example 3) on:

http://wikis.sun.com/display/openmp/Fortran+可分配+数组

这篇关于是否可以使用 openmp 减少数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆