README (3877B)
1 This file is examples/README and is also linked to from the doxygen main page. 2 3 /** 4 @page ExamplesREADME Examples 5 6 The examples/ directory contains usage examples 7 for the components of the Random123 library. 8 9 @section building Compiling and Running the code 10 11 Installing and using Random123 requires only the use 12 of the header files, and has no prerequisites other than 13 a reasonable C99 or C++98 compiler. 14 15 With a modern GNU make (3.80 or newer), building and running the core tests 16 and examples can be as easy as running gmake with no arguments. 17 Note, though, that the provided examples/GNUmakefile intentionally avoids setting 18 any of the standard make variables: CC, CXX, CPPFLAGS, CFLAGS, 19 CXXFLAGS, TARGET_ARCH, LDFLAGS, LOADLIBES, LDLIBS. GNU make 20 will inherit settings for these variables from the environment, 21 or they may be set on the command line. If none are set, 22 compilation will proceed using system-wide default flags, generally 23 without advanced optimization, architectural tuning, warnings, or other 24 common options. 25 26 Before putting the Random123 library to use in an application, 27 it is important to test it using the same compiler flags and 28 features that the application will use. In other words, 29 the conventional make variables should be set 30 the same way when testing the library as they will be set when the 31 library is actually compiled into your application. 32 Something like: 33 @code 34 gmake CFLAGS="-std=c99" CXXFLAGS="-std=c++0x" CPPFLAGS="/alternate/location/include -O3 -Wall -Wstrict-aliasing=2" TARGET_ARCH="-march=native" 35 @endcode 36 would confirm that all is well with optimization on, and output targeted at 37 an architecture with the same capabilities as the machine running the compilation. 38 39 Very old versions of GNU make (pre-2002) or non-GNU 40 make will not work with examples/GNUmakefile.. Lacking a suitably modern GNU make, 41 our advice is to invoke the 42 C or C++ compiler directly on the source files in the examples/ directory. 43 44 @section examples Examples 45 46 @subsection simple Simple examples in C and C++ 47 48 There are two extremely short examples that show all the code necessary to 49 obtain and print a few random numbers in C and C++: 50 <ul> 51 <li> simple.c 52 <li> simplepp.cpp 53 </ul> 54 55 @subsection pi Estimating pi using different APIs 56 57 Using random numbers to estimate pi is a classic example. The idea 58 is to choose points at random in a square and to count how many of 59 them lie within the inscribed circle. Since the area of the square 60 is 4*r^2 and the area of the circle is pi*r^2, the ratio of the 61 number of points in the circle to the total number of points should 62 approach pi/4 as the number of points grows. 63 64 We give several examples of pi estimation, each of 65 which illustrates a slightly different API 66 67 <ul> 68 <li> pi_capi - using only the basic C API 69 <li> pi_cppapi - using only the basic C++ API 70 <li> pi_u01 - using the C++ API and uniform.hpp 71 <li> pi_gsl - using a Random123 generator, but a gsl distribution to obtain real-valued random numbers. <b>Requires the GNU Scientific Library</b> 72 <li> pi_microurng - using a Random123 generator, but a C++11 \<random\> distribution to obtain real-valued random numbers 73 <li> pi_cuda - using the Random123 library with CUDA, runnable on an NVIDIA GPU 74 <li> pi_cudapp - using the C++ API with CUDA, runnable on an NVIDIA GPU 75 <li> pi_opencl - using the Random123 library with OpenCL, runnable on any OpenCL platform: e.g. NVIDIA or ATI GPUs or Intel or AMD CPUs. The actual 76 compute kernel lives in the \c pi_opencl_kernel.ocl file and is transformed by \c gencl.sh into strings that get included in \c pi_opencl.c, since 77 the OpenCL kernels get compiled for the target OpenCL platform at run-time. Note that Apple deprecated OpenCL in MacOS 10.14 (2018). See pi_metal. 78 <li> pi_aes - uses the AESNI4x32 Random123 generator 79 <li> pi_metal - uses Apple's Metal framework (replacement for OpenCL). 80 </ul> 81 82 83 */