Flow123d  release_2.1.0-87-gfbc1563
application_base.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file application_base.cc
15  * @brief
16  */
17 
19 #include "system/sys_profiler.hh"
20 #include "system/logger_options.hh"
22 #include "system/file_path.hh"
23 #include <signal.h>
24 
25 #ifdef FLOW123D_HAVE_PETSC
26 #include <petsc.h>
27 #include <petscsys.h>
28 #endif
29 
30 
31 
32 
33 // Function that catches all program signals.
34 PetscErrorCode petsc_signal_handler(int signal, void *context)
35 {
36  if (signal == SIGINT) {
37  cout << "SIGINT\n";
38  }
39  if (signal == SIGFPE || // FPE: Floating Point Exception,probably divide by zero
40  signal == SIGILL || // Illegal instruction: Likely due to memory corruption
41  signal == SIGPIPE || // Broken Pipe: Likely while reading or writing to a socket
42  signal == SIGSEGV ) // SEGV: Segmentation Violation, probably memory access out of range
43  {
44  // Signals handled by us.
45  THROW( ExcSignal() << EI_Signal(signal) << EI_SignalName(strsignal(signal)) );
46  } else {
47  return PetscSignalHandlerDefault(signal,(void*)0);
48  }
49  return 0;
50 }
51 
52 void system_signal_handler(int signal) {
53  petsc_signal_handler(signal, nullptr);
54 }
55 
56 
57 ApplicationBase::ApplicationBase(int argc, char ** argv)
58 : log_filename_(""),
59  signal_handler_off_(false)
60 { }
61 
63 
64 
65 void ApplicationBase::system_init( MPI_Comm comm, const string &log_filename ) {
66  int ierr;
67 
68  sys_info.comm=comm;
69 
70 
71  //Xio::init(); //Initialize XIO library
72 
73  // TODO : otevrit docasne log file jeste pred ctenim vstupu (kvuli zachyceni chyb), po nacteni dokoncit
74  // inicializaci systemu
75 
76  ierr=MPI_Comm_rank(comm, &(sys_info.my_proc));
77  ierr+=MPI_Comm_size(comm, &(sys_info.n_proc));
79  OLD_ASSERT( ierr == MPI_SUCCESS,"MPI not initialized.\n");
80 
81  // determine logfile name or switch it off
82  stringstream log_name;
83 
84  if ( log_filename == "//" ) {
85  // -l option without given name -> turn logging off
86  sys_info.log=NULL;
88  } else {
89  // construct full log name
90  //log_name << log_filename << "." << sys_info.my_proc << ".old.log";
91 
92  //sys_info.log_fname = FilePath(log_name.str(), FilePath::output_file);
93  //sys_info.log=xfopen(sys_info.log_fname.c_str(),"wt");
94 
96  }
97 
100 }
101 
102 
104 
105 #ifdef FLOW123D_HAVE_PETSC
106 PetscErrorCode ApplicationBase::petscvfprintf(FILE *fd, const char format[], va_list Argp) {
107  PetscErrorCode ierr;
108 
109  PetscFunctionBegin;
110  if (fd != stdout && fd != stderr) { /* handle regular files */
111  ierr = PetscVFPrintfDefault(fd,format,Argp); CHKERRQ(ierr);
112  } else {
113  const int buf_size = 65000;
114  char buff[65000];
115  size_t length;
116  ierr = PetscVSNPrintf(buff,buf_size,format,&length,Argp);CHKERRQ(ierr);
117 
118  /* now send buff to whatever stream or whatever you want */
119  fwrite(buff, sizeof(char), length, petsc_output_);
120  }
121  PetscFunctionReturn(0);
122 }
123 #endif
124 
125 
126 void ApplicationBase::petsc_initialize(int argc, char ** argv) {
127 #ifdef FLOW123D_HAVE_PETSC
128  if (petsc_redirect_file_ != "") {
129  petsc_output_ = fopen(petsc_redirect_file_.c_str(), "w");
130  if (! petsc_output_)
131  THROW(FilePath::ExcFileOpen() << FilePath::EI_Path(petsc_redirect_file_));
132  PetscVFPrintf = this->petscvfprintf;
133  }
134 
135 
136  PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL);
137  if (! signal_handler_off_) {
138  // PETSc do not catch SIGINT, but someone on the way does, we try to fix it.
139  signal(SIGINT, system_signal_handler);
140  PetscPushSignalHandler(petsc_signal_handler, nullptr);
141  }
142 
143  int mpi_size;
144  MPI_Comm_size(PETSC_COMM_WORLD, &mpi_size);
145  MessageOut() << "MPI size: " << mpi_size << std::endl;
146 #endif
147 }
148 
149 
150 
152 #ifdef FLOW123D_HAVE_PETSC
153  if ( petsc_initialized )
154  {
155  PetscErrorCode ierr=0;
156 
157  ierr = PetscFinalize(); CHKERRQ(ierr);
158 
159  if (petsc_output_) fclose(petsc_output_);
160 
161  petsc_initialized = false;
162 
163  return ierr;
164  }
165 #endif
166 
167  return 0;
168 }
169 
170 
171 void ApplicationBase::init(int argc, char ** argv) {
172  // parse our own command line arguments, leave others for PETSc
173  this->parse_cmd_line(argc, argv);
175 
176  armadillo_setup(); // set catching armadillo exceptions and reporting stacktrace
177 
178  this->petsc_initialize(argc, argv);
179  petsc_initialized = true;
180 
181  this->system_init(PETSC_COMM_WORLD, log_filename_); // Petsc, open log, read ini file
182 
183 
184  this->run();
185 
186  this->after_run();
187 }
188 
189 
191  //if (sys_info.log) xfclose(sys_info.log);
192  petcs_finalize();
193 }
194 
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:73
void system_signal_handler(int signal)
virtual ~ApplicationBase()
Destructor.
int MPI_Comm
Definition: mpi.h:141
FILE * log
Definition: system.hh:70
virtual void parse_cmd_line(const int argc, char **argv)
#define MessageOut()
Macro defining &#39;message&#39; record of log.
Definition: logger.hh:231
static bool petsc_initialized
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3141
static FILE * petsc_output_
File handler for redirecting PETSc output.
int verbosity
Definition: system.hh:67
#define OLD_ASSERT(...)
Definition: global_defs.h:131
int setup_mpi(MPI_Comm comm)
Set rank of actual process by MPI communicator.
void armadillo_setup()
virtual void run()=0
bool signal_handler_off_
Turn off signal handling useful to debug with valgrind.
#define MPI_Comm_size
Definition: mpi.h:235
void init(int argc, char **argv)
void set_log_file(std::string log_file_base)
Initialize instance object in format &#39;log_file_base.process.log&#39;.
#define MPI_Comm_rank
Definition: mpi.h:236
static LoggerOptions & get_instance()
Getter of singleton instance object.
PetscErrorCode petsc_signal_handler(int signal, void *context)
static void initialize()
SystemInfo sys_info
Definition: system.cc:41
int n_proc
Definition: system.hh:72
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:45
MPI_Comm comm
Definition: system.hh:75
int pause_after_run
Definition: system.hh:68