Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4mpi Namespace Reference

Typedefs

typedef unsigned int rank_t
 
typedef std::pair< rank_t, rank_tcouple_t
 
typedef std::map< int,
std::vector< couple_t > > 
commMap_t
 
typedef std::function< void(std::function
< void(unsigned int)>
, std::function< void(unsigned
int)>, std::function< void(void)>
, unsigned int, unsigned int)> 
mergerHandler_t
 

Functions

commMap_t buildCommunicationMap (std::vector< rank_t > &input)
 
void Merge (std::function< void(unsigned int)> senderF, std::function< void(unsigned int)> receiverF, std::function< void(void)> barrierF, unsigned int commSize, unsigned int myrank)
 

Typedef Documentation

typedef std::map<int,std::vector<couple_t> > G4mpi::commMap_t

Definition at line 50 of file G4MPIutils.hh.

typedef std::pair<rank_t,rank_t> G4mpi::couple_t

Definition at line 47 of file G4MPIutils.hh.

typedef std::function<void(std::function<void(unsigned int)>, std::function<void(unsigned int)>, std::function<void(void)>, unsigned int, unsigned int)> G4mpi::mergerHandler_t

Definition at line 93 of file G4MPIutils.hh.

typedef unsigned int G4mpi::rank_t

Definition at line 45 of file G4MPIutils.hh.

Function Documentation

G4mpi::commMap_t G4mpi::buildCommunicationMap ( std::vector< rank_t > &  input)

Definition at line 37 of file G4MPIutils.cc.

38  {
39  using namespace G4mpi;
40  //Check validity of input
41  std::sort(input.begin(),input.end());
42  if ( input.size() < 1 || input[0] != 0 ) {
43  G4Exception("G4mpi::buildCommunicationMap(...)","G4mpi001",FatalException,
44  "Empty input or cannot find rank 0 in input.");
45  }
46  //Requested that no duplicates!
47  std::vector<rank_t> copy(input.size());
48  std::copy(input.begin(),input.end(),copy.begin());
49  copy.erase( std::unique(copy.begin(),copy.end()),copy.end());
50  if ( copy != input )
51  {
52  G4Exception("G4mpi::buildCommunicationMap(...)","G4mpi001",FatalException,
53  "There are duplicates in list of input ranks.");
54  }
55 
56  //The final communication map
57  commMap_t mymap;
58  //The communication map key
59  int cycle = 0;
60  //The communication map value
61  std::vector<couple_t> couples;
62  //Start a loop (on cycles) that will break
63  do {
64  //An helper container
65  std::vector<rank_t> receiving;
66  couples.clear();
67  //Loop on all input until there is
68  //at least a couple
69  while ( input.size() > 1 ) {
70  //Sort input in ascending order
71  std::sort( input.begin(),input.end() );
72  //Pop from back of the input a couple
73  const auto& send_ = input.back();
74  input.pop_back();
75  const auto& rec_ = input.back();
76  input.pop_back();
77  //Actually the receiving is not empty,
78  //remember it because we have to add it back
79  receiving.push_back( rec_ );
80  //This is a couple for this cycle
81  couples.push_back( std::make_pair(send_,rec_) );
82  }
83  //Populate final map for this cycle
84  mymap[cycle++]=couples;
85  //Let's put back in the input container the receivers
86  input.insert( input.end() , receiving.begin() , receiving.end() );
87  //Let's continue until there is ony one rank in input (number 0)
88  } while ( input.size()!=1 );
89  return mymap;
90 }
std::map< int, std::vector< couple_t > > commMap_t
Definition: G4MPIutils.hh:50
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41

Here is the call graph for this function:

Here is the caller graph for this function:

void G4mpi::Merge ( std::function< void(unsigned int)>  senderF,
std::function< void(unsigned int)>  receiverF,
std::function< void(void)>  barrierF,
unsigned int  commSize,
unsigned int  myrank 
)

Definition at line 161 of file G4MPIutils.cc.

165  {
166  //Optimize communications between ranks
167  std::vector<G4mpi::rank_t> ranks(commSize);
168  std::iota(ranks.begin(),ranks.end(),0); //{0,1,2,3,...}
169  auto comms = G4mpi::buildCommunicationMap(ranks);
170  //Loop on cycles of communications
171  for ( const auto& cycle : comms ) {
172  //Each cycle is a set of communications between ranks, it is guarantted that
173  //each rank participate in one and only one communication for each cycle
174  for (const auto& pattern : cycle.second ) {
175  //pattern is a couple: sender,receiver
176  if ( myrank == pattern.first ) {
177  //Send to destination
178  senderF(pattern.second);
179  }
180  else if ( myrank == pattern.second ) {
181  //Receive from source
182  receiverF(pattern.first);
183  }
184  }
185  //Important: Wait for this cycle to end before going to the next, even if this rank
186  //did not do anything
187  //This is needed to be sure that the redcutions are done correctly
188  barrierF();
189  }
190 }
commMap_t buildCommunicationMap(std::vector< rank_t > &input)
Definition: G4MPIutils.cc:37

Here is the call graph for this function:

Here is the caller graph for this function: