vSMC
vSMC: Scalable Monte Carlo
Public Types | Public Member Functions | List of all members
vsmc::Particle< T > Class Template Reference

Particle class representing the whole particle set. More...

#include <vsmc/core/particle.hpp>

Public Types

typedef ConstSingleParticle< T > csp_type
 
typedef traits::ResampleCopyFromReplicationTypeTrait< T >::type resample_copy_from_replication_type
 
typedef traits::ResamplePostCopyTypeTrait< T >::type resample_post_copy_type
 
typedef traits::ResampleRngTypeTrait< T >::type resample_rng_type
 
typedef cxx11::function< void(std::size_t, std::size_t, resample_rng_type &, const double *, size_type *)> resample_type
 
typedef traits::RngSetTypeTrait< T >::type rng_set_type
 
typedef rng_set_type::rng_type rng_type
 
typedef traits::SizeTypeTrait< T >::type size_type
 
typedef SingleParticle< T > sp_type
 
typedef T value_type
 
typedef traits::WeightSetTypeTrait< T >::type weight_set_type
 

Public Member Functions

 Particle (size_type N)
 
Particle< T > clone (bool new_rng) const
 Clone the particle system except the RNG engines. More...
 
Particle< T > & clone (const Particle< T > &other, bool retain_rng)
 Clone another particle system except the RNG engines. More...
 
Particle< T > & clone (Particle< T > &&other, bool retain_rng)
 
csp_type csp (size_type id)
 Get a ConstSingleParticle. More...
 
csp_type csp (size_type id) const
 Get a ConstSingleParticle. More...
 
bool resample (const resample_type &op, double threshold)
 Performing resampling if ESS/N < threshold. More...
 
resample_rng_typeresample_rng ()
 Get the (sequential) RNG used stream for resampling. More...
 
rng_typerng (size_type id)
 Get an (parallel) RNG stream for a given particle. More...
 
rng_set_typerng_set ()
 Read and write access to the RNG collection object. More...
 
const rng_set_typerng_set () const
 Read only access to the RNG collection object. More...
 
size_type size () const
 Number of particles. More...
 
sp_type sp (size_type id)
 Get a SingleParticle. More...
 
csp_type sp (size_type id) const
 Get a ConstSingleParticle. More...
 
value_typevalue ()
 Read and write access to the value collection object. More...
 
const value_typevalue () const
 Read only access to the value collection object. More...
 
weight_set_typeweight_set ()
 Read and write access to the weight collection object. More...
 
const weight_set_typeweight_set () const
 Read only access to the weight collection object. More...
 

Detailed Description

template<typename T>
class vsmc::Particle< T >

Particle class representing the whole particle set.

Definition at line 48 of file particle.hpp.

Member Typedef Documentation

template<typename T>
typedef ConstSingleParticle<T> vsmc::Particle< T >::csp_type

Definition at line 59 of file particle.hpp.

Definition at line 64 of file particle.hpp.

Definition at line 66 of file particle.hpp.

template<typename T>
typedef traits::ResampleRngTypeTrait<T>::type vsmc::Particle< T >::resample_rng_type

Definition at line 56 of file particle.hpp.

template<typename T>
typedef cxx11::function<void (std::size_t, std::size_t, resample_rng_type &, const double *, size_type *)> vsmc::Particle< T >::resample_type

Definition at line 62 of file particle.hpp.

template<typename T>
typedef traits::RngSetTypeTrait<T>::type vsmc::Particle< T >::rng_set_type

Definition at line 55 of file particle.hpp.

template<typename T>
typedef rng_set_type::rng_type vsmc::Particle< T >::rng_type

Definition at line 57 of file particle.hpp.

template<typename T>
typedef traits::SizeTypeTrait<T>::type vsmc::Particle< T >::size_type

Definition at line 52 of file particle.hpp.

template<typename T>
typedef SingleParticle<T> vsmc::Particle< T >::sp_type

Definition at line 58 of file particle.hpp.

template<typename T>
typedef T vsmc::Particle< T >::value_type

Definition at line 53 of file particle.hpp.

template<typename T>
typedef traits::WeightSetTypeTrait<T>::type vsmc::Particle< T >::weight_set_type

Definition at line 54 of file particle.hpp.

Constructor & Destructor Documentation

template<typename T>
vsmc::Particle< T >::Particle ( size_type  N)
inlineexplicit

Definition at line 68 of file particle.hpp.

Member Function Documentation

template<typename T>
Particle<T> vsmc::Particle< T >::clone ( bool  new_rng) const
inline

Clone the particle system except the RNG engines.

Parameters
new_rngIf true, the new particle system has new-seeded RNG. Otherwise false, it is exactly the same as the current.

Definition at line 80 of file particle.hpp.

