1

我正在使用 OpenCL(通过 JOCL)在光线行进的一系列距离计算中找到最小值。伪代码看起来像这样:

Start with a point in 3d space.
There are a number of functions to calculate distances
    to that point from various other points. 
    These may be rather complex (transforms, csg etc).
Calculate all of the distances, perhaps into an array
Get the index of the minimum distance in the array..
Use that index to do up other stuff (pigmentation etc).

我的实现有点废话。我目前没有并行化距离计算,但我想这样做。这就是我不这样做的原因:

获得最小距离很容易,但检索这个索引并不明显。我最终迭代了距离并跟踪了当前的最小值及其索引,但这在并行环境中显然是垃圾。

基本上可以在这里使用提示将我引向正确的方向,或者告诉我我是否完全吠错了树?(例如,这是一个 CPU 工作吗?)

谢谢!

4

1 回答 1

0

使用低端显卡 RX550 进行测试。

100 万个元素 min() 函数:

__kernel void test(__global float * data,__global int * index)
{
    int id=get_global_id(0);
    float d1=data[id];
    float d2=data[id+get_global_size(0)];
    float f=fmin(d1,d2);
    index[id]=select( index[id+get_global_size(0)], index[id], fabs(f-d1)<=fabs(f-d2) );
    data[id]=f;
}");

具有随机值的初始化数据元素和具有自己索引的索引元素。

通过 pci-e 2.0 8x 将数据和索引上传到 GPU 耗时:3.0 ms

全局范围=512k,256k,128k,...,1(logN 步)的计算耗时:0.3 ms

下载数据[0]和索引[0]耗时:0.002 ms

这是一个简单的版本,可能不是最快的实现。为了更快,可以添加工作组级别的子减少:

  • 用于 OpenCL 2.0+ 的 work_group_scan_inclusive_min(x)
  • __local float reductionArray[256] 用于 OpenCL 1.2-

减少内核排队命令的数量以在不到一百个时间内完成工作?微秒。

于 2017-06-07T08:00:44.327 回答