If statement if语句对OpenACC有害吗?

If statement if语句对OpenACC有害吗?,if-statement,openacc,pgi,If Statement,Openacc,Pgi,我听说OpenACC不能有效地处理if语句,应该尽量避免使用它 例如,在设备/OpenACC: for (m=0; m<polygon2.num_vertices; m++) { polygon2Vertex1 = listOfpolygon2Vertex[m]; if ((m+1) == polygon2.num_vertices){ // last vertex, so we get last and the first vertex

我听说OpenACC不能有效地处理if语句,应该尽量避免使用它

例如,在设备/OpenACC:

for (m=0; m<polygon2.num_vertices; m++) {

    polygon2Vertex1 = listOfpolygon2Vertex[m];

    if ((m+1) == polygon2.num_vertices){
        // last vertex, so we get last and the first vertex
        polygon2Vertex2 = listOfpolygon2Vertex[0];

    } else {
        // not the last vertex, so we get [m] and [m+1] vertex
        polygon2Vertex2 = listOfpolygon2Vertex[m+1];
    }

    result = doIntersect(polygon1Vertex1, polygon1Vertex2, polygon2Vertex1, polygon2Vertex2);

    if (result==1){
        // found that these 2 edges intersect.
        // no need to further check

        break;
    }
}

用于(m=0;m问题在于CUDA扭曲内的分支发散。由于扭曲内的所有线程在同一时间执行相同的指令,如果某些线程执行一个分支,而其他线程执行另一个分支,则您的时间增加了一倍。然而,这只是扭曲内的一个问题。因此,如果同一扭曲内的所有线程都执行一个分支,那么另一个扭曲中的线程采用不同的分支,则不会影响性能

如果您不知道什么是扭曲,请参阅关于CUDA线程模型的一篇古老但仍然不错的文章:

有了这段代码,因为只有最后一个元素的大小写为true,所以if语句的影响很小

我建议将您的逻辑倒置,以便最后一个元素的情况出现在else子句中。这是一个通用的优化,不特定于GPU,以便更常见的情况可以通过,而不必进行跳转