Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Static Public Member Functions
G4MPImanager Class Reference

#include <G4MPImanager.hh>

Public Types

enum  { kRANK_MASTER = 0 }
 
enum  { kTAG_G4COMMAND = 100, kTAG_G4STATUS = 200, kTAG_G4SEED = 300, kTAG_DATA = 1000 }
 

Public Member Functions

 G4MPImanager ()
 
 G4MPImanager (int argc, char **argv)
 
 ~G4MPImanager ()
 
G4MPIsessionGetMPIsession () const
 
G4int GetVerbose () const
 
void SetVerbose (G4int iverbose)
 
G4int GetSize () const
 
G4int GetRank () const
 
G4bool IsMaster () const
 
G4bool IsSlave () const
 
G4bool IsInitMacro () const
 
const G4StringGetInitFileName () const
 
G4bool IsBatchMode () const
 
const G4StringGetMacroFileName () const
 
void SetMasterWeight (G4double aweight)
 
G4double GetMasterWeight () const
 
G4VMPIseedGeneratorGetSeedGenerator () const
 
G4String BcastCommand (const G4String &command)
 
void ShowStatus ()
 
void ShowSeeds ()
 
void SetSeed (G4int inode, G4long seed)
 
void WaitBeamOn ()
 
void DistributeSeeds ()
 
void ExecuteMacroFile (const G4String &fname, G4bool qbatch=false)
 
G4bool CheckThreadStatus ()
 
void ExecuteThreadCommand (const G4String &command)
 
void ExecuteBeamOnThread (const G4String &command)
 
void JoinBeamOnThread ()
 
void BeamOn (G4int nevent, G4bool qdivide=true)
 
void Print (const G4String &message)
 
void ShowHelp () const
 

Static Public Member Functions

static G4MPImanagerGetManager ()
 

Detailed Description

Definition at line 45 of file G4MPImanager.hh.

Member Enumeration Documentation

anonymous enum
Enumerator
kRANK_MASTER 

Definition at line 48 of file G4MPImanager.hh.

anonymous enum
Enumerator
kTAG_G4COMMAND 
kTAG_G4STATUS 
kTAG_G4SEED 
kTAG_DATA 

Definition at line 50 of file G4MPImanager.hh.

Constructor & Destructor Documentation

G4MPImanager::G4MPImanager ( )

Definition at line 68 of file G4MPImanager.cc.

References Initialize().

69  : verbose_(0), qfcout_(false), qinitmacro_(false), qbatchmode_(false),
70  thread_id_(0), master_weight_(1.)
71 {
72  //MPI::Init();
73  MPI::Init_thread(MPI::THREAD_SERIALIZED);
74  Initialize();
75 }
G4MPImanager::G4MPImanager ( int  argc,
char **  argv 
)

Definition at line 78 of file G4MPImanager.cc.

References Initialize().

79  : verbose_(0), qfcout_(false), qinitmacro_(false), qbatchmode_(false),
80  thread_id_(0), master_weight_(1.)
81 {
82  //MPI::Init(argc, argv);
83  MPI::Init_thread(argc, argv, MPI::THREAD_SERIALIZED);
84  Initialize();
85  ParseArguments(argc, argv);
86 }
G4MPImanager::~G4MPImanager ( )

Definition at line 89 of file G4MPImanager.cc.

References Finalize().

90 {
91  if( is_slave_ && qfcout_ ) fscout_.close();
92 
93  delete status_;
94  delete messenger_;
95  delete session_;
96 
97  COMM_G4COMMAND_.Free();
98 
99  MPI::Finalize();
100 }
void Finalize()
Definition: errprop.cc:236

Member Function Documentation

G4String G4MPImanager::BcastCommand ( const G4String command)

Definition at line 416 of file G4MPImanager.cc.

References kRANK_MASTER, and kTAG_G4COMMAND.

