查找内核中数组中Max值的索引

时间:2017-12-06 16:38:38

标签: c++ parallel-processing kernel opencl reduction

我是OpenCL的新手,我正在创建2个函数来查找内核函数中最小OR值的索引。根据这个article,我能够根据并行树减少找到最小值的索引。

如何更改它以仅查找数组中的最大值索引?这是我的工作最小功能:

__kernel void Min(__global float* buffer, __global int* result, __const int length, __local float* scratch, __local int* sc_index)
{

    int global_index = get_global_id(0);
    int local_index = get_local_id(0);

    // Load data into local memory
    if (global_index < length) {
        scratch[local_index] = buffer[global_index];
        sc_index[local_index] = global_index;
    } else {
        // INFINITY is the identity element for the min operation
        scratch[local_index] = INFINITY;
    }

    barrier(CLK_LOCAL_MEM_FENCE);    
    for(int offset = get_local_size(0) / 2;  offset > 0; offset >>= 1)
    {
        if (local_index < offset) {
            float other = scratch[local_index + offset];
            int other_index = sc_index[local_index + offset];

            float mine = scratch[local_index];
            int mine_index = sc_index[local_index];

            if (mine < other) {
                scratch[local_index] = mine;
                sc_index[local_index] = mine_index;
            }
            else {
                scratch[local_index] = other;
                sc_index[local_index] = other_index;
            }
        }
        barrier(CLK_LOCAL_MEM_FENCE);
    }

    if (local_index == 0) {
        result[get_group_id(0)] = sc_index[0];
    }
}

我想我应该将INFINITY更改为-INFINITY以查找最大值的索引。

此处双尖括号的确切含义是什么?

for(int offset = get_local_size(0) / 2;  offset > 0; offset >>= 1) {

感谢您的建议!

0 个答案:

没有答案