vSMC
vSMC: Scalable Monte Carlo
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
vsmc::PhiloxEngine< ResultType, K, Rounds > Class Template Reference

Philox RNG engine reimplemented. More...

#include <vsmc/rng/philox.hpp>

Public Types

typedef Array< ResultType, K > buffer_type
 
typedef Array< ResultType, K > ctr_type
 
typedef Array< ResultType, K/2 > key_type
 
typedef ResultType result_type
 

Public Member Functions

 PhiloxEngine (result_type s=0)
 
template<typename SeedSeq >
 PhiloxEngine (SeedSeq &seq, typename cxx11::enable_if< internal::is_seed_seq< SeedSeq, result_type, key_type, PhiloxEngine< ResultType, K, Rounds > >::value >::type *=nullptr)
 
 PhiloxEngine (const key_type &k)
 
ctr_type ctr () const
 
void ctr (const ctr_type &c)
 
void discard (result_type nskip)
 
key_type key () const
 
void key (const key_type &k)
 
result_type operator() ()
 
buffer_type operator() (const ctr_type &c) const
 Generate a buffer of random bits given a counter using the current key. More...
 
void operator() (const ctr_type &c, buffer_type &buf) const
 Generate random bits in a pre-allocated buffer given a counter using the current key. More...
 
void seed (result_type s)
 
template<typename SeedSeq >
void seed (SeedSeq &seq, typename cxx11::enable_if< internal::is_seed_seq< SeedSeq, result_type, key_type, PhiloxEngine< ResultType, K, Rounds > >::value >::type *=nullptr)
 
void seed (const key_type &k)
 

Static Public Member Functions

static constexpr result_type max ()
 
static constexpr result_type min ()
 

Static Public Attributes

static constexpr const result_type _Max
 
static constexpr const result_type _Min = 0
 

Friends

bool operator!= (const PhiloxEngine< ResultType, K, Rounds > &eng1, const PhiloxEngine< ResultType, K, Rounds > &eng2)
 
template<typename CharT , typename Traits >
std::basic_ostream< CharT, Traits > & operator<< (std::basic_ostream< CharT, Traits > &os, const PhiloxEngine< ResultType, K, Rounds > &eng)
 
bool operator== (const PhiloxEngine< ResultType, K, Rounds > &eng1, const PhiloxEngine< ResultType, K, Rounds > &eng2)
 
template<typename CharT , typename Traits >
std::basic_istream< CharT, Traits > & operator>> (std::basic_istream< CharT, Traits > &is, PhiloxEngine< ResultType, K, Rounds > &eng)
 

Detailed Description

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
class vsmc::PhiloxEngine< ResultType, K, Rounds >

Philox RNG engine reimplemented.

This is a reimplementation of the algorithm Philox as described in Parallel Random Numbers: As Easy as 1, 2, 3 and implemented in Random123.

Depending on the compilers, processors and RNG configurations, it might be slightly faster or slower than the original implementation. At most two-folds performace difference (both faster and slower) were observed.

Currently the 64-bits version is much slower than the original, except when using recent Clang, GCC, Intel C++ or MSVC on x86-64 computers. The original implementation use some platform dependent assembly or intrinsics to optimize the performance. This implementation use standard C99 when used on other platforms.

This implementation is slightly more flexible in the sense that it does not limit the number of rounds. However, larger number of rounds can have undesired effects. To say the least, currently all loops are unrolled, which can slow down significantly when the number of rounds is large.

Compared to r123:Engine<r123::Philox4x32> etc., when using the default constructor or the one with a single seed, the output shall be exactly the same for the first \(2^n\) iterations, where \(n\) is the number of bits (32 or 64). Further iterations may produce different results, as vSMC increment the counter slightly differently, but it still cover the same range and has the same period as the original.

The constants of bumping the key (Weyl constants) and those used in each rounds can be set through traits, vsmc::traits::PhiloxWeylConstantTrait and vsmc::traits::PhiloxRoundConstantTrait.

Definition at line 307 of file philox.hpp.

Member Typedef Documentation

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
typedef Array<ResultType, K> vsmc::PhiloxEngine< ResultType, K, Rounds >::buffer_type

Definition at line 312 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
typedef Array<ResultType, K> vsmc::PhiloxEngine< ResultType, K, Rounds >::ctr_type

Definition at line 313 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
typedef Array<ResultType, K / 2> vsmc::PhiloxEngine< ResultType, K, Rounds >::key_type

Definition at line 314 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
typedef ResultType vsmc::PhiloxEngine< ResultType, K, Rounds >::result_type

