Narsil HF staff commited on
Commit
2e08f77
1 Parent(s): cc2534e

Upload test.swift

Browse files
Files changed (1) hide show
  1. test.swift +104 -0
test.swift ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import Foundation
2
+ import Accelerate
3
+ import MetalPerformanceShaders
4
+
5
+ // Sizes of the matrices: C = A x B.
6
+ private let rowsA = 3
7
+ private let columnsA = 4
8
+ private let rowsB = columnsA
9
+ private let columnsB = 2
10
+ private let rowsC = rowsA
11
+ private let columnsC = columnsB
12
+
13
+ private var device: MTLDevice!
14
+ private var commandQueue: MTLCommandQueue!
15
+
16
+ private var matrixMultiplication: MPSMatrixMultiplication!
17
+ private var matrixA: MPSMatrix!
18
+ private var matrixB: MPSMatrix!
19
+ private var matrixC: MPSMatrix!
20
+
21
+ private var arrayA = [Float](repeating: 0, count: rowsA * columnsA)
22
+ private var arrayB = [Float](repeating: 0, count: rowsB * columnsB * 2)
23
+ private var arrayC = [Float](repeating: 0, count: rowsC * columnsC)
24
+
25
+ func run() {
26
+ randomizeArrays()
27
+ initMPS()
28
+
29
+ }
30
+
31
+ private func randomizeArrays() {
32
+ // Fill up A and B with random floating point numbers (between -1 and +1).
33
+ for i in 0..<arrayA.count {
34
+ arrayA[i] = Float(i)
35
+ }
36
+ for i in 0..<arrayB.count {
37
+ arrayB[i] = Float(i)
38
+ }
39
+ print(arrayB)
40
+ }
41
+
42
+ private func initMPS() {
43
+ device = MTLCreateSystemDefaultDevice()
44
+ guard device != nil else {
45
+ fatalError("Error: This device does not support Metal")
46
+ }
47
+
48
+ guard MPSSupportsMTLDevice(device) else {
49
+ fatalError("Error: This device does not support Metal Performance Shaders")
50
+ }
51
+
52
+ commandQueue = device.makeCommandQueue()
53
+
54
+ matrixMultiplication = MPSMatrixMultiplication(device: device, transposeLeft: false, transposeRight: false, resultRows: rowsC, resultColumns: columnsC, interiorColumns: columnsA, alpha: 1, beta: 0)
55
+
56
+ // For optimal speed, we should use the recommended row stride.
57
+ //let rowBytesA = MPSMatrixDescriptor.rowBytes(fromColumns: columnsA, dataType: .float32)
58
+ //print("preferred stride \(rowBytesA), my stride \(columnsA * MemoryLayout<Float>.stride)")
59
+
60
+ // The contents of the arrays are copied into the MTLBuffers. Note that we
61
+ // don't copy arrayC into bufferC because it's just zeros (arrayC is only
62
+ // used to store the results of the BLAS matrix multiply).
63
+ let bufferA = device.makeBuffer(bytes: arrayA, length: rowsA * columnsA * MemoryLayout<Float>.stride, options: [])
64
+ let bufferB = device.makeBuffer(bytes: arrayB, length: rowsB * columnsB * MemoryLayout<Float>.stride * 2, options: [])
65
+ let bufferC = device.makeBuffer(length: rowsC * columnsC * MemoryLayout<Float>.stride, options: [])
66
+
67
+ let descA = MPSMatrixDescriptor(dimensions: rowsA, columns: columnsA, rowBytes: columnsA * MemoryLayout<Float>.stride, dataType: .float32)
68
+ let descB = MPSMatrixDescriptor(dimensions: rowsB, columns: columnsB, rowBytes: columnsB * MemoryLayout<Float>.stride, dataType: .float32)
69
+ let descC = MPSMatrixDescriptor(dimensions: rowsC, columns: columnsC, rowBytes: columnsC * MemoryLayout<Float>.stride, dataType: .float32)
70
+
71
+ matrixA = MPSMatrix(buffer: bufferA!, descriptor: descA)
72
+ matrixB = MPSMatrix(buffer: bufferB!, offset: 0, descriptor: descB)
73
+ matrixC = MPSMatrix(buffer: bufferC!, descriptor: descC)
74
+ var commandBuffer = commandQueue.makeCommandBuffer()!
75
+
76
+ matrixMultiplication.encode(commandBuffer: commandBuffer, leftMatrix: matrixA, rightMatrix: matrixB, resultMatrix: matrixC)
77
+
78
+ commandBuffer.commit()
79
+ commandBuffer.waitUntilCompleted()
80
+ var contents = bufferC!.contents();
81
+ var count = rowsA * columnsB;
82
+ var typedPointer = contents.bindMemory(to: Float.self, capacity: count)
83
+ var bufferedPointer = UnsafeBufferPointer(start: typedPointer, count: count)
84
+ print(Array(bufferedPointer))
85
+
86
+ print("Offsetted")
87
+ matrixA = MPSMatrix(buffer: bufferA!, descriptor: descA)
88
+ matrixB = MPSMatrix(buffer: bufferB!, offset: 4 * 2 * 4, descriptor: descB)
89
+ matrixC = MPSMatrix(buffer: bufferC!, descriptor: descC)
90
+ commandBuffer = commandQueue.makeCommandBuffer()!
91
+
92
+ matrixMultiplication.encode(commandBuffer: commandBuffer, leftMatrix: matrixA, rightMatrix: matrixB, resultMatrix: matrixC)
93
+
94
+ commandBuffer.commit()
95
+ commandBuffer.waitUntilCompleted()
96
+ contents = bufferC!.contents();
97
+ count = rowsA * columnsB;
98
+ typedPointer = contents.bindMemory(to: Float.self, capacity: count)
99
+ bufferedPointer = UnsafeBufferPointer(start: typedPointer, count: count)
100
+ print(Array(bufferedPointer))
101
+ }
102
+
103
+
104
+ run()