1. The generators and distributions
MtxVec's random number generators fill arrays with values from 12 continuous distributions, in single and double precision, and 12 discrete ones, drawn from four basic generators: MCG31m1, MRG32k3a, MCG59 and Philox4x32-10.
| MCG31m1 | rgMCG31 | multiplicative congruential, modulus 2³¹ − 1 | skip-ahead, leapfrog |
| MRG32k3a | rgMRG32K3A | combined multiple recursive | skip-ahead |
| MCG59 | rgMCG59 | multiplicative congruential, modulus 2⁵⁹ | skip-ahead, leapfrog |
| Philox4x32-10 | rgPHILOX4X32X10 | counter-based, ten rounds on four 32-bit words | skip-ahead |
Each generator follows its published algorithm and, from the same seed, returns the same integers as Intel oneMKL VSL. They can be used to feed the following distributions:
| Continuous, single and double precision | Uniform, Gaussian (Box–Muller, Box–Muller 2 or inverse CDF), multivariate Gaussian, Exponential, Laplace, Weibull, Rayleigh, Lognormal, Beta, Gamma, Gumbel, Cauchy |
| Discrete, integer | Uniform, uniform bits (the generator's words, or uniform 32-bit and 64-bit words), Bernoulli, Geometric, Binomial, Hypergeometric, Negative binomial, Poisson (one lambda, or one per value), Multinomial |
2. Streams and saved state
TRandomGenerator, in the RndGenerators unit, holds a basic generator and its random streams. Init selects the generator and the seed, and StreamCount sets the number of streams. Each stream is a TRngStream, reached as Streams[i], and every distribution is a method of the stream. A new TRandomGenerator holds MCG31m1, seed 1, a BlockStep of 1,024 and no streams.
StreamIndependence decides how the streams relate to one another. Init raises an exception for a method the generator does not support.
rsiNone | Every stream returns the same series. | all four |
rsiLeapfrog | The streams interleave one sequence: with three streams, stream 0 returns the numbers 0, 3, 6, …, stream 1 the numbers 1, 4, 7, … | MCG31m1, MCG59 |
rsiBlockSplitting | Stream i starts BlockStep × i numbers into the sequence. The streams are independent while each draws fewer than BlockStep numbers. | all four |
Each stream can also be positioned and copied on its own:
SkipAhead(Count) | Skips Count numbers of the sequence, a 64-bit count. |
LeapFrog(k, n) | Makes the stream the k-th of n interleaved streams. |
CopyState(Src) | Copies the state of another stream. |
Copy(Src) | Copies the entire descriptor of another stream. |
SaveToStream, LoadFromStream | Save and restore the state of this stream. |
SaveToFile and SaveToStream of TRandomGenerator write the stream count, the independence method, the seed, the generator and BlockStep, then the state of every stream. LoadFromFile and LoadFromStream restore them, so each stream continues where it was saved.
2.1 Save the state and continue later
The second draw before saving and the first draw after loading return the same 1,000 values.
uses MtxExpr, RndGenerators;
procedure SaveAndContinue;
var
Rnd: TRandomGenerator;
a, b: Vector;
begin
a.Size(1000);
b.Size(1000);
Rnd := TRandomGenerator.Create;
try
Rnd.StreamIndependence := rsiBlockSplitting;
Rnd.BlockStep := 1000000;
Rnd.StreamCount := 4;
Rnd.Init(rgMRG32K3A, 2027);
Rnd.Streams[0].RandomGaussian(a, 0, 1);
Rnd.SaveToFile('Simulation.rnd');
Rnd.Streams[0].RandomGaussian(a, 0, 1);
finally
Rnd.Free;
end;
Rnd := TRandomGenerator.Create;
try
Rnd.LoadFromFile('Simulation.rnd');
Rnd.Streams[0].RandomGaussian(b, 0, 1);
finally
Rnd.Free;
end;
Assert(a.IsEqual(b));
end;2.2 Skip ahead
With rsiNone both streams start at the same number. Stream 1 skips the 1,000 numbers stream 0 draws first, and then returns the 1,000 values stream 0 draws second.
uses MtxExpr, RndGenerators;
procedure SkipAhead;
var
Rnd: TRandomGenerator;
a, b: Vector;
begin
a.Size(1000);
b.Size(1000);
Rnd := TRandomGenerator.Create;
try
Rnd.StreamCount := 2;
Rnd.Init(rgMRG32K3A, 7);
Rnd.Streams[0].RandomUniform(a, 0, 1);
Rnd.Streams[0].RandomUniform(a, 0, 1);
Rnd.Streams[1].SkipAhead(1000);
Rnd.Streams[1].RandomUniform(b, 0, 1);
finally
Rnd.Free;
end;
Assert(a.IsEqual(b));
end;3. Performance against Intel oneMKL VSL 2024
The library is designed for balanced performance across AMD and Intel processors. The reference used is Intel oneMKL VSL 2024: oneMKL VSL releases beyond 2024 have no support for AVX2 on AMD processors. The 24 distribution routines were timed on the four generators, at AVX2 and SSE4.2 on Win32 and Win64 and at AVX-512 on Win64, against Intel oneMKL VSL 2024 held to the same instruction set. The benchmark ran on an AMD Ryzen 9 9950X.
Each benchmark case represents one generator, routine and its parameters, precision, platform, and instruction set. Each routine's cases are first reduced to their geometric mean, and a row shows the geometric mean across routines. The ratio divides the MtxVec time by the Intel oneMKL VSL 2024 time. A time of 22.41 in the chart is 2.241 ns per value.
The integer distributions took 59% of Intel oneMKL VSL 2024's time, a 1.69× speedup; the single- and double-precision distributions took 1.45 and 1.54 times as long. At AVX2 MtxVec took 81% of the reference time, at SSE4.2 the two were at parity, and at AVX-512 MtxVec took 1.08 times as long.
In this benchmark MtxVec still calls some Intel IPP functions, in the MtxVec.Dspd and MtxVec.Dsps DLLs, but without the VML math: the vector math is MtxVec's own. Pure pascal/asm variant of this benchmark shows about 30% slower MtxVec. Each case is one timing of at most 30 ms, in which the routine fills 1,024 values per call defeating CPU branch prediction. The values are verified after the timing.