AUTH's THMMY "Parallel and distributed systems" course assignments.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

97 行
2.5 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 {
  21. } endl; //!< a tag object to to use it as a new line request.
  22. //! We provide logging via << operator
  23. template<typename T>
  24. Log &operator<<(T &&t) {
  25. if (session.verbose) {
  26. if (line_) {
  27. std::cout << "[Log]: " << t;
  28. line_ = false;
  29. } else
  30. std::cout << t;
  31. }
  32. return *this;
  33. }
  34. // overload for special end line handling
  35. Log &operator<<(Endl e) {
  36. (void) e;
  37. if (session.verbose) {
  38. std::cout << '\n';
  39. line_ = true;
  40. }
  41. return *this;
  42. }
  43. private:
  44. bool line_{true};
  45. };
  46. extern Log logger;
  47. /*!
  48. * A small timing utility based on chrono.
  49. */
  50. struct Timing {
  51. using Tpoint = std::chrono::steady_clock::time_point;
  52. using microseconds = std::chrono::microseconds;
  53. using milliseconds = std::chrono::milliseconds;
  54. using seconds = std::chrono::seconds;
  55. //! tool to mark the starting point
  56. Tpoint start() noexcept { return start_ = std::chrono::steady_clock::now(); }
  57. //! tool to mark the ending point
  58. Tpoint stop() noexcept { return stop_ = std::chrono::steady_clock::now(); }
  59. auto dt() noexcept {
  60. return std::chrono::duration_cast<std::chrono::microseconds>(stop_ - start_).count();
  61. }
  62. //! tool to print the time interval
  63. void print_dt(const char *what) noexcept {
  64. if (session.timing) {
  65. auto t = stop_ - start_;
  66. if (std::chrono::duration_cast<microseconds>(t).count() < 10000)
  67. std::cout << "[Timing]: " << what << ": "
  68. << std::to_string(std::chrono::duration_cast<microseconds>(t).count()) << " [usec]\n";
  69. else if (std::chrono::duration_cast<milliseconds>(t).count() < 10000)
  70. std::cout << "[Timing]: " << what << ": "
  71. << std::to_string(std::chrono::duration_cast<milliseconds>(t).count()) << " [msec]\n";
  72. else
  73. std::cout << "[Timing]: " << what << ": "
  74. << std::to_string(std::chrono::duration_cast<seconds>(t).count()) << " [sec]\n";
  75. }
  76. }
  77. private:
  78. Tpoint start_;
  79. Tpoint stop_;
  80. };
  81. #endif /* UTILS_HPP_ */