vSMC
vSMC: Scalable Monte Carlo
Classes | Public Types | Public Member Functions | Protected Member Functions | List of all members
vsmc::StateMPI< BaseState, ID > Class Template Reference

Particle::value_type subtype using MPI. More...

#include <vsmc/internal/forward.hpp>

Public Types

typedef ID mpi_id
 
typedef traits::SizeTypeTrait< BaseState >::type size_type
 
typedef WeightSetMPI< typename traits::WeightSetTypeTrait< BaseState >::type, ID > weight_set_type
 

Public Member Functions

 StateMPI (size_type N)
 
template<typename IntType >
void copy (size_type N, const IntType *copy_from)
 Copy particles. More...
 
size_type global_id (size_type local_id) const
 Transfer a local particle id on this node into a global particle id. More...
 
size_type global_size () const
 The number of particles on all nodes. More...
 
bool is_local (size_type global_id) const
 Given a global particle id check if it is on this node More...
 
size_type local_id (size_type global_id) const
 Transfer a global particle id into a local particle id (possibly not on this node, use rank to get the rank of its node) More...
 
size_type offset () const
 The number of particles on nodes with ranks less than the rank of this node. More...
 
int rank (size_type global_id) const
 Given a global particle id return the rank of the node it belongs. More...
 
const ::boost::mpi::communicator & world () const
 A duplicated MPI communicator for this state value object. More...
 

Protected Member Functions

void copy_inter_node (const std::vector< std::pair< int, size_type > > &copy_recv, const std::vector< std::pair< int, size_type > > &copy_send)
 Perform global copy. More...
 
int copy_tag () const
 The MPI recv/send tag used by copy_inter_node More...
 
void copy_this_node (size_type N, const size_type *copy_from, std::vector< std::pair< int, size_type > > &copy_recv, std::vector< std::pair< int, size_type > > &copy_send)
 Perform local copy. More...
 

Detailed Description

template<typename BaseState, typename ID>
class vsmc::StateMPI< BaseState, ID >

Particle::value_type subtype using MPI.

Definition at line 72 of file forward.hpp.

Member Typedef Documentation

template<typename BaseState , typename ID >
typedef ID vsmc::StateMPI< BaseState, ID >::mpi_id

Definition at line 239 of file backend_mpi.hpp.

template<typename BaseState , typename ID >
typedef traits::SizeTypeTrait<BaseState>::type vsmc::StateMPI< BaseState, ID >::size_type

Definition at line 236 of file backend_mpi.hpp.

template<typename BaseState , typename ID >
typedef WeightSetMPI<typename traits::WeightSetTypeTrait<BaseState>::type, ID> vsmc::StateMPI< BaseState, ID >::weight_set_type

Definition at line 238 of file backend_mpi.hpp.

Constructor & Destructor Documentation

template<typename BaseState , typename ID >
vsmc::StateMPI< BaseState, ID >::StateMPI ( size_type  N)
inlineexplicit

Definition at line 241 of file backend_mpi.hpp.

Member Function Documentation

template<typename BaseState , typename ID >
template<typename IntType >
void vsmc::StateMPI< BaseState, ID >::copy ( size_type  N,
const IntType *  copy_from 
)
inline

Copy particles.

Parameters
NThe number of particles on all nodes
copy_fromA vector of length N, for each particle with global id to, copy_from[to] is the global id of the particle it shall copy.

The BaseState type is required to have the following members - state_pack_type: A type that used to pack state values. It shall be serializable. That is, a state_pack_type object is acceptable by boost::mpi::communicator::send etc. Both StateMatrix::state_pack_type and StateTuple::state_pack_type satisfy this requirement if their template type parameter types are serializable. For user defined types, see document of Boost.Serialize of how to serialize a class object.

  • state_pack
    state_pack_type state_pack (size_type id) const;
    Given a local particle id on this node, pack the state values into a state_pack_type object.
  • state_unpack
    void state_unpack (size_type id, const state_pack_type &pack);
    Given a local particle id and a state_pack_type object, unpack it into the given position on this node. If C++11 rvalue reference is supported, then an rvalue version of this function can be defined to improved the performance as pack will be passed as an rvalue. e.g.,
    void state_unpack (size_type id, state_pack_type &&pack);
    As usual, if state_pack_type needs to explicitly define move constructor or assignment operator, care shall be taken to make sure that after the move pack is still a valid, assignable object.

