正确的是将数组从Metal Kernel函数传递给局部函数

And*_*rew 0 metal

我正在将计算模型从Cuda转换为Metal。

我正在尝试从内核函数传递给函数的一些全局结构数组。

我收到以下错误:

  1. 候选函数不可行:第二个参数没有从'float3 device [32]'到'float3 *'(aka'vector_float3 *')的已知转换

对于testFunction。

这是示例代码:

#include <metal_stdlib>
using namespace metal;

struct DOBJ
{
    int      num_vertex;        /* Number of vertcies */
    float3   vert       [32];  /* Rotated on CPU */    
};

bool testFunction(
                          uint num_vertex_B,
                          float3 Vertex_B[32])
{
}

kernel void TestKernel( device DOBJ *VolumeObject )
{

    int d_index = 5;

    bool SP_Check = testFunction(
                                    VolumeObject[d_index].num_vertex,
                                    VolumeObject[d_index].vert );
}
Run Code Online (Sandbox Code Playgroud)

CUDA testFunction的原始格式具有以下格式:

__device__ bool testFunction(
                          uint num_vertex_B,
                          float3 *Vertex_B)
{
}
Run Code Online (Sandbox Code Playgroud)

我在程序中有大量遵循此结构的代码。如何正确格式化testFunction以接受Vertex_B?

在warrenm的评论之后,还有更多测试代码

  1. 这样可以编译,但是我还没有尝试任何运行测试。
  2. 您可以具有两个“ testFunction”,它们的区别仅在于向量的存储位置,而是完全相同的代码。
  3. float3 Vertex_B [5]为线程存储器而不是设备存储器编译。

    #include <metal_stdlib>
    using namespace metal;
    
    struct DOBJ
    {
        int      num_vertex;        /* Number of vertcies */
        float3   vert       [32];  /* Rotated on CPU */ 
    };
    
    bool testFunction(uint num_vertex_B, device float3 *Vertex_B) { return false; }
    
    bool testFunction(uint num_vertex_B, thread float3 *Vertex_B) { return false; }
    
    bool testFunction2( uint num_vertex_B, float3 Vertex_B[5]) { return false; }
    
    kernel void VolumeObject_InteractionPolyhedra( device DOBJ *VolumeObject )
    {
    
        int d_index = 5;
    
        bool SP_Check = testFunction( VolumeObject[d_index].num_vertex, VolumeObject[d_index].vert);
    
        // Compiler error: 1. Candidate function not viable: no known conversion from 'float3 device[32]' to 'float3 *' (aka 'vector_float3 *') for 2nd argument
        // bool SP_Check2 = testFunction2( VolumeObject[d_index].num_vertex, VolumeObject[d_index].vert);
    
        thread float3 *vertList;
        bool SP_Check3 = testFunction( 5, vertList);
    
        bool SP_Check4 = testFunction2( 5, vertList);
    }
    
    Run Code Online (Sandbox Code Playgroud)

war*_*enm 5

无法使用C或C ++(金属阴影语言是方言)中的值传递数组。但是,您的函数可以采用指向数组的指针参数,只要指针与原始缓冲区位于相同的地址空间中即可:

bool testFunction(uint num_vertex_B, device float3 *Vertex_B)
{
}
Run Code Online (Sandbox Code Playgroud)