【问题标题】:openCL Kernel to calculate Pi is not correct valueopenCL 内核计算 Pi 的值不正确
【发布时间】:2019-04-23 04:52:36
【问题描述】:

早安,

我有一个使用 Leibniz 公式计算 pi 的 openCL 内核。目前我的问题是我返回的值不是 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;
    }    
}

我已将所有输入引导至输出,以验证它们是否符合我的预期。 numIterations 是 16,numWorkers 也是 16。

当为第一个工人计算总和时,我希望总和为 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15 + 1/17 - 1/19 + 1/21 - 1/23 + 1/25 - 1/27 + 1/29 - 1/31

前 16 次使用这个计算器,我预计结果在 3.2 左右:https://scratch.mit.edu/projects/19546118/

如果我将最后一段代码修改为这样,以便我可以查看工人的“总和”计算值:

    // Have the first worker add up all of the other worker's values
    // to get the final value
    if (lid == 0)
    {
        outputPi[0] = sum * 4;
    } 

那么第一个工人返回的值是 4 而不是预期的 3.2

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

这是我的 github 的链接,其中已上传内核和主要代码。

https://github.com/TreverWagenhals/TreverWagenhals/tree/master/School/Heterogeneous%20Computing/Lab2

谢谢

【问题讨论】:

  • 我希望 offset = numIterations / (gid*2);还是我错了?
  • 不,我不这么认为。下一个工人(gid==1)的下一个任期应该从 1/33 开始(我在上面展示了前 16 个任期)。因此,偏移量为 16*1*2 = 32。对于 for 循环的第一个循环,计算将是 + 1/(1 +2(0) + 32) = + 1/33,即是期待。出于理智,我也尝试了你所说的,它导致浮点异常 (16/(15*2) = 16/30,它不再是整数,所以它不正确是有道理的。

标签: c for-loop kernel opencl pi


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多