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

Seed generator. More...

#include <vsmc/rng/seed.hpp>

Public Types

using result_type = ResultType
 
using skip_type = ResultType
 

Public Member Functions

 SeedGenerator (const SeedGenerator< ID, ResultType > &)=delete
 
skip_type divisor () const
 The divisor of the output seed. More...
 
result_type get ()
 Get a new seed. More...
 
result_type get_scalar ()
 
void modulo (skip_type div, skip_type rem)
 Set the divisor and the remainder. More...
 
SeedGenerator< ID, ResultType > & operator= (const SeedGenerator< ID, ResultType > &)=delete
 
skip_type remainder () const
 The remainder of the output seed. More...
 
result_type seed () const
 The current internal seed. More...
 
result_type seed_max () const
 The maximum of the internal seed integer. More...
 
template<typename RNGType >
void seed_rng (RNGType &rng)
 Equivalent to rng.seed(get()) More...
 
void set (result_type seed)
 Set the internal seed. More...
 
void skip (skip_type steps)
 Skip the internal seed by a given steps. More...
 
void skip ()
 Skip the internal seed by 1 step. More...
 

Static Public Member Functions

static SeedGenerator< ID, ResultType > & instance ()
 

Friends

template<typename CharT , typename Traits >
std::basic_ostream< CharT, Traits > & operator<< (std::basic_ostream< CharT, Traits > &os, const SeedGenerator< ID, ResultType > &sg)
 
template<typename CharT , typename Traits >
std::basic_istream< CharT, Traits > & operator>> (std::basic_istream< CharT, Traits > &is, SeedGenerator< ID, ResultType > &sg)
 

Detailed Description

template<typename ID, typename ResultType = unsigned>
class vsmc::SeedGenerator< ID, ResultType >

Seed generator.

Let \(S\) be the current internal seed, an integer in the range between 1 and \(S_{\mathrm{max}}\) where \(S_{\mathrm{max}}\) is the maximum of the internal seed. Each time get() is called, the internal is first increased by 1. If it is already equal to \(S_{\mathrm{max}}\), then it is set to 1. The output seed is \(S \times D + R\) where \(D\) is the divisor and \(R\) is the remainder. In other words, the output seed belongs to the equivalent class \(S\ \mathrm{mod}\ D \equiv R\). The divisor and the remainder can be set through the modulo(div, rem) member function.

This property is useful when using programming models such as MPI where one want to have distinct seeds across nodes. Each process can then configure the SeedGenerator using the modulo member such that \(D\) is the number of total nodes and \(R\) is the rank of each node, counting from zero.

For multithreading programs, the access to this member function shall be protected by mutex, if it is called from multiple threads. Or one can use distinct type ID to initialized distinct SeedGenerator instances.

Definition at line 89 of file seed.hpp.

Member Typedef Documentation

template<typename ID, typename ResultType = unsigned>
using vsmc::SeedGenerator< ID, ResultType >::result_type = ResultType

Definition at line 92 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
using vsmc::SeedGenerator< ID, ResultType >::skip_type = ResultType

Definition at line 93 of file seed.hpp.

Constructor & Destructor Documentation

template<typename ID, typename ResultType = unsigned>
vsmc::SeedGenerator< ID, ResultType >::SeedGenerator ( const SeedGenerator< ID, ResultType > &  )
delete

Member Function Documentation

template<typename ID, typename ResultType = unsigned>
skip_type vsmc::SeedGenerator< ID, ResultType >::divisor ( ) const
inline

The divisor of the output seed.

Definition at line 142 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
result_type vsmc::SeedGenerator< ID, ResultType >::get ( )
inline

Get a new seed.

This member function is thread safe

Definition at line 118 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
result_type vsmc::SeedGenerator< ID, ResultType >::get_scalar ( )
inline

Definition at line 125 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
static SeedGenerator<ID, ResultType>& vsmc::SeedGenerator< ID, ResultType >::instance ( )
inlinestatic

Definition at line 100 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
void vsmc::SeedGenerator< ID, ResultType >::modulo ( skip_type  div,
skip_type  rem 
)
inline

Set the divisor and the remainder.

Definition at line 148 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
SeedGenerator<ID, ResultType>& vsmc::SeedGenerator< ID, ResultType >::operator= ( const SeedGenerator< ID, ResultType > &  )
delete
template<typename ID, typename ResultType = unsigned>
skip_type vsmc::SeedGenerator< ID, ResultType >::remainder ( ) const
inline

The remainder of the output seed.

Definition at line 145 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
result_type vsmc::SeedGenerator< ID, ResultType >::seed ( ) const
inline

The current internal seed.

Definition at line 136 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
result_type vsmc::SeedGenerator< ID, ResultType >::seed_max ( ) const
inline

The maximum of the internal seed integer.

Definition at line 139 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
template<typename RNGType >
void vsmc::SeedGenerator< ID, ResultType >::seed_rng ( RNGType &  rng)
inline

Equivalent to rng.seed(get())

Definition at line 109 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
void vsmc::SeedGenerator< ID, ResultType >::set ( result_type  seed)
inline

Set the internal seed.

If seed is larger than the maximum of the internal seed, than it will be round up to fit into the range. For example, say the range is from 1 to 1000, and the new internal seed is 6061, it will be round up to 61.

Definition at line 133 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
void vsmc::SeedGenerator< ID, ResultType >::skip ( skip_type  steps)
inline

Skip the internal seed by a given steps.

Definition at line 164 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
void vsmc::SeedGenerator< ID, ResultType >::skip ( )
inline

Skip the internal seed by 1 step.

Definition at line 172 of file seed.hpp.

Friends And Related Function Documentation

template<typename ID, typename ResultType = unsigned>
template<typename CharT , typename Traits >
std::basic_ostream<CharT, Traits>& operator<< ( std::basic_ostream< CharT, Traits > &  os,
const SeedGenerator< ID, ResultType > &  sg 
)
friend

Definition at line 181 of file seed.hpp.

template<typename ID, typename ResultType = unsigned>
template<typename CharT , typename Traits >
std::basic_istream<CharT, Traits>& operator>> ( std::basic_istream< CharT, Traits > &  is,
SeedGenerator< ID, ResultType > &  sg 
)
friend

Definition at line 196 of file seed.hpp.