Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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,
  kTAG_HISTO = 1001, kTAG_RUN = 1002, kTAG_CMDSCR = 1003
}
 

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 
kTAG_HISTO 
kTAG_RUN 
kTAG_CMDSCR 

Definition at line 50 of file G4MPImanager.hh.

Constructor & Destructor Documentation

G4MPImanager::G4MPImanager ( )

Definition at line 68 of file G4MPImanager.cc.

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 }

Here is the call graph for this function:

G4MPImanager::G4MPImanager ( int  argc,
char **  argv 
)

Definition at line 78 of file G4MPImanager.cc.

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 }

Here is the call graph for this function:

G4MPImanager::~G4MPImanager ( )

Definition at line 89 of file G4MPImanager.cc.

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:257

Here is the call graph for this function:

Member Function Documentation

G4String G4MPImanager::BcastCommand ( const G4String command)

Definition at line 416 of file G4MPImanager.cc.

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 }
const XML_Char int len
Definition: expat.h:262
int G4int
Definition: G4Types.hh:78
void G4MPImanager::BeamOn ( G4int  nevent,
G4bool  qdivide = true 
)

Definition at line 460 of file G4MPImanager.cc.

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:372
int G4int
Definition: G4Types.hh:78
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:59
G4GLOB_DLL std::ostream G4cout
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
#define G4endl
Definition: G4ios.hh:61
void BeamOn(G4int nevent, G4bool qdivide=true)
double G4double
Definition: G4Types.hh:76

Here is the call graph for this function:

G4bool G4MPImanager::CheckThreadStatus ( )

Definition at line 325 of file G4MPImanager.cc.

326 {
327  unsigned buff;
328  unsigned qstatus = 0;
329 
330  if( is_master_ ) {
331  qstatus = (thread_id_ != 0);
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 = (thread_id_ !=0);
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

Here is the caller graph for this function:

void G4MPImanager::DistributeSeeds ( )

Definition at line 287 of file G4MPImanager.cc.

288 {
289  std::vector<G4long> seed_list = seed_generator_-> GetSeedList();
290  G4Random::setTheSeed(seed_list[rank_]);
291 }
void G4MPImanager::ExecuteBeamOnThread ( const G4String command)

Definition at line 385 of file G4MPImanager.cc.

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 }
string cmdstr
Definition: test.py:123
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()

Here is the call graph for this function:

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.

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:59
#define G4endl
Definition: G4ios.hh:61
G4GLOB_DLL std::ostream G4cerr

Here is the call graph for this function:

const G4String & G4MPImanager::GetInitFileName ( ) const
inline

Definition at line 203 of file G4MPImanager.hh.

204 {
205  return init_file_name_;
206 
207 }

Here is the caller graph for this function:

const G4String & G4MPImanager::GetMacroFileName ( ) const
inline

Definition at line 214 of file G4MPImanager.hh.

215 {
216  return macro_file_name_;
217 }

Here is the caller graph for this function:

G4MPImanager * G4MPImanager::GetManager ( )
static

Definition at line 103 of file G4MPImanager.cc.

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

Here is the call graph for this function:

Here is the caller graph for this function:

G4double G4MPImanager::GetMasterWeight ( ) const
inline

Definition at line 227 of file G4MPImanager.hh.

228 {
229  return master_weight_;
230 }

Here is the caller graph for this function:

G4MPIsession * G4MPImanager::GetMPIsession ( ) const
inline

Definition at line 157 of file G4MPImanager.hh.

158 {
159  return session_;
160 }
G4int G4MPImanager::GetRank ( ) const
inline

Definition at line 177 of file G4MPImanager.hh.

178 {
179  return rank_;
180 }
G4VMPIseedGenerator * G4MPImanager::GetSeedGenerator ( ) const
inline

Definition at line 232 of file G4MPImanager.hh.

233 {
234  return seed_generator_;
235 }
G4int G4MPImanager::GetSize ( ) const
inline

Definition at line 182 of file G4MPImanager.hh.

183 {
184  return size_;
185 }
G4int G4MPImanager::GetVerbose ( ) const
inline

Definition at line 162 of file G4MPImanager.hh.

163 {
164  return verbose_;
165 }

Here is the caller graph for this function:

G4bool G4MPImanager::IsBatchMode ( ) const
inline

Definition at line 209 of file G4MPImanager.hh.

210 {
211  return qbatchmode_;
212 }
G4bool G4MPImanager::IsInitMacro ( ) const
inline

Definition at line 197 of file G4MPImanager.hh.

198 {
199  return qinitmacro_;
200 
201 }
G4bool G4MPImanager::IsMaster ( ) const
inline

Definition at line 187 of file G4MPImanager.hh.

188 {
189  return is_master_;
190 }
G4bool G4MPImanager::IsSlave ( ) const
inline

Definition at line 192 of file G4MPImanager.hh.

193 {
194  return is_slave_;
195 }
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.

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 }

Here is the caller graph for this function:

void G4MPImanager::SetMasterWeight ( G4double  aweight)
inline

Definition at line 219 of file G4MPImanager.hh.

220 {
221  master_weight_ = aweight;
222 
223  if( aweight < 0. ) master_weight_ = 0.;
224  if( aweight > 1. ) master_weight_ = 1.;
225 }
void G4MPImanager::SetSeed ( G4int  inode,
G4long  seed 
)

Definition at line 317 of file G4MPImanager.cc.

318 {
319  if( rank_ == inode ) {
321  }
322 }
static void setTheSeed(long seed, int lux=3)
Definition: Random.cc:231
long seed
Definition: chem4.cc:68

Here is the call graph for this function:

void G4MPImanager::SetVerbose ( G4int  iverbose)
inline

Definition at line 167 of file G4MPImanager.hh.

168 {
169  G4int lv = iverbose;
170  if( iverbose > 1 ) lv = 1;
171  if( iverbose < 0 ) lv = 0;
172 
173  verbose_ = lv;
174  return;
175 }
int G4int
Definition: G4Types.hh:78
void G4MPImanager::ShowHelp ( ) const

Definition at line 546 of file G4MPImanager.cc.

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.

295 {
296  G4long buff;
297 
298  if ( is_master_ ) {
299  // print master
300  G4cout << "* rank= " << rank_
301  << " seed= " << G4Random::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
311  buff = G4Random::getTheSeed();
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
#define G4endl
Definition: G4ios.hh:61
void G4MPImanager::ShowStatus ( )

Definition at line 238 of file G4MPImanager.cc.

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

Here is the call graph for this function:

void G4MPImanager::WaitBeamOn ( )

Definition at line 511 of file G4MPImanager.cc.

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: