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