如何一起使用 omp parallel for 和 omp simd?
How to use omp parallel for and omp simd together?
我想测试 #pragma omp parallel for
和 #pragma omp simd
的简单矩阵加法程序。当我分别使用它们时,我没有收到任何错误,而且看起来还不错。但是,我想测试使用它们两者可以获得多少性能。如果我在外部循环之前使用 #pragma omp parallel for
并在内部循环之前使用 #pragma omp simd
我也不会出错。当我在外循环之前同时使用它们时会发生错误。我在 运行 时间而不是编译时间得到一个错误。 ICC
和 GCC
return 错误但 Clang
没有。可能是因为 Clang
regect 并行化。在我的实验中,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
for outer loop 获得更好的性能我需要把它放在那里(不是吗?)。
平台:英特尔酷睿 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 ...
}
我想测试 #pragma omp parallel for
和 #pragma omp simd
的简单矩阵加法程序。当我分别使用它们时,我没有收到任何错误,而且看起来还不错。但是,我想测试使用它们两者可以获得多少性能。如果我在外部循环之前使用 #pragma omp parallel for
并在内部循环之前使用 #pragma omp simd
我也不会出错。当我在外循环之前同时使用它们时会发生错误。我在 运行 时间而不是编译时间得到一个错误。 ICC
和 GCC
return 错误但 Clang
没有。可能是因为 Clang
regect 并行化。在我的实验中,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
for outer loop 获得更好的性能我需要把它放在那里(不是吗?)。
平台:英特尔酷睿 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 ...
}