417 {
418  enum { kBUFF_SIZE = 512 };
419  static char sbuff[kBUFF_SIZE];
420  command.copy(sbuff, kBUFF_SIZE);
421  G4int len = command.size();
422  sbuff[len] ='\0'; // no boundary check
423 
424  // "command" is not yet fixed in slaves at this time.
425 
426  // waiting message exhausts CPU in LAM!
427  //COMM_G4COMMAND_.Bcast(sbuff, ssize, MPI::CHAR, RANK_MASTER);
428 
429  // another implementation
430  if( is_master_ ) {
431  for ( G4int islave = 1; islave < size_; islave++ ) {
432  COMM_G4COMMAND_.Send(sbuff, kBUFF_SIZE, MPI::CHAR,
433  islave, kTAG_G4COMMAND);
434  }
435  } else {
436  // try non-blocking receive
437  MPI::Request request= COMM_G4COMMAND_.Irecv(sbuff, kBUFF_SIZE, MPI::CHAR,
439  // polling...
440  while(! request.Test()) {
441  ::Wait(1000);
442  }
443  }
444 
445  return G4String(sbuff);
446 }
int G4int
Definition: G4Types.hh:78
const XML_Char int len
void G4MPImanager::BeamOn ( G4int  nevent,
G4bool  qdivide = true 
)

Definition at line 460 of file G4MPImanager.cc.

References G4UIcommand::ConvertToString(), G4cout, G4endl, G4RunManager::GetRunManager(), and G4UImanager::GetUIpointer().

461 {
462 #ifndef G4MULTITHREADED
464 #endif
465 
466  if ( qdivide ) { // events are divided
467  G4double ntot = master_weight_ + size_ - 1.;
468  G4int nproc = G4int(nevent/ntot);
469  G4int nproc0 = nevent - nproc*(size_-1);
470 
471  if ( verbose_ > 0 && is_master_ ) {
472  G4cout << "#events in master=" << nproc0 << " / "
473  << "#events in slave=" << nproc << G4endl;
474  }
475 
476  status_-> StartTimer(); // start timer
477 
478 #ifdef G4MULTITHREADED
479  G4String str_nevt;
480  if ( is_master_ ) str_nevt = G4UIcommand::ConvertToString(nproc0);
481  else str_nevt = G4UIcommand::ConvertToString(nproc);
483  UI-> ApplyCommand("/run/beamOn " + str_nevt);
484 #else
485  if ( is_master_ ) runManager-> BeamOn(nproc0);
486  else runManager-> BeamOn(nproc);
487 #endif
488 
489  status_-> StopTimer(); // stop timer
490 
491  } else { // same events are generated in each node (for test use)
492  if( verbose_ > 0 && is_master_ ) {
493  G4cout << "#events in master=" << nevent << " / "
494  << "#events in slave=" << nevent << G4endl;
495  }
496  status_-> StartTimer(); // start timer
497 
498 #ifdef G4MULTITHREADED
499  G4String str_nevt = G4UIcommand::ConvertToString(nevent);
501  UI-> ApplyCommand("/run/beamOn " + str_nevt);
502 #else
503  runManager-> BeamOn(nevent);
504 #endif
505 
506  status_-> StopTimer(); // stop timer
507  }
508 }
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:357
int G4int
Definition: G4Types.hh:78
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:58
G4GLOB_DLL std::ostream G4cout
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:74
#define G4endl
Definition: G4ios.hh:61
void BeamOn(G4int nevent, G4bool qdivide=true)
double G4double
Definition: G4Types.hh:76
G4bool G4MPImanager::CheckThreadStatus ( )

Definition at line 325 of file G4MPImanager.cc.

References kRANK_MASTER, and kTAG_G4STATUS.

Referenced by ExecuteBeamOnThread(), and ShowStatus().

326 {
327  unsigned buff;
328  unsigned qstatus = 0;
329 
330  if( is_master_ ) {
331  qstatus = unsigned(thread_id_);
332  // get slave status
333  for ( G4int islave = 1; islave < size_; islave++ ) {
334  MPI::Request request = COMM_G4COMMAND_.Irecv(&buff, 1, MPI::UNSIGNED,
335  islave, kTAG_G4STATUS);
336  while( ! request.Test() ) {
337  ::Wait(1000);
338  }
339  qstatus |= buff;
340  }
341  } else {
342  buff = unsigned(thread_id_);
343  COMM_G4COMMAND_.Send(&buff, 1, MPI::UNSIGNED, kRANK_MASTER, kTAG_G4STATUS);
344  }
345 
346  // broadcast
347  buff = qstatus; // for master
348  COMM_G4COMMAND_.Bcast(&buff, 1, MPI::UNSIGNED, kRANK_MASTER);
349  qstatus = buff; // for slave
350 
351  if ( qstatus != 0 ) return true;
352  else return false;
353 }
int G4int
Definition: G4Types.hh:78
void G4MPImanager::DistributeSeeds ( )

Definition at line 287 of file G4MPImanager.cc.

References CLHEP::HepRandom::setTheSeed().

288 {
289  std::vector<G4long> seed_list = seed_generator_-> GetSeedList();
290  CLHEP::HepRandom::setTheSeed(seed_list[rank_]);
291 }
static void setTheSeed(long seed, int lux=3)
Definition: Random.cc:131
void G4MPImanager::ExecuteBeamOnThread ( const G4String command)

Definition at line 385 of file G4MPImanager.cc.

References CheckThreadStatus(), test::cmdstr, FatalException, G4cout, G4endl, and G4Exception().

386 {
387  G4bool threadStatus = CheckThreadStatus();
388 
389  if (threadStatus) {
390  if ( is_master_ ) {
391  G4cout << "G4MPIsession:: beamOn is still running." << G4endl;
392  }
393  } else { // ok
394  static G4String cmdstr;
395  cmdstr = command;
396  G4int rc = pthread_create(&thread_id_, 0,
397  (Func_t)thread_ExecuteThreadCommand,
398  (void*)&cmdstr);
399  if (rc != 0)
400  G4Exception("G4MPImanager::ExecuteBeamOnThread()",
401  "MPI003", FatalException,
402  "Failed to create a beamOn thread.");
403  }
404 }
int G4int
Definition: G4Types.hh:78
void *(* Func_t)(void *)
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
#define G4endl
Definition: G4ios.hh:61
G4bool CheckThreadStatus()
void G4MPImanager::ExecuteMacroFile ( const G4String fname,
G4bool  qbatch = false 
)

Definition at line 449 of file G4MPImanager.cc.

450 {
451  G4bool currentmode = qbatchmode_;
452  qbatchmode_ = true;
453  G4MPIbatch* batchSession = new G4MPIbatch(fname, qbatch);
454  batchSession-> SessionStart();
455  delete batchSession;
456  qbatchmode_ = currentmode;
457 }
bool G4bool
Definition: G4Types.hh:79
void G4MPImanager::ExecuteThreadCommand ( const G4String command)

Definition at line 356 of file G4MPImanager.cc.

References fCommandSucceeded, fIllegalApplicationState, G4cerr, G4endl, and G4UImanager::GetUIpointer().

357 {
358  // this method is a thread function.
360  G4int rc = UI-> ApplyCommand(command);
361 
362  G4int commandStatus = rc - (rc%100);
363 
364  switch( commandStatus ) {
365  case fCommandSucceeded:
366  break;
368  G4cerr << "illegal application state -- command refused" << G4endl;
369  break;
370  default:
371  G4cerr << "command refused (" << commandStatus << ")" << G4endl;
372  break;
373  }
374 
375  // thread is joined
376  if ( thread_id_ ) {
377  pthread_join(thread_id_, 0);
378  thread_id_ = 0;
379  }
380 
381  return;
382 }
int G4int
Definition: G4Types.hh:78
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:58
#define G4endl
Definition: G4ios.hh:61
G4GLOB_DLL std::ostream G4cerr
const G4String & G4MPImanager::GetInitFileName ( ) const
inline

Definition at line 199 of file G4MPImanager.hh.

Referenced by G4MPIsession::SessionStart().

200 {
201  return init_file_name_;
202 
203 }
const G4String & G4MPImanager::GetMacroFileName ( ) const
inline

Definition at line 210 of file G4MPImanager.hh.

Referenced by G4MPIsession::SessionStart().

211 {
212  return macro_file_name_;
213 }
G4MPImanager * G4MPImanager::GetManager ( )
static

Definition at line 103 of file G4MPImanager.cc.

References FatalException, and G4Exception().

Referenced by G4MPIrandomSeedGenerator::CheckDoubleCount(), G4VMPIsession::G4VMPIsession(), and G4MPIrandomSeedGenerator::GenerateSeeds().

104 {
105  if ( g4mpi_ == NULL ) {
106  G4Exception("G4MPImanager::GetManager()", "MPI001",
107  FatalException, "G4MPImanager is not instantiated.");
108  }
109  return g4mpi_;
110 }
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4double G4MPImanager::GetMasterWeight ( ) const
inline

Definition at line 223 of file G4MPImanager.hh.

Referenced by G4MPImessenger::GetCurrentValue().

224 {
225  return master_weight_;
226 }
G4MPIsession * G4MPImanager::GetMPIsession ( ) const
inline

Definition at line 153 of file G4MPImanager.hh.

154 {
155  return session_;
156 }
G4int G4MPImanager::GetRank ( ) const
inline

Definition at line 173 of file G4MPImanager.hh.

174 {
175  return rank_;
176 }
G4VMPIseedGenerator * G4MPImanager::GetSeedGenerator ( ) const
inline

Definition at line 228 of file G4MPImanager.hh.

229 {
230  return seed_generator_;
231 }
G4int G4MPImanager::GetSize ( ) const
inline

Definition at line 178 of file G4MPImanager.hh.

179 {
180  return size_;
181 }
G4int G4MPImanager::GetVerbose ( ) const
inline

Definition at line 158 of file G4MPImanager.hh.

Referenced by G4MPImessenger::GetCurrentValue().

159 {
160  return verbose_;
161 }
G4bool G4MPImanager::IsBatchMode ( ) const
inline

Definition at line 205 of file G4MPImanager.hh.

206 {
207  return qbatchmode_;
208 }
G4bool G4MPImanager::IsInitMacro ( ) const
inline

Definition at line 193 of file G4MPImanager.hh.

194 {
195  return qinitmacro_;
196 
197 }
G4bool G4MPImanager::IsMaster ( ) const
inline

Definition at line 183 of file G4MPImanager.hh.

184 {
185  return is_master_;
186 }
G4bool G4MPImanager::IsSlave ( ) const
inline

Definition at line 188 of file G4MPImanager.hh.

189 {
190  return is_slave_;
191 }
void G4MPImanager::JoinBeamOnThread ( )

Definition at line 407 of file G4MPImanager.cc.

408 {
409  if ( thread_id_ ) {
410  pthread_join(thread_id_, 0);
411  thread_id_ = 0;
412  }
413 }
void G4MPImanager::Print ( const G4String message)

Definition at line 532 of file G4MPImanager.cc.

Referenced by ShowStatus().

533 {
534  if ( is_master_ ){
535  std::cout << message << std::flush;
536  } else {
537  if ( qfcout_ ) { // output to a file
538  fscout_ << message << std::flush;
539  } else { // output to stdout
540  std::cout << rank_ << ":" << message << std::flush;
541  }
542  }
543 }
void G4MPImanager::SetMasterWeight ( G4double  aweight)
inline

Definition at line 215 of file G4MPImanager.hh.

216 {
217  master_weight_ = aweight;
218 
219  if( aweight < 0. ) master_weight_ = 0.;
220  if( aweight > 1. ) master_weight_ = 1.;
221 }
void G4MPImanager::SetSeed ( G4int  inode,
G4long  seed 
)

Definition at line 317 of file G4MPImanager.cc.

References CLHEP::HepRandom::setTheSeed().

318 {
319  if( rank_ == inode ) {
321  }
322 }
static void setTheSeed(long seed, int lux=3)
Definition: Random.cc:131
void G4MPImanager::SetVerbose ( G4int  iverbose)
inline

Definition at line 163 of file G4MPImanager.hh.

164 {
165  G4int lv = iverbose;
166  if( iverbose > 1 ) lv = 1;
167  if( iverbose < 0 ) lv = 0;
168 
169  verbose_ = lv;
170  return;
171 }
int G4int
Definition: G4Types.hh:78
void G4MPImanager::ShowHelp ( ) const

Definition at line 546 of file G4MPImanager.cc.

References G4cout, and G4endl.

547 {
548  if (is_slave_ ) return;
549 
550  G4cout << "Geant4 MPI interface" << G4endl;
551  G4cout << "usage:" << G4endl;
552  G4cout << "<app> [options] [macro file]"
553  << G4endl << G4endl;
554  G4cout << " -h, --help show this message."
555  << G4endl;
556  G4cout << " -v, --verbose show verbose message"
557  << G4endl;
558  G4cout << " -i, --init=FNAME set an init macro file"
559  << G4endl;
560  G4cout << " -o, --ofile[=FNAME] set slave output to a flie"
561  << G4endl;
562  G4cout << G4endl;
563 }
G4GLOB_DLL std::ostream G4cout
#define G4endl
Definition: G4ios.hh:61
void G4MPImanager::ShowSeeds ( )

Definition at line 294 of file G4MPImanager.cc.

References G4cout, G4endl, CLHEP::HepRandom::getTheSeed(), kRANK_MASTER, and kTAG_G4SEED.

295 {
296  G4long buff;
297 
298  if ( is_master_ ) {
299  // print master
300  G4cout << "* rank= " << rank_
301  << " seed= " << CLHEP::HepRandom::getTheSeed()
302  << G4endl;
303  // receive from each slave
304  for ( G4int islave = 1; islave < size_; islave++ ) {
305  COMM_G4COMMAND_.Recv(&buff, 1, MPI::LONG, islave, kTAG_G4SEED);
306  G4cout << "* rank= " << islave
307  << " seed= " << buff
308  << G4endl;
309  }
310  } else { // slaves
312  COMM_G4COMMAND_.Send(&buff, 1, MPI::LONG, kRANK_MASTER, kTAG_G4SEED);
313  }
314 }
long G4long
Definition: G4Types.hh:80
int G4int
Definition: G4Types.hh:78
G4GLOB_DLL std::ostream G4cout
static long getTheSeed()
Definition: Random.cc:136
#define G4endl
Definition: G4ios.hh:61
void G4MPImanager::ShowStatus ( )

Definition at line 238 of file G4MPImanager.cc.

References CheckThreadStatus(), G4cout, G4endl, G4FFGEnumerations::INT, G4MPIstatus::kNSIZE, kRANK_MASTER, kTAG_G4STATUS, and Print().

239 {
241 
242  UpdateStatus();
243  G4bool gstatus = CheckThreadStatus();
244 
245  if ( is_master_ ) {
246  status_-> Print(); // for maser itself
247 
248  G4int nev = status_-> GetEventID();
249  G4int nevtp = status_-> GetNEventToBeProcessed();
250  G4double cputime = status_-> GetCPUTime();
251 
252  // receive from each slave
253  for ( G4int islave = 1; islave < size_; islave++ ) {
254  COMM_G4COMMAND_.Recv(buff, G4MPIstatus::kNSIZE, MPI::INT,
255  islave, kTAG_G4STATUS);
256  status_-> UnPack(buff);
257  status_-> Print();
258 
259  // aggregation
260  nev += status_-> GetEventID();
261  nevtp += status_-> GetNEventToBeProcessed();
262  cputime += status_-> GetCPUTime();
263  }
264 
265  G4String strStatus;
266  if ( gstatus ) {
267  strStatus = "Run";
268  } else {
269  strStatus = "Idle";
270  }
271 
272  G4cout << "-------------------------------------------------------"
273  << G4endl
274  << "* #ranks= " << size_
275  << " event= " << nev << "/" << nevtp
276  << " state= " << strStatus
277  << " time= " << cputime << "s"
278  << G4endl;
279  } else {
280  status_-> Pack(buff);
281  COMM_G4COMMAND_.Send(buff, G4MPIstatus::kNSIZE, MPI::INT,
283  }
284 }
int G4int
Definition: G4Types.hh:78
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
void Print(const G4String &message)
#define G4endl
Definition: G4ios.hh:61
G4bool CheckThreadStatus()
double G4double
Definition: G4Types.hh:76
void G4MPImanager::WaitBeamOn ( )

Definition at line 511 of file G4MPImanager.cc.

References G4FFGEnumerations::INT, kRANK_MASTER, and kTAG_G4STATUS.

512 {
513  G4int buff = 0;
514  if ( qbatchmode_ ) { // valid only in batch mode
515  if ( is_master_ ) {
516  // receive from each slave
517  for (G4int islave = 1; islave < size_; islave++) {
518  MPI::Request request = COMM_G4COMMAND_.Irecv(&buff, 1, MPI::INT,
519  islave, kTAG_G4STATUS);
520  while(! request.Test()) {
521  ::Wait(1000);
522  }
523  }
524  } else {
525  buff = 1;
526  COMM_G4COMMAND_.Send(&buff, 1, MPI::INT, kRANK_MASTER, kTAG_G4STATUS);
527  }
528  }
529 }
int G4int
Definition: G4Types.hh:78

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