PlasCom2  1.0
XPACC Multi-physics simluation application
IX/include/Application.H
Go to the documentation of this file.
1 #ifndef __APPLICATION_H__
9 #define __APPLICATION_H__
10 
11 #include "Profiler.H"
12 #include "Global.H"
13 #include "ComLine.H"
14 
15 namespace ix { namespace app {
16 
19 
20  class program2 : public Global
21  {
22  public:
23  program2() : Global() {};
24  program2(Global &inglob) : Global(inglob) {};
25  program2(int narg,char **args) : Global(narg,args){};
26  program2(const std::string &name) : Global(name){};
27  program2(const std::string &name,int id) : Global(name,id){};
28  virtual int Initialize(){return(0);};
29  virtual int Run(){return(0);};
30  virtual int Finalize(){return(Global::Finalize());};
31  virtual ~program2(){};
32  };
33 
34  template<typename CommandLineT>
36  {
37  protected:
38  CommandLineT commandLine;
39  public:
41  command_line_program(Global &inglob) : program2(inglob) {};
42  command_line_program(const std::string &name) : program2(name){};
43  command_line_program(const std::string &name,int id) : program2(name,id){};
44  command_line_program(CommandLineT &incom) : program2(), commandLine(incom) {};
45  command_line_program(CommandLineT &incom,Global &inglob) : program2(inglob),
46  commandLine(incom) {};
47  command_line_program(int narg,char **args) : program2(narg,args)
48  {
49  commandLine.Record((const char **)args);
50  };
51  virtual int Initialize()
52  {
53  commandLine.Initialize();
54  return(commandLine.ProcessOptions());
55  };
56  CommandLineT &CommandLine() {return commandLine; };
57  virtual int Run(){return(0);};
58  virtual int Finalize(){return(program2::Finalize());};
59  virtual ~command_line_program(){};
60  };
61 
63  {
64  public:
65  command_line() : ix::util::ComLineObject() {};
66  command_line(const char *args[]) : ix::util::ComLineObject(args) {};
67  command_line(command_line &inComLine) : ix::util::ComLineObject(inComLine) {};
68  virtual void Initialize() {
69  AddOption('d',"debuglevel",1);
70  AddOption('h',"help");
71  AddOption('v',"verblevel",1);
72  AddOption('p',"enable-profiling",1);
73  AddHelp('h',"Produces this long help message");
74  AddHelp('d',"Set the debugging level for the program.(default=1)");
75  AddHelp('p',std::string("Enable profiling. Profiling level > 1 enables")+
76  std::string(" profiling\n\t\tbarriers. (default=1)"));
77  AddHelp('v',std::string("Set the verbosity level for the program.")+
78  std::string(" Anything\n\t\tlarger than 2 is debugging level.")+
79  std::string("\n\t\tExamples:\n\t\t\t-v (defaults to 1)\n\t\t\t")+
80  std::string("-v 0 (silence)\n\t\t\t--verblevel=2")+
81  std::string(" (high verbosity)"));
82  SetDescription(std::string("This text should describe the program."));
83  };
84  };
85 
87 
88  class program : public ProgramType
89  {
90 
91  public:
92  program(int narg,char **args) :
93  ProgramType(narg,args){};
94  program(Global &inglob) :
95  ProgramType(inglob){};
97  ProgramType(incom){};
98  program(command_line &incom,Global &inglob) :
99  ProgramType(incom,inglob){};
100  virtual int Initialize()
101  {
103  ErrOut("Failed to initialize.\n");
104  ErrOut(CommandLine().ErrorReport()+std::string("\n"));
105  ErrOut("Use -h,--help for usage help.\n\n");
106  ErrOut(CommandLine().ShortUsage()+std::string("\n"));
107  return(1);
108  }
109  if(!CommandLine().GetOption('h').empty()){
110  StdOut(CommandLine().LongUsage());
111  return(1);
112  }
113  // Verbosity level processing intent:
114  // 0 - be silent
115  // 1 - normal verbosity
116  // 2 - increased verbosity
117  // 3 - debug mode engaged (debug stream engaged for root process)
118  // 4+ - debug mode (with debugging logged on all processors)
119  std::string sverb(CommandLine().GetOption('v'));
120  if(!sverb.empty()){
121  if(sverb == ".true.")
122  SetVerbLevel(1);
123  else {
124  std::istringstream Istr(sverb);
125  int verbLevel;
126  Istr >> verbLevel;
127  SetVerbLevel(verbLevel);
128  if(verbLevel > 1){
130  SetDebugLevel(verbLevel - 1);
131  }
132  }
133  }
134  // Process profiling control
135  std::string sprof(CommandLine().GetOption('p'));
136  if(!sprof.empty()){
137  if(sprof == ".true.")
138  Profiling(true);
139  else {
140  std::istringstream Istr(sprof);
141  int profLevel;
142  Istr >> profLevel;
143  if(profLevel == 0)
144  Profiling(false);
145  if(profLevel >= 1){
146  Profiling(true);
147  }
148  }
149  }
150  // Process profiling control
151  std::string sdebug(CommandLine().GetOption('d'));
152  if(!sdebug.empty()){
153  if(sdebug == ".true.")
154  SetDebugLevel(1);
155  else {
156  std::istringstream Istr(sdebug);
157  int debugLevel;
158  Istr >> debugLevel;
159  if(debugLevel >= 1)
160  SetDebugLevel(debugLevel);
161  }
162  }
163  return(0);
164  };
165  virtual int Run(){return(0);};
166  virtual int Finalize()
167  {
168  if(OutStreamReady())
169  Report(OutStream());
170  if(ProgramType::Finalize()){
171  ErrOut("Failed to finalize.\n");
173  return(1);
174  }
175  return(0);
176  };
177  virtual ~program(){};
178  };
179 
180  template<typename ProgramType>
181  int ProgramDriver(ProgramType &applicationProgram){
182  // ProgramType applicationProgram(nargs,args);
183  int errCode = applicationProgram.Initialize();
184  if(errCode < 0){
185  applicationProgram.ErrOut("Failed to initialize.\n");
186  return(1);
187  } else if (errCode) {
188  return(1);
189  } else {
190  applicationProgram.DebugOut("Initialized.\n",1);
191  }
192  if(applicationProgram.Run()){
193  applicationProgram.ErrOut("Failed to run.\n");
194  return(1);
195  } else {
196  applicationProgram.DebugOut("Ran.\n",1);
197  }
198  if(applicationProgram.Finalize()){
199  applicationProgram.ErrOut("Failed to finalize.\n");
200  return(1);
201  } else {
202  applicationProgram.DebugOut("Finalized.\n",1);
203  }
204  return(0);
205  }
206 
207  }
208 }
209 
210 #endif
void SetDebugLevel(unsigned char l)
Sets the level of debugging.
Definition: Global.H:388
int ProgramDriver(ProgramType &applicationProgram)
program2(Global &inglob)
virtual void Report(std::ostream &ReportStream)
Creates the final profiling report on the specified stream.
Definition: Global.H:240
program(command_line &incom, Global &inglob)
Performance profiling object.
Definition: Profiler.H:398
virtual void Initialize()
virtual function for program specific Initialization.
virtual int ErrOut(const std::string &outstr)
Definition: Global.H:456
program(command_line &incom)
virtual int Finalize()
Finalizes the global object, and it&#39;s profiler object.
Definition: Global.H:202
profiler::ProfilerObj Profiler
virtual bool Profiling()
Get profiling state.
Definition: Global.H:229
virtual int Finalize()
Finalizes the global object, and it&#39;s profiler object.
Defines MPI-specific parallel global and program classes.
virtual int Initialize()
Definition: Global.H:661
std::ostream & OutStream()
Gets the stdout stream object.
Definition: Global.H:348
void SetDebugStream(std::ostream &dbstream)
Sets the debug stream object.
Definition: Global.H:315
program2(int narg, char **args)
virtual void DumpErrors(std::ostream &Ostr)
Dumps the errors themselves to the indicated stream.
Definition: Global.H:296
Performance Profiling interface definition.
virtual int Finalize()
Definition: Global.H:668
command_line_program(CommandLineT &incom, Global &inglob)
virtual int StdOut(const std::string &outstr, unsigned char inlev=1)
Definition: Global.H:439
program2(const std::string &name)
std::ostream & ErrStream()
Gets the error stream object.
Definition: Global.H:334
global::GlobalObj< std::string, std::string, Profiler > Global
Base global object header.
ComLineObject header.
bool OutStreamReady()
Checks out stream.
Definition: Global.H:340
program(Global &inglob)
global::Program< Global, command_line > ProgramType
command_line_program(const std::string &name, int id)
virtual int Run()
Definition: Global.H:667
command_line(command_line &inComLine)
Base global object.
Definition: Global.H:25
void SetVerbLevel(unsigned char l)
Definition: Global.H:393
Command line processing.
Definition: ComLine.H:62
command_line_program(int narg, char **args)
command_line(const char *args[])
program2(const std::string &name, int id)
program(int narg, char **args)
command_line_program(CommandLineT &incom)
command_line_program(const std::string &name)
virtual int Finalize()
Finalizes the global object, and it&#39;s profiler object.