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

#include <RunActionMaster.hh>

Inheritance diagram for RunActionMaster:
Collaboration diagram for RunActionMaster:

Public Member Functions

 RunActionMaster ()
 
 ~RunActionMaster ()
 
virtual G4RunGenerateRun ()
 
virtual void BeginOfRunAction (const G4Run *arun)
 
virtual void EndOfRunAction (const G4Run *arun)
 
- Public Member Functions inherited from G4UserRunAction
 G4UserRunAction ()
 
virtual ~G4UserRunAction ()
 
virtual void SetMaster (G4bool val=true)
 
G4bool IsMaster () const
 

Additional Inherited Members

- Protected Attributes inherited from G4UserRunAction
G4bool isMaster
 

Detailed Description

Definition at line 36 of file RunActionMaster.hh.

Constructor & Destructor Documentation

RunActionMaster::RunActionMaster ( )

Definition at line 43 of file RunActionMaster.cc.

44 {
45 }
RunActionMaster::~RunActionMaster ( )

Definition at line 54 of file RunActionMaster.cc.

55 {
56 }

Member Function Documentation

void RunActionMaster::BeginOfRunAction ( const G4Run arun)
virtual

Reimplemented from G4UserRunAction.

Definition at line 60 of file RunActionMaster.cc.

61 {
63  myana-> Clear();
64  myana->Book();
65 }
void Clear(Node *)
static Analysis * GetAnalysis()
Definition: Analysis.cc:45
void Book()
Definition: Analysis.cc:65

Here is the call graph for this function:

void RunActionMaster::EndOfRunAction ( const G4Run arun)
virtual

Reimplemented from G4UserRunAction.

Definition at line 69 of file RunActionMaster.cc.

