OpenCL查找数组中最小的索引

时间:2017-03-21 19:54:48

标签: graphics opencl gpu jocl

我正在使用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工作吗?)

谢谢!

1 个答案:

答案 0 :(得分: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毫秒

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

下载数据[0]和索引[0]花了:0.002毫秒

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

  • work_group_scan_inclusive_min(x)for OpenCL 2.0 +
  • __ local float reductionArray [256] for OpenCL 1.2 -

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