Definition at line 311 of file philox.hpp.

Constructor & Destructor Documentation

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
vsmc::PhiloxEngine< ResultType, K, Rounds >::PhiloxEngine ( result_type  s = 0)
inlineexplicit

Definition at line 322 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
template<typename SeedSeq >
vsmc::PhiloxEngine< ResultType, K, Rounds >::PhiloxEngine ( SeedSeq &  seq,
typename cxx11::enable_if< internal::is_seed_seq< SeedSeq, result_type, key_type, PhiloxEngine< ResultType, K, Rounds > >::value >::type = nullptr 
)
inlineexplicit

Definition at line 329 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
vsmc::PhiloxEngine< ResultType, K, Rounds >::PhiloxEngine ( const key_type k)
inline

Definition at line 338 of file philox.hpp.

Member Function Documentation

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
ctr_type vsmc::PhiloxEngine< ResultType, K, Rounds >::ctr ( ) const
inline

Definition at line 370 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
void vsmc::PhiloxEngine< ResultType, K, Rounds >::ctr ( const ctr_type c)
inline

Definition at line 374 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
void vsmc::PhiloxEngine< ResultType, K, Rounds >::discard ( result_type  nskip)
inline

Definition at line 412 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
key_type vsmc::PhiloxEngine< ResultType, K, Rounds >::key ( ) const
inline

Definition at line 372 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
void vsmc::PhiloxEngine< ResultType, K, Rounds >::key ( const key_type k)
inline

Definition at line 380 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
static constexpr result_type vsmc::PhiloxEngine< ResultType, K, Rounds >::max ( )
inlinestatic

Definition at line 439 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
static constexpr result_type vsmc::PhiloxEngine< ResultType, K, Rounds >::min ( )
inlinestatic

Definition at line 438 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
result_type vsmc::PhiloxEngine< ResultType, K, Rounds >::operator() ( )
inline

Definition at line 386 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
buffer_type vsmc::PhiloxEngine< ResultType, K, Rounds >::operator() ( const ctr_type c) const
inline

Generate a buffer of random bits given a counter using the current key.

Definition at line 399 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
void vsmc::PhiloxEngine< ResultType, K, Rounds >::operator() ( const ctr_type c,
buffer_type buf 
) const
inline

Generate random bits in a pre-allocated buffer given a counter using the current key.

Definition at line 409 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
void vsmc::PhiloxEngine< ResultType, K, Rounds >::seed ( result_type  s)
inline

Definition at line 344 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
template<typename SeedSeq >
void vsmc::PhiloxEngine< ResultType, K, Rounds >::seed ( SeedSeq &  seq,
typename cxx11::enable_if< internal::is_seed_seq< SeedSeq, result_type, key_type, PhiloxEngine< ResultType, K, Rounds > >::value >::type = nullptr 
)
inline

Definition at line 353 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
void vsmc::PhiloxEngine< ResultType, K, Rounds >::seed ( const key_type k)
inline

Definition at line 363 of file philox.hpp.

Friends And Related Function Documentation

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
bool operator!= ( const PhiloxEngine< ResultType, K, Rounds > &  eng1,
const PhiloxEngine< ResultType, K, Rounds > &  eng2 
)
friend

Definition at line 451 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
template<typename CharT , typename Traits >
std::basic_ostream<CharT, Traits>& operator<< ( std::basic_ostream< CharT, Traits > &  os,
const PhiloxEngine< ResultType, K, Rounds > &  eng 
)
friend

Definition at line 457 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
bool operator== ( const PhiloxEngine< ResultType, K, Rounds > &  eng1,
const PhiloxEngine< ResultType, K, Rounds > &  eng2 
)
friend

Definition at line 441 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
template<typename CharT , typename Traits >
std::basic_istream<CharT, Traits>& operator>> ( std::basic_istream< CharT, Traits > &  is,
PhiloxEngine< ResultType, K, Rounds > &  eng 
)
friend

Definition at line 473 of file philox.hpp.

Member Data Documentation

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
constexpr const result_type vsmc::PhiloxEngine< ResultType, K, Rounds >::_Max
static
Initial value:
= static_cast<result_type>(
~(static_cast<result_type>(0)))

Definition at line 435 of file philox.hpp.

template<typename ResultType, std::size_t K, std::size_t Rounds = 10>
constexpr const result_type vsmc::PhiloxEngine< ResultType, K, Rounds >::_Min = 0
static

Definition at line 434 of file philox.hpp.