70 {
71  //This is executed by master thread only. Worker threads have already
72  //merged their results into this master threads.
73 
74  //We are going to merge the results via MPI for:
75  // 1. User-defined "Run" object
76  // 2. Command line scorers, if exists
77  // 3. G4Analysis objects
78 
79  // For debugging purposes in the following we write out results twice:
80  // BEFORE and AFTER the merging, so that rank 0 actually
81  // writes two files, the one called "*rank000*" will contain the partial
82  // results only from rank #0,
83  // the file *merged* contains the reduction from all ranks.
84  // It should be very easy to adapt this code
85 
86  const G4int rank = G4MPImanager::GetManager()-> GetRank();
87 
88  G4cout << "=====================================================" << G4endl;
89  G4cout << "Start EndOfRunAction for master thread in rank: " << rank<<G4endl;
90  G4cout << "=====================================================" << G4endl;
91 
92  //Merging of G4Run object:
93  //All ranks > 0 merge to rank #0
94  RunMerger rm(static_cast<const Run*>(arun));
95  G4int ver = 0 ; //Use 4 for lots of info
96  if ( rank == 0 && ver == 0) ver = 1;
97 
98  if ( ver > 1 ) {
99  G4cout<<"Before merge the Run object has test counter value: "
100  <<static_cast<const Run*>(arun)->GetCounter()<<G4endl;
101  }
102  rm.SetVerbosity( ver );
103  rm.Merge();
104  if ( ver > 1 ) {
105  G4cout<<"After merge the Run object has test counter value: "
106  <<static_cast<const Run*>(arun)->GetCounter()
107  <<" (with 1 thread== number of ranks)"<<G4endl;
108  }
109 
110  //Merge of scorers
111  ver = 0;
113  const auto scor = G4ScoringManager::GetScoringManager();
114  G4MPIscorerMerger sm(scor);
115  sm.SetVerbosity(ver);
116  //Debug!
117  auto debugme = [&scor](){
118  for ( size_t idx = 0 ; idx < scor->GetNumberOfMesh() ; ++idx) {
119  const auto m = scor->GetMesh(idx);
120  const MeshScoreMap& map = m->GetScoreMap();
121  std::for_each(map.begin(),map.end(),
122  [](const MeshScoreMap::value_type& e) {
123  G4cout<<e.first<<"("<<e.second<<"):"<<G4endl;
124  const auto data = e.second->GetMap();
125  for( auto it = data->begin() ; it != data->end() ; ++it ) {
126  G4cout<<it->first<<" => G4StatDouble(n,sum_w,sum_w2,sum_wx,sum_wx2): "
127  <<it->second->n()<<" "<<it->second->sum_w()<<" "
128  <<it->second->sum_w2()<<" "<<it->second->sum_wx()<<" "
129  <<it->second->sum_wx2()<<G4endl;
130  }
131  });
132  }
133  };
134  //Debug!
135  if ( ver > 4 ) {
136  G4cout<<"Before merging: Meshes dump"<<G4endl;
137  debugme();
138  }
139  //Write partial scorers from single ranks *before* merging
140  //Do not rely on UI command to write out scorers, because rank-specific
141  //files will have same file name: need to add rank # to file name
142  if ( true ) {
143  for ( size_t idx = 0 ; idx < scor->GetNumberOfMesh() ; ++idx) {
144  const auto m = scor->GetMesh(idx);
145  const auto& mn = m->GetWorldName();
146  std::ostringstream fname;
147  fname<<"scorer-"<<mn<<"-rank"<<rank<<".csv";
148  scor->DumpAllQuantitiesToFile(mn,fname.str());
149  }
150  }
151 
152  //Now reduce all scorers to rank #0
153  sm.Merge();
154 
155  //Debug!
156  if ( ver > 4 ) {
157  G4cout<<"After merging: Meshes dump"<<G4endl;
158  debugme();
159  }
160  //For rank #0 write out the merged files
161  if ( rank == 0 ) {
162  for ( size_t idx = 0 ; idx < scor->GetNumberOfMesh() ; ++idx) {
163  const auto m = scor->GetMesh(idx);
164  const auto& mn = m->GetWorldName();
165  std::ostringstream fname;
166  fname<<"scorer-"<<mn<<"-merged.csv";
167  scor->DumpAllQuantitiesToFile(mn,fname.str());
168  }
169  }
170  }
171 
172  //Save histograms *before* MPI merging for rank #0
173  if (rank == 0)
174  {
175  G4String fname("dose-rank0");
176  Analysis* myana = Analysis::GetAnalysis();
177  myana-> Save(fname);
178  }
179  //Merge of g4analysis objects
180  ver=0;
181  G4MPIhistoMerger hm(G4AnalysisManager::Instance());
182  hm.SetVerbosity(ver);
183  hm.Merge();
184 
185  //Save g4analysis objects to a file
186  //NB: It is important that the save is done *after* MPI-merging of histograms
187 
188  //One can save all ranks or just rank0, chane the if
189  if (true /*rank == 0*/)
190  {
191  std::ostringstream fname;
192  fname<<"dose-rank"<<rank;
193  if (rank == 0) {
194  fname.str("dose-merged");
195  }
196  Analysis* myana = Analysis::GetAnalysis();
197  myana-> Save(fname.str());
198  }
199  Analysis* myana = Analysis::GetAnalysis();
200  myana-> Close();
201 
202  G4cout << "===================================================" << G4endl;
203  G4cout << "End EndOfRunAction for master thread in rank: " << rank << G4endl;
204  G4cout << "===================================================" << G4endl;
205 
206 }
int G4int
Definition: G4Types.hh:78
const XML_Char const XML_Char * data
Definition: expat.h:268
G4GLOB_DLL std::ostream G4cout
static G4ScoringManager * GetScoringManagerIfExist()
static constexpr double m
Definition: G4SIunits.hh:129
static Analysis * GetAnalysis()
Definition: Analysis.cc:45
static G4MPImanager * GetManager()
#define G4endl
Definition: G4ios.hh:61
string fname
Definition: test.py:308
static G4ScoringManager * GetScoringManager()
Definition: Run.hh:46
std::map< G4String, RunScore * > MeshScoreMap

Here is the call graph for this function:

G4Run * RunActionMaster::GenerateRun ( void  )
virtual

Reimplemented from G4UserRunAction.

Definition at line 48 of file RunActionMaster.cc.

49 {
50  return new Run;
51 }
Definition: Run.hh:46

The documentation for this class was generated from the following files: