3

我需要从MTLBufferGPU 上的后处理中读取数据。到目前为止,我已经尝试过以下代码,但它总是因EXC_BAD_ACCESS错误代码而崩溃。

struct gauss_model {
  var mean : [Float32] = [0.0, 0.0, 0.0];
  var covMat : [[Float32]] = [[0.0, 0.0, 0.0],[0.0, 0.0, 0.0],[0.0, 0.0, 0.0]];
  var invCovMat : [[Float32]] = [[0.0, 0.0, 0.0],[0.0, 0.0, 0.0],[0.0, 0.0, 0.0]];
  var samples : Int32 = 0;
}

self.gaussModels = [gauss_model](count: Int(10), repeatedValue: gauss_model())
self.modelsBuffer = self.device.newBufferWithBytes(self.gaussModels, length:      self.gaussModels.count * sizeof(gauss_model), options: MTLResourceOptions.OptionCPUCacheModeDefault)
commandEncoder.setBuffer(self.modelsBuffer, offset: 0, atIndex: 0)

    // execute GPU code

var model = unsafeBitCast(self.modelsBuffer.contents(), UnsafeMutablePointer<gauss_model>.self)
NSLog("%@", model.memory.mean) // crashes on this statement

我也尝试过不同的方法来获得价值,比如

var model = UnsafeMutablePointer<gauss_model>(self.modelsBuffer.contents())
// iterate over models with model.memory and model.successor()

或者

var model = UnsafeMutablePointer<[gauss_model]>(self.modelBuffer.contents())
let models : [gauss_model] = model.memory

但他们都没有工作。有没有办法做到这一点?

4

2 回答 2

0

I've managed to fix the problem. The problem was in my wrong assumption about memory management in Swift and the fact that the function newBufferWithBytes does only a shallow copy. Calling the function only copied the pointers to mean, covMat and invCovMat arrays and self.modelBuffer.contents() contained pointers to uninitialized memory. Accessing the memory was causing the crash.

于 2015-01-02T20:32:59.153 回答
0

如果你来自谷歌试图读取一个简单的整数 MTLBuffer,我就是这样做的。

我对我的代码不是很有信心,但它似乎有效。(我还向 Apple 发送了一个错误报告,因为这方面的 swift 文档是用 Objective-C 编写的)

let bufferSize = 10
let rawbufferSize = bufferSize * MemoryLayout<Int>.stride

let oneArray: [Int] = Array(repeating: 1, count: bufferSize)

let BufferA = device.makeBuffer(bytes: oneArray, length: rawbufferSize, options: MTLResourceOptions.storageModeShared)
let BufferB = device.makeBuffer(bytes: oneArray, length: rawbufferSize, options: MTLResourceOptions.storageModeShared)
let BufferResult = device.makeBuffer(bytes: oneArray, length: rawbufferSize, options: MTLResourceOptions.storageModeShared)

let commandBuffer = queue?.makeCommandBuffer()
let computeEncoder = commandBuffer?.makeComputeCommandEncoder()

computeEncoder?.setComputePipelineState(pipelineState)
computeEncoder?.setBuffer(BufferA, offset: 0, index: 0)
computeEncoder?.setBuffer(BufferB, offset: 0, index: 1)
computeEncoder?.setBuffer(BufferResult, offset: 0, index: 2)

let gridSize = MTLSizeMake(bufferSize * MemoryLayout<Int>.stride, 1, 1)

let threadGroupSize = pipelineState.maxTotalThreadsPerThreadgroup > bufferSize ? bufferSize : pipelineState.maxTotalThreadsPerThreadgroup

computeEncoder?.dispatchThreads(
    MTLSizeMake(rawbufferSize, 1, 1),
    threadsPerThreadgroup: MTLSizeMake(threadGroupSize, 1, 1 ))

computeEncoder?.endEncoding()
commandBuffer?.commit()
commandBuffer?.waitUntilCompleted()

// Ewww
var rawPointer = BufferResult?.contents()
var typedPointer = rawPointer?.bindMemory(to: Int.self, capacity: rawbufferSize)
let bufferedPointer = UnsafeBufferPointer(start: typedPointer, count: rawbufferSize)
for i in 0..<bufferSize {
    print(bufferedPointer[i])
}
于 2021-10-29T06:34:31.137 回答