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