pi_microurng.cpp (4248B)
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 // Everyone's favorite PRNG example: calculate pi/4 by throwing darts 33 // at a square board and counting the fraction that are inside the 34 // inscribed circle. 35 36 // This version uses Philox4x32 with a MicroURNG and the C++11 standard 37 // library std::uniform_real distribution to generate floats in [-1..1] 38 39 // N.B. The results are hardware dependent even though the underlying 40 // counter based RNG is hardware and endian-invariant. On x86, 41 // floating point temporaries, e.g., x, y, x*x, etc., are stored in 42 // 80-bit extended precision registers. On x86-64 (and other IEEE-754 43 // systems), temporaries are stored in 32-bit SSE registers. 44 45 #include <Random123/philox.h> 46 #include <Random123/MicroURNG.hpp> 47 #include <Random123/ReinterpretCtr.hpp> 48 #if R123_USE_CXX11_RANDOM 49 #include <random> 50 #endif 51 #include <iostream> 52 #include <iomanip> 53 #include "pi_check.h" 54 55 using namespace r123; 56 57 int main(int, char**){ 58 typedef Philox4x32 RNG; 59 RNG::ctr_type c = {{}}; 60 RNG::key_type k = {{}}; 61 MicroURNG<RNG> longmurng(c.incr(), k); 62 #if R123_USE_STD_RANDOM 63 std::uniform_real_distribution<float> u(-1., 1.); 64 65 // First, compute pi with a nice long MicroURNG that we cancall 66 // billions of times (2^31) before it runs out of state: 67 unsigned long hits=0; 68 std::cout << "Calling a single MicroURNG " << NTRIES << " times" << std::endl; 69 for(unsigned long i=0; i<NTRIES; ++i){ 70 float x = u(longmurng); 71 float y = u(longmurng); 72 if( (x*x + y*y) < 1.0f ) 73 hits++; 74 } 75 if (pi_check(hits, NTRIES) != 0) { 76 return 1; 77 } 78 // MicroURNGs are very light-weight. It shouldn't be 79 // too expensive to create a new one every time through the loop: 80 std::cout << "Creating and calling a new MicroURNG " << NTRIES << " times" << std::endl; 81 hits=0; 82 for(unsigned long i=0; i<NTRIES; ++i){ 83 MicroURNG<RNG> shorturng(c.incr(), k); 84 float x = u(shorturng); 85 float y = u(shorturng); 86 if( (x*x + y*y) < 1.0f ) 87 hits++; 88 } 89 return pi_check(hits, NTRIES); 90 #else 91 // MicroURNG's are interesting because they allow us to use std::distributions, 92 // as in the above code. Std::distributions are nice, but if all we need is 93 // a uniform integer, we can do without such fancy C++11 features: 94 unsigned long hits=0; 95 std::cout << "Calling a single MicroURNG " << NTRIES << " times" << std::endl; 96 for(unsigned long i=0; i<NTRIES; ++i){ 97 float x = 2.*longmurng()/(double)std::numeric_limits<uint32_t>::max() - 1.; 98 float y = 2.*longmurng()/(double)std::numeric_limits<uint32_t>::max() - 1.; 99 if( (x*x + y*y) < 1.0f ) 100 hits++; 101 } 102 if (pi_check(hits, NTRIES) != 0) { 103 return 1; 104 } 105 #endif 106 107 108 }