source: trunk/examples/extended/parallel/MPI/mpi_interface/src/G4VMPIsession.cc @ 1279

Last change on this file since 1279 was 807, checked in by garnier, 16 years ago

update

File size: 7.6 KB
Line 
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// ********************************************************************
25//
26// $Id: G4VMPIsession.cc,v 1.1 2007/11/16 14:05:41 kmura Exp $
27// $Name:  $
28//
29// ====================================================================
30//   G4VMPIsession.cc
31//
32//                                         2007 Q
33// ====================================================================
34#include "G4VMPIsession.hh"
35#include "G4MPImanager.hh"
36#include "G4UImanager.hh"
37#include "G4UIcommand.hh"
38
39// ====================================================================
40//
41// class description
42//
43// ====================================================================
44
45//////////////////////////////
46G4VMPIsession::G4VMPIsession()
47  : G4VBasicShell()
48//////////////////////////////
49{
50  // MPI
51  g4MPI= G4MPImanager::GetManager();
52
53  isMaster= g4MPI-> IsMaster();
54  isSlave= g4MPI-> IsSlave();
55  rank= g4MPI-> GetRank();
56}
57
58///////////////////////////////
59G4VMPIsession::~G4VMPIsession()
60///////////////////////////////
61{
62}
63
64
65////////////////////////////////////////////////
66G4bool G4VMPIsession::GetHelpChoice(G4int& aval)
67////////////////////////////////////////////////
68{
69  G4cin >> aval;
70  if(!G4cin.good()){
71    G4cin.clear();
72    G4cin.ignore(30,'\n');
73    return false;
74  }
75  return true;
76}
77
78
79//////////////////////////////
80void G4VMPIsession::ExitHelp()
81//////////////////////////////
82{
83  char temp[100];
84  G4cin.getline(temp, 100);
85}
86
87
88///////////////////////////////////////////////
89void G4VMPIsession::PauseSessionStart(G4String)
90///////////////////////////////////////////////
91{
92}
93
94
95///////////////////////////////////////////////////
96G4int G4VMPIsession::ExecCommand(G4String aCommand)
97///////////////////////////////////////////////////
98{
99  if(aCommand.length()<2) return fCommandSucceeded;
100
101  G4UImanager* UI= G4UImanager::GetUIpointer();
102  G4int returnVal= 0;
103
104  G4String command= BypassCommand(aCommand);
105 
106  // "/mpi/beamOn is threaded out.
107  if(command(0,11) == "/mpi/beamOn") {
108    g4MPI-> ExecuteBeamOnThread(aCommand);
109    returnVal= fCommandSucceeded;
110  } else if(command(0,12) == "/mpi/.beamOn") { // care for beamOn
111    G4bool threadStatus= g4MPI-> CheckThreadStatus();
112    if (threadStatus) { // still /run/beamOn is active
113      if(isMaster) {
114        G4cout << "G4MPIsession:: beamOn is still running." << G4endl;
115      }
116      returnVal= fCommandSucceeded;
117    } else {
118      returnVal = UI-> ApplyCommand(command);
119    }
120  } else { // normal command
121    returnVal = UI-> ApplyCommand(command);
122  }
123
124  G4int paramIndex = returnVal % 100;
125  // 0 - 98 : paramIndex-th parameter is invalid
126  // 99     : convination of parameters is invalid
127  G4int commandStatus = returnVal - paramIndex;
128
129  G4UIcommand* cmd = 0;
130  if(commandStatus!=fCommandSucceeded) {
131    cmd = FindCommand(command);
132  }
133
134  switch(commandStatus) {
135  case fCommandSucceeded:
136    break;
137  case fCommandNotFound:
138    G4cerr << "command <" << UI->SolveAlias(command)
139           << "> not found" << G4endl;
140    break;
141  case fIllegalApplicationState:
142    G4cerr << "illegal application state -- command refused" << G4endl;
143    break;
144  case fParameterOutOfRange:
145    // ...
146    break;
147  case fParameterOutOfCandidates:
148    G4cerr << "Parameter is out of candidate list (index "
149           << paramIndex << ")" << G4endl;
150    G4cerr << "Candidates : "
151           << cmd->GetParameter(paramIndex)-> GetParameterCandidates()
152           << G4endl;
153    break;
154  case fParameterUnreadable:
155    G4cerr << "Parameter is wrong type and/or is not omittable (index "
156           << paramIndex << ")" << G4endl;
157    break;
158  case fAliasNotFound:
159    // ...
160    break;
161  default:
162    G4cerr << "command refused (" << commandStatus << ")" << G4endl;
163  }
164
165  return returnVal;
166}
167
168
169//////////////////////////////////////////////////////////////////////
170G4String G4VMPIsession::TruncateCommand(const G4String& command) const
171//////////////////////////////////////////////////////////////////////
172{
173  // replace "//" with "/" in G4command
174  G4String acommand= command;
175  G4String strarg;
176
177  str_size iarg= acommand.find(' ');
178  if(iarg != G4String::npos) {
179    strarg= acommand(iarg, acommand.size()-iarg);
180    acommand= acommand(0,iarg);
181  }
182
183  str_size idx;
184  while( (idx= acommand.find("//")) != G4String::npos)  {
185    G4String command1= acommand(0,idx+1); 
186    G4String command2= acommand(idx+2, acommand.size()-idx-2);
187    acommand= command1 + command2;
188  }
189
190  acommand += strarg;
191
192  return acommand;
193}
194
195
196////////////////////////////////////////////////////////////////////
197G4String G4VMPIsession::BypassCommand(const G4String& command) const
198////////////////////////////////////////////////////////////////////
199{
200  // bypass some commands
201  // /run/beamon -> /mpi/.beamOn
202  // /control/execute -> /mpi/execute
203
204  G4String acommand= command;
205
206  if(acommand(0,11) == "/run/beamOn") {
207    if(g4MPI-> GetVerbose()>0 && isMaster) {
208      G4cout << "/run/beamOn is overridden by /mpi/.beamOn" 
209             << G4endl;
210    }
211
212    acommand= "/mpi/.beamOn ";
213
214    G4String strarg= "";
215    G4bool qget= false;
216    G4bool qdone= false;
217
218    for (str_size idx=10; idx< command.size(); idx++) {
219      if(command[idx] == ' ' || command[idx] == '\011') {
220        qget= true;
221        if(qdone) break;
222        continue;
223      }
224
225      if(qget) {
226        strarg+= command[idx];
227        qdone= true;
228      }
229    }
230    acommand += strarg;
231  } 
232
233  if(acommand(0,16) == "/control/execute") {
234    if(g4MPI-> GetVerbose()>0 && isMaster) {
235      G4cout << "/control/execute is overridden by /mpi/execute" 
236             << G4endl;
237    }
238   
239    acommand.replace(0, 16, "/mpi/execute    ");
240  }
241
242  return acommand;
243}
244
245// ====================================================================
246
247//////////////////////////////////////////////////////
248G4int G4VMPIsession::ReceiveG4cout(G4String coutString)
249//////////////////////////////////////////////////////
250{
251  g4MPI-> Print(coutString);
252  return 0;
253}
254
255
256//////////////////////////////////////////////////////
257G4int G4VMPIsession::ReceiveG4cerr(G4String cerrString)
258//////////////////////////////////////////////////////
259{
260  g4MPI-> Print(cerrString);
261  return 0;
262}
263
Note: See TracBrowser for help on using the repository browser.