我想测试 #pragma omp parallel for
和 #pragma omp simd
一个简单的矩阵加法程序。当我分别使用它们时,我没有收到任何错误,而且看起来还不错。但是,我想测试使用它们两者可以获得多少性能。如果我在外部循环之前使用 #pragma omp parallel for
并在内部循环之前使用 #pragma omp simd
我也不会出错。当我在外循环之前同时使用它们时会发生错误。我在运行时而不是编译时收到错误。 ICC
和 GCC
返回错误,但 Clang
没有。可能是因为 Clang
调节了并行化。在我的实验中,Clang 不会并行化并仅使用一个线程运行程序。
程序在这里:
#include <stdio.h>
//#include <x86intrin.h>
#define N 512
#define M N
int __attribute__(( aligned(32))) a[N][M],
__attribute__(( aligned(32))) b[N][M],
__attribute__(( aligned(32))) c_result[N][M];
int main()
{
int i, j;
#pragma omp parallel for
#pragma omp simd
for( i=0;i<N;i++){
for(j=0;j<M;j++){
c_result[i][j]= a[i][j] + b[i][j];
}
}
return 0;
}
错误: 国际刑事法院:
IMP1.c(20): error: omp directive is not followed by a parallelizable for loop #pragma omp parallel for ^
compilation aborted for IMP1.c (code 2)
海湾合作委员会:
IMP1.c: In function ‘main’:
IMP1.c:21:10: error: for statement expected before ‘#pragma’ #pragma omp simd
因为在我的其他睾丸中,外循环的 pragma omp simd
获得更好的性能,所以我需要把它放在那里(不是吗?)。
平台:Intel Core i7 6700 HQ、Fedora 27
经过测试的编译器:ICC 18、GCC 7.2、Clang 5
编译器命令行:
icc -O3 -qopenmp -xHOST -no-vec
gcc -O3 -fopenmp -march=native -fno-tree-vectorize -fno-tree-slp-vectorize
clang -O3 -fopenmp=libgomp -march=native -fno-vectorize -fno-slp-vectorize
最佳答案
来自 OpenMP 4.5 规范:
2.11.4 Parallel Loop SIMD Construct
The parallel loop SIMD construct is a shortcut for specifying a parallel construct containing one loop SIMD construct and no other statement.
The syntax of the parallel loop SIMD construct is as follows:
#pragma omp parallel for simd
...
你也可以这样写:
#pragma omp parallel
{
#pragma omp for simd
for ...
}
关于c - 如何一起使用 omp parallel for 和 omp simd?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48711367/