|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "recorders.h" |
| 4 | +#include <mutex> |
| 5 | +#include <thread> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +namespace celerity::detail { |
| 9 | +/** |
| 10 | + * @brief This class is a wrapper around a 1D vector that allows us to access it as a 2D array. |
| 11 | + * |
| 12 | + * It is used to send the task hashes to other nodes using MPI while keeping the code simple and readable. |
| 13 | + */ |
| 14 | +template <typename T> |
| 15 | +struct mpi_2d_send_wrapper { |
| 16 | + public: |
| 17 | + const T& operator[](std::pair<int, int> ij) const { |
| 18 | + assert(ij.first * m_width + ij.second < m_data.size()); |
| 19 | + return m_data[ij.first * m_width + ij.second]; |
| 20 | + } |
| 21 | + |
| 22 | + T* data() { return m_data.data(); } |
| 23 | + |
| 24 | + mpi_2d_send_wrapper(size_t width, size_t height) : m_data(width * height), m_width(width){}; |
| 25 | + |
| 26 | + private: |
| 27 | + std::vector<T> m_data; |
| 28 | + const size_t m_width; |
| 29 | +}; |
| 30 | + |
| 31 | +/** |
| 32 | + * @brief This class gives a view into a const vector. |
| 33 | + * |
| 34 | + * It is used to give us the currently unhashed task records while keeping track of the offset and width. |
| 35 | + */ |
| 36 | +template <typename T> |
| 37 | +struct window { |
| 38 | + public: |
| 39 | + window(const std::vector<T>& value) : m_value(value) {} |
| 40 | + |
| 41 | + const T& operator[](size_t i) const { |
| 42 | + assert(i >= 0 && i < m_width); |
| 43 | + return m_value[m_offset + i]; |
| 44 | + } |
| 45 | + |
| 46 | + size_t size() { |
| 47 | + m_width = m_value.size() - m_offset; |
| 48 | + return m_width; |
| 49 | + } |
| 50 | + |
| 51 | + void slide(size_t i) { |
| 52 | + assert(i == 0 || (i >= 0 && i <= m_width)); |
| 53 | + m_offset += i; |
| 54 | + m_width -= i; |
| 55 | + } |
| 56 | + |
| 57 | + private: |
| 58 | + const std::vector<T>& m_value; |
| 59 | + size_t m_offset = 0; |
| 60 | + size_t m_width = 0; |
| 61 | +}; |
| 62 | + |
| 63 | +using task_hash = size_t; |
| 64 | +using task_hash_data = mpi_2d_send_wrapper<task_hash>; |
| 65 | +using divergence_map = std::unordered_map<task_hash, std::vector<node_id>>; |
| 66 | + |
| 67 | +/** |
| 68 | + * @brief This class is the base implementation for the divergence check. |
| 69 | + * |
| 70 | + * It is responsible for collecting the task hashes from all nodes and checking for differences -> divergence. |
| 71 | + * When a divergence is found, the task record for the diverging task is printed and the program is terminated. |
| 72 | + * Additionally it also checks for deadlocks and prints a warning if one is detected. |
| 73 | + * |
| 74 | + * The class is abstract to allow a different divergence check implementation in tests |
| 75 | + */ |
| 76 | +class abstract_block_chain { |
| 77 | + friend struct abstract_block_chain_testspy; |
| 78 | + |
| 79 | + public: |
| 80 | + virtual void stop() { m_is_running = false; }; |
| 81 | + |
| 82 | + abstract_block_chain(const abstract_block_chain&) = delete; |
| 83 | + abstract_block_chain& operator=(const abstract_block_chain&) = delete; |
| 84 | + abstract_block_chain& operator=(abstract_block_chain&&) = delete; |
| 85 | + |
| 86 | + abstract_block_chain(abstract_block_chain&&) = default; |
| 87 | + |
| 88 | + abstract_block_chain(size_t num_nodes, node_id local_nid, const std::vector<task_record>& task_recorder, MPI_Comm comm) |
| 89 | + : m_local_nid(local_nid), m_num_nodes(num_nodes), m_sizes(num_nodes), m_task_recorder_window(task_recorder), m_comm(comm) {} |
| 90 | + |
| 91 | + virtual ~abstract_block_chain() = default; |
| 92 | + |
| 93 | + protected: |
| 94 | + void start() { m_is_running = true; }; |
| 95 | + |
| 96 | + virtual void run() = 0; |
| 97 | + |
| 98 | + virtual void divergence_out(const divergence_map& check_map, const int task_num) = 0; |
| 99 | + |
| 100 | + void add_new_hashes(); |
| 101 | + void clear(const int min_progress); |
| 102 | + virtual void allgather_sizes(); |
| 103 | + virtual void allgather_hashes(const int max_size, task_hash_data& data); |
| 104 | + std::pair<int, int> collect_sizes(); |
| 105 | + task_hash_data collect_hashes(const int max_size); |
| 106 | + divergence_map create_check_map(const task_hash_data& task_graphs, const int task_num) const; |
| 107 | + |
| 108 | + void check_for_deadlock() const; |
| 109 | + |
| 110 | + static void print_node_divergences(const divergence_map& check_map, const int task_num); |
| 111 | + |
| 112 | + static void print_task_record(const divergence_map& check_map, const task_record& task, const task_hash hash); |
| 113 | + |
| 114 | + virtual void dedub_print_task_record(const divergence_map& check_map, const int task_num) const; |
| 115 | + |
| 116 | + bool check_for_divergence(); |
| 117 | + |
| 118 | + protected: |
| 119 | + node_id m_local_nid; |
| 120 | + size_t m_num_nodes; |
| 121 | + |
| 122 | + std::vector<task_hash> m_hashes; |
| 123 | + std::vector<int> m_sizes; |
| 124 | + |
| 125 | + bool m_is_running = true; |
| 126 | + |
| 127 | + window<task_record> m_task_recorder_window; |
| 128 | + |
| 129 | + std::chrono::time_point<std::chrono::steady_clock> m_last_cleared = std::chrono::steady_clock::now(); |
| 130 | + |
| 131 | + MPI_Comm m_comm; |
| 132 | +}; |
| 133 | + |
| 134 | +/** |
| 135 | + * @brief This class is the main implementation for the divergence check. |
| 136 | + */ |
| 137 | +class divergence_block_chain : public abstract_block_chain { |
| 138 | + public: |
| 139 | + void start(); |
| 140 | + void stop() override; |
| 141 | + |
| 142 | + divergence_block_chain(size_t num_nodes, node_id local_nid, const std::vector<task_record>& task_record, MPI_Comm comm, bool test_mode = false) |
| 143 | + : abstract_block_chain(num_nodes, local_nid, task_record, comm), m_test_mode(test_mode) { |
| 144 | + divergence_block_chain::start(); |
| 145 | + } |
| 146 | + |
| 147 | + divergence_block_chain(const divergence_block_chain&) = delete; |
| 148 | + divergence_block_chain& operator=(const divergence_block_chain&) = delete; |
| 149 | + divergence_block_chain& operator=(divergence_block_chain&&) = delete; |
| 150 | + |
| 151 | + divergence_block_chain(divergence_block_chain&&) = default; |
| 152 | + |
| 153 | + ~divergence_block_chain() override { divergence_block_chain::stop(); } |
| 154 | + |
| 155 | + private: |
| 156 | + void run() override; |
| 157 | + |
| 158 | + void divergence_out(const divergence_map& check_map, const int task_num) override; |
| 159 | + |
| 160 | + private: |
| 161 | + std::thread m_thread; |
| 162 | + bool m_test_mode = false; |
| 163 | +}; |
| 164 | +}; // namespace celerity::detail |
0 commit comments