Flow123d  last_with_con_2.0.0-663-gd0e2296
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 
24 #ifdef FLOW123D_HAVE_PETSC
25 #include <petsc.h>
26 #include <petscsys.h>
27 #endif
28 
29 
30 // Function that catches all program signals.
31 PetscErrorCode signal_handler(int signal, void *context)
32 {
33  THROW( ExcSignal() << EI_Signal(signal) << EI_SignalName(strsignal(signal)) );
34 
35  return 0;
36 }
37 
38 
39 ApplicationBase::ApplicationBase(int argc, char ** argv)
40 : log_filename_(""),
41  signal_handler_off_(false)
42 { }
43 
45 
46 
47 void ApplicationBase::system_init( MPI_Comm comm, const string &log_filename ) {
48  int ierr;
49 
50  sys_info.comm=comm;
51 
52 
53  //Xio::init(); //Initialize XIO library
54 
55  // TODO : otevrit docasne log file jeste pred ctenim vstupu (kvuli zachyceni chyb), po nacteni dokoncit
56  // inicializaci systemu
57 
58  ierr=MPI_Comm_rank(comm, &(sys_info.my_proc));
59  ierr+=MPI_Comm_size(comm, &(sys_info.n_proc));
61  OLD_ASSERT( ierr == MPI_SUCCESS,"MPI not initialized.\n");
62 
63  // determine logfile name or switch it off
64  stringstream log_name;
65 
66  if ( log_filename == "//" ) {
67  // -l option without given name -> turn logging off
68  sys_info.log=NULL;
70  } else {
71  // construct full log name
72  //log_name << log_filename << "." << sys_info.my_proc << ".old.log";
73 
74  //sys_info.log_fname = FilePath(log_name.str(), FilePath::output_file);
75  //sys_info.log=xfopen(sys_info.log_fname.c_str(),"wt");
76 
78  }
79 
82 }
83 
84 
86 
87 #ifdef FLOW123D_HAVE_PETSC
88 PetscErrorCode ApplicationBase::petscvfprintf(FILE *fd, const char format[], va_list Argp) {
89  PetscErrorCode ierr;
90 
91  PetscFunctionBegin;
92  if (fd != stdout && fd != stderr) { /* handle regular files */
93  ierr = PetscVFPrintfDefault(fd,format,Argp); CHKERRQ(ierr);
94  } else {
95  const int buf_size = 65000;
96  char buff[65000];
97  size_t length;
98  ierr = PetscVSNPrintf(buff,buf_size,format,&length,Argp);CHKERRQ(ierr);
99 
100  /* now send buff to whatever stream or whatever you want */
101  fwrite(buff, sizeof(char), length, petsc_output_);
102  }
103  PetscFunctionReturn(0);
104 }
105 #endif
106 
107 
108 void ApplicationBase::petsc_initialize(int argc, char ** argv) {
109 #ifdef FLOW123D_HAVE_PETSC
110  if (petsc_redirect_file_ != "") {
111  petsc_output_ = fopen(petsc_redirect_file_.c_str(), "w");
112  if (! petsc_output_)
113  THROW(FilePath::ExcFileOpen() << FilePath::EI_Path(petsc_redirect_file_));
114  PetscVFPrintf = this->petscvfprintf;
115  }
116 
117 
118  PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL);
119  if (! signal_handler_off_) {
120  PetscPushSignalHandler(signal_handler, nullptr);
121  }
122 
123  int mpi_size;
124  MPI_Comm_size(PETSC_COMM_WORLD, &mpi_size);
125  MessageOut() << "MPI size: " << mpi_size << std::endl;
126 #endif
127 }
128 
129 
130 
132 #ifdef FLOW123D_HAVE_PETSC
133  if ( petsc_initialized )
134  {
135  PetscErrorCode ierr=0;
136 
137  ierr = PetscFinalize(); CHKERRQ(ierr);
138 
139  if (petsc_output_) fclose(petsc_output_);
140 
141  petsc_initialized = false;
142 
143  return ierr;
144  }
145 #endif
146 
147  return 0;
148 }
149 
150 
151 void ApplicationBase::init(int argc, char ** argv) {
152  // parse our own command line arguments, leave others for PETSc
153  this->parse_cmd_line(argc, argv);
155 
156  armadillo_setup(); // set catching armadillo exceptions and reporting stacktrace
157 
158  this->petsc_initialize(argc, argv);
159  petsc_initialized = true;
160 
161  this->system_init(PETSC_COMM_WORLD, log_filename_); // Petsc, open log, read ini file
162 
163 
164  this->run();
165 
166  this->after_run();
167 }
168 
169 
171  //if (sys_info.log) xfclose(sys_info.log);
172  petcs_finalize();
173 }
174 
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
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
PetscErrorCode signal_handler(int signal, void *context)
#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.
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