PlasCom2  1.0
XPACC Multi-physics simluation application
ParallelTestingProgram.H
Go to the documentation of this file.
1 // Parallel stuff
5  class testing_parallel_program : public XPACCParallelProgramType
6  {
7  protected:
9  std::string outFileName;
11  int verblevel;
13  std::ofstream *outFilePtr;
15  int ndiv;
16  public:
21  XPACCParallelProgramType()
22  {
23  verblevel = 0;
24  outFilePtr = NULL;
25  };
29  testing_parallel_program(int nargs,char **args) :
30  XPACCParallelProgramType(nargs,args)
31  {
32  verblevel = 0;
33  outFilePtr = NULL;
34  };
38  testing_parallel_program(XPACCCommandLineType &comline,XPACCCommType &incomm)
39  {
40  this->_command_line.Copy(comline);
41  this->Init(_command_line.ProgramName(),incomm);
42  verblevel = 0;
43  outFilePtr = NULL;
44  };
45 
49  virtual int Initialize()
50  {
52  ErrOut("Failed to initialize.\n");
53  ErrOut(CommandLine().ErrorReport()+std::string("\n"));
54  ErrOut("Use -h,--help for usage help.\n\n");
55  ErrOut(CommandLine().ShortUsage()+std::string("\n"));
56  return(1);
57  }
58  if(!CommandLine().GetOption('h').empty()){
59  StdOut(CommandLine().LongUsage());
60  return(1);
61  }
62  std::string applicationName(CommandLine().ProgramName());
63 
64  // Verbosity level processing intent:
65  // 0 - be silent
66  // 1 - normal verbosity
67  // 2 - increased verbosity
68  // 3 - debug mode engaged (debug stream engaged for root process)
69  // 4+ - debug mode (with debugging logged on all processors)
70  std::string sverb(CommandLine().GetOption('v'));
71  if(!sverb.empty()){
72  if(sverb == ".true.")
73  SetVerbLevel(1);
74  else {
75  int rank = Rank();
76  int nproc = NProc();
77  int nZeros = std::log10((double)nproc);
78  std::istringstream Istr(sverb);
79  int verbLevel;
80  Istr >> verbLevel;
81  SetVerbLevel(verbLevel);
82  if(verbLevel > 2){
83  if(rank > 0){
84  outFilePtr = new std::ofstream;
85  std::ostringstream outFileNameStream;
86  outFileNameStream << applicationName << "_";
87  for(int i = nZeros;i > 0;i--){
88  int numRanks = std::pow((double)10,(double)i);
89  if(rank < numRanks)
90  outFileNameStream << "0";
91  }
92  if(!ErrStreamReady())
93  SetErrStream(std::cerr);
94  outFileNameStream << rank;
95  outFileName.assign(outFileNameStream.str());
96  outFilePtr->open(outFileNameStream.str().c_str());
97  if(!(*outFilePtr)){
98  globalCommunicator.SetErr(1);
99  }
100  }
101  if(globalCommunicator.Check()){
102  if(!rank)
103  ErrOut("Unable to open output on all processors.\n");
104  return(1);
105  } else if(rank > 0) {
106  SetOutStream(*outFilePtr);
107  }
108  SetDebugStream(OutStream());
109  SetDebugLevel(verbLevel - 2);
110  }
111  }
112  }
113  // Process profiling control
114  std::string sprof(CommandLine().GetOption('p'));
115  Profiling(false);
116  DisableProfilingBarriers();
117  if(!sprof.empty()){
118  if(sprof == ".true.")
119  Profiling(true);
120  else {
121  std::istringstream Istr(sprof);
122  int profLevel;
123  Istr >> profLevel;
124  if(profLevel == 0)
125  Profiling(false);
126  if(profLevel >= 1){
127  Profiling(true);
128  if(profLevel > 1)
129  EnableProfilingBarriers();
130  }
131  }
132  }
133  // Process profiling control
134  std::string sdebug(CommandLine().GetOption('d'));
135  if(!sdebug.empty()){
136  if(sdebug == ".true.")
137  SetDebugLevel(1);
138  else {
139  std::istringstream Istr(sdebug);
140  int debugLevel;
141  Istr >> debugLevel;
142  if(debugLevel >= 1)
143  SetDebugLevel(debugLevel);
144  }
145  }
146  return(0);
147  };
148 
152  int VerbLevel() const { return verblevel;};
153 
158  if(outFilePtr){
159  outFilePtr->close();
160  delete outFilePtr;
161  }
162  if(Rank() > 0)
163  UnsetOutStream();
164  SetOutStream(std::cout);
165  };
166 
170  virtual int Run(){};
171  virtual int Finalize() {
172  if(OutStreamReady())
173  Report(OutStream());
174  if(XPACCParallelProgramType::Finalize()){
175  ErrOut("Failed to finalize.\n");
176  if(ErrStreamReady())
177  DumpErrors(ErrStream());
178  return(1);
179  }
180  return(0);
181  };
182  };
183 
184  template<typename ProgramType>
185  int XPACCMainDriver(int argc,char *argv[])
186  {
187  ProgramType mainProgram(argc,argv);
188  if(mainProgram.Initialize()){
189  return(1);
190  }
191  std::string programName(mainProgram.CommandLine().ProgramName());
192  // mainProgram.StdOut("Initialized\n");
193  if(mainProgram.Run()){
194  return(1);
195  }
196  // mainProgram.StdOut("Ran\n");
197  if(mainProgram.Finalize()){
198  return(1);
199  }
200  // mainProgram.StdOut("Finalized\n");
201  return(0);
202  }
203 
204 
205 
virtual int Run()
This function implements the main function executed by the program.
int VerbLevel() const
Returns verbosity level.
int ndiv
Number of partitions for domain.
int verblevel
Verbosity level.
testing_parallel_program()
Default constructor.
testing_parallel_program(int nargs, char **args)
Constructor designed to take all commandline args.
int XPACCMainDriver(int argc, char *argv[])
virtual int Initialize()
Populates native data structures from commandline.
std::string outFileName
Name of file for output.
Main encapsulation of MPI.
Definition: COMM.H:62
ix::global::Program< ParallelGlobalType, CommandLineType > ParallelProgramType
ix::global::ParallelGlobalObj< CommType, std::string, int, ProfilerType > ParallelGlobalType
ix::comm::CommunicatorObject CommType
std::ofstream * outFilePtr
Output file stream object for output.
global::Program< Global, command_line > ProgramType
int Initialize(base &stencilSet, int interiorOrder)
Initialize the sbp::base stencilset with the SBP operator of given order.
Definition: Stencil.C:360
testing_parallel_program(XPACCCommandLineType &comline, XPACCCommType &incomm)
Constructor designed to take all commandline args.
virtual ~testing_parallel_program()
Destructor.