Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4MPIbatch.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 "G4MPIbatch.hh"
29 #include "G4MPImanager.hh"
30 #include "G4UImanager.hh"
31 #include "G4UIcommandStatus.hh"
32 #include <vector>
33 
34 // --------------------------------------------------------------------------
35 static void Tokenize(const G4String& str, std::vector<G4String>& tokens)
36 {
37  const char* delimiter = " ";
38 
39  str_size pos0 = str.find_first_not_of(delimiter);
40  str_size pos = str.find_first_of(delimiter, pos0);
41 
42  while (pos != G4String::npos || pos0 != G4String::npos) {
43  if (str[pos0] == '\"') {
44  pos = str.find_first_of("\"", pos0+1);
45  if(pos != G4String::npos) pos++;
46  }
47  if (str[pos0] == '\'') {
48  pos = str.find_first_of("\'", pos0+1);
49  if(pos != G4String::npos) pos++;
50  }
51 
52  tokens.push_back(str.substr(pos0, pos-pos0));
53  pos0 = str.find_first_not_of(delimiter, pos);
54  pos = str.find_first_of(delimiter, pos0);
55  }
56 }
57 
58 // --------------------------------------------------------------------------
60  : G4VMPIsession(), isOpened(false), isBatchMode(qbatch)
61 {
62  if(isMaster) {
63  batchStream.open(fname, std::ios::in);
64  if(batchStream.fail()) {
65  G4cerr << "cannot open a macro file(" << fname << ")."
66  << G4endl;
67  } else {
68  isOpened = true;
69  }
70  }
71 }
72 
73 // --------------------------------------------------------------------------
75 {
76  if(isOpened) batchStream.close();
77 }
78 
79 // --------------------------------------------------------------------------
81 {
82  enum { BUFSIZE = 4096 };
83  static char linebuf[BUFSIZE];
84 
85  G4String cmdtotal = "";
86  G4bool qcontinued = false;
87  while(batchStream.good()) {
88  batchStream.getline(linebuf, BUFSIZE);
89 
90  G4String cmdline(linebuf);
91 
92  // TAB-> ' ' conversion
93  str_size nb = 0;
94  while ((nb= cmdline.find('\t',nb)) != G4String::npos) {
95  cmdline.replace(nb, 1, " ");
96  }
97 
98  // strip
99  cmdline = cmdline.strip(G4String::both);
100 
101  // skip null line if single line
102  if(!qcontinued && cmdline.size() == 0) continue;
103 
104  // '#' is treated as echoing something
105  if(cmdline(0) == '#') return cmdline;
106 
107  // tokenize...
108  std::vector<G4String> tokens;
109  Tokenize(cmdline, tokens);
110  qcontinued = false;
111  for (G4int i = 0; i < G4int(tokens.size()); i++) {
112  // string after '#" is ignored
113  if(tokens[i](0) == '#' ) break;
114  // '\' or '_' is treated as continued line.
115  if(tokens[i] == '\\' || tokens[i] == '_' ) {
116  qcontinued = true;
117  // check nothing after line continuation character
118  if( i != G4int(tokens.size())-1) {
119  G4Exception("G4MPIbatch::ReadCommand", "MPI002", JustWarning,
120  "unexpected character after line continuation character");
121  }
122  break; // stop parsing
123  }
124  cmdtotal += tokens[i];
125  cmdtotal += " ";
126  }
127 
128  if(qcontinued) continue; // read the next line
129 
130  if(cmdtotal.size() != 0) break;
131  if(batchStream.eof()) break;
132  }
133 
134  // strip again
135  cmdtotal = cmdtotal.strip(G4String::both);
136 
137  // bypass some commands
138  cmdtotal = BypassCommand(cmdtotal);
139 
140  // finally,
141  if(batchStream.eof() && cmdtotal.size()==0) {
142  return "exit";
143  }
144 
145  return cmdtotal;
146 }
147 
148 // --------------------------------------------------------------------------
150 {
151  if( isMaster && !isOpened ){ // macro file is not found
152  g4MPI-> BcastCommand("exit");
153  return NULL;
154  }
155 
156  G4String newCommand = "", scommand; // newCommand is always "" in slaves
157 
158  while(1) {
159  if (isMaster) newCommand = ReadCommand();
160  // broadcast a new G4 command
161  scommand = g4MPI-> BcastCommand(newCommand);
162  if(scommand == "exit") {
163  g4MPI-> WaitBeamOn();
164  return 0;
165  }
166 
167  // just echo something
168  if( scommand(0) == '#') {
169  if(G4UImanager::GetUIpointer()-> GetVerboseLevel() == 2) {
170  G4cout << scommand << G4endl;
171  }
172  continue;
173  }
174 
175  G4int rc = ExecCommand(scommand);
176  if (rc != fCommandSucceeded) {
177  G4cerr << G4endl << "***** Batch is interupted!! *****" << G4endl;
178  break;
179  }
180  }
181 
182  return NULL;
183 }
184