我试图让一个神经网络在金属上运行.基本思想是数据重复.每个gpu线程为随机数据点运行一个网络版本.
我写过其他着色器工作正常.
我还在c ++命令行应用程序中尝试了我的代码.没有错误.也没有编译错误.
我使用apple文档转换为metal c ++,因为不支持c ++ 11中的所有内容.
它在加载内核函数后以及在尝试分配newComputePipelineStateWithFunction给金属设备时崩溃.这意味着在编译时未捕获的代码存在问题.
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;
}
Run Code Online (Sandbox Code Playgroud)
更新
它与动态数组有关.
由于它无法创建管道状态并且不会崩溃运行实际着色器,因此它必须是编码问题.不是输入问题.
将动态数组中的值分配给缓冲区会使其失败.
真正的问题: 这是一个内存问题!
对于所有说这是内存问题的人,您是对的!这是一些伪代码来说明它。抱歉,它是“Swift”格式的,但更容易阅读。金属着色器以一种时髦的方式变得栩栩如生。它们首先在没有值的情况下进行初始化以获取内存。这一步失败了,因为它依赖于后面的步骤:设置缓冲区。
这一切都取决于哪些值何时可用。我的理解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]
}
}
}
Run Code Online (Sandbox Code Playgroud)
使固定:
我终于意识到缓冲区在技术上是动态数组,我也可以添加更多缓冲区,而不是在着色器内创建数组。这显然是有效的。