Flow123d  release_1.8.3-6-gc7eaf42
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 // Function that catches all program signals.
15 PetscErrorCode signal_handler(int signal, void *context)
16 {
17  THROW( ExcSignal() << EI_Signal(signal) << EI_SignalName(strsignal(signal)) );
18 
19  return 0;
20 }
21 
22 
23 ApplicationBase::ApplicationBase(int argc, char ** argv)
24 : log_filename_("")
25 { }
26 
28 
29 
30 void ApplicationBase::system_init( MPI_Comm comm, const string &log_filename ) {
31  int ierr;
32 
33  petsc_initialized = true;
34  sys_info.comm=comm;
35 
36 
37  Xio::init(); //Initialize XIO library
38 
39  // TODO : otevrit docasne log file jeste pred ctenim vstupu (kvuli zachyceni chyb), po nacteni dokoncit
40  // inicializaci systemu
41 
42  ierr=MPI_Comm_rank(comm, &(sys_info.my_proc));
43  ierr+=MPI_Comm_size(comm, &(sys_info.n_proc));
44  ASSERT( ierr == MPI_SUCCESS,"MPI not initialized.\n");
45 
46  // determine logfile name or switch it off
47  stringstream log_name;
48 
49  if ( log_filename == "//" ) {
50  // -l option without given name -> turn logging off
51  sys_info.log=NULL;
52  } else {
53  // construct full log name
54  log_name << log_filename << "." << sys_info.my_proc << ".log";
56  sys_info.log=xfopen(sys_info.log_fname.c_str(),"wt");
57  }
58 
61 }
62 
63 
65 
66 #ifdef HAVE_PETSC
67 PetscErrorCode ApplicationBase::petscvfprintf(FILE *fd, const char format[], va_list Argp) {
68  PetscErrorCode ierr;
69 
70  PetscFunctionBegin;
71  if (fd != stdout && fd != stderr) { /* handle regular files */
72  ierr = PetscVFPrintfDefault(fd,format,Argp); CHKERRQ(ierr);
73  } else {
74  const int buf_size = 65000;
75  char buff[65000];
76  size_t length;
77  ierr = PetscVSNPrintf(buff,buf_size,format,&length,Argp);CHKERRQ(ierr);
78 
79  /* now send buff to whatever stream or whatever you want */
80  fwrite(buff, sizeof(char), length, petsc_output_);
81  }
82  PetscFunctionReturn(0);
83 }
84 #endif
85 
86 
87 void ApplicationBase::petsc_initialize(int argc, char ** argv) {
88 #ifdef HAVE_PETSC
89  if (petsc_redirect_file_ != "") {
90  petsc_output_ = fopen(petsc_redirect_file_.c_str(), "w");
91  PetscVFPrintf = this->petscvfprintf;
92  }
93 
94 
95  PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL);
96  PetscPushSignalHandler(signal_handler, nullptr);
97 
98  int mpi_size;
99  MPI_Comm_size(PETSC_COMM_WORLD, &mpi_size);
100  xprintf(Msg, "MPI size: %d\n", mpi_size);
101 #endif
102 }
103 
104 
105 
107 #ifdef HAVE_PETSC
108  if ( petsc_initialized )
109  {
110  PetscErrorCode ierr=0;
111 
112  ierr = PetscFinalize(); CHKERRQ(ierr);
113 
114  if (petsc_output_) fclose(petsc_output_);
115 
116  petsc_initialized = false;
117 
118  return ierr;
119  }
120 #endif
121 
122  return 0;
123 }
124 
125 
126 void ApplicationBase::init(int argc, char ** argv) {
127  // parse our own command line arguments, leave others for PETSc
128  this->parse_cmd_line(argc, argv);
130 
131  this->petsc_initialize(argc, argv);
132 
133  this->system_init(PETSC_COMM_WORLD, log_filename_); // Petsc, open log, read ini file
134 
135 
136  this->run();
137 
138  this->after_run();
139 }
140 
141 
144  petcs_finalize();
145 }
146 
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
PetscErrorCode signal_handler(int signal, void *context)
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)
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:34
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