Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4MPIsession.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 "G4MPIsession.hh"
29 #include "G4MPImanager.hh"
30 #include "G4RunManager.hh"
31 #include "G4UImanager.hh"
32 #include "G4UIcommand.hh"
33 #include "G4UIcsh.hh"
34 #include "G4UItcsh.hh"
35 #include "G4UImpish.hh"
36 
37 #ifndef WIN32
38 static const G4bool tcsh_build = true;
39 #else
40 static const G4bool tcsh_build = false;
41 #endif
42 
43 // --------------------------------------------------------------------------
45  : G4VMPIsession()
46 {
48  UI-> SetSession(this);
49  UI-> SetCoutDestination(this);
50 
51  // shell
52  if(isMaster) {
53  if(ashell) {
54  shell = ashell;
55  } else {
56  if ( g4MPI->GetSize() == 1 && tcsh_build ) shell = new G4UItcsh;
57  else shell = new G4UIcsh;
58  }
59  } else {
60  shell = new G4UImpish;
61  }
62 }
63 
64 // --------------------------------------------------------------------------
66 {
67  delete shell;
68 
70  UI-> SetSession(0);
71  UI-> SetCoutDestination(0);
72 }
73 
74 // --------------------------------------------------------------------------
75 void G4MPIsession::SetPrompt(const G4String& prompt)
76 {
77  shell-> SetPrompt(prompt);
78 }
79 
80 // --------------------------------------------------------------------------
82 {
83  delete shell;
84  shell = ashell;
85 }
86 
87 // --------------------------------------------------------------------------
89 {
91 
92  G4String newCommand;
93  const G4String nullString="";
94 
95  // get command from shell...
96  newCommand = shell-> GetCommandLineString(msg);
97 
98  G4String nC= newCommand.strip(G4String::leading);
99  if( nC.length() == 0 ) {
100  newCommand = nullString;
101 
102  } else if( nC(0) == '#' ) {
103  G4cout << nC << G4endl;
104  newCommand = nullString;
105 
106  } else if(nC=="ls" || nC(0,3)=="ls " ) { // list commands
107  ListDirectory(nC);
108  newCommand = nullString;
109 
110  } else if(nC=="lc" || nC(0,3)=="lc " ) { // ... by shell
111  shell-> ListCommand(nC.remove(0,2));
112  newCommand = nullString;
113 
114  } else if(nC == "pwd") { // show current directory
115  G4cout << "Current Command Directory : "
117  newCommand = nullString;
118 
119  } else if(nC == "cwd") { // ... by shell
120  shell-> ShowCurrentDirectory();
121  newCommand = nullString;
122 
123  } else if(nC == "cd" || nC(0,3) == "cd ") { // "cd"
125  shell-> SetCurrentDirectory(GetCurrentWorkingDirectory());
126  newCommand = nullString;
127 
128  } else if(nC == "help" || nC(0,5) == "help ") { // "help"
129  TerminalHelp(nC);
130  newCommand = nullString;
131 
132  } else if(nC(0) == '?') { // "show current value of a parameter"
133  ShowCurrent(nC);
134  newCommand = nullString;
135 
136  } else if(nC == "hist" || nC == "history") { // "hist/history"
137  G4int nh= UI-> GetNumberOfHistory();
138  for (G4int i = 0; i<nh; i++) {
139  G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl;
140  }
141  newCommand = nullString;
142 
143  } else if(nC(0) == '!') { // "!"
144  G4String ss= nC(1, nC.length()-1);
145  G4int vl;
146  const char* tt = ss;
147  std::istringstream is(tt);
148  is >> vl;
149  G4int nh= UI-> GetNumberOfHistory();
150  if(vl>=0 && vl<nh) {
151  newCommand = UI-> GetPreviousCommand(vl);
152  G4cout << newCommand << G4endl;
153  } else {
154  G4cerr << "history " << vl << " is not found." << G4endl;
155  newCommand = nullString;
156  }
157 
158  } else if( nC.empty() ){ // NULL command
159  newCommand = nullString;
160 
161  } else if( nC == "exit" ){ // "exit"
162  return "exit";
163 
164  } else { // ...
165 
166  }
167 
168  newCommand = TruncateCommand(newCommand);
169  return ModifyToFullPathCommand(newCommand);
170 }
171 
172 // --------------------------------------------------------------------------
174 {
175  // execute init macro
176  if( g4MPI-> IsInitMacro() ) {
177  g4MPI-> ExecuteMacroFile(g4MPI->GetInitFileName());
178  }
179 
180  // batch mode
181  if( g4MPI-> IsBatchMode() ) {
182  g4MPI-> ExecuteMacroFile(g4MPI->GetMacroFileName(), true);
183  return NULL;
184  }
185 
186  // interactive session
187  G4String newCommand = "", scommand; // newCommand is always "" in slaves
188 
189  while(1) {
190  if(isMaster) newCommand = GetCommand();
191  // broadcast a new G4 command
192  scommand = g4MPI-> BcastCommand(newCommand);
193  if(scommand == "exit") {
194  G4bool qexit = TryForcedTerminate();
195  if(qexit) break;
196  else scommand = "";
197  }
198  ExecCommand(scommand);
199  }
200 
201  return NULL;
202 }
203 
204 // --------------------------------------------------------------------------
206 {
207  if(! g4MPI-> CheckThreadStatus()) {
208  return true;
209  }
210 
211  G4String xmessage;
212 
213  // beamOn is still running
214  if(isMaster) {
215  char c[1024];
216  while(1) {
217  G4cout << "Run is still running. Do you abort a run? [y/N]:"
218  << std::flush;
219  G4cin.getline(c,1024);
220  G4String yesno= c;
221  if(yesno == "y" || yesno == "Y" ||
222  yesno == "n" || yesno == "N" || yesno == "") {
223  break;
224  }
225  }
226  if(c[0] == 'y' || c[0] == 'Y') {
227  G4cout << "Aborting a run..." << G4endl;
228  xmessage = g4MPI-> BcastCommand("kill me");
229  } else {
230  xmessage = g4MPI-> BcastCommand("alive");
231  }
232  } else {
233  xmessage = g4MPI-> BcastCommand("");
234  }
235 
236  if(xmessage == "kill me") {
238  runManager-> AbortRun(true); // soft abort
239  }
240 
241  return false;
242 }