Skip to content

MultithreadCorner/ranluxpp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

This program is an implementation of Linear Congruential Generator (LCG) defined as:

x_{i+1} = A * x_{i} mod m

in long integer arithmetic with the modulus m = 2^576 - 2^240 + 1 and the multiplier A = a^p, a = m-(m-1)/2^24 and p = 24..thousands, where m and a are derived from the subtract-with-borrow random number generator aka RANLUX. It is a well known high statistical quality random number generator included in the modern C++ standard.

One step of the subtract-with-borrow generator uses only one subtraction of 24 bit numbers and carry bit propagation and is equivalent to one modular multiplication (a * x mod m) so the generator presents a clever way to avoid large integer arithmetic. Despite RANLUX uses only simple arithmetic operations it is one of the slowest generators on the market since internally large portion of generated numbers are wasted or skipped to break correlation between nearby numbers in the sequence to achieve high statistical quality. Whereas in the suggested approach the skipping is equivalent again to only one modular multiplication with the multiplier A = (a^p mod m), where p-24 is the number of skipped elements in the RANLUX sequence.

The core of the generator is the fast multiplication of two 576 bit numbers which is implemented in assembler for AMD64 architecture using the simple school-book method with 81 64x64 bit multiplication. Recent progress in CPU development allows to achieve speed 119 (CPU clock) per 576x576 bit multiplication and in conjunction with modular reduction achieved speed is 155 (CPU clock) per modular multiplication at Skylake CPU. At the moment there are three versions suitable for generic AMD64 CPUs, with mulx and adcx/adox instructions available.

Finally, generation speed achieved (20 clock/float) is order of magnitude faster than the usual approach, for example, GCC C++ RANLUX implementation where 198 clock/float is measured at Haswell CPU.

The conventional RANLUX algorithm is also optimized to get profit from modern CPU improvements. Scalar as well as SIMD optimized versions are implemented. The achieved speed of 40 clock/float is only two times less than in the LCG approach and those optimizations can be easily applied to RANLUX implementations in other packages. The SSE2 and AVX2 versions is approximately 4 and 8 times faster correspondingly.

File descriptions

src/mul9x9mod.asm -- modular multiplication code.
src/mulmod.cxx -- C interface with GCC function multiversioning to the modular multiplication code.
src/ranluxpp.cxx -- generator itself using modular multiplication.
src/ranlux.cxx -- optimized version of the conventional RANLUX algorithm.
src/skipstates.asm -- asm optimization for hardware carry bit propagation in the conventional RANLUX algorithm.
src/divmult.asm -- fractional expansion of LCG state x divided by the modulus m to get RANLUX sequence.
src/lcg2ranlux.cxx -- transform LCG state to RANLUX sequence.

tests/ranluxpp_test.cxx -- usage example and benchmarks of the generator with modular multiplication.
tests/ranlux_test.cxx -- usage example and benchmarks of the generators based on the conventional RANLUX algorithm.
tests/std_random_test.cxx -- benchmarks of the standard C++ random number generators.

Compilation

Type "make" in this directory to build the generator library and test executables.

Tests and benchmarks

Type "./ranluxpp_test", "./ranlux_test" or "./std_random_test" to see the command help and the command options.

Contact

Alexei Sibidanov [email protected]

About

No description, website, or topics provided.

Resources

License

LGPL-3.0, GPL-3.0 licenses found

Licenses found

LGPL-3.0
COPYING.LESSER
GPL-3.0
COPYING

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 68.4%
  • Assembly 26.1%
  • C 4.4%
  • Makefile 1.1%