Logo  0.95.0-final
Finite Element Embedded Library and Language in C++
Feel++ Feel++ on Github Feel++ community
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
environment.hpp
Go to the documentation of this file.
1 /* -*- mode: c++; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; show-trailing-whitespace: t -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
2 
3  This file is part of the Feel library
4 
5  Author(s): Christophe Prud'homme <christophe.prudhomme@feelpp.org>
6  Date: 2010-04-14
7 
8  Copyright (C) 2010-2012 Université Joseph Fourier (Grenoble I)
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 2.1 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
29 #ifndef __Environment_H
30 #define __Environment_H 1
31 
32 #include <cstdlib>
33 
34 #include <boost/noncopyable.hpp>
35 #include <boost/signals2.hpp>
36 #include <boost/format.hpp>
37 
38 
39 
40 #include <feel/feelcore/feel.hpp>
41 #include <feel/feelcore/parameter.hpp>
43 #include <feel/feelcore/about.hpp>
44 #include <feel/options.hpp>
45 #if defined ( FEELPP_HAS_PETSC_H )
46 #include <petscsys.h>
47 #endif
48 namespace Feel
49 {
50 namespace detail
51 {
52 struct MemoryUsage
53 {
54 #if defined ( FEELPP_HAS_PETSC_H )
55  PetscLogDouble memory_usage;
56  PetscLogDouble petsc_malloc_usage;
57  PetscLogDouble petsc_malloc_maximum_usage;
58 #endif
59 };
60 inline
61 AboutData
62 makeAbout( char* name )
63 {
64  AboutData about( name,
65  name,
66  "0.1",
67  name,
68  AboutData::License_GPL,
69  "Copyright (c) 2012 Feel++ Consortium" );
70 
71  about.addAuthor( "Feel++ Consortium",
72  "",
73  "feelpp-devel@feelpp.org", "" );
74  return about;
75 }
76 
101 class Environment : boost::noncopyable
102 {
103 public:
104 
105 
109 
110 
112 
116  typedef WorldComm worldcomm_type;
117  typedef boost::shared_ptr<WorldComm> worldcomm_ptrtype;
118 
120 
124 
130  Environment();
131 
144  Environment( int& argc, char** &argv );
145 
146  BOOST_PARAMETER_MEMBER_FUNCTION(
147  (void), static changeRepository, tag,
148  (required
149  (directory,(boost::format)))
150  (optional
151  (filename,*( boost::is_convertible<mpl::_,std::string> ),"logfile")
152  (subdir,*( boost::is_convertible<mpl::_,bool> ),true)
153  ))
154  {
155  changeRepositoryImpl( directory, filename, subdir );
156  }
157 
158  template <class ArgumentPack>
159  Environment(ArgumentPack const& args)
160  {
161  char** argv = args[_argv];
162  int argc = args[_argc];
163  S_desc = boost::shared_ptr<po::options_description>( new po::options_description( args[_desc | Feel::feel_options()] ) );
164  AboutData about = args[_about| makeAbout(argv[0])];
165  S_desc->add( file_options( about.appName() ) );
166 
167  init( argc, argv, *S_desc, about );
168  if ( S_vm.count("nochdir") == 0 )
169  {
170  std::string defaultdir = about.appName();
171  if ( S_vm.count("directory") )
172  defaultdir = S_vm["directory"].as<std::string>();
173  std::string d = args[_directory|defaultdir];
174  LOG(INFO) << "change directory to " << d << "\n";
175  boost::format f( d );
176  changeRepository( _directory=f );
177  }
178  }
179 
180  void init( int argc, char** argv, po::options_description const& desc, AboutData const& about );
181 
189  ~Environment();
190 
192 
196 
198 
202 
209  static bool initialized();
210 
217  static bool finalized();
218 
222  static WorldComm& worldComm() { return *S_worldcomm; }
223 
227  static std::vector<WorldComm> const& worldsComm( int n );
228 
229  static std::vector<WorldComm> const& worldsCommGroupBySubspace( int n );
230 
234  static WorldComm const& masterWorldComm( int n );
235 
239  static int numberOfProcessors() { return S_worldcomm->godSize(); }
240 
244  static int rank() { return S_worldcomm->globalRank(); }
245 
249  static po::variables_map const& vm() { return S_vm; }
250 
251  static AboutData const& about() { return S_about; }
252 
256  static po::options_description const& optionsDescription() { return *S_desc; }
257 
259 
263 
267  static void setWorldComm( WorldComm& worldcomm ) { S_worldcomm = worldcomm.shared_from_this(); }
268 
269 
270 
272 
276 
278  static std::string rootRepository();
279 
291  static std::string findFile( std::string const& filename );
292 
296  static std::vector<std::string> geoPathList();
297 
299  static std::string localGeoRepository();
300 
306  static boost::tuple<std::string,bool> systemGeoRepository();
307 
308 
310  static std::string localConfigRepository();
311 
317  static boost::tuple<std::string,bool> systemConfigRepository();
318 
319  BOOST_PARAMETER_MEMBER_FUNCTION(
320  (po::variable_value), static vm, tag,
321  (required
322  (name,(std::string)))
323  (optional
324  (worldcomm, ( WorldComm ), Environment::worldComm() )
325  (sub,( std::string ),"")
326  (prefix,( std::string ),"")
327  ))
328  {
329  std::ostringstream os;
330  if ( !prefix.empty() )
331  os << prefix << ".";
332  if ( !sub.empty() )
333  os << sub << "-";
334  os << name;
335  auto it = Environment::vm().find(os.str());
336  CHECK( it != Environment::vm().end() ) << "Invalid option " << os.str() << "\n";
337  return it->second;
338  }
339 
344  static MemoryUsage logMemoryUsage( std::string const& message );
345 
347  //static po::variables_map vm( po::options_description const& desc );
348 
353  static void setLogs( std::string const& prefix );
354 
355  template<typename Observer>
356  static void
357  addDeleteObserver( Observer const& obs )
358  {
359  S_deleteObservers.connect( obs );
360  }
361  template<typename Observer>
362  static void
363  addDeleteObserver( boost::shared_ptr<Observer> const& obs )
364  {
365  S_deleteObservers.connect(boost::bind(&Observer::operator(), obs));
366  }
367 
368  static void clearSomeMemory();
369 
373  static const fs::path& scratchDirectory() { return S_scratchdir; }
374 
376 
377 
378 private:
379 
381  static void changeRepositoryImpl( boost::format fmt, std::string const& logfile, bool add_subdir_np );
382 
384  static void doOptions( int argc, char** argv, po::options_description const& desc, std::string const& appName );
385  static void processGenericOptions();
386  static void parseAndStoreOptions( po::command_line_parser parser, bool extra_parser = false );
387 
388 private:
390  bool i_initialized;
391  mpi::environment M_env;
392 
393  static std::vector<fs::path> S_paths;
394 
395  static fs::path S_scratchdir;
396 
397  static AboutData S_about;
398  static po::variables_map S_vm;
399  static boost::shared_ptr<po::options_description> S_desc;
400  static std::vector<std::string> S_to_pass_further;
401 
402  static boost::signals2::signal<void()> S_deleteObservers;
403 
404  static boost::shared_ptr<WorldComm> S_worldcomm;
405 };
406 } // detail
407 
408 
409 
410 class Environment : public detail::Environment
411 {
412 public:
413  BOOST_PARAMETER_CONSTRUCTOR(
414  Environment, (detail::Environment), tag,
415  (required
416  (argc,*)
417  (argv,*))
418  (optional
419  (desc,*)
420  (about,*)
421  (directory,( std::string ))
422  )) // no semicolon
423 };
424 
425 
426 BOOST_PARAMETER_FUNCTION(
427  (po::variable_value), option, tag,
428  (required
429  (name,(std::string)))
430  (optional
431  (worldcomm, ( WorldComm ), Environment::worldComm() )
432  (sub,( std::string ),"")
433  (prefix,( std::string ),"")
434  ))
435 {
436  return Environment::vm(_name=name,_worldcomm=worldcomm,_sub=sub,_prefix=prefix);
437 }
438 
439 namespace detail
440 {
441 template<typename Args, typename Tag=tag::opt>
442 struct option
443 {
444  typedef typename boost::remove_pointer<
445  typename boost::remove_const<
446  typename boost::remove_reference<
447  typename parameter::binding<Args, Tag>::type
448  >::type
449  >::type
450  >::type type;
451 };
452 
453 }
454 
455 BOOST_PARAMETER_FUNCTION(
456  ( typename Feel::detail::option<Args>::type ),
457  optionT, tag,
458  (required
459  (name,(std::string))
460  (in_out(opt),*))
461  (optional
462  (worldcomm, ( WorldComm ), Environment::worldComm() )
463  (sub,( std::string ),"")
464  (prefix,( std::string ),"")
465  ))
466 {
467  try
468  {
469  opt = Environment::vm(_name=name,_worldcomm=worldcomm,_sub=sub,_prefix=prefix).template as<typename Feel::detail::option<Args>::type>();
470  }
471  catch (boost::bad_any_cast bac)
472  {
473  CHECK( false ) <<"problem in conversion type of argument "<< name << " : check the option type"<<std::endl;
474  }
475  return opt;
476 }
477 
478 }
479 #endif /* __Environment_H */

Generated on Sun Oct 20 2013 08:24:56 for Feel++ by doxygen 1.8.4