|
| 1 | +import "../index.js"; |
| 2 | + |
| 3 | +objc.import("Metal"); |
| 4 | + |
| 5 | +const arrayLength = 1 << 24; |
| 6 | +const bufferSize = arrayLength * Float32Array.BYTES_PER_ELEMENT; |
| 7 | + |
| 8 | +function MTLSizeMake(width, height, depth) { |
| 9 | + return { width, height, depth }; |
| 10 | +} |
| 11 | + |
| 12 | +function getFloat32Array(ptr, size) { |
| 13 | + const ab = objc.getArrayBuffer(ptr, size); |
| 14 | + return new Float32Array(ab); |
| 15 | +} |
| 16 | + |
| 17 | +class MetalAdder { |
| 18 | + constructor(device) { |
| 19 | + this.device = device; |
| 20 | + |
| 21 | + const library = device.newLibraryWithSourceOptionsError( |
| 22 | + ` |
| 23 | + #include <metal_stdlib> |
| 24 | + using namespace metal; |
| 25 | + |
| 26 | + kernel void add_arrays(device const float* inA, |
| 27 | + device const float* inB, |
| 28 | + device float* result, |
| 29 | + uint index [[thread_position_in_grid]]) |
| 30 | + { |
| 31 | + result[index] = inA[index] + inB[index]; |
| 32 | + } |
| 33 | + `, |
| 34 | + null, |
| 35 | + null, |
| 36 | + ); |
| 37 | + |
| 38 | + const addFunction = library.newFunctionWithName("add_arrays"); |
| 39 | + |
| 40 | + const error = new ObjectRef(); |
| 41 | + this.pipelineState = device.newComputePipelineStateWithFunctionError( |
| 42 | + addFunction, |
| 43 | + error, |
| 44 | + ); |
| 45 | + if (!this.pipelineState) { |
| 46 | + console.log(error.value); |
| 47 | + } |
| 48 | + |
| 49 | + this.commandQueue = device.newCommandQueue(); |
| 50 | + } |
| 51 | + |
| 52 | + prepareData() { |
| 53 | + this.mBufferA = this.device.newBufferWithLengthOptions( |
| 54 | + bufferSize, |
| 55 | + MTLResourceOptions.storageModeShared, |
| 56 | + ); |
| 57 | + this.mBufferB = this.device.newBufferWithLengthOptions( |
| 58 | + bufferSize, |
| 59 | + MTLResourceOptions.storageModeShared, |
| 60 | + ); |
| 61 | + this.mBufferResult = this.device.newBufferWithLengthOptions( |
| 62 | + bufferSize, |
| 63 | + MTLResourceOptions.storageModeShared, |
| 64 | + ); |
| 65 | + |
| 66 | + this.generateRandomFloatData(this.mBufferA); |
| 67 | + this.generateRandomFloatData(this.mBufferB); |
| 68 | + } |
| 69 | + |
| 70 | + sendComputeCommand() { |
| 71 | + const commandBuffer = this.commandQueue.commandBuffer(); |
| 72 | + if (commandBuffer === null) { |
| 73 | + throw new Error("Failed to create command buffer"); |
| 74 | + } |
| 75 | + |
| 76 | + const commandEncoder = commandBuffer.computeCommandEncoder(); |
| 77 | + if (commandEncoder === null) { |
| 78 | + throw new Error("Failed to create command encoder"); |
| 79 | + } |
| 80 | + |
| 81 | + this.encodeAddCommand(commandEncoder); |
| 82 | + |
| 83 | + commandEncoder.endEncoding(); |
| 84 | + |
| 85 | + commandBuffer.commit(); |
| 86 | + |
| 87 | + commandBuffer.waitUntilCompleted(); |
| 88 | + |
| 89 | + this.verifyResults(); |
| 90 | + } |
| 91 | + |
| 92 | + encodeAddCommand(computeEncoder) { |
| 93 | + computeEncoder.setComputePipelineState(this.pipelineState); |
| 94 | + computeEncoder.setBufferOffsetAtIndex(this.mBufferA, 0, 0); |
| 95 | + computeEncoder.setBufferOffsetAtIndex(this.mBufferB, 0, 1); |
| 96 | + computeEncoder.setBufferOffsetAtIndex(this.mBufferResult, 0, 2); |
| 97 | + |
| 98 | + const gridSize = MTLSizeMake(arrayLength, 1, 1); |
| 99 | + |
| 100 | + const threadGroupSize = this.pipelineState.maxTotalThreadsPerThreadgroup; |
| 101 | + if (threadGroupSize > arrayLength) { |
| 102 | + threadGroupSize = arrayLength; |
| 103 | + } |
| 104 | + |
| 105 | + const threadgroupSize = MTLSizeMake(threadGroupSize, 1, 1); |
| 106 | + |
| 107 | + computeEncoder.dispatchThreadsThreadsPerThreadgroup( |
| 108 | + gridSize, |
| 109 | + threadgroupSize, |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + generateRandomFloatData(buffer) { |
| 114 | + const contents = getFloat32Array(buffer.contents(), bufferSize); |
| 115 | + for (let i = 0; i < arrayLength; i++) { |
| 116 | + contents[i] = Math.floor(Math.random() * 100); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + verifyResults() { |
| 121 | + const a = getFloat32Array(this.mBufferA.contents(), bufferSize); |
| 122 | + const b = getFloat32Array(this.mBufferB.contents(), bufferSize); |
| 123 | + const result = getFloat32Array(this.mBufferResult.contents(), bufferSize); |
| 124 | + |
| 125 | + for (let i = 0; i < arrayLength; i++) { |
| 126 | + if (a[i] + b[i] !== result[i]) { |
| 127 | + console.log("Results don't match!", i, a[i], b[i], result[i]); |
| 128 | + return; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + console.log("Results match!"); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +const device = MTLCopyAllDevices().firstObject; |
| 137 | + |
| 138 | +const adder = new MetalAdder(device); |
| 139 | + |
| 140 | +adder.prepareData(); |
| 141 | + |
| 142 | +adder.sendComputeCommand(); |
0 commit comments