diff --git a/dune/xt/common/CMakeLists.txt b/dune/xt/common/CMakeLists.txt index ce6607f2653179b123bc190b5827546c931e8f95..e682678536eb3ae497292f5a88bc035e932c2a99 100644 --- a/dune/xt/common/CMakeLists.txt +++ b/dune/xt/common/CMakeLists.txt @@ -11,6 +11,7 @@ set(lib_dune_xt_common_sources filesystem.cc color.cc convergence-study.cc + exceptions.cc localization-study.cc logging.cc timedlogging.cc @@ -19,6 +20,7 @@ set(lib_dune_xt_common_sources configuration.cc signals.cc math.cc + memory.cc misc.cc parallel/threadmanager.cc parallel/helper.cc diff --git a/dune/xt/common/exceptions.cc b/dune/xt/common/exceptions.cc new file mode 100644 index 0000000000000000000000000000000000000000..dc883017347d7077a897651222e38ca9cb7dc70c --- /dev/null +++ b/dune/xt/common/exceptions.cc @@ -0,0 +1,42 @@ +#include <config.h> + +#include "exceptions.hh" + +#include <dune/xt/common/timings.hh> +#include <dune/xt/common/memory.hh> +#include <dune/xt/common/parallel/helper.hh> + +#if HAVE_TBB +#include <tbb/tbb_exception.h> +#endif +namespace Dune { +namespace XT { +namespace Common { + +int handle_exception(const Dune::Exception& exp) +{ + std::cerr << "Failed with Dune::Exception: " << exp.what(); + DXTC_TIMINGS.output_per_rank("profiler"); + mem_usage(); + return abort_all_mpi_processes(); +} + +int handle_exception(const std::exception& exp) +{ + std::cerr << "Failed with std::exception: " << exp.what(); + DXTC_TIMINGS.output_per_rank("profiler"); + mem_usage(); + return abort_all_mpi_processes(); +} +#if HAVE_TBB +int handle_exception(const tbb::tbb_exception& exp) +{ + std::cerr << "Failed with tbb::exception" << exp.name() << ": " << exp.what(); + DXTC_TIMINGS.output_per_rank("profiler"); + mem_usage(); + return abort_all_mpi_processes(); +} +#endif +} // namespace Common +} // namespace XT +} // namespace Dune diff --git a/dune/xt/common/exceptions.hh b/dune/xt/common/exceptions.hh index adf746523b2051c6e0ca36b2107333e8083f6e89..3ac49f3548f67c0728fb798251a1af16a9a2ce37 100644 --- a/dune/xt/common/exceptions.hh +++ b/dune/xt/common/exceptions.hh @@ -20,6 +20,11 @@ #undef DUNE_THROW #endif +#if HAVE_TBB +namespace tbb { +class tbb_exception; +} +#endif /** * \brief Macro to throw a colorfull exception. * @@ -127,6 +132,12 @@ class spe10_data_file_missing : public Dune::IOError }; } // namespace Exceptions + +int handle_exception(const Dune::Exception& exp); +int handle_exception(const std::exception& exp); +#if HAVE_TBB +int handle_exception(const tbb::tbb_exception& exp); +#endif } // namespace Common } // namespace XT } // namespace Dune diff --git a/dune/xt/common/memory.cc b/dune/xt/common/memory.cc new file mode 100644 index 0000000000000000000000000000000000000000..deb4c165a52496371e81bb422856fadcf9d47298 --- /dev/null +++ b/dune/xt/common/memory.cc @@ -0,0 +1,44 @@ +#include <config.h> + +#include "memory.hh" + +#include <dune/xt/common/timings.hh> +#include <dune/xt/common/filesystem.hh> +#include <dune/xt/common/configuration.hh> + +#include <sys/resource.h> +#include <boost/filesystem.hpp> +#include <boost/filesystem/fstream.hpp> + +namespace Dune { +namespace XT { +namespace Common { + +void mem_usage(std::string filename) +{ + auto comm = Dune::MPIHelper::getCollectiveCommunication(); + // Compute the peak memory consumption of each processes + int who = RUSAGE_SELF; + struct rusage usage; + getrusage(who, &usage); + long peakMemConsumption = usage.ru_maxrss; + // compute the maximum and mean peak memory consumption over all processes + long maxPeakMemConsumption = comm.max(peakMemConsumption); + long totalPeakMemConsumption = comm.sum(peakMemConsumption); + long meanPeakMemConsumption = totalPeakMemConsumption / comm.size(); + // write output on rank zero + if (comm.rank() == 0) { + std::unique_ptr<boost::filesystem::ofstream> memoryConsFile(make_ofstream(filename)); + *memoryConsFile << "global.maxPeakMemoryConsumption,global.meanPeakMemoryConsumption\n" + << maxPeakMemConsumption << "," << meanPeakMemConsumption << std::endl; + } +} + +void mem_usage() +{ + mem_usage(std::string(DXTC_CONFIG_GET("global.datadir", "data/")) + std::string("/memory.csv")); +} + +} // namespace Common +} // namespace XT +} // namespace Dune diff --git a/dune/xt/common/memory.hh b/dune/xt/common/memory.hh index 63e42da7458410524a42fc1c3b950c5e9db5a810..dd93c3d4ed0716998528d370d0db2e806f16a447 100644 --- a/dune/xt/common/memory.hh +++ b/dune/xt/common/memory.hh @@ -389,6 +389,10 @@ private: std::unique_ptr<internal::AccessInterface<T>> storage_; }; // class StorageProvider +//! dumps kernel stats into a file +void mem_usage(std::string filename); +void mem_usage(); + } // namespace Common } // namespace XT } // namespace Dune