【问题标题】:PGI Compiler Parallelization +=PGI 编译器并行化 +=
【发布时间】:2013-08-15 05:55:35
【问题描述】:

我正在努力使向量和矩阵类并行化,但遇到了问题。任何时候我都有一个循环形式为

for (int i = 0; i

代码具有数据依赖性,不会并行化。使用 intel 编译器时,它很聪明,可以在没有任何编译指示的情况下处理这个问题(我想避免使用编译指示进行无依赖检查,只是因为有大量与此类似的循环,而且情况实际上比这更复杂,并且我希望它检查以防万一确实存在)。

有人知道 PGI 编译器的编译器标志允许这样做吗?

谢谢,

贾斯汀

编辑:for 循环中的错误。不是复制粘贴实际循环

【问题讨论】:

  • 抱歉,这不是我的实际循环,只是试图展示一个示例并弄乱了格式。

标签: parallel-processing compiler-flags pgi


【解决方案1】:

我认为问题在于您没有在这些例程中使用 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

为我们提供了以下信息:不带限制关键字的 dbpa() 例程未并行化,但 dbpa_restict() 例程已并行化。

确实,对于这类东西,您最好只使用 OpenMP(或 TBB 或 ABB 或...),而不是试图说服编译器为您自动并行化;可能更好的是使用现有的线性代数包,无论是密集的还是稀疏的,这取决于你在做什么。

【讨论】:

  • 乔纳森,感谢您的 cmets。我可以试试 OpenMP(对 c++ 编程来说还是新手)。我会使用现有的线性代数包,但不幸的是,这不是我的决定。我也会使用restrict 和OpenMP。谢谢
猜你喜欢
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
  • 2020-10-22
  • 2016-02-12
  • 1970-01-01
  • 2018-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多