mwc64x32 and mwc128x32#

Include <cuda/experimental/random.cuh> to use these engines in the cuda::experimental namespace.

These scrambled Multiply-With-Carry (MWC) engines implement the RandomNumberEngine interface and produce unsigned 32-bit values over the entire range [0, 2^32 - 1]. They use the MWC64X/32 and MWC128X/32 algorithms from Scrambled Multiply-with-Carry Pseudorandom Number Generators.

Note

These engines are not cryptographic generators and are not recommended for simulation or scientific computing requiring independently validated streams.

Engine

State size

Multiplier

Period

cuda::experimental::mwc64x32

64 bits

0xFFF145F8

(0xFFF145F8 * 2^32 - 2) / 2 (approximately 2^63)

cuda::experimental::mwc128x32

128 bits

0xFFCCCC66

(0xFFCCCC66 * 2^96 - 2) / 2 (approximately 2^127)

Both engines support host and device execution, constant evaluation, and logarithmic-time discard(unsigned long long).

Performance Considerations#

Both engines have similar throughput. The following table shows approximate performance ratio compared to other engines:

Compared with

MWC64X/32, MWC128X/32

cuRAND XORWOW

1.4x

cuda::pcg64

2.6x

cuRAND Philox4x32-10

3x

cuda::std::philox4x32

20x

Seeding#

Both engines can be initialized or reseeded with a 64-bit unsigned integer, including zero, or a seed sequence. The default seed is 1.

Parallel streams#

Randomly seeding many mwc64x32 engines can lead to overlapping streams because its period is relatively short. The user should use discard instead to partition a sequence into non-overlapping bounded streams. See the example below:

#include <cuda/experimental/random.cuh>

__global__ void sample(unsigned* output) {
    auto tid = static_cast<unsigned long long>(blockIdx.x) * blockDim.x + threadIdx.x;
    cuda::experimental::mwc128x32 engine(42);
    engine.discard(tid * 1024); // Reserve 1024 outputs per thread
    output[tid] = engine();
}

For performance reasons, each stream should be initialized once, then reuse the engine multiple times. Jumping ahead is more expensive than generating a value.