m3_bug / test.swift
Narsil's picture
Narsil HF staff
Upload test.swift
2e08f77
import Foundation
import Accelerate
import MetalPerformanceShaders
// Sizes of the matrices: C = A x B.
private let rowsA = 3
private let columnsA = 4
private let rowsB = columnsA
private let columnsB = 2
private let rowsC = rowsA
private let columnsC = columnsB
private var device: MTLDevice!
private var commandQueue: MTLCommandQueue!
private var matrixMultiplication: MPSMatrixMultiplication!
private var matrixA: MPSMatrix!
private var matrixB: MPSMatrix!
private var matrixC: MPSMatrix!
private var arrayA = [Float](repeating: 0, count: rowsA * columnsA)
private var arrayB = [Float](repeating: 0, count: rowsB * columnsB * 2)
private var arrayC = [Float](repeating: 0, count: rowsC * columnsC)
func run() {
randomizeArrays()
initMPS()
}
private func randomizeArrays() {
// Fill up A and B with random floating point numbers (between -1 and +1).
for i in 0..<arrayA.count {
arrayA[i] = Float(i)
}
for i in 0..<arrayB.count {
arrayB[i] = Float(i)
}
print(arrayB)
}
private func initMPS() {
device = MTLCreateSystemDefaultDevice()
guard device != nil else {
fatalError("Error: This device does not support Metal")
}
guard MPSSupportsMTLDevice(device) else {
fatalError("Error: This device does not support Metal Performance Shaders")
}
commandQueue = device.makeCommandQueue()
matrixMultiplication = MPSMatrixMultiplication(device: device, transposeLeft: false, transposeRight: false, resultRows: rowsC, resultColumns: columnsC, interiorColumns: columnsA, alpha: 1, beta: 0)
// For optimal speed, we should use the recommended row stride.
//let rowBytesA = MPSMatrixDescriptor.rowBytes(fromColumns: columnsA, dataType: .float32)
//print("preferred stride \(rowBytesA), my stride \(columnsA * MemoryLayout<Float>.stride)")
// The contents of the arrays are copied into the MTLBuffers. Note that we
// don't copy arrayC into bufferC because it's just zeros (arrayC is only
// used to store the results of the BLAS matrix multiply).
let bufferA = device.makeBuffer(bytes: arrayA, length: rowsA * columnsA * MemoryLayout<Float>.stride, options: [])
let bufferB = device.makeBuffer(bytes: arrayB, length: rowsB * columnsB * MemoryLayout<Float>.stride * 2, options: [])
let bufferC = device.makeBuffer(length: rowsC * columnsC * MemoryLayout<Float>.stride, options: [])
let descA = MPSMatrixDescriptor(dimensions: rowsA, columns: columnsA, rowBytes: columnsA * MemoryLayout<Float>.stride, dataType: .float32)
let descB = MPSMatrixDescriptor(dimensions: rowsB, columns: columnsB, rowBytes: columnsB * MemoryLayout<Float>.stride, dataType: .float32)
let descC = MPSMatrixDescriptor(dimensions: rowsC, columns: columnsC, rowBytes: columnsC * MemoryLayout<Float>.stride, dataType: .float32)
matrixA = MPSMatrix(buffer: bufferA!, descriptor: descA)
matrixB = MPSMatrix(buffer: bufferB!, offset: 0, descriptor: descB)
matrixC = MPSMatrix(buffer: bufferC!, descriptor: descC)
var commandBuffer = commandQueue.makeCommandBuffer()!
matrixMultiplication.encode(commandBuffer: commandBuffer, leftMatrix: matrixA, rightMatrix: matrixB, resultMatrix: matrixC)
commandBuffer.commit()
commandBuffer.waitUntilCompleted()
var contents = bufferC!.contents();
var count = rowsA * columnsB;
var typedPointer = contents.bindMemory(to: Float.self, capacity: count)
var bufferedPointer = UnsafeBufferPointer(start: typedPointer, count: count)
print(Array(bufferedPointer))
print("Offsetted")
matrixA = MPSMatrix(buffer: bufferA!, descriptor: descA)
matrixB = MPSMatrix(buffer: bufferB!, offset: 4 * 2 * 4, descriptor: descB)
matrixC = MPSMatrix(buffer: bufferC!, descriptor: descC)
commandBuffer = commandQueue.makeCommandBuffer()!
matrixMultiplication.encode(commandBuffer: commandBuffer, leftMatrix: matrixA, rightMatrix: matrixB, resultMatrix: matrixC)
commandBuffer.commit()
commandBuffer.waitUntilCompleted()
contents = bufferC!.contents();
count = rowsA * columnsB;
typedPointer = contents.bindMemory(to: Float.self, capacity: count)
bufferedPointer = UnsafeBufferPointer(start: typedPointer, count: count)
print(Array(bufferedPointer))
}
run()