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.
 
 
 
 
 
 

151 lignes
4.1 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 <mpi.h>
  15. //#include "matrix.hpp"
  16. #include "config.h"
  17. template<typename TID = int>
  18. struct MPI_t {
  19. using ID_t = TID; // Export TID type (currently int defined by the standard)
  20. void init(int *argc, char ***argv) {
  21. // Initialize the MPI environment
  22. MPI_Init(argc, argv);
  23. // Get the number of processes
  24. int size_value, rank_value;
  25. size_ = static_cast<ID_t>(MPI_Comm_size(MPI_COMM_WORLD, &size_value));
  26. rank_ = static_cast<ID_t>(MPI_Comm_rank(MPI_COMM_WORLD, &rank_value));
  27. // Get the name of the processor
  28. char processor_name[MPI_MAX_PROCESSOR_NAME];
  29. int name_len;
  30. MPI_Get_processor_name(processor_name, &name_len);
  31. name_ = std::string (processor_name, name_len);
  32. }
  33. void finalize() {
  34. // Finalize the MPI environment.
  35. MPI_Finalize();
  36. }
  37. bool exchange(ID_t partner, const void *send_data, void *recv_data, int data_count, MPI_Datatype datatype) {
  38. bool ret = true;
  39. MPI_Status status;
  40. MPI_Sendrecv(
  41. send_data, data_count, datatype, partner, 0,
  42. recv_data, data_count, datatype, partner, 0,
  43. MPI_COMM_WORLD, &status
  44. );
  45. if (status.MPI_ERROR != MPI_SUCCESS)
  46. ret = false;
  47. return ret;
  48. }
  49. // Accessors
  50. [[nodiscard]] ID_t rank() const noexcept { return rank_; }
  51. [[nodiscard]] ID_t size() const noexcept { return size_; }
  52. [[nodiscard]] const std::string& name() const noexcept { return name_; }
  53. private:
  54. ID_t rank_{};
  55. ID_t size_{};
  56. std::string name_{};
  57. };
  58. extern MPI_t<> mpi;
  59. using mpi_id_t = MPI_t<>::ID_t;
  60. /*!
  61. * A Logger for entire program.
  62. */
  63. struct Log {
  64. struct Endl {
  65. } endl; //!< a tag object to to use it as a new line request.
  66. //! We provide logging via << operator
  67. template<typename T>
  68. Log &operator<<(T &&t) {
  69. if (session.verbose) {
  70. if (line_) {
  71. std::cout << "[Log]: " << t;
  72. line_ = false;
  73. } else
  74. std::cout << t;
  75. }
  76. return *this;
  77. }
  78. // overload for special end line handling
  79. Log &operator<<(Endl e) {
  80. (void) e;
  81. if (session.verbose) {
  82. std::cout << '\n';
  83. line_ = true;
  84. }
  85. return *this;
  86. }
  87. private:
  88. bool line_{true};
  89. };
  90. extern Log logger;
  91. /*!
  92. * A small timing utility based on chrono.
  93. */
  94. struct Timing {
  95. using Tpoint = std::chrono::steady_clock::time_point;
  96. using microseconds = std::chrono::microseconds;
  97. using milliseconds = std::chrono::milliseconds;
  98. using seconds = std::chrono::seconds;
  99. //! tool to mark the starting point
  100. Tpoint start() noexcept { return start_ = std::chrono::steady_clock::now(); }
  101. //! tool to mark the ending point
  102. Tpoint stop() noexcept { return stop_ = std::chrono::steady_clock::now(); }
  103. auto dt() noexcept {
  104. return std::chrono::duration_cast<std::chrono::microseconds>(stop_ - start_).count();
  105. }
  106. //! tool to print the time interval
  107. void print_dt(const char *what) noexcept {
  108. if (session.timing) {
  109. auto t = stop_ - start_;
  110. if (std::chrono::duration_cast<microseconds>(t).count() < 10000)
  111. std::cout << "[Timing]: " << what << ": "
  112. << std::to_string(std::chrono::duration_cast<microseconds>(t).count()) << " [usec]\n";
  113. else if (std::chrono::duration_cast<milliseconds>(t).count() < 10000)
  114. std::cout << "[Timing]: " << what << ": "
  115. << std::to_string(std::chrono::duration_cast<milliseconds>(t).count()) << " [msec]\n";
  116. else
  117. std::cout << "[Timing]: " << what << ": "
  118. << std::to_string(std::chrono::duration_cast<seconds>(t).count()) << " [sec]\n";
  119. }
  120. }
  121. private:
  122. Tpoint start_;
  123. Tpoint stop_;
  124. };
  125. #endif /* UTILS_HPP_ */