openCL内核计算Pi的值不正确

openCL内核计算Pi的值不正确,c,for-loop,kernel,opencl,pi,C,For Loop,Kernel,Opencl,Pi,你好 我有一个openCL内核,它使用莱布尼兹公式来计算pi。目前我的问题是,我得到的值不是pi,而是4 __kernel void calculatePi(int numIterations, __global float *outputPi, __local float* local_result, int numWorkers) { __private const uint gid = get_global_id(0);

你好

我有一个openCL内核,它使用莱布尼兹公式来计算pi。目前我的问题是,我得到的值不是pi,而是4

__kernel void calculatePi(int numIterations, __global float *outputPi,
                          __local float* local_result, int numWorkers)
{
    __private const uint gid = get_global_id(0);
    __private const uint lid = get_local_id(0);
    __private const uint offset = numIterations*gid*2; 
    __private float sum = 0.0f;

    // Have the first worker initialize local_result
    if (gid == 0)
    {
        for (int i = 0; i < numWorkers; i++)
        {
            local_result[i] = 0.0f;
        }
    }

    // Have all workers wait until this is completed
    barrier(CLK_GLOBAL_MEM_FENCE);

    // Have each worker calculate their portion of pi
    // This is a private value
    for (int i=0; i<numIterations; i++) 
    {
        if (i % 2 == 0)
        {
            sum += 1 / (1 + 2*i + offset);
        }
        else
        {
            sum -= 1 / (1 + 2*i + offset);
        }
    }

    // Have each worker move their value to the appropriate
    // local_result slot so that the first worker can see it
    // when reducing next
    local_result[gid] = sum;    

    // Make sure all workers complete this task before continuing
    barrier(CLK_LOCAL_MEM_FENCE);

    // Have the first worker add up all of the other worker's values
    // to get the final value
    if (lid == 0)
    {
        outputPi[0] = 0;
        for (int i = 0; i < numWorkers; i++)
        {
            outputPi[0] += local_result[i]; 
        }

        outputPi[0] *= 4;
    }    
}
然后,为第一个辅助进程返回的值是4,而不是预期的3.2

修改为除lid==0之外的任何其他数字时,所有其他工人都将其总和报告为0。所以我的问题是为什么这是计算值?我的求和变量有问题吗?这应该是一个私有变量,根据我对每个工作进程的理解,for循环应该是顺序的,但是许多循环是基于工作进程的数量并行执行的

这里有一个到我的github的链接,它上载了内核和主代码


谢谢

您正在代码中执行整数除法,应该是浮点数:

if (i % 2 == 0)
{
   sum += 1. / (1 + 2*i + offset); // notice the 1.
}
else
{
   sum -= 1. / (1 + 2*i + offset);
}

我希望偏移量=numIterations/(gid*2);还是我错了?不,我不这么认为。下一个worker的下一个术语(gid==1)应该从1/33开始(我显示了上面的前16个术语)。所以,偏移量是16*1*2=32。对于For循环的第一个循环,计算结果将是+1/(1+2(0)+32)=+1/33,这是预期的结果。为了理智起见,我也尝试了你所说的,结果产生了一个浮点异常(16/(15*2)=16/30,它不再是一个整数了,所以它是不正确的。
if (i % 2 == 0)
{
   sum += 1. / (1 + 2*i + offset); // notice the 1.
}
else
{
   sum -= 1. / (1 + 2*i + offset);
}