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.
 
 
 
 
 
 

406 lignes
15 KiB

  1. /**
  2. * \file
  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 <vector>
  12. #include <iostream>
  13. #include <chrono>
  14. #include <unistd.h>
  15. #include <mpi.h>
  16. #include "config.h"
  17. /*
  18. * MPI_<type> dispatcher mechanism
  19. */
  20. template <typename T> struct MPI_TypeMapper { };
  21. template <> struct MPI_TypeMapper<char> { static MPI_Datatype getType() { return MPI_CHAR; } };
  22. template <> struct MPI_TypeMapper<short> { static MPI_Datatype getType() { return MPI_SHORT; } };
  23. template <> struct MPI_TypeMapper<int> { static MPI_Datatype getType() { return MPI_INT; } };
  24. template <> struct MPI_TypeMapper<long> { static MPI_Datatype getType() { return MPI_LONG; } };
  25. template <> struct MPI_TypeMapper<long long> { static MPI_Datatype getType() { return MPI_LONG_LONG; } };
  26. template <> struct MPI_TypeMapper<unsigned char> { static MPI_Datatype getType() { return MPI_UNSIGNED_CHAR; } };
  27. template <> struct MPI_TypeMapper<unsigned short>{ static MPI_Datatype getType() { return MPI_UNSIGNED_SHORT; } };
  28. template <> struct MPI_TypeMapper<unsigned int> { static MPI_Datatype getType() { return MPI_UNSIGNED; } };
  29. template <> struct MPI_TypeMapper<unsigned long> { static MPI_Datatype getType() { return MPI_UNSIGNED_LONG; } };
  30. template <> struct MPI_TypeMapper<unsigned long long> { static MPI_Datatype getType() { return MPI_UNSIGNED_LONG_LONG; } };
  31. template <> struct MPI_TypeMapper<float> { static MPI_Datatype getType() { return MPI_FLOAT; } };
  32. template <> struct MPI_TypeMapper<double> { static MPI_Datatype getType() { return MPI_DOUBLE; } };
  33. /*!
  34. * MPI wrapper type to provide MPI functionality and RAII to MPI as a resource
  35. *
  36. * @tparam TID The MPI type for process id [default: int]
  37. */
  38. template<typename TID = int>
  39. struct MPI_t {
  40. using ID_t = TID; // Export TID type (currently int defined by the standard)
  41. /*!
  42. * Initializes the MPI environment, must called from each process
  43. *
  44. * @param argc [int*] POINTER to main's argc argument
  45. * @param argv [char***] POINTER to main's argv argument
  46. */
  47. void init(int* argc, char*** argv) {
  48. // Initialize the MPI environment
  49. int err;
  50. if ((err = MPI_Init(argc, argv)) != MPI_SUCCESS)
  51. mpi_throw(err, "(MPI) MPI_Init() - ");
  52. initialized_ = true;
  53. // Get the number of processes
  54. int size_value, rank_value;
  55. if ((err = MPI_Comm_size(MPI_COMM_WORLD, &size_value)) != MPI_SUCCESS)
  56. mpi_throw(err, "(MPI) MPI_Comm_size() - ");
  57. if ((err = MPI_Comm_rank(MPI_COMM_WORLD, &rank_value)) != MPI_SUCCESS)
  58. mpi_throw(err, "(MPI) MPI_Comm_rank() - ");
  59. size_ = static_cast<ID_t>(size_value);
  60. rank_ = static_cast<ID_t>(rank_value);
  61. // Get the name of the processor
  62. char processor_name[MPI_MAX_PROCESSOR_NAME];
  63. int name_len;
  64. if ((err = MPI_Get_processor_name(processor_name, &name_len)) != MPI_SUCCESS)
  65. mpi_throw(err, "(MPI) MPI_Get_processor_name() - ");
  66. name_ = std::string (processor_name, name_len);
  67. }
  68. /*!
  69. * Exchange data with partner as part of the sorting network of both bubbletonic or bitonic
  70. * sorting algorithms.
  71. *
  72. * This function matches a transmit and a receive in order for fully exchanged data between
  73. * current node and partner.
  74. *
  75. * @tparam T The inner valur type used in buffer
  76. *
  77. * @param ldata [std::vector<T>] Reference to local data to send
  78. * @param rdata [std::vector<T>] Reference to buffer to receive data from partner
  79. * @param partner [mpi_id_t] The partner for the exchange
  80. * @param tag [int] The tag to use for the MPI communication
  81. */
  82. template<typename T>
  83. void exchange_data(const std::vector<T>& ldata, std::vector<T>& rdata, ID_t partner, int tag) {
  84. if (tag < 0)
  85. throw std::runtime_error("(MPI) exchange_data() [tag] - Out of bound");
  86. MPI_Datatype datatype = MPI_TypeMapper<T>::getType();
  87. int count = static_cast<int>(ldata.size());
  88. MPI_Status status;
  89. int err;
  90. if ((err = MPI_Sendrecv(
  91. ldata.data(), count, datatype, partner, tag,
  92. rdata.data(), count, datatype, partner, tag,
  93. MPI_COMM_WORLD, &status
  94. )) != MPI_SUCCESS)
  95. mpi_throw(err, "(MPI) MPI_Sendrecv() [data] - ");
  96. }
  97. /*!
  98. * Exchange a data object with partner as part of the sorting network of both bubbletonic
  99. * or bitonic sorting algorithms.
  100. *
  101. * This function matches a transmit and a receive in order for fully exchanged the data object
  102. * between current node and partner.
  103. *
  104. * @tparam T The object type
  105. *
  106. * @param local [const T&] Reference to the local object to send
  107. * @param remote [T&] Reference to the object to receive data from partner
  108. * @param partner [mpi_id_t] The partner for the exchange
  109. * @param tag [int] The tag to use for the MPI communication
  110. */
  111. template<typename T>
  112. void exchange_it(const T& local, T& remote, ID_t partner, int tag) {
  113. if (tag < 0)
  114. throw std::runtime_error("(MPI) exchange_it() [tag] - Out of bound");
  115. MPI_Status status;
  116. int err;
  117. if ((err = MPI_Sendrecv(
  118. &local, sizeof(T), MPI_BYTE, partner, tag,
  119. &remote, sizeof(T), MPI_BYTE, partner, tag,
  120. MPI_COMM_WORLD, &status
  121. )) != MPI_SUCCESS)
  122. mpi_throw(err, "(MPI) MPI_Sendrecv() [item] - ");
  123. }
  124. // Accessors
  125. [[nodiscard]] ID_t rank() const noexcept { return rank_; }
  126. [[nodiscard]] ID_t size() const noexcept { return size_; }
  127. [[nodiscard]] const std::string& name() const noexcept { return name_; }
  128. // Mutators
  129. ID_t rank(ID_t rank) noexcept { return rank_ = rank; }
  130. ID_t size(ID_t size) noexcept { return size_ = size; }
  131. std::string& name(const std::string& name) noexcept { return name_ = name; }
  132. /*!
  133. * Finalized the MPI
  134. */
  135. void finalize() {
  136. // Finalize the MPI environment
  137. initialized_ = false;
  138. MPI_Finalize();
  139. }
  140. //! RAII MPI finalization
  141. ~MPI_t() {
  142. // Finalize the MPI environment even on unexpected errors
  143. if (initialized_)
  144. MPI_Finalize();
  145. }
  146. // Local functionality
  147. private:
  148. /*!
  149. * Throw exception helper. It bundles the prefix msg with the MPI error string retrieved by
  150. * MPI API.
  151. *
  152. * @param err The MPI error code
  153. * @param prefixMsg The prefix text for the exception error message
  154. */
  155. void mpi_throw(int err, const char* prefixMsg) {
  156. char err_msg[MPI_MAX_ERROR_STRING];
  157. int msg_len;
  158. MPI_Error_string(err, err_msg, &msg_len);
  159. throw std::runtime_error(prefixMsg + std::string (err_msg) + '\n');
  160. }
  161. private:
  162. ID_t rank_{}; //!< MPI rank of the process
  163. ID_t size_{}; //!< MPI total size of the execution
  164. std::string name_{}; //!< The name of the local machine
  165. bool initialized_{}; //!< RAII helper flag
  166. };
  167. /*
  168. * Exported data types
  169. */
  170. extern MPI_t<> mpi;
  171. using mpi_id_t = MPI_t<>::ID_t;
  172. /*!
  173. * @brief A std::vector wrapper with 2 vectors, an active and a shadow.
  174. *
  175. * This type exposes the standard vector
  176. * functionality of the active vector. The shadow can be used when we need to use the vector as mutable
  177. * data in algorithms that can not support "in-place" editing (like elbow-sort for example)
  178. *
  179. * @tparam Value_t the underlying data type of the vectors
  180. */
  181. template <typename Value_t>
  182. struct ShadowedVec_t {
  183. // STL requirements
  184. using value_type = Value_t;
  185. using iterator = typename std::vector<Value_t>::iterator;
  186. using const_iterator = typename std::vector<Value_t>::const_iterator;
  187. using size_type = typename std::vector<Value_t>::size_type;
  188. // Default constructor
  189. ShadowedVec_t() = default;
  190. // Constructor from an std::vector
  191. explicit ShadowedVec_t(const std::vector<Value_t>& vec)
  192. : North(vec), South(), active(north) {
  193. South.resize(North.size());
  194. }
  195. explicit ShadowedVec_t(std::vector<Value_t>&& vec)
  196. : North(std::move(vec)), South(), active(north) {
  197. South.resize(North.size());
  198. }
  199. // Copy assignment operator
  200. ShadowedVec_t& operator=(const ShadowedVec_t& other) {
  201. if (this != &other) { // Avoid self-assignment
  202. North = other.North;
  203. South = other.South;
  204. active = other.active;
  205. }
  206. return *this;
  207. }
  208. // Move assignment operator
  209. ShadowedVec_t& operator=(ShadowedVec_t&& other) noexcept {
  210. if (this != &other) { // Avoid self-assignment
  211. North = std::move(other.North);
  212. South = std::move(other.South);
  213. active = other.active;
  214. // There is no need to zero out other since it is valid but in a non-defined state
  215. }
  216. return *this;
  217. }
  218. // Type accessors
  219. std::vector<Value_t>& getActive() { return (active == north) ? North : South; }
  220. std::vector<Value_t>& getShadow() { return (active == north) ? South : North; }
  221. const std::vector<Value_t>& getActive() const { return (active == north) ? North : South; }
  222. const std::vector<Value_t>& getShadow() const { return (active == north) ? South : North; }
  223. // Swap vectors
  224. void switch_active() { active = (active == north) ? south : north; }
  225. // Dispatch vector functionality to active vector
  226. Value_t& operator[](size_type index) { return getActive()[index]; }
  227. const Value_t& operator[](size_type index) const { return getActive()[index]; }
  228. Value_t& at(size_type index) { return getActive().at(index); }
  229. const Value_t& at(size_type index) const { return getActive().at(index); }
  230. void push_back(const Value_t& value) { getActive().push_back(value); }
  231. void push_back(Value_t&& value) { getActive().push_back(std::move(value)); }
  232. void pop_back() { getActive().pop_back(); }
  233. Value_t& front() { return getActive().front(); }
  234. Value_t& back() { return getActive().back(); }
  235. const Value_t& front() const { return getActive().front(); }
  236. const Value_t& back() const { return getActive().back(); }
  237. iterator begin() { return getActive().begin(); }
  238. const_iterator begin() const { return getActive().begin(); }
  239. iterator end() { return getActive().end(); }
  240. const_iterator end() const { return getActive().end(); }
  241. size_type size() const { return getActive().size(); }
  242. void resize(size_t new_size) {
  243. North.resize(new_size);
  244. South.resize(new_size);
  245. }
  246. void reserve(size_t new_capacity) {
  247. North.reserve(new_capacity);
  248. South.reserve(new_capacity);
  249. }
  250. [[nodiscard]] size_t capacity() const { return getActive().capacity(); }
  251. [[nodiscard]] bool empty() const { return getActive().empty(); }
  252. void clear() { getActive().clear(); }
  253. void swap(std::vector<Value_t>& other) { getActive().swap(other); }
  254. // Comparisons
  255. bool operator== (const ShadowedVec_t& other) { return getActive() == other.getActive(); }
  256. bool operator!= (const ShadowedVec_t& other) { return getActive() != other.getActive(); }
  257. bool operator== (const std::vector<value_type>& other) { return getActive() == other; }
  258. bool operator!= (const std::vector<value_type>& other) { return getActive() != other; }
  259. private:
  260. std::vector<Value_t> North{}; //!< Actual buffer to be used either as active or shadow
  261. std::vector<Value_t> South{}; //!< Actual buffer to be used either as active or shadow
  262. enum {
  263. north, south
  264. } active{north}; //!< Flag to select between North and South buffer
  265. };
  266. /*
  267. * Exported data types
  268. */
  269. using distBuffer_t = ShadowedVec_t<distValue_t>;
  270. extern distBuffer_t Data;
  271. /*!
  272. * A Logger for entire program.
  273. */
  274. struct Log {
  275. struct Endl {} endl; //!< a tag object to to use it as a new line request.
  276. //! We provide logging via << operator
  277. template<typename T>
  278. Log &operator<<(T &&t) {
  279. if (config.verbose) {
  280. if (line_) {
  281. std::cout << "[Log]: " << t;
  282. line_ = false;
  283. } else
  284. std::cout << t;
  285. }
  286. return *this;
  287. }
  288. // overload for special end line handling
  289. Log &operator<<(Endl e) {
  290. (void) e;
  291. if (config.verbose) {
  292. std::cout << '\n';
  293. line_ = true;
  294. }
  295. return *this;
  296. }
  297. private:
  298. bool line_{true};
  299. };
  300. extern Log logger;
  301. /*!
  302. * A small timing utility based on chrono.
  303. */
  304. struct Timing {
  305. using Tpoint = std::chrono::steady_clock::time_point;
  306. using Tduration = std::chrono::microseconds;
  307. using microseconds = std::chrono::microseconds;
  308. using milliseconds = std::chrono::milliseconds;
  309. using seconds = std::chrono::seconds;
  310. //! tool to mark the starting point
  311. Tpoint start() noexcept { return mark_ = std::chrono::steady_clock::now(); }
  312. //! tool to mark the ending point
  313. Tpoint stop() noexcept {
  314. Tpoint now = std::chrono::steady_clock::now();
  315. duration_ += dt(now, mark_);
  316. return now;
  317. }
  318. //! A duration calculation utility
  319. static Tduration dt(Tpoint t2, Tpoint t1) noexcept {
  320. return std::chrono::duration_cast<Tduration>(t2 - t1);
  321. }
  322. //! Tool to print the time interval
  323. void print_duration(const char *what, mpi_id_t rank) noexcept {
  324. if (std::chrono::duration_cast<microseconds>(duration_).count() < 10000)
  325. std::cout << "[Timing] (Rank " << rank << ") " << what << ": "
  326. << std::to_string(std::chrono::duration_cast<microseconds>(duration_).count()) << " [usec]\n";
  327. else if (std::chrono::duration_cast<milliseconds>(duration_).count() < 10000)
  328. std::cout << "[Timing] (Rank " << rank << ") " << what << ": "
  329. << std::to_string(std::chrono::duration_cast<milliseconds>(duration_).count()) << " [msec]\n";
  330. else
  331. std::cout << "[Timing] (Rank " << rank << ") " << what << ": "
  332. << std::to_string(std::chrono::duration_cast<seconds>(duration_).count()) << " [sec]\n";
  333. }
  334. private:
  335. Tpoint mark_{};
  336. Tduration duration_{};
  337. };
  338. /*!
  339. * Utility "high level function"-like macro to forward a function call
  340. * and accumulate the execution time to the corresponding timing object.
  341. *
  342. * @param Tim The Timing object [Needs to have methods start() and stop()]
  343. * @param Func The function name
  344. * @param ... The arguments to pass to function (the preprocessor way)
  345. */
  346. #define timeCall(Tim, Func, ...) \
  347. Tim.start(); \
  348. Func(__VA_ARGS__); \
  349. Tim.stop(); \
  350. #endif /* UTILS_HPP_ */