使用Opencl有效地找到最小的大型数组

mTO*_*ger 5 c++ hierarchical-clustering opencl

我正在开发opencl中的层次聚类算法.对于每个步骤,我在一个非常大的数组中找到最小值(大约10 ^ 8个条目),这样我就知道哪些元素必须组合成一个新的簇.最小值的识别必须进行9999次.使用我当前的内核,找到最小值(在所有迭代中累积)大约需要200秒.我是如何解决这个问题的方法是将数组分成2560个大小相同的片段(我的Radeon 7970上有2560个流处理器),并找到每个片段的最小值.我运行第二个内核,将这些最小值组合成全局最小值.

有没有更有效的方法来解决这个问题?最初的想法是通过使用OpenCL来加速HCA,但是识别最小值所花费的时间比CPU上的matlab HCA长得多.我究竟做错了什么?

__kernel void findMinValue(__global float * myArray, __global double * mins, __global int * elementsToWorkOn, __global int * arraysize){
int gid = get_global_id(0);
int minloc = 0;
float mymin = INFINITY;
int eltoWorkOn = *elementsToWorkOn;
int offset = gid*eltoWorkOn;
int target = offset + eltoWorkOn;

if (offset<*arraysize){
    //make sure the array size is not exceeded
    if (target > *arraysize){
        target = *arraysize;
    }

    //find minimum for the kernel
    for (int i = offset; i < target; i++){
        if (*(myArray + i) < mymin){
            mymin = *(myArray + i);
            minloc = i;
        }
    }
}
*(mins + gid * 2) = minloc;
*(mins + gid * 2 + 1) = mymin;
}


__kernel void getGlobalMin(__global double * mins, __global double * gmin, __global int * pixelsInImage){
    int nWorkitems = 2560;
    float globalMin = INFINITY;
    double globalMinLoc;
    float tempMin;
    for (int i = 0; i < nWorkitems; i++){
        tempMin = *(mins + 2 * i + 1);
        if (tempMin < globalMin){
            globalMin = tempMin;
            globalMinLoc = *(mins + 2 * i);
        }
    }
    *(gmin + 0) = globalMinLoc;
    *(gmin + 1) = globalMin;
}
Run Code Online (Sandbox Code Playgroud)

UPDATE

我根据你的建议重新设计了findMinValue内核.内存访问现在是合并的,我将工作分成工作组,这样我就可以减少全局内存访问量.之前,每个内核都将其最小值写入全局mins缓冲区.现在每个worg组只有一个内核写入一个值(即组最小值).此外,我增加了全局工作大小以隐藏内存延迟.

这些变化允许将识别最小值所需的时间从> 200s减少到仅59s!非常感谢您的帮助!

在优化内核时还有什么我可以错过的吗?你有什么进一步的建议吗?我无法弄清楚如何使用setArg().我是否必须将指向int值的指针传递给它(如下所示:) err = clSetKernelArg(kernel[2], 3, sizeof(int), &variable);.在这种情况下,内核声明看起来如何?

这是我的新内核:

__kernel void findMinValue(__global float * myArray, __global double * mins, __global int * arraysize,__global int * elToWorkOn,__global int * dummy){
int gid = get_global_id(0);
int lid = get_local_id(0);
int groupID = get_group_id(0);
int lsize = get_local_size(0);
int gsize = get_global_id(0);
int minloc = 0;
int arrSize = *arraysize;
int elPerGroup = *elToWorkOn;
float mymin = INFINITY;


__local float lmins[128];
//initialize local memory
*(lmins + lid) = INFINITY;
__local int lminlocs[128];

//this private value will reduce global memory access in the for loop (temp = *(myArray + i);)
float temp;

//ofset and target of the for loop
int offset = elPerGroup*groupID + lid;
int target = elPerGroup*(groupID + 1);

//prevent that target<arrsize (may happen due to rounding errors or arrSize not a multiple of elPerGroup
target = min(arrSize, target);

//find minimum for the kernel
//offset is different for each lid, leading to sequential memory access
if (offset < arrSize){
    for (int i = offset; i < target; i += lsize){
        temp = *(myArray + i);
        if (temp < mymin){
            mymin = temp;
            minloc = i;
        }
    }

    //store kernel minimum in local memory
    *(lminlocs + lid) = minloc;
    *(lmins + lid) = mymin;

    //find work group minimum (reduce global memory accesses)
    lsize = lsize >> 1;
    while (lsize > 0){
        if (lid < lsize){
            if (*(lmins + lid)> *(lmins + lid + lsize)){
                *(lmins + lid) = *(lmins + lid + lsize);
                *(lminlocs + lid) = *(lminlocs + lid + lsize);
            }
        }
        lsize = lsize >> 1;
    }
}
//write group minimum to global buffer
if (lid == 0){
    *(mins + groupID * 2 + 0) = *(lminlocs + 0);
    *(mins + groupID * 2 + 1) = *(lmins + 0);
}
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*ros 1

通过WI访问连续的内存比访问分散的内存要高效得多。此外,您应该先在工作组中求和,然后将其传递到全局内存。并使用单个setArg()整数,而不是为此目的使用缓冲区。至少,你应该这样做:

__kernel void findMinValue(__global float * myArray, __global double * mins, __global int arraysize){
    int gid = get_global_id(0);
    int minloc = 0;
    float mymin = INFINITY;

    //find minimum for the kernel
    for (int i = gid ; i < arraysize; i+= get_global_size(0)){
        if (*(myArray + i) < mymin){
            mymin = *(myArray + i);
            minloc = i;
        }
    }

    *(mins + gid * 2) = minloc;
    *(mins + gid * 2 + 1) = mymin;
}
Run Code Online (Sandbox Code Playgroud)