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