In vSMC, the resampling algorithms generate the number of replications of each particle. Particles with replication zero need to copy other particles. The vector of the number of replications is transfered to copy_from by Particle::resample, and it is generated in such a way that each particle will copy from somewhere close to itself. Therefore, transferring between nodes is minimized.

This default implementation perform three stages of copy.

  • Stage one: Generate a local duplicate of copy_from on node 0 and broadcast it to all nodes.
  • Stage two: Perform local copy, copy those particles where the destination and source are both on this node. This is performed in parallel on each node.
  • Stage three: copy particles that need message passing between nodes.

A derived class can override this copy method. For the following possible reasons,

  • Stage one is not needed or too expansive
  • Stage three is too expansive. The default implementation assumes this->state_pack(id) and `this->state_unpack(id, pack) is not too expansive, and inter-node copy is rare anyway. If this is not the case, then it can be a performance bottle neck.

Definition at line 320 of file backend_mpi.hpp.

template<typename BaseState , typename ID >
void vsmc::StateMPI< BaseState, ID >::copy_inter_node ( const std::vector< std::pair< int, size_type > > &  copy_recv,
const std::vector< std::pair< int, size_type > > &  copy_send 
)
inlineprotected

Perform global copy.

Parameters
copy_recvThe output vector copy_recv from copy_this_node
copy_sendThe output vector copy_send from copy_this_node

Definition at line 456 of file backend_mpi.hpp.

template<typename BaseState , typename ID >
int vsmc::StateMPI< BaseState, ID >::copy_tag ( ) const
inlineprotected

The MPI recv/send tag used by copy_inter_node

Definition at line 392 of file backend_mpi.hpp.

template<typename BaseState , typename ID >
void vsmc::StateMPI< BaseState, ID >::copy_this_node ( size_type  N,
const size_type copy_from,
std::vector< std::pair< int, size_type > > &  copy_recv,
std::vector< std::pair< int, size_type > > &  copy_send 
)
inlineprotected

Perform local copy.

Parameters
NThe number of particles on all nodes
copy_fromThe beginning of the copy_from vector
copy_recvAll particles that shall be received at this node
copy_sendAll particles that shall be send from this node

copy_from is a pointer that can access a vector of size N. For each to in the range 0 to N - 1

  • If both to and from = copy_from[to] are particles on this node, use BaseState::copy to copy the parties. Otherwise,
  • If to is a particle on this node, insert a pair into copy_recv, whose values are the rank of the node from which this node shall receive the particle and the particle id on this node where the particle received shall be unpacked. Otherwise,
  • If from = copy_from[to] is a particle on this node, insert a pair into copy_send, whose values are the rank of the node to which this node shall send the particle and the particle id on this node where the particle sent shall be packed. Otherwise do nothing.

It is important the the vector accessed through copy_from_first is the same for all nodes. Otherwise the behavior is undefined.

Definition at line 417 of file backend_mpi.hpp.

template<typename BaseState , typename ID >
size_type vsmc::StateMPI< BaseState, ID >::global_id ( size_type  local_id) const
inline

Transfer a local particle id on this node into a global particle id.

Definition at line 387 of file backend_mpi.hpp.

template<typename BaseState , typename ID >
size_type vsmc::StateMPI< BaseState, ID >::global_size ( ) const
inline

The number of particles on all nodes.

Definition at line 338 of file backend_mpi.hpp.

template<typename BaseState , typename ID >
bool vsmc::StateMPI< BaseState, ID >::is_local ( size_type  global_id) const
inline

Given a global particle id check if it is on this node

Definition at line 362 of file backend_mpi.hpp.

template<typename BaseState , typename ID >
size_type vsmc::StateMPI< BaseState, ID >::local_id ( size_type  global_id) const
inline

Transfer a global particle id into a local particle id (possibly not on this node, use rank to get the rank of its node)

Definition at line 367 of file backend_mpi.hpp.

template<typename BaseState , typename ID >
size_type vsmc::StateMPI< BaseState, ID >::offset ( ) const
inline

The number of particles on nodes with ranks less than the rank of this node.

Definition at line 342 of file backend_mpi.hpp.

template<typename BaseState , typename ID >
int vsmc::StateMPI< BaseState, ID >::rank ( size_type  global_id) const
inline

Given a global particle id return the rank of the node it belongs.

Definition at line 346 of file backend_mpi.hpp.

template<typename BaseState , typename ID >
const ::boost::mpi::communicator& vsmc::StateMPI< BaseState, ID >::world ( ) const
inline

A duplicated MPI communicator for this state value object.

Definition at line 335 of file backend_mpi.hpp.