AUTH's THMMY "Parallel and distributed systems" course assignments.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

utils.hpp 2.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_ */