c++ - newComputePipelineStateWithFunction 失败

标签 c++ metal

我正在尝试让神经网络在 Metal 上运行。 基本思想是数据复制。每个 GPU 线程为随机数据点运行一个版本的网络。

我编写了其他运行良好的着色器。

我还在 C++ 命令行应用程序中尝试了我的代码。那里没有错误。 也没有编译错误。

我使用 Apple 文档转换为 Metal C++,因为并非支持 C++11 的所有内容。

它在加载内核函数后以及尝试将 newComputePipelineStateWithFunction 分配给 Metal 设备时崩溃。这意味着在编译时未捕获的代码存在问题。

MCVE:

kernel void net(const device float *inputsVector [[ buffer(0) ]], // layout of net *
                uint id [[ thread_position_in_grid ]]) {

    uint floatSize = sizeof(tempFloat);
    uint inputsVectorSize = sizeof(inputsVector) / floatSize;

    float newArray[inputsVectorSize];


    float test = inputsVector[id];

    newArray[id] = test;

}

更新

它与动态数组有关。

由于它无法创建管道状态并且不会在运行实际着色器时崩溃,所以它一定是编码问题。不是输入问题。

将动态数组中的值分配给缓冲区会使它失败。

最佳答案

真正的问题: 是内存问题!

对于所有说这是内存问题的人,您是对的! 这是一些伪代码来说明它。抱歉,它在“Swift”中,但更容易阅读。 Metal 着色器有一种时髦的方式来生活。它们首先在没有值的情况下初始化以获取内存。正是这一步失败了,因为它依赖于后面的一步:设置缓冲区。

这一切都归结为哪些值在何时可用。我对 newComputePipelineStateWithFunction 的理解是错误的。它不是简单地获取着色器功能。这也是初始化过程中的一小步。

class MetalShader {

    // buffers
    var aBuffer : [Float]
    var aBufferCount : Int

    // step One : newComputePipelineStateWithFunction
    memory init() {
        // assign shader memory

        // create memory for one int
        let aStaticValue : Int
        // create memory for one int
        var aNotSoStaticValue : Int // this wil succeed, assigns memory for one int

        // create memory for 10 floats
        var aStaticArray : [Float] = [Float](count: aStaticValue, repeatedValue: y) // this will succeed

        // create memory for x floats
        var aDynamicArray : [Float] = [Float](count: aBuffer.count, repeatedValue: y) // this will fail
        var aDynamicArray : [Float] = [Float](count: aBufferCount, repeatedValue: y) // this will fail

        let tempValue : Float // one float from a loop

    }

    // step Two : commandEncoder.setBuffer()
    assign buffers (buffers) {

        aBuffer = cpuMemoryBuffer

    }

    // step Three : commandEncoder.endEncoding()
    actual init() {
        // set shader values

        let aStaticValue : Int = 0

        var aNotSoStaticValue : Int = aBuffer.count

        var aDynamicArray : [Float] = [Float](count: aBuffer.count, repeatedValue: 1) // this could work, but the app already crashed before getting to this point.

    }

    // step Four : commandBuffer.commit()
    func shaderFunction() {
        // do stuff
        for i in 0..<aBuffer.count {

            let tempValue = aBuffer[i]

        }
    }
}

修复:

我终于意识到缓冲区在技术上是动态数组,而不是在着色器中创建数组,我也可以只添加更多缓冲区。这显然有效。

关于c++ - newComputePipelineStateWithFunction 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32193726/

相关文章:

c++ - 在 C++ 中优化 3D 成像过程

C++ boost 线程重用线程

ios - 关于 Metal 中线程组内存的问题

ios - 为什么基于OpenGL的iOS应用程序将在审查过程中被拒绝?

metal - 是否有 Metal 库函数来创建 simd 旋转矩阵?

c++ - 查找数字数组中最大差异的算法

c++ - 在 osgSim::DOFTransform 中获取旋转轴?

c++ - 用32位原子实现64位原子计数器

ios - 在 iOS 上渲染到 CVPixelBuffer

buffer - Metal 实例渲染——几个问题