47 static void thread_ExecuteThreadCommand(
const G4String* command)
54 : verbose(0), qfcout(false), qinitmacro(false), qbatchmode(false),
55 threadID(0), masterWeight(1.)
58 MPI::Init_thread(MPI::THREAD_SERIALIZED);
64 : verbose(0), qfcout(false), qinitmacro(false), qbatchmode(false),
65 threadID(0), masterWeight(1.)
68 MPI::Init_thread(argc, argv, MPI::THREAD_SERIALIZED);
70 ParseArguments(argc, argv);
76 if(isSlave && qfcout) fscout.close();
82 COMM_G4COMMAND.Free();
92 assert( theManager != 0 );
97 void G4MPImanager::Initialize()
99 assert( theManager == 0 );
104 size = MPI::COMM_WORLD.Get_size();
105 rank = MPI::COMM_WORLD.Get_rank();
110 COMM_G4COMMAND = MPI::COMM_WORLD.Dup();
123 void G4MPImanager::ParseArguments(
int argc,
char** argv)
130 G4int option_index= 0;
131 static struct option long_options[] = {
133 {
"verbose", 0, 0, 0},
140 c= getopt_long(argc, argv,
"hvi:o", long_options, &option_index);
147 switch(option_index) {
160 if(optarg) ofprefix =
optarg;
190 if(isSlave && qfcout) {
193 sprintf(str, prefix.c_str(), rank);
195 fscout.open(
fname.c_str(), std::ios::out);
199 if (optind < argc ) {
201 macroFileName = argv[
optind];
206 void G4MPImanager::Wait(
G4int ausec)
const
208 struct timespec treq, trem;
210 treq.tv_nsec = ausec*1000;
212 nanosleep(&treq, &trem);
216 void G4MPImanager::UpdateStatus()
219 const G4Run* run = runManager-> GetCurrentRun();
221 G4int runid, eventid, neventTBP;
227 runid = run-> GetRunID();
228 neventTBP = run -> GetNumberOfEventToBeProcessed();
229 eventid = run-> GetNumberOfEvent();
231 status-> StopTimer();
239 status-> SetStatus(rank, runid, neventTBP, eventid, g4state);
253 G4int nev = status-> GetEventID();
254 G4int nevtp = status-> GetNEventToBeProcessed();
255 G4double cputime = status-> GetCPUTime();
258 for (
G4int islave = 1; islave < size; islave++) {
261 status-> UnPack(buff);
265 nev += status-> GetEventID();
266 nevtp += status-> GetNEventToBeProcessed();
267 cputime += status-> GetCPUTime();
277 G4cout <<
"-------------------------------------------------------"
279 <<
"* #ranks= " << size
280 <<
" event= " << nev <<
"/" << nevtp
281 <<
" state= " << strStatus
282 <<
" time= " << cputime <<
"s"
294 std::vector<G4long> seedList = seedGenerator-> GetSeedList();
305 G4cout <<
"* rank= " << rank
309 for (
G4int islave = 1; islave < size; islave++) {
310 COMM_G4COMMAND.Recv(&buff, 1, MPI::LONG, islave,
TAG_G4SEED);
311 G4cout <<
"* rank= " << islave
338 for (
G4int islave = 1; islave < size; islave++) {
339 MPI::Request request = COMM_G4COMMAND.Irecv(&buff, 1, MPI::UNSIGNED,
342 while(! request.Test(status)) {
348 buff = unsigned(threadID);
354 COMM_G4COMMAND.Bcast(&buff, 1, MPI::UNSIGNED,
RANK_MASTER);
365 G4int rc = UI-> ApplyCommand(command);
367 G4int commandStatus = rc - (rc%100);
369 switch(commandStatus) {
373 G4cerr <<
"illegal application state -- command refused" <<
G4endl;
376 G4cerr <<
"command refused (" << commandStatus <<
")" <<
G4endl;
382 pthread_join(threadID, 0);
396 G4cout <<
"G4MPIsession:: beamOn is still running." <<
G4endl;
401 G4int rc = pthread_create(&threadID, 0,
402 (
Func_t)thread_ExecuteThreadCommand,
408 "Failed to create a beamOn thread.");
416 pthread_join(threadID, 0);
424 enum { BUFF_SIZE = 512 };
425 static char sbuff[BUFF_SIZE];
426 command.copy(sbuff,BUFF_SIZE);
437 for (
G4int islave = 1; islave < size; islave++) {
438 COMM_G4COMMAND.Send(sbuff, BUFF_SIZE, MPI::CHAR, islave,
TAG_G4COMMAND);
442 MPI::Request request= COMM_G4COMMAND.Irecv(sbuff, BUFF_SIZE, MPI::CHAR,
446 while(! request.Test(status)) {
457 G4bool currentmode = qbatchmode;
460 batchSession-> SessionStart();
462 qbatchmode = currentmode;
471 G4double ntot = masterWeight+size-1.;
473 G4int nproc0 = nevent-nproc*(size-1);
475 if(verbose>0 && isMaster) {
476 G4cout <<
"#events in master=" << nproc0 <<
" / "
477 <<
"#events in slave=" << nproc <<
G4endl;
480 status-> StartTimer();
481 if(isMaster) runManager->
BeamOn(nproc0);
482 else runManager->
BeamOn(nproc);
483 status-> StopTimer();
486 if(verbose>0 && isMaster) {
487 G4cout <<
"#events in master=" << nevent <<
" / "
488 <<
"#events in slave=" << nevent <<
G4endl;
490 status-> StartTimer();
491 runManager->
BeamOn(nevent);
492 status-> StopTimer();
503 for (
G4int islave = 1; islave < size; islave++) {
504 MPI::Request request = COMM_G4COMMAND.Irecv(&buff, 1, MPI::INT,
507 while(! request.Test(status)) {
522 std::cout << message << std::flush;
525 fscout << message << std::flush;
527 std::cout << rank <<
":" << message << std::flush;
539 G4cout <<
"<app> [options] [macro file]"
541 G4cout <<
" -h, --help show this message."
543 G4cout <<
" -v, --verbose show verbose message"
545 G4cout <<
" -i, --init=FNAME set an init macro file"
547 G4cout <<
" -o, --ofile[=FNAME] set slave output to a flie"