Flow123d  release_2.2.0-26-ge868538
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 "system/system.hh"
24 #include <signal.h>
25 
26 #ifdef FLOW123D_HAVE_PETSC
27 //#include <petsc.h>
28 #include <petscsys.h>
29 #endif
30 
31 #include <string.h> // for strsignal
32 #include <boost/exception/detail/error_info_impl.hpp> // for error_info
33 #include <iostream> // for cout
34 #include <sstream> // for operator<<, endl
35 #include "mpi.h" // for MPI_Comm_size
36 #include "petscerror.h" // for CHKERRQ, Petsc...
37 #include "system/exc_common.hh" // for ExcAssertMsg
38 #include "system/global_defs.h" // for OLD_ASSERT, msg
39 #include "system/logger.hh" // for Logger, operat...
40 #include "system/system.hh" // for SystemInfo
41 
42 
43 
44 
45 // Function that catches all program signals.
46 PetscErrorCode petsc_signal_handler(int signal, void *context)
47 {
48  if (signal == SIGINT) {
49  cout << "SIGINT\n";
50  }
51  if (signal == SIGFPE || // FPE: Floating Point Exception,probably divide by zero
52  signal == SIGILL || // Illegal instruction: Likely due to memory corruption
53  signal == SIGPIPE || // Broken Pipe: Likely while reading or writing to a socket
54  signal == SIGSEGV ) // SEGV: Segmentation Violation, probably memory access out of range
55  {
56  // Signals handled by us.
57  THROW( ExcSignal() << EI_Signal(signal) << EI_SignalName(strsignal(signal)) );
58  } else {
59  return PetscSignalHandlerDefault(signal,(void*)0);
60  }
61  return 0;
62 }
63 
64 void system_signal_handler(int signal) {
65  petsc_signal_handler(signal, nullptr);
66 }
67 
68 
69 ApplicationBase::ApplicationBase(int argc, char ** argv)
70 : log_filename_(""),
71  signal_handler_off_(false)
72 { }
73 
75 
76 
77 void ApplicationBase::system_init( MPI_Comm comm, const string &log_filename ) {
78  int ierr;
79 
80  sys_info.comm=comm;
81 
82 
83  //Xio::init(); //Initialize XIO library
84 
85  // TODO : otevrit docasne log file jeste pred ctenim vstupu (kvuli zachyceni chyb), po nacteni dokoncit
86  // inicializaci systemu
87 
88  ierr=MPI_Comm_rank(comm, &(sys_info.my_proc));
89  ierr+=MPI_Comm_size(comm, &(sys_info.n_proc));
91  OLD_ASSERT( ierr == MPI_SUCCESS,"MPI not initialized.\n");
92 
93  // determine logfile name or switch it off
94  stringstream log_name;
95 
96  if ( log_filename == "//" ) {
97  // -l option without given name -> turn logging off
98  sys_info.log=NULL;
100  } else {
101  // construct full log name
102  //log_name << log_filename << "." << sys_info.my_proc << ".old.log";
103 
104  //sys_info.log_fname = FilePath(log_name.str(), FilePath::output_file);
105  //sys_info.log=xfopen(sys_info.log_fname.c_str(),"wt");
106 
108  }
109 
112 }
113 
114 
116 
117 #ifdef FLOW123D_HAVE_PETSC
118 PetscErrorCode ApplicationBase::petscvfprintf(FILE *fd, const char format[], va_list Argp) {
119  PetscErrorCode ierr;
120 
121  PetscFunctionBegin;
122  if (fd != stdout && fd != stderr) { /* handle regular files */
123  ierr = PetscVFPrintfDefault(fd,format,Argp); CHKERRQ(ierr);
124  } else {
125  const int buf_size = 65000;
126  char buff[65000];
127  size_t length;
128  ierr = PetscVSNPrintf(buff,buf_size,format,&length,Argp);CHKERRQ(ierr);
129 
130  /* now send buff to whatever stream or whatever you want */
131  fwrite(buff, sizeof(char), length, petsc_output_);
132  }
133  PetscFunctionReturn(0);
134 }
135 #endif
136 
137 
138 void ApplicationBase::petsc_initialize(int argc, char ** argv) {
139 #ifdef FLOW123D_HAVE_PETSC
140  if (petsc_redirect_file_ != "") {
141  petsc_output_ = fopen(petsc_redirect_file_.c_str(), "w");
142  if (! petsc_output_)
143  THROW(FilePath::ExcFileOpen() << FilePath::EI_Path(petsc_redirect_file_));
144  PetscVFPrintf = this->petscvfprintf;
145  }
146 
147 
148  PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL);
149  if (! signal_handler_off_) {
150  // PETSc do not catch SIGINT, but someone on the way does, we try to fix it.
151  signal(SIGINT, system_signal_handler);
152  PetscPushSignalHandler(petsc_signal_handler, nullptr);
153  }
154 
155  int mpi_size;
156  MPI_Comm_size(PETSC_COMM_WORLD, &mpi_size);
157  MessageOut() << "MPI size: " << mpi_size << std::endl;
158 #endif
159 }
160 
161 
162 
164 #ifdef FLOW123D_HAVE_PETSC
165  if ( petsc_initialized )
166  {
167  PetscErrorCode ierr=0;
168 
169  ierr = PetscFinalize(); CHKERRQ(ierr);
170 
171  if (petsc_output_) fclose(petsc_output_);
172 
173  petsc_initialized = false;
174 
175  return ierr;
176  }
177 #endif
178 
179  return 0;
180 }
181 
182 
183 void ApplicationBase::init(int argc, char ** argv) {
184  // parse our own command line arguments, leave others for PETSc
185  this->parse_cmd_line(argc, argv);
187 
188  armadillo_setup(); // set catching armadillo exceptions and reporting stacktrace
189 
190  this->petsc_initialize(argc, argv);
191  petsc_initialized = true;
192 
193  this->system_init(PETSC_COMM_WORLD, log_filename_); // Petsc, open log, read ini file
194 
195 
196  this->run();
197 
198  this->after_run();
199 }
200 
201 
203  //if (sys_info.log) xfclose(sys_info.log);
204  petcs_finalize();
205 }
206 
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:78
void system_signal_handler(int signal)
virtual ~ApplicationBase()
Destructor.
int MPI_Comm
Definition: mpi.h:141
FILE * log
Definition: system.hh:75
virtual void parse_cmd_line(const int argc, char **argv)
#define MessageOut()
Macro defining &#39;message&#39; record of log.
Definition: logger.hh:233
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:72
#define OLD_ASSERT(...)
Definition: global_defs.h:131
int setup_mpi(MPI_Comm comm)
Set rank of actual process by MPI communicator.
Global macros to enhance readability and debugging, general constants.
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:77
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:53
MPI_Comm comm
Definition: system.hh:80
int pause_after_run
Definition: system.hh:73