source: trunk/examples/extended/parallel/MPI/mpi_interface/src/G4MPIbatch.cc@ 1237

Last change on this file since 1237 was 807, checked in by garnier, 17 years ago

update

File size: 4.3 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// $Id: G4MPIbatch.cc,v 1.1 2007/11/16 14:05:41 kmura Exp $
27// $Name: $
28//
29// ====================================================================
30// G4MPIsession.cc
31//
32// 2007 Q
33// ====================================================================
34#include "G4MPIbatch.hh"
35#include "G4MPImanager.hh"
36#include "G4UIcommandStatus.hh"
37
38// ====================================================================
39//
40// class description
41//
42// ====================================================================
43
44////////////////////////////////////////////////////////////
45G4MPIbatch::G4MPIbatch(const G4String& fname, G4bool qbatch)
46 : G4VMPIsession(),
47 isOpened(false), isBatchMode(qbatch)
48////////////////////////////////////////////////////////////
49{
50 if(isMaster) {
51 batchStream.open(fname, std::ios::in);
52 if(batchStream.fail()) {
53 G4cerr << "cannot open a macro file(" << fname << ")."
54 << G4endl;
55 } else {
56 isOpened= true;
57 }
58 }
59}
60
61
62/////////////////////////////
63G4MPIbatch::~G4MPIbatch()
64/////////////////////////////
65{
66 if(isOpened) batchStream.close();
67}
68
69
70//////////////////////////////////
71G4String G4MPIbatch::ReadCommand()
72//////////////////////////////////
73{
74 enum { BUFSIZE= 256 };
75 char linebuf[BUFSIZE];
76
77 while(batchStream.good()) {
78 batchStream.getline(linebuf, BUFSIZE);
79
80 G4String cmdline(linebuf);
81 cmdline= cmdline.strip(G4String::both);
82 cmdline= cmdline.strip(G4String::both, '\011'); // remove TAB
83 cmdline= TruncateCommand(cmdline);
84
85 str_size ic= cmdline.find_first_of('#');
86 if(ic != G4String::npos) {
87 cmdline= cmdline(0, ic);
88 }
89
90 if(batchStream.eof()) {
91 if(cmdline.size()==0) {
92 return "exit";
93 } else {
94 return cmdline;
95 }
96 }
97
98 if(cmdline.size()==0) continue; // skip null line
99 return cmdline;
100 }
101
102 return "exit"; // dummy
103}
104
105
106///////////////////////////////////////
107G4UIsession* G4MPIbatch::SessionStart()
108///////////////////////////////////////
109{
110 G4String newCommand="", scommand; // newCommand is always "" in slaves
111
112 if(isMaster) newCommand= ReadCommand();
113 // broadcast a new G4 command
114 scommand= g4MPI-> BcastCommand(newCommand);
115 if(scommand == "exit" ) return 0;
116
117 while(1){
118 G4int rc= ExecCommand(scommand);
119 if(rc != fCommandSucceeded) break;
120
121 if(isMaster) newCommand= ReadCommand();
122 scommand= g4MPI-> BcastCommand(newCommand);
123 if(scommand == "exit" ) {
124 if(isBatchMode) {
125 if(g4MPI-> CheckThreadStatus()) {
126 g4MPI-> JoinBeamOnThread();
127 break;
128 }
129 } else {
130 break;
131 }
132 }
133 }
134
135 return 0;
136}
137
Note: See TracBrowser for help on using the repository browser.