kat_metal.m (3530B)
1 /* 2 Copyright 2010-2011, D. E. Shaw Research. 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are 7 met: 8 9 * Redistributions of source code must retain the above copyright 10 notice, this list of conditions, and the following disclaimer. 11 12 * Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions, and the following disclaimer in the 14 documentation and/or other materials provided with the distribution. 15 16 * Neither the name of D. E. Shaw Research nor the names of its 17 contributors may be used to endorse or promote products derived from 18 this software without specific prior written permission. 19 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 // Simple Metal device kernel and host main program to 33 // compute pi via random darts at a square 34 // 35 // Written by Tom Schoonjans <Tom.Schoonjans@me.com> 36 37 // We're compiling on the host, so we don't include metalfeatures.h, 38 // but metal doesn't have 64-bit arithmetic, so we turn it off here. 39 #define R123_USE_64BIT 0 40 // functions to do boilerplate Metal begin and end 41 #include "../tests/util_metal.h" 42 #include "kat_main.h" 43 44 void host_execute_tests(kat_instance *tests, unsigned ntests){ 45 UMetalInfo *infop; 46 size_t i, nthreads = 1024, hits_sz; 47 uint *tests_host; 48 NSString *kernelname = @"dev_execute_tests"; 49 NSError *err = nil; 50 id<MTLFunction> function; 51 id<MTLBuffer> tests_dev; 52 size_t tests_sz; 53 id<MTLCommandBuffer> buffer; 54 id<MTLComputeCommandEncoder> encoder; 55 id<MTLComputePipelineState> pipeline; 56 57 infop = metal_init(NULL, "kat_metal_kernel.metallib"); 58 CHECKNOTZERO(function = [infop->library newFunctionWithName: kernelname]); 59 tests_sz = sizeof(kat_instance) * (ntests+1); // +1 for sentinel test with method==last 60 CHECKNOTZERO(tests_dev = [infop->device newBufferWithBytes: tests length: tests_sz options:0]); 61 62 CHECKNOTZERO(buffer = [infop->queue commandBuffer]); 63 CHECKNOTZERO(encoder = [buffer computeCommandEncoder]); 64 CHECKERR(pipeline = [infop->device newComputePipelineStateWithFunction:function error:&err]); 65 [encoder setComputePipelineState:pipeline]; 66 [encoder setBuffer:tests_dev offset:0 atIndex:0]; 67 68 MTLSize threadsPerThreadgroup = MTLSizeMake([pipeline maxTotalThreadsPerThreadgroup], 1, 1); 69 MTLSize grid = MTLSizeMake(nthreads, 1, 1); 70 71 [encoder dispatchThreads:grid threadsPerThreadgroup:threadsPerThreadgroup]; 72 [encoder endEncoding]; 73 [buffer commit]; 74 [buffer waitUntilCompleted]; 75 76 tests_host = [tests_dev contents]; 77 78 memcpy(tests, tests_host, tests_sz); 79 metal_done(infop); 80 [function release]; 81 [tests_dev release]; 82 [buffer release]; 83 [encoder release]; 84 [pipeline release]; 85 }