PlasCom2  1.0
XPACC Multi-physics simluation application
ParallelApplication.H
Go to the documentation of this file.
1 #ifndef __PARALLEL_APPLICATION_H__
9 #define __PARALLEL_APPLICATION_H__
10 
11 #include "Application.H"
12 #include "COMM.H"
13 #include <cmath>
14 
15 namespace ix { namespace app {
16 
20 
21  class parallel_program : public ParallelProgramType
22  {
23  protected:
25  std::string outFileName;
27  std::ofstream *outFilePtr;
28  public:
33  ParallelProgramType()
34  {
35  verblevel = 0;
36  outFilePtr = NULL;
37  };
41  parallel_program(int nargs,char **args) :
42  ParallelProgramType(nargs,args)
43  {
44  verblevel = 0;
45  outFilePtr = NULL;
46  };
50  parallel_program(CommandLineType &comline,CommunicatorType &incomm)
51  {
52  this->_command_line.Copy(comline);
53  this->Init(_command_line.ProgramName(),incomm);
54  verblevel = 0;
55  outFilePtr = NULL;
56  };
57 
61  virtual int Initialize()
62  {
64  ErrOut("Failed to initialize.\n");
65  ErrOut(CommandLine().ErrorReport()+std::string("\n"));
66  ErrOut("Use -h,--help for usage help.\n\n");
67  ErrOut(CommandLine().ShortUsage()+std::string("\n"));
68  return(1);
69  }
70  if(!CommandLine().GetOption('h').empty()){
71  StdOut(CommandLine().LongUsage());
72  return(1);
73  }
74  std::string applicationName(CommandLine().ProgramName());
75 
76  // Verbosity level processing intent:
77  // 0 - be silent
78  // 1 - normal verbosity
79  // 2 - increased verbosity
80  // 3 - debug mode engaged (debug stream engaged for root process)
81  // 4+ - debug mode (with debugging logged on all processors)
82  std::string sverb(CommandLine().GetOption('v'));
83  if(!sverb.empty()){
84  if(sverb == ".true.")
85  SetVerbLevel(1);
86  else {
87  int rank = Rank();
88  int nproc = NProc();
89  int nZeros = std::log10((double)nproc);
90  std::istringstream Istr(sverb);
91  int verbLevel;
92  Istr >> verbLevel;
93  SetVerbLevel(verbLevel);
94  if(verbLevel > 2){
95  if(rank > 0){
96  outFilePtr = new std::ofstream;
97  std::ostringstream outFileNameStream;
98  outFileNameStream << applicationName << "_";
99  for(int i = nZeros;i > 0;i--){
100  int numRanks = std::pow((double)10,(double)i);
101  if(rank < numRanks)
102  outFileNameStream << "0";
103  }
104  if(!ErrStreamReady())
105  SetErrStream(std::cerr);
106  outFileNameStream << rank;
107  outFileName.assign(outFileNameStream.str());
108  outFilePtr->open(outFileNameStream.str().c_str());
109  if(!(*outFilePtr)){
110  globalCommunicator.SetErr(1);
111  }
112  }
113  if(globalCommunicator.Check()){
114  if(!rank)
115  ErrOut("Unable to open output on all processors.\n");
116  return(1);
117  } else if(rank > 0) {
118  SetOutStream(*outFilePtr);
119  }
120  SetDebugStream(OutStream());
121  SetDebugLevel(verbLevel - 2);
122  }
123  }
124  }
125  // Process profiling control
126  std::string sprof(CommandLine().GetOption('p'));
127  Profiling(false);
128  DisableProfilingBarriers();
129  if(!sprof.empty()){
130  if(sprof == ".true.")
131  Profiling(true);
132  else {
133  std::istringstream Istr(sprof);
134  int profLevel;
135  Istr >> profLevel;
136  if(profLevel == 0)
137  Profiling(false);
138  if(profLevel >= 1){
139  Profiling(true);
140  if(profLevel > 1)
141  EnableProfilingBarriers();
142  }
143  }
144  }
145  // Process profiling control
146  std::string sdebug(CommandLine().GetOption('d'));
147  if(!sdebug.empty()){
148  if(sdebug == ".true.")
149  SetDebugLevel(1);
150  else {
151  std::istringstream Istr(sdebug);
152  int debugLevel;
153  Istr >> debugLevel;
154  if(debugLevel >= 1)
155  SetDebugLevel(debugLevel);
156  }
157  }
158  return(0);
159  };
160 
161 
165  virtual ~parallel_program() {
166  if(outFilePtr){
167  outFilePtr->close();
168  delete outFilePtr;
169  }
170  if(Rank() > 0)
171  UnsetOutStream();
172  SetOutStream(std::cout);
173  };
174 
178  virtual int Run(){};
179  virtual int Finalize() {
180  if(OutStreamReady())
181  Report(OutStream());
183  ErrOut("Failed to finalize.\n");
184  if(ErrStreamReady())
185  DumpErrors(ErrStream());
186  return(1);
187  }
188  return(0);
189  };
190  };
191  }
192 }
193 
194 #endif
ComLineType _command_line
Definition: Global.H:637
ix::comm::CommunicatorObject CommunicatorType
std::string outFileName
Name of file for output.
Communication utilities.
Defines MPI-specific parallel global and program classes.
virtual int Initialize()
Populates native data structures from commandline.
virtual int Initialize()
Definition: Global.H:661
ComLineType & CommandLine()
Definition: Global.H:666
virtual int Run()
This function implements the main function executed by the program.
parallel_program(CommandLineType &comline, CommunicatorType &incomm)
Constructor designed to take all commandline args.
virtual int Finalize()
Definition: Global.H:668
parallel_program()
Default constructor.
Definitions for serial applications.
Main encapsulation of MPI.
Definition: COMM.H:62
parallel_program(int nargs, char **args)
Constructor designed to take all commandline args.
ix::global::ParallelGlobalObj< CommunicatorType, std::string, int, ProfilerType > ParallelGlobalType
ix::global::Program< ParallelGlobalType, CommandLineType > ParallelProgramType
virtual ~parallel_program()
Destructor.
Command line processing.
Definition: AppTools.H:411
std::ofstream * outFilePtr
Output file stream object for output.