0
votes

What I'm trying to do

I found an example for a 1d array. Works great. I have no idea how to actually make the buffer the appropriate size, and how to cast the array I get back to a 2d array. Does it have to be Float?

Swift


# input buffer
// Create the buffers to be sent to the gpu from our arrays
let arr1Buff = device?.makeBuffer(bytes: arr1,
                                      length: MemoryLayout<Float>.size * count,
                                      options: .storageModeShared)

let arr2Buff = device?.makeBuffer(bytes: arr2,
                                      length: MemoryLayout<Float>.size * count,
                                      options: .storageModeShared)

let resultBuff = device?.makeBuffer(length: MemoryLayout<Float>.size * count,
                                        options: .storageModeShared)

// Create a buffer to be sent to the command queue
let commandBuffer = commandQueue?.makeCommandBuffer()

// Create an encoder to set values on the compute function
let commandEncoder = commandBuffer?.makeComputeCommandEncoder()
commandEncoder?.setComputePipelineState(additionComputePipelineState)

// Set the parameters of our gpu function
commandEncoder?.setBuffer(arr1Buff, offset: 0, index: 0)
commandEncoder?.setBuffer(arr2Buff, offset: 0, index: 1)
commandEncoder?.setBuffer(resultBuff, offset: 0, index: 2)

// left some stuff out for readability
...

// read result buffer
for i in 0..<3 {
        print("\(arr1[i]) + \(arr2[i]) = \(Float(resultBufferPointer!.pointee) as Any)")
        resultBufferPointer = resultBufferPointer?.advanced(by: 1)
    }

Metal cpp

#include <metal_stdlib>
using namespace metal;

kernel void addition_compute_function(constant float *arr1        [[ buffer(0) ]],
                                      constant float *arr2        [[ buffer(1) ]],
                                      device   float *resultArray [[ buffer(2) ]],
                                               uint   index [[ thread_position_in_grid ]]) {



    resultArray[index] = arr1[index] + arr2[index];
}

What I tried

I found this question asking the same, but for Objective-C. It's missing the part where you put stuff into the buffer and read it back. It's also not in cpp / Swift. I have a syntax problem, not a theoretical one.