【问题标题】:Passing array to function parameter in OpenCL将数组传递给 OpenCL 中的函数参数
【发布时间】:2012-06-15 14:26:47
【问题描述】:

如何将数组传递给 OpenCL 中的函数?我在行 c[n]=FindIndexFromArray(a,3) 中收到错误“..argument of type "_global float *" is incompatible with type of "float *";

float FindIndexFromArray(float myArray[], float Key)
{
    // simple looping to find the index
    for (int i=0;i<sizeof(myArray);i++)
       if (myArray[i]==Key)
         return i; 

}

kernel void ProcessArray(
    global read_only float* myArray,
    global read_only float* Key,
    global write_only float* c )
{
    int index = get_global_id(0); 
    c[index] = FindIndexFromArray(myArray, Key); // How do I pass myArray parameter?
}

我编辑的源代码:

float FindIndexFromArray(__global read_only float* myArray[], __global read_only float* Key)
{
    // simple looping to find the index
    for (int i=0;i<sizeof(myArray);i++)
       if (myArray[i]==Key)
         return i; 

}

kernel void ProcessArray(
    __global read_only float* myArray,
    __global read_only float* Key,
    __global write_only float* c )
{
    int index = get_global_id(0);
    c[index] = FindIndexFromArray(myArray, Key); // How do I pass myArray parameter?
}

【问题讨论】:

    标签: arrays opencl


    【解决方案1】:

    如错误消息中所述。您的myArrayKey 带有globalread-only 类型,因此在将其传递给另一个函数时必须声明相同的类型。简而言之,您的 FindIndexFromArray 应该是

    FindIndexFromArray(global read_only float* myArray, global read_only float* Key)
    

    【讨论】:

    • 我仍然收到几个警告,但这是错误消息:Operand types are incompatible ("float" and "__global float*" 在这一行 if (myArray[i]==Key)。我以为我已将 myArray 和 Key 声明为 __global 类型。我该如何解决这个问题?
    • 消除 myArray 中的 [],因为它改变了类型。关于 sizeof(myArray),它不会给你 malloc 的 myArray 的大小。而是将 malloc 大小传递给您的内核,然后再传递给您的设备函数。
    • 对不起,我忘记删除 [],编辑后的源代码现在会产生错误消息,正如我在第一条评论中所说的那样。
    • 好吧..你的代码在这里太乱了..首先,如果你想让你的函数返回int,然后从float更改为int。其次,你必须像我说的那样对sizeof(myArray) 做点什么。第三,无法完成(myArray[i]==Key) 行,因为您将浮点值与指向浮点的指针进行比较。应该是(myArray[i]==Key[i])(myArray[i]==*Key)..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 2013-01-27
    • 1970-01-01
    • 2012-12-07
    相关资源
    最近更新 更多