在OpenCL私有内存中对小数组进行部分排序

时间:2016-02-03 22:26:19

标签: opencl gpgpu

任务是在数组中找到N个最大元素。该阵列非常小(约40项)。我正在使用这个算法:

    float max1 = -inf;
    int max1I = -1;
    float max2 = -inf;
    int max2I = -1;
    float max3 = -inf;
    int max3I = -1;
    float max4 = -inf;
    int max4I = -1;
    float max5 = -inf;
    int max5I = -1;
    float performances[MAX_NUMBER_OF_SELECTIONS];
    for (int i = 0; i < numberOfSelections; ++i) {
        float performance = /*some calculations*/;
        performances[i] = performance;
        if (performance > max1) {
            max5 = max4; max5I = max4I;
            max4 = max3; max4I = max3I;
            max3 = max2; max3I = max2I;
            max2 = max1; max2I = max1I;
            max1 = performance; max1I = i;
        } else if (performance > max2) {
            max5 = max4; max5I = max4I;
            max4 = max3; max4I = max3I;
            max3 = max2; max3I = max2I;
            max2 = performance; max2I = i;
        } else if (performance > max3) {
            max5 = max4; max5I = max4I;
            max4 = max3; max4I = max3I;
            max3 = performance; max3I = i;
        } else if (performance > max4) {
            max5 = max4; max5I = max4I;
            max4 = performance; max4I = i;
        } else if (performance > max5) {
            max5 = performance; max5I = i;
        }
    }

这种方法已经足够好了,但现在我需要将它作为top10而不是top5。我应该复制粘贴这种模式吗?或者有更好的东西?

1 个答案:

答案 0 :(得分:1)

如果您想在大型数组上执行此代码,则该代码无效。 我假设你有很多小数组,每个工作项都适用于其中一个。

我会做类似的事情:

.placeholded label {
    font-size: 0.8rem;
    -webkit-transform: translateY(-140%);
    transform: translateY(-140%);
}

现在你有一个11个元素的数组,从小到高排序,只需要最后10个元素。

代码可以进一步优化,但非常简单。