50 void thread_ExecuteThreadCommand(
const G4String* command)
56 void Wait(
G4int ausec)
58 struct timespec treq, trem;
60 treq.tv_nsec = ausec*1000;
62 nanosleep(&treq, &trem);
69 : verbose_(0), qfcout_(false), qinitmacro_(false), qbatchmode_(false),
70 thread_id_(0), master_weight_(1.)
73 MPI::Init_thread(MPI::THREAD_SERIALIZED);
79 : verbose_(0), qfcout_(false), qinitmacro_(false), qbatchmode_(false),
80 thread_id_(0), master_weight_(1.)
83 MPI::Init_thread(argc, argv, MPI::THREAD_SERIALIZED);
85 ParseArguments(argc, argv);
91 if( is_slave_ && qfcout_ ) fscout_.close();
97 COMM_G4COMMAND_.Free();
105 if ( g4mpi_ == NULL ) {
106 G4Exception(
"G4MPImanager::GetManager()",
"MPI001",
113 void G4MPImanager::Initialize()
115 if ( g4mpi_ != NULL ) {
116 G4Exception(
"G4MPImanager::Initialize()",
"MPI002",
123 size_ = MPI::COMM_WORLD.Get_size();
124 rank_ = MPI::COMM_WORLD.Get_rank();
129 COMM_G4COMMAND_ = MPI::COMM_WORLD.Dup();
133 messenger_-> SetTargetObject(
this);
143 void G4MPImanager::ParseArguments(
int argc,
char** argv)
150 G4int option_index = 0;
151 static struct option long_options[] = {
152 {
"help", no_argument, NULL,
'h'},
153 {
"verbose", no_argument, NULL,
'v'},
154 {
"init", required_argument, NULL,
'i'},
155 {
"ofile", optional_argument, NULL,
'o'},
160 c = getopt_long(argc, argv,
"hvi:o", long_options, &option_index);
182 std::exit(EXIT_FAILURE);
191 std::exit(EXIT_SUCCESS);
195 if( is_slave_ && qfcout_ ) {
198 sprintf(str, prefix.c_str(), rank_);
200 fscout_.open(
fname.c_str(), std::ios::out);
206 macro_file_name_ = argv[
optind];
211 void G4MPImanager::UpdateStatus()
214 const G4Run* run = runManager-> GetCurrentRun();
216 G4int runid, eventid, neventTBP;
222 runid = run-> GetRunID();
223 neventTBP = run -> GetNumberOfEventToBeProcessed();
224 eventid = run-> GetNumberOfEvent();
226 status_-> StopTimer();
234 status_-> SetStatus(rank_, runid, neventTBP, eventid, g4state);
248 G4int nev = status_-> GetEventID();
249 G4int nevtp = status_-> GetNEventToBeProcessed();
250 G4double cputime = status_-> GetCPUTime();
253 for (
G4int islave = 1; islave < size_; islave++ ) {
256 status_-> UnPack(buff);
260 nev += status_-> GetEventID();
261 nevtp += status_-> GetNEventToBeProcessed();
262 cputime += status_-> GetCPUTime();
272 G4cout <<
"-------------------------------------------------------"
274 <<
"* #ranks= " << size_
275 <<
" event= " << nev <<
"/" << nevtp
276 <<
" state= " << strStatus
277 <<
" time= " << cputime <<
"s"
280 status_-> Pack(buff);
289 std::vector<G4long> seed_list = seed_generator_-> GetSeedList();
290 G4Random::setTheSeed(seed_list[rank_]);
300 G4cout <<
"* rank= " << rank_
301 <<
" seed= " << G4Random::getTheSeed()
304 for (
G4int islave = 1; islave < size_; islave++ ) {
305 COMM_G4COMMAND_.Recv(&buff, 1, MPI::LONG, islave,
kTAG_G4SEED);
306 G4cout <<
"* rank= " << islave
311 buff = G4Random::getTheSeed();
319 if( rank_ == inode ) {
328 unsigned qstatus = 0;
331 qstatus = (thread_id_ != 0);
333 for (
G4int islave = 1; islave < size_; islave++ ) {
334 MPI::Request request = COMM_G4COMMAND_.Irecv(&buff, 1, MPI::UNSIGNED,
336 while( ! request.Test() ) {
342 buff = (thread_id_ !=0);
348 COMM_G4COMMAND_.Bcast(&buff, 1, MPI::UNSIGNED,
kRANK_MASTER);
351 if ( qstatus != 0 )
return true;
360 G4int rc = UI-> ApplyCommand(command);
362 G4int commandStatus = rc - (rc%100);
364 switch( commandStatus ) {
368 G4cerr <<
"illegal application state -- command refused" <<
G4endl;
371 G4cerr <<
"command refused (" << commandStatus <<
")" <<
G4endl;
377 pthread_join(thread_id_, 0);
391 G4cout <<
"G4MPIsession:: beamOn is still running." <<
G4endl;
396 G4int rc = pthread_create(&thread_id_, 0,
397 (
Func_t)thread_ExecuteThreadCommand,
402 "Failed to create a beamOn thread.");
410 pthread_join(thread_id_, 0);
418 enum { kBUFF_SIZE = 512 };
419 static char sbuff[kBUFF_SIZE];
420 command.copy(sbuff, kBUFF_SIZE);
431 for (
G4int islave = 1; islave < size_; islave++ ) {
432 COMM_G4COMMAND_.Send(sbuff, kBUFF_SIZE, MPI::CHAR,
437 MPI::Request request= COMM_G4COMMAND_.Irecv(sbuff, kBUFF_SIZE, MPI::CHAR,
440 while(! request.Test()) {
451 G4bool currentmode = qbatchmode_;
454 batchSession-> SessionStart();
456 qbatchmode_ = currentmode;
462 #ifndef G4MULTITHREADED
467 G4double ntot = master_weight_ + size_ - 1.;
469 G4int nproc0 = nevent - nproc*(size_-1);
471 if ( verbose_ > 0 && is_master_ ) {
472 G4cout <<
"#events in master=" << nproc0 <<
" / "
473 <<
"#events in slave=" << nproc <<
G4endl;
476 status_-> StartTimer();
478 #ifdef G4MULTITHREADED
483 UI-> ApplyCommand(
"/run/beamOn " + str_nevt);
485 if ( is_master_ ) runManager->
BeamOn(nproc0);
486 else runManager->
BeamOn(nproc);
489 status_-> StopTimer();
492 if( verbose_ > 0 && is_master_ ) {
493 G4cout <<
"#events in master=" << nevent <<
" / "
494 <<
"#events in slave=" << nevent <<
G4endl;
496 status_-> StartTimer();
498 #ifdef G4MULTITHREADED
501 UI-> ApplyCommand(
"/run/beamOn " + str_nevt);
503 runManager->
BeamOn(nevent);
506 status_-> StopTimer();
517 for (
G4int islave = 1; islave < size_; islave++) {
518 MPI::Request request = COMM_G4COMMAND_.Irecv(&buff, 1,
MPI::INT,
520 while(! request.Test()) {
535 std::cout << message << std::flush;
538 fscout_ << message << std::flush;
540 std::cout << rank_ <<
":" << message << std::flush;
548 if (is_slave_ )
return;
552 G4cout <<
"<app> [options] [macro file]"
554 G4cout <<
" -h, --help show this message."
556 G4cout <<
" -v, --verbose show verbose message"
558 G4cout <<
" -i, --init=FNAME set an init macro file"
560 G4cout <<
" -o, --ofile[=FNAME] set slave output to a flie"
A terminal session for MPI application.
An implementation of random number seed distribution.
void ExecuteThreadCommand(const G4String &command)
status of MPI application
static G4String ConvertToString(G4bool boolVal)
static void setTheSeed(long seed, int lux=3)
static G4UImanager * GetUIpointer()
static G4StateManager * GetStateManager()
G4String BcastCommand(const G4String &command)
G4GLOB_DLL std::ostream G4cout
static G4MPImanager * GetManager()
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
static G4RunManager * GetRunManager()
void Print(const G4String &message)
G4bool CheckThreadStatus()
void BeamOn(G4int nevent, G4bool qdivide=true)
void ExecuteMacroFile(const G4String &fname, G4bool qbatch=false)
G4GLOB_DLL std::ostream G4cerr
void ExecuteBeamOnThread(const G4String &command)
void SetSeed(G4int inode, G4long seed)