多维嵌套循环的OpenMP [英] Multi-dimensional nested OpenMP loop
问题描述
什么是并行OpenMP中多维尴尬并行循环的正确方法?维数在编译时是已知的,但它的尺寸将是大是没有的。它们中的任何可以是一个,两个,或一百万。当然,我不希望ñ OMP并行的一个N维环
...
What is the proper way to parallelize a multi-dimensional embarrassingly parallel loop in OpenMP? The number of dimensions is known at compile-time, but which dimensions will be large is not. Any of them may be one, two, or a million. Surely I don't want N omp parallel
's for an N-dimensional loop...
思考:
-
问题的概念很简单。只有最外层的大循环需要进行并行处理,但环尺寸在编译时未知的,可能会改变。
The problem is conceptually simple. Only the outermost 'large' loop needs to be parallelized, but the loop dimensions are unknown at compile-time and may change.
将动态设置 OMP_SET_NUM_THREADS(1)
和的#pragma OMP的时间表(静态,huge_number)
使某些环路并行化无操作?这会不会有意外的副作用/开销?感觉就像一个杂牌组装电脑。
Will dynamically setting omp_set_num_threads(1)
and #pragma omp for schedule(static, huge_number)
make certain loop parallelizations a no-op? Will this have undesired side-effects/overhead? Feels like a kludge.
借助 OpenMP规范(2.10,A.38,A.39)告诉符合和不符合嵌套并行,但不建议这个问题的最好方法之间的区别。
The OpenMP Specification (2.10, A.38, A.39) tells the difference between conforming and non-conforming nested parallelism, but doesn't suggest the best approach to this problem.
重新排序的循环是可能的但可能会导致大量的缓存未命中。展开是可能的,但不平凡的。有另一种方式?
Re-ordering the loops is possible but may result in a lot of cache-misses. Unrolling is possible but non-trivial. Is there another way?
下面是想什么我并行:
for(i0=0; i0<n[0]; i0++) {
for(i1=0; i1<n[1]; i1++) {
...
for(iN=0; iN<n[N]; iN++) {
<embarrasingly parallel operations>
}
...
}
}
谢谢!
推荐答案
的崩溃
指令可能是你在找什么,所描述的此处。这将基本上形成单个环路,其然后parallized,并且被设计为恰好这些类型的情形。所以,你会怎么做:
The collapse
directive is probably what you're looking for, as described here. This will essentially form a single loop, which is then parallized, and is designed for exactly these sorts of situations. So you'd do:
#pragma omp parallel for collapse(N)
for(int i0=0; i0<n[0]; i0++) {
for(int i1=0; i1<n[1]; i1++) {
...
for(int iN=0; iN<n[N]; iN++) {
<embarrasingly parallel operations>
}
...
}
}
和准备就绪。
这篇关于多维嵌套循环的OpenMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!