Flow123d  jenkins-Flow123d-windows-release-multijob-285
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  petsc_initialized = true;
26  sys_info.comm=comm;
27 
28 
29  Xio::init(); //Initialize XIO library
30 
31  // TODO : otevrit docasne log file jeste pred ctenim vstupu (kvuli zachyceni chyb), po nacteni dokoncit
32  // inicializaci systemu
33 
34  ierr=MPI_Comm_rank(comm, &(sys_info.my_proc));
35  ierr+=MPI_Comm_size(comm, &(sys_info.n_proc));
36  ASSERT( ierr == MPI_SUCCESS,"MPI not initialized.\n");
37 
38  // determine logfile name or switch it off
39  stringstream log_name;
40 
41  if ( log_filename == "//" ) {
42  // -l option without given name -> turn logging off
43  sys_info.log=NULL;
44  } else {
45  // construct full log name
46  log_name << log_filename << "." << sys_info.my_proc << ".log";
48  sys_info.log=xfopen(sys_info.log_fname.c_str(),"wt");
49  }
50 
53 }
54 
55 
57 
58 #ifdef HAVE_PETSC
59 PetscErrorCode ApplicationBase::petscvfprintf(FILE *fd, const char format[], va_list Argp) {
60  PetscErrorCode ierr;
61 
62  PetscFunctionBegin;
63  if (fd != stdout && fd != stderr) { /* handle regular files */
64  ierr = PetscVFPrintfDefault(fd,format,Argp); CHKERRQ(ierr);
65  } else {
66  const int buf_size = 65000;
67  char buff[65000];
68  size_t length;
69  ierr = PetscVSNPrintf(buff,buf_size,format,&length,Argp);CHKERRQ(ierr);
70 
71  /* now send buff to whatever stream or whatever you want */
72  fwrite(buff, sizeof(char), length, petsc_output_);
73  }
74  PetscFunctionReturn(0);
75 }
76 #endif
77 
78 void ApplicationBase::petsc_initialize(int argc, char ** argv) {
79 #ifdef HAVE_PETSC
80  if (petsc_redirect_file_ != "") {
81  petsc_output_ = fopen(petsc_redirect_file_.c_str(), "w");
82  PetscVFPrintf = this->petscvfprintf;
83  }
84 
85 
86  PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL);
87 
88  int mpi_size;
89  MPI_Comm_size(PETSC_COMM_WORLD, &mpi_size);
90  xprintf(Msg, "MPI size: %d\n", mpi_size);
91 #endif
92 }
93 
94 
95 
97 #ifdef HAVE_PETSC
98  if ( petsc_initialized )
99  {
100  PetscErrorCode ierr=0;
101 
102  ierr = PetscFinalize(); CHKERRQ(ierr);
103 
104  if (petsc_output_) fclose(petsc_output_);
105 
106  petsc_initialized = false;
107 
108  return ierr;
109  }
110 #endif
111 
112  return 0;
113 }
114 
115 
116 void ApplicationBase::init(int argc, char ** argv) {
117  // parse our own command line arguments, leave others for PETSc
118  this->parse_cmd_line(argc, argv);
120 
121  this->petsc_initialize(argc, argv);
122 
123  this->system_init(PETSC_COMM_WORLD, log_filename_); // Petsc, open log, read ini file
124 
125 
126  this->run();
127 
128  this->after_run();
129 }
130 
131 
134  petcs_finalize();
135 }
136 
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:86
Definition: system.hh:72
virtual ~ApplicationBase()
Destructor.
int MPI_Comm
Definition: mpi.h:141
FILE * log
Definition: system.hh:83
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:80
string log_fname
Definition: system.hh:82
#define ASSERT(...)
Definition: global_defs.h:121
#define xprintf(...)
Definition: system.hh:100
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:32
static void initialize()
SystemInfo sys_info
Definition: system.cc:53
int n_proc
Definition: system.hh:85
void petsc_initialize(int argc, char **argv)
ApplicationBase(int argc, char **argv)
MPI_Comm comm
Definition: system.hh:88
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:81