ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4VMPIsession.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4VMPIsession.cc
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 
46 // --------------------------------------------------------------------------
48 {
49 }
50 
51 // --------------------------------------------------------------------------
53 {
54  G4cin >> aval;
55  if( !G4cin.good() ){
56  G4cin.clear();
57  G4cin.ignore(30,'\n');
58  return false;
59  }
60  return true;
61 }
62 
63 // --------------------------------------------------------------------------
65 {
66  char temp[100];
67  G4cin.getline(temp, 100);
68 }
69 
70 // --------------------------------------------------------------------------
72 {
73 }
74 
75 // --------------------------------------------------------------------------
77 {
78  if( acommand.length() < 2 ) return fCommandSucceeded;
79 
81  G4int returnVal = 0;
82 
83  G4String command = BypassCommand(acommand);
84 
85  // "/mpi/beamOn is threaded out.
86  if( command(0,11) == "/mpi/beamOn" ) {
87  g4mpi_-> ExecuteBeamOnThread(command);
88  returnVal = fCommandSucceeded;
89  } else if( command(0,12) == "/mpi/.beamOn" ) { // care for beamOn
90  G4bool threadStatus = g4mpi_-> CheckThreadStatus();
91  if ( threadStatus ) { // still /run/beamOn is active
92  if( is_master_ ) {
93  G4cout << "G4MPIsession:: beamOn is still running." << G4endl;
94  }
95  returnVal = fCommandSucceeded;
96  } else {
97  returnVal = UI-> ApplyCommand(command);
98  }
99  } else { // normal command
100  returnVal = UI-> ApplyCommand(command);
101  }
102 
103  G4int paramIndex = returnVal % 100;
104  // 0 - 98 : paramIndex-th parameter is invalid
105  // 99 : convination of parameters is invalid
106  G4int commandStatus = returnVal - paramIndex;
107 
108  G4UIcommand* cmd = 0;
109  if( commandStatus != fCommandSucceeded ) {
110  cmd = FindCommand(command);
111  }
112 
113  switch( commandStatus ) {
114  case fCommandSucceeded:
115  break;
116  case fCommandNotFound:
117  G4cerr << "command <" << UI-> SolveAlias(command)
118  << "> not found" << G4endl;
119  break;
121  G4cerr << "illegal application state -- command refused" << G4endl;
122  break;
124  // ...
125  break;
127  G4cerr << "Parameter is out of candidate list (index "
128  << paramIndex << ")" << G4endl;
129  G4cerr << "Candidates : "
130  << cmd->GetParameter(paramIndex)-> GetParameterCandidates()
131  << G4endl;
132  break;
134  G4cerr << "Parameter is wrong type and/or is not omittable (index "
135  << paramIndex << ")" << G4endl;
136  break;
137  case fAliasNotFound:
138  // ...
139  break;
140  default:
141  G4cerr << "command refused (" << commandStatus << ")" << G4endl;
142  }
143 
144  return returnVal;
145 }
146 
147 // --------------------------------------------------------------------------
149 {
150  // replace "//" with "/" in G4command
151  G4String acommand = command;
152  G4String strarg;
153 
154  str_size iarg = acommand.find(' ');
155  if( iarg != G4String::npos ) {
156  strarg = acommand(iarg, acommand.size()-iarg);
157  acommand = acommand(0,iarg);
158  }
159 
160  str_size idx;
161  while( (idx = acommand.find("//")) != G4String::npos) {
162  G4String command1 = acommand(0,idx+1);
163  G4String command2 = acommand(idx+2, acommand.size()-idx-2);
164  acommand = command1 + command2;
165  }
166 
167  acommand += strarg;
168 
169  return acommand;
170 }
171 
172 // --------------------------------------------------------------------------
174 {
175  // bypass some commands
176  // * /mpi/beamOn
177  // -> /mpi/.beamOn (batch session)
178  // -> /mpi/.beamOn (MT mode)
179  //
180  // * /run/beamOn
181  // -> /mpi/.beamOn (batch session)
182  // -> /mpi/beamOn (interactive session)
183  // -> /mpi/.beamOn (MT mode)
184  //
185  // * /control/execute -> /mpi/execute
186 
187  G4String acommand = command;
188 
189  // /mpi/beamOn
190  if( acommand(0,11) == "/mpi/beamOn" ) {
191 #ifdef G4MULTITHREADED
192  acommand = "/mpi/.beamOn";
193  if(command.length() > 11) {
194  acommand += command.substr(11);
195  }
196 #else
197  if( g4mpi_-> IsBatchMode()) {
198  acommand = "/mpi/.beamOn";
199  if(command.length() > 11) {
200  acommand += command.substr(11);
201  }
202  }
203 #endif
204  }
205 
206  // /run/beamOn
207  if( acommand(0,11) == "/run/beamOn" ) {
208  G4String strarg = "";
209  G4bool qget = false;
210  G4bool qdone = false;
211 
212  for ( str_size idx = 10; idx < command.size(); idx++ ) {
213  if( command[idx] == ' ' || command[idx] == '\011' ) {
214  qget = true;
215  if(qdone) break;
216  continue;
217  }
218  if( qget ) {
219  strarg += command[idx];
220  qdone = true;
221  }
222  }
223 
224  if( g4mpi_-> IsBatchMode() ) { // batch session
225  acommand = "/mpi/.beamOn ";
226  if( command.length() > 11 ) acommand += strarg;
227  } else { // interactive session
228 #ifdef G4MULTITHREADED
229  if( g4mpi_-> GetVerbose()>0 && is_master_ ) {
230  G4cout << "/run/beamOn is overridden by /mpi/.beamOn" << G4endl;
231  }
232  acommand = "/mpi/.beamOn ";
233  if( command.length() > 11 ) acommand += strarg;
234 #else
235  if( g4mpi_-> GetVerbose()>0 && is_master_ ) {
236  G4cout << "/run/beamOn is overridden by /mpi/beamOn" << G4endl;
237  }
238  acommand = "/mpi/beamOn ";
239  if( command.length() > 11 ) acommand += strarg;
240 #endif
241  }
242  }
243 
244  // /control/execute
245  if( acommand(0,16) == "/control/execute" ) {
246  if( g4mpi_-> GetVerbose()>0 && is_master_ ) {
247  G4cout << "/control/execute is overridden by /mpi/execute"
248  << G4endl;
249  }
250  acommand.replace(0, 16, "/mpi/execute ");
251  }
252 
253  return acommand;
254 }
255 
256 // --------------------------------------------------------------------------
258 {
259  g4mpi_-> Print(coutString);
260  return 0;
261 }
262 
263 // --------------------------------------------------------------------------
265 {
266  g4mpi_-> Print(cerrString);
267  return 0;
268 }