random123

Counter-based Random Number Generators
git clone git://git.meso-star.com/random123.git
Log | Files | Refs | README | LICENSE

pi_metal.m (4022B)


      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 // functions to do boilerplate Metal begin and end
     38 #include "../tests/util_metal.h"
     39 #include "pi_check.h"
     40 
     41 const char *progname;
     42 int verbose = 0;
     43 int debug = 0;
     44 
     45 int
     46 main(int argc, char **argv)
     47 {
     48     unsigned count = argc > 1 ? atoi(argv[1]) : 0;
     49     UMetalInfo *infop;
     50     size_t i, nthreads = 1024, hits_sz;
     51     uint *hits_host;
     52     uint *tries_host;
     53     NSString *kernelname = @"counthits";
     54     NSError *err = nil;
     55     double d = 0.;
     56     id<MTLFunction> function;
     57     id<MTLBuffer> hits_dev, tries_dev;
     58     id<MTLCommandBuffer> buffer;
     59     id<MTLComputeCommandEncoder> encoder;
     60     id<MTLComputePipelineState> pipeline;
     61 
     62     d = timer(&d);
     63     progname = argv[0];
     64     verbose = debug = argc > 2 ? atoi(argv[2]): 0;
     65     infop = metal_init(argc > 3 ? argv[3] : NULL, "pi_metal_kernel.metallib");
     66     CHECKNOTZERO(function = [infop->library newFunctionWithName: kernelname]);
     67     CHECKNOTZERO(hits_dev = [infop->device newBufferWithLength: sizeof(uint) * nthreads options:0]);
     68     CHECKNOTZERO(tries_dev = [infop->device newBufferWithLength: sizeof(uint) * nthreads options:0]);
     69 
     70     if (count == 0)
     71 	count = NTRIES/nthreads;
     72 
     73     CHECKNOTZERO(buffer = [infop->queue commandBuffer]);
     74     CHECKNOTZERO(encoder = [buffer computeCommandEncoder]);
     75     CHECKERR(pipeline = [infop->device newComputePipelineStateWithFunction:function error:&err]);
     76     [encoder setComputePipelineState:pipeline];
     77     [encoder setBytes:&count length:sizeof(uint) atIndex:0];
     78     [encoder setBuffer:hits_dev offset:0 atIndex:1];
     79     [encoder setBuffer:tries_dev offset:0 atIndex:2];
     80 
     81     MTLSize threadsPerThreadgroup = MTLSizeMake([pipeline maxTotalThreadsPerThreadgroup], 1, 1);
     82     MTLSize grid = MTLSizeMake(nthreads, 1, 1);
     83 
     84     [encoder dispatchThreads:grid threadsPerThreadgroup:threadsPerThreadgroup];
     85     [encoder endEncoding];
     86     [buffer commit];
     87     [buffer waitUntilCompleted];
     88 
     89     hits_host = [hits_dev contents];
     90     tries_host = [tries_dev contents];
     91 
     92     unsigned long hits = 0, tries = 0;
     93     for (i = 0; i < nthreads; i++) {
     94 	if (debug)
     95 	    printf("%lu %u %u\n", (unsigned long)i, hits_host[i], tries_host[i]);
     96 	hits += hits_host[i];
     97 	tries += tries_host[i];
     98     }
     99     metal_done(infop);
    100     [function release];
    101     [hits_dev release];
    102     [tries_dev release];
    103     [buffer release];
    104     [encoder release];
    105     [pipeline release];
    106     return pi_check(hits, tries);
    107 }