【问题标题】:OpenAcc if-condition not working?OpenAcc if 条件不起作用?
【发布时间】:2016-05-08 21:15:22
【问题描述】:

我有以下代码:

#pragma acc kernels
for (i = 0; i<shape1Count; i++){
    for (j = 0; j<shape2Count; j++){

        if (kkk==1){
            intersectionsCount++;
        } else {
            intersectionsCount++;
        }

    }
}

kkk 被赋值为 1。
我发现 if 条件甚至没有运行,这意味着无论是真还是假,intersectionCount 都不会增加。

我的假设是,GPU/加速器无法处理 if 条件。这是真的吗?
如果是真的,我该怎么办?

附言我对 GPU 编程非常陌生。

非常感谢

【问题讨论】:

  • 您使用的是什么编译器,在什么平台上?这应该会运行,尽管如果增量是使用原子指令实现的,它可能会运行得更好。

标签: if-statement openacc


【解决方案1】:

通常编译器可以自动检测归约,但在这种情况下不能。因此,您需要自己添加一个缩减子句。这是使用 pgcc 16.4 版时的输出:

% cat test.c
#include <stdlib.h>
#include <stdio.h>

int main() {

int i, j, kkk, intersectionsCount;
int shape1Count,shape2Count;

shape1Count=32;
shape2Count=32;
intersectionsCount=0;
kkk=1;

#pragma acc kernels loop reduction(+:intersectionsCount)
for (i = 0; i<shape1Count; i++){
    for (j = 0; j<shape2Count; j++){
        if (kkk==1){
            intersectionsCount++;
        } else {
            intersectionsCount++;
        }
    }
}
printf("%d\n",intersectionsCount);
exit(0);
}
% pgcc test.c -Minfo=accel -acc; a.out
main:
     15, Loop is parallelizable
     16, Loop is parallelizable
         Accelerator kernel generated
         Generating Tesla code
         15, #pragma acc loop gang, vector(4) /* blockIdx.y threadIdx.y */
         16, #pragma acc loop gang, vector(32) /* blockIdx.x threadIdx.x */
             Generating reduction(+:intersectionsCount)
1024

【讨论】:

  • 男人!你救了我的命! “减少”是我在这里学到的新东西。非常感谢
猜你喜欢
  • 2018-10-16
  • 2015-04-18
  • 1970-01-01
  • 2011-01-31
  • 2017-10-26
  • 1970-01-01
  • 2020-03-11
  • 2014-03-21
  • 1970-01-01
相关资源
最近更新 更多