.. _program_listing_file_include_parpecommon_misc.h: Program Listing for File misc.h =============================== |exhale_lsh| :ref:`Return to documentation for file ` (``include/parpecommon/misc.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #ifndef CPP_MISC_H #define CPP_MISC_H #include #include #include #include #include template std::ostream& operator<<(std::ostream& o, std::vector const& v) { o << "[ "; for (auto const& e : v) o << e << " "; o << "]"; return o; } template std::ostream& operator<<(std::ostream& o, gsl::span const& v) { o << "[ "; for (auto const& e : v) o << e << " "; o << "]"; return o; } namespace parpe { class WallTimer { public: WallTimer(); void reset(); double getRound(); double getTotal() const; private: std::chrono::time_point start; std::chrono::time_point roundStart; }; class CpuTimer { public: CpuTimer() = default; void reset(); double getRound(); double getTotal() const; private: clock_t start = clock(); clock_t roundStart = clock(); }; #define RELEASE_ASSERT(expr, msg) \ if (!(expr)) { \ /* NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay, \ * cppcoreguidelines-pro-type-vararg) */ \ printf("CRITICAL: Assertion %s in %s:%d failed (%s)\n", \ (#expr), \ __FILE__, \ __LINE__, \ msg); \ abort(); \ } void strFormatCurrentLocaltime(gsl::span buffer, const char* format); void printBacktrace(int nMaxFrames = 20); std::string getBacktrace(int nMaxFrames = 20); double randDouble(double min, double max); void fillArrayRandomDoubleIndividualInterval(gsl::span min, gsl::span max, gsl::span buffer); void fillArrayRandomDoubleSameInterval(double min, double max, gsl::span buffer); int getMpiRank(); int getMpiCommSize(); int getMpiActive(); void finalizeMpiIfNeeded(); template bool withinBounds(long int n, T_TEST const* x, const T_BOUNDS* min, const T_BOUNDS* max) { for (int i = 0; i < n; ++i) if (x[i] < min[i]) return false; for (int i = 0; i < n; ++i) if (x[i] > max[i]) return false; return true; } template class InverseUniqueLock { public: explicit InverseUniqueLock(MUTEX* mutex) : mutex(mutex) { mutex->unlock(); } InverseUniqueLock(InverseUniqueLock& other) = delete; InverseUniqueLock& operator=(const InverseUniqueLock& other) = delete; InverseUniqueLock(InverseUniqueLock&& other) noexcept { mutex = other.mutex; other.mutex = nullptr; } InverseUniqueLock const& operator=(InverseUniqueLock&& fp) = delete; ~InverseUniqueLock() { mutex->lock(); } private: MUTEX* mutex = nullptr; }; bool almostEqual(double a, double b); } // namespace parpe #ifndef __cpp_lib_make_unique // custom make_unique while we are still using c++11 namespace std { template std::unique_ptr make_unique(Args&&... args) { return std::unique_ptr(new T(std::forward(args)...)); } } #endif #endif