source: trunk/examples/extended/parallel/MPI/mpi_interface/src/G4MPIbatch.cc @ 1341

Last change on this file since 1341 was 1337, checked in by garnier, 14 years ago

tag geant4.9.4 beta 1 + modifs locales

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