template<typename T>
Particle<T>& vsmc::Particle< T >::clone ( const Particle< T > &  other,
bool  retain_rng 
)
inline

Clone another particle system except the RNG engines.

Parameters
otherThe particle system to be cloned
retain_rngIf true, retain the current system's RNG. Otherwise, it is exactly the same as the new one.

Definition at line 96 of file particle.hpp.

template<typename T>
Particle<T>& vsmc::Particle< T >::clone ( Particle< T > &&  other,
bool  retain_rng 
)
inline

Definition at line 126 of file particle.hpp.

template<typename T>
csp_type vsmc::Particle< T >::csp ( size_type  id)
inline

Get a ConstSingleParticle.

Definition at line 176 of file particle.hpp.

template<typename T>
csp_type vsmc::Particle< T >::csp ( size_type  id) const
inline

Get a ConstSingleParticle.

Definition at line 179 of file particle.hpp.

template<typename T>
bool vsmc::Particle< T >::resample ( const resample_type op,
double  threshold 
)
inline

Performing resampling if ESS/N < threshold.

Parameters
opThe resampling operation funcitor
thresholdThe threshold of ESS/N below which resampling will be performed
Returns
true if resampling was performed

The out of box behavior are suitable for most applications. However, the user can also gain full control over the internal of this function by changing the weight_set_type and other type traits for specific value collection type. The sequence of operations of this function is listed as in the following pseudo-code

  1. Determine if resampling is required
    • Set N = weight_set.resample_size()
    • Set resampled = weight_set.ess() < threshold * N
    • If !resampled, GO TO Step 8. Otherwise, GO TO Step 2
  2. Determine if resampling algorithms shall be performed. This is only useful for MPI implementations. the boolean value resampled ensures that collective actions like copy will be called by all nodes. However, the resampling replication numbers can be computed by one node instead of by all. Therefore, the program can actually collect and read the resampling weights on one node, and do nothing on other nodes. This difference shall be reflected by the returning iterator of read_resample_weight
    • (Allocate double *weight for size N)
    • Set double *end = weight_set.read_resample_weight(weight)
    • If end != weight + N, GO TO step 5. Otherwise, GO TO Step 3
  3. Performing resampling, produce replication number of each particle
    • (Allocate size_type *replication for size N)
    • (Create RNG engine resample_rng according to traits::ResampleRngTypeTrait)
    • Call op(N, resample_rng, weight, replication)
  4. Transform replication numbers into parent particle indices
  5. Set const size_type *cptr according to results of Step 2.
    • If Step 3 and 4 are skipped according to Step 2, then set cptr = 0. Otherwise,
    • Set cptr = copy_from
  6. Performing copying of particles
    • Call value.copy(N, cptr)
  7. Performing post resampling weight manipulation
  8. return resampled

Definition at line 234 of file particle.hpp.

template<typename T>
resample_rng_type& vsmc::Particle< T >::resample_rng ( )
inline

Get the (sequential) RNG used stream for resampling.

Definition at line 182 of file particle.hpp.

template<typename T>
rng_type& vsmc::Particle< T >::rng ( size_type  id)
inline

Get an (parallel) RNG stream for a given particle.

Definition at line 167 of file particle.hpp.

template<typename T>
rng_set_type& vsmc::Particle< T >::rng_set ( )
inline

Read and write access to the RNG collection object.

Definition at line 161 of file particle.hpp.

template<typename T>
const rng_set_type& vsmc::Particle< T >::rng_set ( ) const
inline

Read only access to the RNG collection object.

Definition at line 164 of file particle.hpp.

template<typename T>
size_type vsmc::Particle< T >::size ( ) const
inline

Number of particles.

Definition at line 146 of file particle.hpp.

template<typename T>
sp_type vsmc::Particle< T >::sp ( size_type  id)
inline

Get a SingleParticle.

Definition at line 170 of file particle.hpp.

template<typename T>
csp_type vsmc::Particle< T >::sp ( size_type  id) const
inline

Get a ConstSingleParticle.

Definition at line 173 of file particle.hpp.

template<typename T>
value_type& vsmc::Particle< T >::value ( )
inline

Read and write access to the value collection object.

Definition at line 149 of file particle.hpp.

template<typename T>
const value_type& vsmc::Particle< T >::value ( ) const
inline

Read only access to the value collection object.

Definition at line 152 of file particle.hpp.

template<typename T>
weight_set_type& vsmc::Particle< T >::weight_set ( )
inline

Read and write access to the weight collection object.

Definition at line 155 of file particle.hpp.

template<typename T>
const weight_set_type& vsmc::Particle< T >::weight_set ( ) const
inline

Read only access to the weight collection object.

Definition at line 158 of file particle.hpp.