AUTH's THMMY "Parallel and distributed systems" course assignments.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

88 lines
2.4 KiB

  1. /**
  2. * \file utils.hpp
  3. * \brief Utilities header
  4. *
  5. * \author
  6. * Christos Choutouridis AEM:8997
  7. * <cchoutou@ece.auth.gr>
  8. */
  9. #ifndef UTILS_HPP_
  10. #define UTILS_HPP_
  11. #include <iostream>
  12. #include <chrono>
  13. #include <unistd.h>
  14. #include "matrix.hpp"
  15. #include "config.h"
  16. /*!
  17. * A Logger for entire program.
  18. */
  19. struct Log {
  20. struct Endl {} endl; //!< a tag object to to use it as a new line request.
  21. //! We provide logging via << operator
  22. template<typename T>
  23. Log& operator<< (T&& t) {
  24. if (session.verbose) {
  25. if (line_) {
  26. std::cout << "[Log]: " << t;
  27. line_ = false;
  28. }
  29. else
  30. std::cout << t;
  31. }
  32. return *this;
  33. }
  34. // overload for special end line handling
  35. Log& operator<< (Endl e) { (void)e;
  36. if (session.verbose) {
  37. std::cout << '\n';
  38. line_ = true;
  39. }
  40. return *this;
  41. }
  42. private:
  43. bool line_ {true};
  44. };
  45. extern Log logger;
  46. /*!
  47. * A small timing utility based on chrono.
  48. */
  49. struct Timing{
  50. using Tpoint = std::chrono::steady_clock::time_point;
  51. using microseconds = std::chrono::microseconds;
  52. using milliseconds = std::chrono::milliseconds;
  53. using seconds = std::chrono::seconds;
  54. //! tool to mark the starting point
  55. Tpoint start () noexcept { return start_ = std::chrono::steady_clock::now(); }
  56. //! tool to mark the ending point
  57. Tpoint stop () noexcept { return stop_ = std::chrono::steady_clock::now(); }
  58. auto dt () noexcept {
  59. return std::chrono::duration_cast<std::chrono::microseconds>(stop_ - start_).count();
  60. }
  61. //! tool to print the time interval
  62. void print_dt (const char* what) noexcept {
  63. if (session.timing) {
  64. auto t = stop_ - start_;
  65. if (std::chrono::duration_cast<microseconds>(t).count() < 10000)
  66. std::cout << "[Timing]: " << what << ": " << std::to_string(std::chrono::duration_cast<microseconds>(t).count()) << " [usec]\n";
  67. else if (std::chrono::duration_cast<milliseconds>(t).count() < 10000)
  68. std::cout << "[Timing]: " << what << ": " << std::to_string(std::chrono::duration_cast<milliseconds>(t).count()) << " [msec]\n";
  69. else
  70. std::cout << "[Timing]: " << what << ": " << std::to_string(std::chrono::duration_cast<seconds>(t).count()) << " [sec]\n";
  71. }
  72. }
  73. private:
  74. Tpoint start_;
  75. Tpoint stop_;
  76. };
  77. #endif /* UTILS_HPP_ */