Sample code for MPSMatrixDecompositionCholesky?

I am trying to get my code to use MPSMatrixDecompositionCholesky correctly but it is giving me an incorrect matrix result.

Any help would be greatly appreciated!


let device = MTLCreateSystemDefaultDevice()!

let commandQueue = device.makeCommandQueue()


let M = 2


let row = M * MemoryLayout<Float>.stride

let matlength = M * row

let mdesc = MPSMatrixDescriptor(

dimensions: M, columns: M, rowBytes: row, dataType: MPSDataType.float32)


let arrayA: [Float] = [ 2.0 , 1.0 , 1.0 , 2.0 ]

let buffA = device.makeBuffer(bytes: arrayA, length: matlength)

let matA = MPSMatrix(buffer: buffA!, descriptor: mdesc)

let arrayB: [Float] = [ 1.0 , 0.0 , 0.0 , 0.0 ]

let buffB = device.makeBuffer(bytes: arrayB, length: matlength)

let matB = MPSMatrix(buffer: buffB!, descriptor: mdesc)

let arrayX: [Float] = [ 0.0 , 0.0 , 0.0 , 0.0 ]

let buffX = device.makeBuffer(bytes: arrayX, length: matlength)

let matX = MPSMatrix(buffer: buffX!, descriptor: mdesc)

let arrayL: [Float] = [ 0.0 , 0.0 , 0.0 , 0.0 ]

let buffL = device.makeBuffer(bytes: arrayL, length: matlength)

let matL = MPSMatrix(buffer: buffL!, descriptor: mdesc)


let decomp = MPSMatrixDecompositionCholesky(device: device, lower: true, order: M )


let commandBuffer = commandQueue?.makeCommandBuffer()


decomp.encode(commandBuffer: commandBuffer!,

sourceMatrix: matA, resultMatrix: matL, status: buffX)


commandBuffer?.commit()


let rawPointerL = matL.data.contents()

let countL = matL.rows * matL.columns

let typedPointerL = rawPointerL.bindMemory(to: Float.self, capacity: countL)

let bufferedPointerL = UnsafeBufferPointer(start: typedPointerL, count: countL)

print(" ")

print(" ",bufferedPointerL[0], bufferedPointerL[1])

print(" ",bufferedPointerL[2], bufferedPointerL[3])

print(" ")

So I think I'll focus on doing the decomposition on the CPU side and passing that on to the MPSMatrixSolveCholesky if it makes sense to use the GPU when using large matrices.

Instead of using MPSMatrixDecompositionCholesky on my iMac I am using CLPK sgotrf and then I pass the factorization to MPSMatrixSolveCholesky.

I know this is a lot of 'motion' ... and not really what I should be doing. Also, I didn't know how to pass my SparseFactor(SparseFactorizationCholesky, A) to Metal so that is why I am using sgotrf. I think the SparseFactor is opaque and I didn't know how to create the array for the Metal buffer from that.

that was supposed to be spotrf not sgotrf

How may I determine the Macintosh hardware model, as well as its GPU, during the execution of the code? What code will need to be added to determine the model ... so that I can branch to use the cpu, or, the gpu for my calculations?

My Cholesky decomposition issue has been addressed with the Mojave macOS (Apple said that it would be fixed with Mojave). When I went to clear the bug report that I had originally filed I couldn't find the bug report in Bug Reporter. So I am just reporting that the issue has been cleared here.

I discovered my Bug report on my other developer account. I closed the bug. The code now yields the correct results when using MPS Cholesky factorization on the iMac (2017) after updating to macOS Mojave 10.14.

Sample code for MPSMatrixDecompositionCholesky?
 
 
Q