Be the first to rate this file! 5 Downloads (last 30 days) File Size: 161.47 KB File ID: #33004

Faster/parallel random number generator via standard C++

by Peter Li

 

24 Sep 2011 (Updated 29 Sep 2011)

Mersenne Twister from Boost Random: same as modern Matlab, faster. Plus flexible distributions!

| Watch this File

File Information
Description

Update: Version 0.9.9

It became clear that things didn't really work properly with Boost 1.42, so I've refactored the package to try to help you kludge Boost Random 1.47 into place if necessary; this seems to work; see with cpprandpar.m file for build help.

===============

Update: Version 0.9.5 now has wired in many flexible distributions, so you can use the same RNG to generate pseudorandom samples from many distributions. What I have for now is: uniform_01, uniform_int, uniform_smallint, uniform_real, poisson, normal, triangle. I also have piecewise_linear, which allows you to specify your own continuous probability density. Should be simple to get more in.

New syntax:
  d = createdist('normal_distribution', [0 1.5]);
  A = cpprand(10000, 100, uint32(5489), d);
  hist(A(:), 100);

This was a little kludgy to wire together, so I'm keeping both v0.8.0 and v0.9.5 in the zip for now in case people have trouble building or running the new version. I think it should be possible to flexibly wire in different underlying generators as well, but this will be messy and I don't have a real need at the moment for anything other than Mersenne Twister. Will probably try at least CMWC though, in case it is much faster.

===============

This package wraps standard C++ Random Number Generators (RNGs). Although it currently relies on the Boost Random library, this is included in the new C++11 standard, so as C++11 compilers become available we should be able to drop the Boost dependency. Therefore I named the package CppRand.

This is a little harder to get set up than some mex libraries, so some experience with compiling C++ is recommended.

Currently I have only implemented wrappers for the Mersenne Twister implementation. This is the same as the default RNG for recent versions of Matlab. Importantly, the Boost 1.42 implementation is actually slower than the latest Matlab implementations, but the Boost 1.47 implementation is faster.

In addition to the simple wrapper, I have included a multithreaded version for parallel RNG. This relies on the Theron Actors library, which in turn relies on either Windows threads or Boost Threads. Again, thread support is coming in C++11, at which point external dependencies can be removed. See http://absurdlycertain.blogspot.com/2011/09/preamble-what-follows-is-guide.html for a detailed guide on getting Theron set up.

Please see the included M-files for more documentation.

Benchmarks on a 4-core Linux machine:
  Boost 1.47:
    >> tic; for i = 1:10, rand(1000000,10); clear ans; end; toc; clear i
    Elapsed time is 1.151257 seconds.
    >> tic; for i = 1:10, cpprand(1000000,10,uint32(5489)); clear ans; end; toc; clear i
    Elapsed time is 0.729212 seconds.
    >> tic; for i = 1:10, cpprandpar(1000000,10,uint32(5489:5492)); clear ans; end; toc; clear i
    Elapsed time is 0.335498 seconds.

  Boost 1.42:
    >> tic; for i = 1:10, cpprandpar(1000000,10,uint32(5489:5492)); clear ans; end; toc; clear i
    Elapsed time is 1.405573 seconds.
  SLOWER than Matab rand even with 4 threads!

MATLAB release MATLAB 7.10 (2010a)
Other requirements Boost Random is required. Boost 1.42 is no faster than Matlab rand, but Boost 1.47 is faster. For the multithreaded method, the Theron Actors library is required.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
29 Sep 2011 Peter Li

Please let me know if you are having trouble building; it's a little complicated if you don't have Boost 1.47, and then complicated again to get the multithreading working.

03 Oct 2011 Peter Li

I've noticed that the minimum spacing of random numbers generated by Boost Random 1.47 is greater than the minimum produced by Matlab; it appears to me that Matlab is using twice as many underlying integer seeds from the Twister to produce each double compared to Boost's behavior. So if you need very fine-grained doubles, this may be an issue for you.

03 Oct 2011 Peter Li

It appears that the minimum spacing for Boost Random 1.47 uniform_01 distribution (doubles between 0 and 1) is ~2.3e-10, which indicates that the RNG is simply taking each int generated by the Twister and (effectively) dividing it by the maximum int.

Matlab's RNG appears to use two int from the Twister for each [0,1] double, using the first int for the more significant bits and the second for the less significant bits.

So essentially using this library will give you faster RNG over Matlab at the expense of lower resolution.

Please login to add a comment or rating.
Updates
27 Sep 2011

Significant addition of flexible distributions

28 Sep 2011

Added/fixed documentation, fixed bugs that may have prevented building on newer compilers, fixed to allow compiling with older Boost.

28 Sep 2011

Bugfix from 0.9.7

29 Sep 2011

Making this more fully workable on systems that can't get up to Boost 1.47 yet.

Tag Activity for this File
Tag Applied By Date/Time
random number Peter Li 26 Sep 2011 10:50:16
monte carlo Peter Li 26 Sep 2011 10:50:16
mersenne twister Peter Li 26 Sep 2011 10:50:16
boost Peter Li 26 Sep 2011 10:50:16
c Peter Li 26 Sep 2011 10:50:16
actors Peter Li 26 Sep 2011 10:50:16
theron Peter Li 26 Sep 2011 10:50:16
parallel Peter Li 26 Sep 2011 10:50:16

Contact us at files@mathworks.com