【问题标题】:Opencl loops in kernel内核中的 Opencl 循环
【发布时间】:2014-04-09 17:24:40
【问题描述】:

在代码块中有我的内核函数。它本质上计算哪个点离所有集群最远,结果保存在长度[3](点的id)中,并输出[0]与所属集群的距离。 while 部分进行简单的总和减少。我知道这不是最好的方法,但我需要了解为什么让一个集群代码正常工作,而不是两个或更多集群返回错误值。

__kernel void computeDistances(__global t_cluster *points,__global t_cluster *clusters,     __global float *output,__global t_cluster *support,__global short *lengths)
{
    int threadId = get_global_id(0);
    float bestVal = 0;
    int counter, offset;

    short idPoint, idCluster;
    for(idPoint = 0; idPoint < lengths[0]; idPoint++)
    {

        for(idCluster = 0; idCluster < lengths[2]; idCluster++)
        {     
            support[0].attributes[threadId] = pow( (points[idPoint].attributes[threadId] - clusters[idCluster].attributes[threadId]) , 2 );

            counter = SIZE;
            offset = 1;

            while(counter != 1)
            {
                counter = counter / 2 + (counter % 2);

                barrier(CLK_GLOBAL_MEM_FENCE);

                if(threadId % (2*offset) == 0)
                    if(threadId + offset < lengths[1])
                        support[0].attributes[threadId] = support[0].attributes[threadId] + support[0].attributes[threadId+offset];

                offset = offset * 2 ;
             }

             barrier(CLK_GLOBAL_MEM_FENCE);

            if(support[0].attributes[threadId] > bestVal)
                bestVal = support[0].attributes[threadId];

    }

    barrier(CLK_GLOBAL_MEM_FENCE);

    if(threadId == 0 && bestVal > output[threadId])
    {
        output[0] = bestVal;
        lengths[3] = idPoint;
    }
}

}

【问题讨论】:

    标签: opencl reduction


    【解决方案1】:

    您正在使用无法用于跨多个计算核心(工作组)同步的障碍。

    屏障同步只在同一个逻辑工作组内起作用。请参阅this post on Khronos 以更好地了解我所指的内容。

    增加集群大小也会增加正在使用的工作项的数量,这可能会使用多个工作组,这就是您遇到此问题的原因。

    编辑:可能值得指出的是,不能跨工作组使用同步原语。

    【讨论】:

    • 好的,这就是解决方案。谢谢!
    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 2016-04-06
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多