4

I'm working on a project that requires massive parallel computing. However, the tricky problem is that, the project contains a nested loop, like this:

for(int i=0; i<19; ++i){
    for(int j=0; j<57; ++j){
        //the computing section
    }
}

To achieve the highest gain, I need to parallelise those two levels of loops. Like this:

parallel_for_each{
    parallel_for_each{
        //computing section
    }
}

I tested and found that AMP doesn't support nested for loops. Anyone have any idea on this problem? Thanks

4

2 回答 2

4

正如@High Performance Mark 建议的那样,您可以将两个循环合二为一。但是,您不需要使用 C++ AMP 执行此操作,因为它支持s 和sextent上的 2 维和 3 维s。您可以使用 an作为多维索引。arrayarray_viewindex

array<float, 2> x(19,57);
parallel_for_each(x.extent, [=](index<2> idx) restrict(amp)
{
    x[idx] = func(x[idx]);
});

float func(const float v) restrict(amp) { return v * v; }

您可以使用以下方法访问各个子索引idx

int row = idx[0]; 
int col = idx[1];

您还应该考虑computing section. 如果它相对较小,您可能希望每个线程处理多个数组元素,x.

下面的文章也值得一读,就像 CPU 一样,如果你的循环不能有效地访问内存,它会对性能产生很大的影响。数组是 C++ AMP 中的主要行

于 2014-03-06T21:27:31.070 回答
0

所以折叠循环:

for(int ij=0; ij<19*57; ++ij){
        //if required extract i and j from ij
        //the computing section
    }
}
于 2014-03-06T09:11:44.367 回答