Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 "G4VMPIsession.hh"
29 #include "G4MPImanager.hh"
30 #include "G4UImanager.hh"
31 #include "G4UIcommand.hh"
32 
33 // --------------------------------------------------------------------------
35  : G4VBasicShell()
36 {
37  // MPI
39 
40  isMaster = g4MPI-> IsMaster();
41  isSlave = 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(isMaster) {
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  //
178  // * /run/beamOn
179  // -> /mpi/.beamOn (batch session)
180  // -> /mpi/beamOn (interactive session)
181  //
182  // * /control/execute -> /mpi/execute
183 
184  G4String acommand = command;
185 
186  // /mpi/beamOn
187  if(acommand(0,11) == "/mpi/beamOn") {
188  if(g4MPI-> IsBatchMode()) {
189  acommand = "/mpi/.beamOn";
190  if(command.length() > 11) {
191  acommand += command.substr(11);
192  }
193  }
194  }
195 
196  // /run/beamOn
197  if(acommand(0,11) == "/run/beamOn") {
198  G4String strarg = "";
199  G4bool qget = false;
200  G4bool qdone = false;
201 
202  for (str_size idx = 10; idx < command.size(); idx++) {
203  if(command[idx] == ' ' || command[idx] == '\011') {
204  qget = true;
205  if(qdone) break;
206  continue;
207  }
208  if(qget) {
209  strarg += command[idx];
210  qdone = true;
211  }
212  }
213 
214  if(g4MPI-> IsBatchMode()) { // batch session
215  acommand = "/mpi/.beamOn ";
216  if(command.length() > 11) acommand += strarg;
217  } else { // interactive session
218  if(g4MPI-> GetVerbose()>0 && isMaster) {
219  G4cout << "/run/beamOn is overridden by /mpi/.beamOn" << G4endl;
220  }
221  acommand = "/mpi/beamOn ";
222  if(command.length() > 11) acommand += strarg;
223  }
224  }
225 
226  // /control/execute
227  if(acommand(0,16) == "/control/execute") {
228  if(g4MPI-> GetVerbose()>0 && isMaster) {
229  G4cout << "/control/execute is overridden by /mpi/execute"
230  << G4endl;
231  }
232  acommand.replace(0, 16, "/mpi/execute ");
233  }
234 
235  return acommand;
236 }
237 
238 // --------------------------------------------------------------------------
240 {
241  g4MPI-> Print(coutString);
242  return 0;
243 }
244 
245 // --------------------------------------------------------------------------
247 {
248  g4MPI-> Print(cerrString);
249  return 0;
250 }
251