Flow123d  jenkins-Flow123d-windows32-release-multijob-28
application_base.cc
Go to the documentation of this file.
1 /*
2  * aplication_base.cc
3  *
4  */
5 
6 #ifdef HAVE_PETSC
7 #include <petsc.h>
8 #include <petscsys.h>
9 #endif
10 
12 #include "system/sys_profiler.hh"
13 
14 
15 ApplicationBase::ApplicationBase(int argc, char ** argv)
16 : log_filename_("")
17 { }
18 
20 
21 
22 void ApplicationBase::system_init( MPI_Comm comm, const string &log_filename ) {
23  int ierr;
24 
25  //for(int i=0;i<argc;i++) xprintf(Msg,"%s,",argv[i]);
26  petsc_initialized = true;
27  sys_info.comm=comm;
28 
29 
30  Xio::init(); //Initialize XIO library
31 
32  // TODO : otevrit docasne log file jeste pred ctenim vstupu (kvuli zachyceni chyb), po nacteni dokoncit
33  // inicializaci systemu
34 
35  ierr=MPI_Comm_rank(comm, &(sys_info.my_proc));
36  ierr+=MPI_Comm_size(comm, &(sys_info.n_proc));
37  ASSERT( ierr == MPI_SUCCESS,"MPI not initialized.\n");
38 
39  // determine logfile name or switch it off
40  stringstream log_name;
41 
42  if ( log_filename == "//" ) {
43  // -l option without given name -> turn logging off
44  sys_info.log=NULL;
45  } else {
46  // construct full log name
47  log_name << log_filename << "." << sys_info.my_proc << ".log";
49  sys_info.log=xfopen(sys_info.log_fname.c_str(),"wt");
50  }
51 
54 }
55 
56 
58 
59 #ifdef HAVE_PETSC
60 PetscErrorCode ApplicationBase::petscvfprintf(FILE *fd, const char format[], va_list Argp) {
61  PetscErrorCode ierr;
62 
63  PetscFunctionBegin;
64  if (fd != stdout && fd != stderr) { /* handle regular files */
65  ierr = PetscVFPrintfDefault(fd,format,Argp); CHKERRQ(ierr);
66  } else {
67  const int buf_size = 65000;
68  char buff[65000];
69  size_t length;
70  ierr = PetscVSNPrintf(buff,buf_size,format,&length,Argp);CHKERRQ(ierr);
71 
72  /* now send buff to whatever stream or whatever you want */
73  fwrite(buff, sizeof(char), length, petsc_output_);
74  }
75  PetscFunctionReturn(0);
76 }
77 #endif
78 
79 void ApplicationBase::petsc_initialize(int argc, char ** argv) {
80 #ifdef HAVE_PETSC
81  if (petsc_redirect_file_ != "") {
82  petsc_output_ = fopen(petsc_redirect_file_.c_str(), "w");
83  PetscVFPrintf = this->petscvfprintf;
84  }
85 
86 
87  PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL);
88 
89  int mpi_size;
90  MPI_Comm_size(PETSC_COMM_WORLD, &mpi_size);
91  xprintf(Msg, "MPI size: %d\n", mpi_size);
92 #endif
93 }
94 
95 
96 
98 #ifdef HAVE_PETSC
99  if ( petsc_initialized )
100  {
101  PetscErrorCode ierr=0;
102 
103  ierr = PetscFinalize(); CHKERRQ(ierr);
104 
105  if (petsc_output_) fclose(petsc_output_);
106 
107  petsc_initialized = false;
108 
109  return ierr;
110  }
111 #endif
112 
113  return 0;
114 }
115 
116 
117 void ApplicationBase::init(int argc, char ** argv) {
119  // parse our own command line arguments, leave others for PETSc
120  this->parse_cmd_line(argc, argv);
121 
122  this->petsc_initialize(argc, argv);
123 
124  this->system_init(PETSC_COMM_WORLD, log_filename_); // Petsc, open log, read ini file
125 
126 
127  this->run();
128 
129  this->after_run();
130 }
131 
132 
135  petcs_finalize();
136 }
137 
virtual void after_run()
void system_init(MPI_Comm comm, const string &log_filename)
#define MPI_SUCCESS
Definition: mpi.c:17
int my_proc
Definition: system.hh:89
Definition: system.hh:75
virtual ~ApplicationBase()
Destructor.
int MPI_Comm
Definition: mpi.h:141
FILE * log
Definition: system.hh:86
virtual void parse_cmd_line(const int argc, char **argv)
int xfclose(FILE *stream)
FCLOSE WITH ERROR HANDLING.
Definition: xio.cc:309
static bool petsc_initialized
static FILE * petsc_output_
File handler for redirecting PETSc output.
int verbosity
Definition: system.hh:83
string log_fname
Definition: system.hh:85
#define ASSERT(...)
Definition: global_defs.h:120
#define xprintf(...)
Definition: system.hh:104
virtual void run()=0
#define MPI_Comm_size
Definition: mpi.h:235
void init(int argc, char **argv)
#define MPI_Comm_rank
Definition: mpi.h:236
Dedicated class for storing path to input and output files.
Definition: file_path.hh:34
static void initialize()
SystemInfo sys_info
Definition: system.cc:55
int n_proc
Definition: system.hh:88
void petsc_initialize(int argc, char **argv)
ApplicationBase(int argc, char **argv)
MPI_Comm comm
Definition: system.hh:91
static void init()
initialize XIO library
Definition: xio.cc:89
FILE * xfopen(const std::string &fname, const char *mode)
Definition: xio.cc:246
int pause_after_run
Definition: system.hh:84