我正在进行一个向量和矩阵类的并行化工作,但遇到了一个问题。任何时候,我有一个循环的形式
对于(int i= 0;i< n;i++) bi += ai;
该代码具有数据依赖项,不会并行化。当使用英特尔编译器时,它足够聪明,可以在没有任何编译指示的情况下处理这个问题(我想避免使用编译指示进行依赖检查,因为有大量类似于此的循环,并且因为情况实际上比这个复杂,我希望它只在存在的情况下进行检查)。
有没有人知道PGI编译器的编译器标志允许这样做?
谢谢,
贾斯汀
编辑: for循环中的错误。没有复制粘贴实际的循环
发布于 2013-08-15 02:47:00
我认为问题是在这些例程中没有使用restrict关键字,所以C编译器不得不担心指针别名。
编译这个程序:
#include <stdlib.h>
#include <stdio.h>
void dbpa(double *b, double *a, const int n) {
for (int i = 0; i < n; i++) b[i] += a[i] ;
return;
}
void dbpa_restrict(double *restrict b, double *restrict a, const int n) {
for (int i = 0; i < n; i++) b[i] += a[i] ;
return;
}
int main(int argc, char **argv) {
const int n=10000;
double *a = malloc(n*sizeof(double));
double *b = malloc(n*sizeof(double));
for (int i=0; i<n; i++) {
a[i] = 1;
b[i] = 2;
}
dbpa(b, a, n);
double error = 0.;
for (int i=0; i<n; i++)
error += (3 - b[i]);
if (error < 0.1)
printf("Success\n");
dbpa_restrict(b, a, n);
error = 0.;
for (int i=0; i<n; i++)
error += (4 - b[i]);
if (error < 0.1)
printf("Success\n");
free(b);
free(a);
return 0;
}使用PGI编译器:
$ pgcc -o tryautop tryautop.c -Mconcur -Mvect -Minfo
dbpa:
5, Loop not vectorized: data dependency
dbpa_restrict:
11, Parallel code generated with block distribution for inner loop if trip count is greater than or equal to 100
main:
21, Loop not vectorized: data dependency
28, Loop not parallelized: may not be beneficial
36, Loop not parallelized: may not be beneficial告诉我们没有使用restrict关键字的dbpa()例程没有被并行化,但是dbpa_restict()例程被并行化。
实际上,对于这类东西,您最好使用OpenMP (或TBB或ABB或...)与其试图说服编译器为您自动并行化,也许更好的方法是使用现有的线性代数包,无论是密集的还是稀疏的,这取决于您正在做什么。
https://stackoverflow.com/questions/18235972
复制相似问题