Geant4  10.01.p02
G4MPIRunMerger.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 #include "G4MPIRunMerger.hh"
27 #include <mpi.h>
28 
30  G4int destination ,
31  G4int ver) :
32  destinationRank(destination),
33  run(const_cast<G4Run*>(aRun)),
34  commSize(0),
35  verbose(ver) {}
36 
37 #define DMSG( LVL , MSG ) { if ( verbose > LVL ) { G4cout << MSG << G4endl; } }
38 
40 {
41  DMSG( 2 , "Sending double from "<<val<<" with size: "<<size);
42  COMM_G4COMMAND_.Send( val, size , MPI::DOUBLE,
44  DMSG( 2 , "Sent "<<( size > 1 ? val[0] : *val) );
45 }
46 
47 void G4MPIRunMerger::SendInt( G4int* val , G4int size )
48 {
49  DMSG( 2 , "Sending int from "<<val<<" with size: "<<size);
50  COMM_G4COMMAND_.Send( val, size , MPI::INT,
52  DMSG( 2 , "Sent "<<( size > 1 ? val[0] : *val) );
53 }
54 
56 {
57  DMSG( 2 , "Receiving double at "<<val<<" with size "<<size );
58  COMM_G4COMMAND_.Recv( val, size, MPI::DOUBLE, rank , G4MPImanager::kTAG_DATA);
59  DMSG( 2 , "Received "<<( size > 1 ? val[0] : *val) );
60 }
61 
62 void G4MPIRunMerger::ReceiveInt( G4int rank, G4int* val , G4int size )
63 {
64  DMSG( 2 , "Receiving int at "<<val<<" with size "<<size );
65  COMM_G4COMMAND_.Recv( val, size, MPI::INT, rank , G4MPImanager::kTAG_DATA);
66  DMSG( 2 , "Received "<<( size > 1 ? val[0] : *val) );
67 }
68 
70 {
71  G4int nevts = run->GetNumberOfEvent();
72  DMSG( 1 , "G4MPIRunMerger::Send() : Sending a G4run ("
73  <<run<<") with "<<nevts<<" events,");
74  SendInt( &nevts );
75  DMSG( 1 , "G4MPIRunMerger::Send() : Done ");
76 }
78 {
79  DMSG( 1 , "G4MPIRunMerger::Receive(...) : Receiving from rank "<<rank);
80  G4Run* anEmptyRun = new G4Run;
81  G4int nevts = 0;
82  ReceiveInt( rank, &nevts );
83 
84  //Increment internal counter up to nevets
85  for ( G4int i = 0 ; i<nevts ; ++i ) anEmptyRun->RecordEvent( NULL );
86 
87  //User data can go here
88  //
89  //Now merge received MPI run with global one
90  DMSG(2,"Before G4Run::Merge : "<<run->GetNumberOfEvent());
91 
92  run->Merge( anEmptyRun );
93  DMSG(2,"After G4Run::Merge : "<<run->GetNumberOfEvent());
94  delete anEmptyRun;
95 }
96 
98 {
99  DMSG(0, "G4MPIRunMerger::Merge called");
100  G4int myrank = MPI::COMM_WORLD.Get_rank();
101  commSize = MPI::COMM_WORLD.Get_size();
102  COMM_G4COMMAND_ = MPI::COMM_WORLD.Dup();
103  DMSG(0,"Comm world size: "<<commSize<<" this rank is: "
104  <<myrank<<" sending to rank "<<destinationRank);
105  for ( G4int i = 0 ; i < commSize; ++i ) {
106  //Send for all ranks except receiver
107  if ( myrank != destinationRank ) Send();
108  }
109  //Receiver receives from all ranks
110  if ( myrank == destinationRank ) {
111  for (G4int fromRank = 0 ; fromRank < commSize; ++fromRank) {
112  //Do not receive from myself
113  if ( fromRank != destinationRank ) Receive(fromRank);
114  }
115  }
116  DMSG(0,"G4MPIRunMerger::Merge done");
117 }
118 
virtual void Merge(const G4Run *)
Definition: G4Run.cc:54
int G4int
Definition: G4Types.hh:78
virtual void Merge()
void SendInt(G4int *val, G4int size=1)
G4int GetNumberOfEvent() const
Definition: G4Run.hh:79
Definition: G4Run.hh:46
void SendDouble(G4double *val, G4int size=1)
virtual void Receive(G4int rank)
#define DMSG(LVL, MSG)
void ReceiveInt(G4int rank, G4int *val, G4int size=1)
virtual void Send()
G4MPIRunMerger(const G4Run *aRun, G4int destination=G4MPImanager::kRANK_MASTER, G4int verbosity=0)
virtual void RecordEvent(const G4Event *)
Definition: G4Run.cc:51
MPI::Intracomm COMM_G4COMMAND_
double G4double
Definition: G4Types.hh:76
void ReceiveDouble(G4int rank, G4double *val, G4int size=1)