Geant4_10
G4VMPIsession.cc
Go to the documentation of this file.
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 // ********************************************************************
27 
28 #include "mpi.h"
29 #include "G4UIcommand.hh"
30 #include "G4UImanager.hh"
31 #include "G4MPImanager.hh"
32 #include "G4VMPIsession.hh"
33 
34 // --------------------------------------------------------------------------
36  : G4VBasicShell()
37 {
39 
40  is_master_ = g4mpi_-> IsMaster();
41  is_slave_ = g4mpi_-> IsSlave();
42  rank_ = g4mpi_-> GetRank();
43 }
44 
45 // --------------------------------------------------------------------------
47 {
48 }
49 
50 // --------------------------------------------------------------------------
52 {
53  G4cin >> aval;
54  if( !G4cin.good() ){
55  G4cin.clear();
56  G4cin.ignore(30,'\n');
57  return false;
58  }
59  return true;
60 }
61 
62 // --------------------------------------------------------------------------
64 {
65  char temp[100];
66  G4cin.getline(temp, 100);
67 }
68 
69 // --------------------------------------------------------------------------
71 {
72 }
73 
74 // --------------------------------------------------------------------------
76 {
77  if( acommand.length() < 2 ) return fCommandSucceeded;
78 
80  G4int returnVal = 0;
81 
82  G4String command = BypassCommand(acommand);
83 
84  // "/mpi/beamOn is threaded out.
85  if( command(0,11) == "/mpi/beamOn" ) {
86  g4mpi_-> ExecuteBeamOnThread(command);
87  returnVal = fCommandSucceeded;
88  } else if( command(0,12) == "/mpi/.beamOn" ) { // care for beamOn
89  G4bool threadStatus = g4mpi_-> CheckThreadStatus();
90  if ( threadStatus ) { // still /run/beamOn is active
91  if( is_master_ ) {
92  G4cout << "G4MPIsession:: beamOn is still running." << G4endl;
93  }
94  returnVal = fCommandSucceeded;
95  } else {
96  returnVal = UI-> ApplyCommand(command);
97  }
98  } else { // normal command
99  returnVal = UI-> ApplyCommand(command);
100  }
101 
102  G4int paramIndex = returnVal % 100;
103  // 0 - 98 : paramIndex-th parameter is invalid
104  // 99 : convination of parameters is invalid
105  G4int commandStatus = returnVal - paramIndex;
106 
107  G4UIcommand* cmd = 0;
108  if( commandStatus != fCommandSucceeded ) {
109  cmd = FindCommand(command);
110  }
111 
112  switch( commandStatus ) {
113  case fCommandSucceeded:
114  break;
115  case fCommandNotFound:
116  G4cerr << "command <" << UI-> SolveAlias(command)
117  << "> not found" << G4endl;
118  break;
120  G4cerr << "illegal application state -- command refused" << G4endl;
121  break;
123  // ...
124  break;
126  G4cerr << "Parameter is out of candidate list (index "
127  << paramIndex << ")" << G4endl;
128  G4cerr << "Candidates : "
129  << cmd->GetParameter(paramIndex)-> GetParameterCandidates()
130  << G4endl;
131  break;
133  G4cerr << "Parameter is wrong type and/or is not omittable (index "
134  << paramIndex << ")" << G4endl;
135  break;
136  case fAliasNotFound:
137  // ...
138  break;
139  default:
140  G4cerr << "command refused (" << commandStatus << ")" << G4endl;
141  }
142 
143  return returnVal;
144 }
145 
146 // --------------------------------------------------------------------------
148 {
149  // replace "//" with "/" in G4command
150  G4String acommand = command;
151  G4String strarg;
152 
153  str_size iarg = acommand.find(' ');
154  if( iarg != G4String::npos ) {
155  strarg = acommand(iarg, acommand.size()-iarg);
156  acommand = acommand(0,iarg);
157  }
158 
159  str_size idx;
160  while( (idx = acommand.find("//")) != G4String::npos) {
161  G4String command1 = acommand(0,idx+1);
162  G4String command2 = acommand(idx+2, acommand.size()-idx-2);
163  acommand = command1 + command2;
164  }
165 
166  acommand += strarg;
167 
168  return acommand;
169 }
170 
171 // --------------------------------------------------------------------------
173 {
174  // bypass some commands
175  // * /mpi/beamOn
176  // -> /mpi/.beamOn (batch session)
177  // -> /mpi/.beamOn (MT mode)
178  //
179  // * /run/beamOn
180  // -> /mpi/.beamOn (batch session)
181  // -> /mpi/beamOn (interactive session)
182  // -> /mpi/.beamOn (MT mode)
183  //
184  // * /control/execute -> /mpi/execute
185 
186  G4String acommand = command;
187 
188  // /mpi/beamOn
189  if( acommand(0,11) == "/mpi/beamOn" ) {
190 #ifdef G4MULTITHREADED
191  acommand = "/mpi/.beamOn";
192  if(command.length() > 11) {
193  acommand += command.substr(11);
194  }
195 #else
196  if( g4mpi_-> IsBatchMode()) {
197  acommand = "/mpi/.beamOn";
198  if(command.length() > 11) {
199  acommand += command.substr(11);
200  }
201  }
202 #endif
203  }
204 
205  // /run/beamOn
206  if( acommand(0,11) == "/run/beamOn" ) {
207  G4String strarg = "";
208  G4bool qget = false;
209  G4bool qdone = false;
210 
211  for ( str_size idx = 10; idx < command.size(); idx++ ) {
212  if( command[idx] == ' ' || command[idx] == '\011' ) {
213  qget = true;
214  if(qdone) break;
215  continue;
216  }
217  if( qget ) {
218  strarg += command[idx];
219  qdone = true;
220  }
221  }
222 
223  if( g4mpi_-> IsBatchMode() ) { // batch session
224  acommand = "/mpi/.beamOn ";
225  if( command.length() > 11 ) acommand += strarg;
226  } else { // interactive session
227 #ifdef G4MULTITHREADED
228  if( g4mpi_-> GetVerbose()>0 && is_master_ ) {
229  G4cout << "/run/beamOn is overridden by /mpi/.beamOn" << G4endl;
230  }
231  acommand = "/mpi/.beamOn ";
232  if( command.length() > 11 ) acommand += strarg;
233 #else
234  if( g4mpi_-> GetVerbose()>0 && is_master_ ) {
235  G4cout << "/run/beamOn is overridden by /mpi/beamOn" << G4endl;
236  }
237  acommand = "/mpi/beamOn ";
238  if( command.length() > 11 ) acommand += strarg;
239 #endif
240  }
241  }
242 
243  // /control/execute
244  if( acommand(0,16) == "/control/execute" ) {
245  if( g4mpi_-> GetVerbose()>0 && is_master_ ) {
246  G4cout << "/control/execute is overridden by /mpi/execute"
247  << G4endl;
248  }
249  acommand.replace(0, 16, "/mpi/execute ");
250  }
251 
252  return acommand;
253 }
254 
255 // --------------------------------------------------------------------------
257 {
258  g4mpi_-> Print(coutString);
259  return 0;
260 }
261 
262 // --------------------------------------------------------------------------
264 {
265  g4mpi_-> Print(cerrString);
266  return 0;
267 }
char cmd[1024]
Definition: tracer.cxx:25
std::string::size_type str_size
Definition: G4String.hh:57
G4MPImanager * g4mpi_
int G4int
Definition: G4Types.hh:78
virtual G4bool GetHelpChoice(G4int &aval)
G4String & replace(unsigned int, unsigned int, const char *, unsigned int)
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:58
G4String BypassCommand(const G4String &command) const
virtual void ExitHelp() const
#define G4cin
Definition: G4ios.hh:60
G4GLOB_DLL std::ostream G4cout
MPI manager class.
bool G4bool
Definition: G4Types.hh:79
G4UIparameter * GetParameter(G4int i) const
Definition: G4UIcommand.hh:145
G4String TruncateCommand(const G4String &command) const
virtual G4int ReceiveG4cerr(const G4String &cerrString)
virtual void PauseSessionStart(const G4String &msg)
static G4MPImanager * GetManager()
virtual G4int ReceiveG4cout(const G4String &coutString)
#define G4endl
Definition: G4ios.hh:61
G4UIcommand * FindCommand(const char *commandName) const
G4int ExecCommand(const G4String &acommand)
G4GLOB_DLL std::ostream G4cerr
A base class for MPI sessions.
void Print(G4Element &ele)
Definition: pyG4Element.cc:56