PlasCom2  1.0
XPACC Multi-physics simluation application
PCPPProgram.H
Go to the documentation of this file.
1 #ifndef __PC2_PROGRAM_GLOBAL_H__
9 #define __PC2_PROGRAM_GLOBAL_H__
10 
11 #include <cmath>
12 #include "PCPPTypes.H"
16 
17 namespace pcpp {
18 
20  {
21  public:
23  commandline(const char *args[]) : CommandLineType(args) {};
24  commandline(commandline &inComLine) : CommandLineType(inComLine) {};
25  virtual void Initialize();
27  AddOption('d',"debuglevel",1);
28  AddOption('h',"help");
29  AddOption('v',"verblevel",1);
30  AddOption('p',"enable-profiling",1);
31  AddHelp('h',"Produces this long help message");
32  AddHelp('d',"Set the debugging level for the program.(default=1)");
33  AddHelp('p',std::string("Enable profiling. Profiling level > 1 enables profiling")+
34  std::string("\n\t\tbarriers, with the following settings available:\n\t\t\t")+
35  std::string("level=1 enables async profiling w/report on rank 0.\n\t\t\t")+
36  std::string("level=2 enables barrier sync w/report on rank 0.\n\t\t\t")+
37  std::string("level=3 enables async with output on all ranks.\n\t\t\t")+
38  std::string("level=4 enables sync with output on all ranks."));
39  AddHelp('v',std::string("Set the verbosity level for the program.")+
40  std::string(" Anything\n\t\tlarger than 3 is debugging level.")+
41  std::string("\n\t\tExamples:\n\t\t\t-v (defaults to 1)\n\t\t\t")+
42  std::string("-v 0 (silence)\n\t\t\t")+
43  std::string("-v 1 (default: some normal output)\n\t\t\t")+
44  std::string("-v 2 (verbose: more than normal output)\n\t\t\t")+
45  std::string("-v 3 (verbose indeed: some control flow information)\n\t\t\t")+
46  std::string("-v 4 (ridiculously verbose debugging mode)\n\t\t\t")+
47  std::string("-v 10 (ranks other than 0 create output logs."));
48  SetDescription(std::string("This text should describe the program."));
49  };
50  };
51 
54 
55  class serialprogram : public SerialProgramType
56  {
57 
58  public:
59  serialprogram(int narg,char **args) :
60  SerialProgramType(narg,args){};
62  SerialProgramType(inglob){};
64  SerialProgramType(incom){};
66  SerialProgramType(incom,inglob){};
67  virtual int Initialize()
68  {
70  ErrOut("Failed to initialize.\n");
71  ErrOut(CommandLine().ErrorReport()+std::string("\n"));
72  ErrOut("Use -h,--help for usage help.\n\n");
73  ErrOut(CommandLine().ShortUsage()+std::string("\n"));
74  return(1);
75  }
76  if(!CommandLine().GetOption('h').empty()){
77  StdOut(CommandLine().LongUsage());
78  return(1);
79  }
80  // Verbosity level processing intent:
81  // 0 - be silent
82  // 1 - normal verbosity
83  // 2 - increased verbosity
84  // 3 - increased verbosity with some control flow information
85  // 4+ - debug mode (with debugging logged on all processors)
86  std::string sverb(CommandLine().GetOption('v'));
87  if(!sverb.empty()){
88  if(sverb == ".true.")
89  SetVerbLevel(1);
90  else {
91  std::istringstream Istr(sverb);
92  int verbLevel;
93  Istr >> verbLevel;
94  SetVerbLevel(verbLevel);
95  if(verbLevel > 1){
96  SetDebugStream(ErrStream());
97  SetDebugLevel(verbLevel - 1);
98  }
99  }
100  }
101  // Process profiling control
102  std::string sprof(CommandLine().GetOption('p'));
103  if(!sprof.empty()){
104  if(sprof == ".true.")
105  Profiling(true);
106  else {
107  std::istringstream Istr(sprof);
108  int profLevel;
109  Istr >> profLevel;
110  if(profLevel == 0)
111  Profiling(false);
112  if(profLevel >= 1){
113  Profiling(true);
114  }
115  }
116  }
117  return(0);
118  };
119  virtual int Run(){return(0);};
120  virtual int Finalize()
121  {
122  if(OutStreamReady())
123  Report(OutStream());
125  ErrOut("Failed to finalize.\n");
126  DumpErrors(ErrStream());
127  return(1);
128  }
129  return(0);
130  };
131  virtual ~serialprogram(){};
132  };
133 
134  class parallelprogram : public ParallelProgramType
135  {
136  protected:
138  std::string outFileName;
142  std::ofstream *outFilePtr;
144  int ndiv;
149  public:
154  ParallelProgramType()
155  {
156  verblevel = 1;
157  outFilePtr = NULL;
158  };
159 
163  parallelprogram(int nargs,char **args) :
164  ParallelProgramType(nargs,args)
165  {
166  verblevel = 1;
167  outFilePtr = NULL;
168  };
169 
173  parallelprogram(int nargs,char **args, MPI_Comm inMPICommunicator) :
174  ParallelProgramType(nargs,args,inMPICommunicator)
175  {
176  verblevel = 1;
177  outFilePtr = NULL;
178  };
179 
184  {
185  this->_command_line.Copy(comline);
186  this->Init(_command_line.ProgramName(),incomm);
187  verblevel = 1;
188  outFilePtr = NULL;
189  };
190 
194  virtual int Initialize()
195  {
197  ErrOut("Failed to initialize.\n");
198  ErrOut(CommandLine().ErrorReport()+std::string("\n"));
199  ErrOut("Use -h,--help for usage help.\n\n");
200  ErrOut(CommandLine().ShortUsage()+std::string("\n"));
201  globalCommunicator.SetExit(1);
202  if(globalCommunicator.Check())
203  return(-1);
204  }
205  if(!CommandLine().GetOption('h').empty()){
206  StdOut(CommandLine().LongUsage());
207  globalCommunicator.SetExit(1);
208  if(globalCommunicator.Check())
209  return(1);
210  }
211  std::string applicationName(CommandLine().ProgramName());
212 
213  // Verbosity level processing intent:
214  // 0 - be silent
215  // 1 - normal verbosity
216  // 2 - increased verbosity
217  // 3 - debug mode engaged (debug stream engaged for root process)
218  // 4+ - debug mode (with debugging logged on all processors)
219  SetVerbLevel(1);
220  std::string sverb(CommandLine().GetOption('v'));
221  if(!sverb.empty()){
222  if(sverb == ".true.")
223  SetVerbLevel(1);
224  else {
225  int rank = Rank();
226  int nproc = NProc();
227  int nZeros = std::log10((double)nproc);
228  std::istringstream Istr(sverb);
229  int verbLevel;
230  Istr >> verbLevel;
231  SetVerbLevel(verbLevel);
232  if(verbLevel > 9){
233  if(rank > 0){
234  SetVerbLevel(verbLevel - 9);
235  outFilePtr = new std::ofstream;
236  std::ostringstream outFileNameStream;
237  outFileNameStream << applicationName << "_";
238  for(int i = nZeros;i > 0;i--){
239  int numRanks = std::pow((double)10,(double)i);
240  if(rank < numRanks)
241  outFileNameStream << "0";
242  }
243  if(!ErrStreamReady())
244  SetErrStream(std::cerr);
245  outFileNameStream << rank;
246  outFileNameStream << ".out";
247  outFileName.assign(outFileNameStream.str());
248  outFilePtr->open(outFileNameStream.str().c_str());
249  if(!(*outFilePtr)){
250  globalCommunicator.SetErr(1);
251  }
252  }
253 
254  if(globalCommunicator.Check()){
255  if(!rank)
256  ErrOut("Unable to open output on all processors.\n");
257  return(-1);
258  } else if(rank > 0) {
259  SetOutStream(*outFilePtr);
260  }
261  SetDebugStream(OutStream());
262  SetDebugLevel(verbLevel - 3);
263  if(rank > 0)
264  SetDebugLevel(verbLevel - 12);
265  }
266  }
267  }
268  // Process profiling control
269  std::string sprof(CommandLine().GetOption('p'));
270  Profiling(false);
271  DisableProfilingBarriers();
272  if(!sprof.empty()){
273  if(sprof == ".true.")
274  Profiling(true);
275  else {
276  std::istringstream Istr(sprof);
277  int profLevel;
278  Istr >> profLevel;
279  if(profLevel == 0)
280  Profiling(false);
281  if(profLevel >= 1){
282  Profiling(true);
283  if(profLevel==2 || profLevel == 4)
284  EnableProfilingBarriers();
285  else
286  DisableProfilingBarriers();
287  if(profLevel > 2)
288  WriteProfiles(true);
289  }
290  }
291  }
292  // Process profiling control
293  std::string sdebug(CommandLine().GetOption('d'));
294  if(!sdebug.empty()){
295  if(sdebug == ".true.")
296  SetDebugLevel(1);
297  else {
298  std::istringstream Istr(sdebug);
299  int debugLevel;
300  Istr >> debugLevel;
301  if(debugLevel >= 1)
302  SetDebugLevel(debugLevel);
303  }
304  }
305  return(0);
306  };
307 
311  virtual ~parallelprogram() {
312  if(outFilePtr){
313  outFilePtr->close();
314  delete outFilePtr;
315  }
316  if(Rank() > 0)
317  UnsetOutStream();
318  SetOutStream(std::cout);
319  };
320 
324  virtual int Run(){return(0);};
325  virtual int Finalize() {
326  if(OutStreamReady())
327  Report(OutStream());
329  ErrOut("Failed to finalize.\n");
330  if(ErrStreamReady())
331  DumpErrors(ErrStream());
332  return(1);
333  }
334  return(0);
335  };
336  };
337 
338  template<typename ProgramType>
339  int MainDriver(int argc,char *argv[])
340  {
341  ProgramType mainProgram(argc,argv);
342  if(mainProgram.Initialize()){
343  return(1);
344  }
345  std::string programName(mainProgram.CommandLine().ProgramName());
346  // mainProgram.StdOut("Initialized\n");
347  if(mainProgram.Run()){
348  return(1);
349  }
350  // mainProgram.StdOut("Ran\n");
351  if(mainProgram.Finalize()){
352  return(1);
353  }
354  // mainProgram.StdOut("Finalized\n");
355  return(0);
356  }
357 }
358 #endif
parallelprogram()
Default constructor.
Definition: PCPPProgram.H:153
std::string outFileName
Name of file for output.
Definition: PCPPProgram.H:138
virtual int Initialize()
Definition: MPIGlobal.H:115
virtual int Initialize()
Definition: PCPPProgram.H:67
std::string ErrorReport()
Error reporting.
Definition: ComLine.C:473
pcpp::ConfigType ConfigurationType
Definition: PCPPProgram.H:13
std::string GetOption(const char &s)
Get the value of an option.
Definition: ComLine.H:291
ix::global::mpiprogram< commandline > ParallelProgramType
Definition: PCPPProgram.H:53
std::string ProgramName() const
Program name access.
Definition: ComLine.H:310
virtual int Finalize()
Definition: PCPPProgram.H:120
virtual int Run()
This function implements the main function executed by the program.
Definition: PCPPProgram.H:324
ix::global::Program< GlobalType, commandline > SerialProgramType
Definition: PCPPProgram.H:52
virtual void Initialize()
defines PlasCom2-specific command-line arguments
Definition: PC2Initialize.C:11
virtual int Run()
Definition: PCPPProgram.H:119
int MainDriver(int argc, char *argv[])
Definition: PCPPProgram.H:339
virtual int Initialize()
Definition: Global.H:661
virtual int Finalize()
Finalizes the global object, and it&#39;s profiler object.
Definition: MPIGlobal.H:122
pcpp::PartitionInfoType PartitionInfoType
Definition: PCPPProgram.H:14
int numProcessors
Number of processors.
Definition: PCPPProgram.H:146
virtual int Finalize()
Definition: Global.H:668
std::ofstream * outFilePtr
Output file stream object for output.
Definition: PCPPProgram.H:142
parallelprogram(commandline &comline, CommunicatorType &incomm)
Constructor designed to take a commandline object, and communicator object.
Definition: PCPPProgram.H:183
serialprogram(GlobalType &inglob)
Definition: PCPPProgram.H:61
virtual ~parallelprogram()
Destructor.
Definition: PCPPProgram.H:311
void SetDescription(const std::string &desc)
Set description string.
Definition: ComLine.H:318
int verblevel
Verbosity level.
Definition: PCPPProgram.H:140
commandline(commandline &inComLine)
Definition: PCPPProgram.H:24
Main encapsulation of MPI.
Definition: COMM.H:62
void AddOption(char s, const std::string &l, int=0)
User interface to describe simple option.
Definition: ComLine.C:295
serialprogram(commandline &incom)
Definition: PCPPProgram.H:63
virtual int Finalize()
Finalizes the global object, and it&#39;s profiler object.
Definition: PCPPProgram.H:325
pcpp::CommunicatorType CommunicatorType
Definition: PCPPProgram.H:15
virtual ~serialprogram()
Definition: PCPPProgram.H:131
global::Program< Global, command_line > ProgramType
commandline(const char *args[])
Definition: PCPPProgram.H:23
std::string LongUsage()
Generate long usage string.
Definition: ComLine.C:188
Base global object.
Definition: Global.H:25
int ndiv
Number of partitions for domain.
Definition: PCPPProgram.H:144
parallelprogram(int nargs, char **args, MPI_Comm inMPICommunicator)
Constructor designed to take all commandline args and an MPI Communicator.
Definition: PCPPProgram.H:173
Command line processing.
Definition: ComLine.H:62
virtual int Initialize()
Populates native data structures from commandline.
Definition: PCPPProgram.H:194
serialprogram(commandline &incom, GlobalType &inglob)
Definition: PCPPProgram.H:65
void InitializeDefaults()
Definition: PCPPProgram.H:26
void AddHelp(char s, const std::string &help)
Specify usage for an option.
Definition: ComLine.H:227
parallelprogram(int nargs, char **args)
Constructor designed to take all commandline args.
Definition: PCPPProgram.H:163
int numThreads
Number of threads.
Definition: PCPPProgram.H:148
serialprogram(int narg, char **args)
Definition: PCPPProgram.H:59
std::string ShortUsage()
Generate short usage string.
Definition: ComLine.C:244