ipc_debug.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include <new>
  2. #include <numeric>
  3. #include <unistd.h>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <cstdint>
  8. #include <cassert>
  9. #include <errno.h>
  10. #include <iostream>
  11. #include <vector>
  12. #include <mpi.h>
  13. #include <map>
  14. #include <iostream>
  15. #include <fstream>
  16. #include <string>
  17. #include <ipc_debug.h>
  18. bool debug_ipc_is_root = false;
  19. FILE *debug_ipc_file = nullptr;
  20. FILE *debug_ipc_response_file = nullptr;
  21. int rank, cluster_size;
  22. std::map<uint64_t, int> start_indices_map; // maps indices to MPI ranks
  23. std::vector<int> start_indices_list; // maps ranks to start indices
  24. std::vector<int> recv_counts;
  25. std::vector<char> buffer;
  26. std::vector<double> float_buffer;
  27. constexpr size_t INITIAL_BUFFER_SIZE = 5 * 1024 * 1024; // 5 MiB
  28. void print_backtrace();
  29. void debug_ipc_init() {
  30. int mpi_initialized;
  31. MPI_Initialized(&mpi_initialized);
  32. if (mpi_initialized) {
  33. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  34. MPI_Comm_size(MPI_COMM_WORLD, &cluster_size);
  35. }
  36. if (rank == 0) {
  37. const char *root_env = std::getenv("IPC_DEBUG_ROOT");
  38. const char *file_env = std::getenv("IPC_DEBUG_FILE");
  39. const char *response_file_env = std::getenv("IPC_DEBUG_RESPONSE_FILE");
  40. if (file_env == nullptr) {
  41. return;
  42. }
  43. if (root_env != nullptr && 0 == strcmp(root_env, "1")) {
  44. debug_ipc_is_root = true;
  45. printf("[IPCDBG] I am host\n");
  46. } else {
  47. printf("[IPCDBG] I am client\n");
  48. }
  49. if (debug_ipc_is_root) {
  50. debug_ipc_file = fopen(file_env, "r");
  51. } else {
  52. debug_ipc_file = fopen(file_env, "w");
  53. }
  54. if (debug_ipc_file == nullptr) {
  55. printf("[IPCDBG] Error, could not open named pipe: %s", strerror(errno));
  56. exit(-1);
  57. }
  58. // Open response file for the back channel
  59. if (response_file_env != nullptr) {
  60. if (debug_ipc_is_root) {
  61. debug_ipc_response_file = fopen(response_file_env, "w");
  62. } else {
  63. debug_ipc_response_file = fopen(response_file_env, "r");
  64. }
  65. if (debug_ipc_file == nullptr) {
  66. printf("[IPCDBG] Error, could not open named pipe for responses: %s", strerror(errno));
  67. exit(-1);
  68. }
  69. }
  70. buffer.resize(INITIAL_BUFFER_SIZE);
  71. }
  72. }
  73. /** Communicate assertion result to client and possibly hang if assertion failed
  74. */
  75. inline void handle_assertion(bool assertion_status = true) {
  76. if (!debug_ipc_response_file)
  77. return;
  78. char res = assertion_status ? 1 : 0;
  79. if (debug_ipc_is_root) {
  80. fwrite(&res, 1, 1, debug_ipc_response_file);
  81. } else {
  82. fread(&res, 1, 1, debug_ipc_response_file);
  83. }
  84. if (res == 0) {
  85. std::cout << "[IPCDBG] Entering endless loop, attach debugger to PID " << getpid();
  86. while (1) {
  87. sleep(1);
  88. }
  89. }
  90. }
  91. template<typename T>
  92. void debug_ipc_assert_equal(T value) {
  93. if (debug_ipc_file == nullptr) {
  94. return;
  95. }
  96. const size_t expected_size = sizeof(T);
  97. if (debug_ipc_is_root) {
  98. T other_value;
  99. size_t read = fread(&other_value, expected_size, 1, debug_ipc_file);
  100. if (read != 1) {
  101. printf("[IPCDBG] Could not read enough bytes. Error: %s\n", strerror(errno));
  102. exit(-1);
  103. }
  104. if (other_value != value) {
  105. std::cout << "[IPCDBG] Assertion failed!"
  106. << " Root has " << value << " but client has " << other_value << std::endl;
  107. print_backtrace();
  108. } else {
  109. #ifdef TRACE
  110. std::cout << "[IPCDBG] Assertion passed, value = " << value << std::endl;
  111. #endif
  112. }
  113. handle_assertion(other_value == value);
  114. } else {
  115. size_t written = fwrite(&value, expected_size, 1, debug_ipc_file);
  116. if (written != 1) {
  117. printf("[IPCDBG] Could not write enough bytes. Error: %s\n", strerror(errno));
  118. exit(-1);
  119. }
  120. handle_assertion();
  121. }
  122. }
  123. template<typename T>
  124. void debug_ipc_assert_equal_vector(std::vector<T> value) {
  125. if (debug_ipc_file == nullptr) {
  126. return;
  127. }
  128. debug_ipc_assert_equal(value.size());
  129. const size_t array_byte_length = sizeof(T) * value.size();
  130. if (debug_ipc_is_root) {
  131. buffer.resize(array_byte_length);
  132. size_t read = fread(buffer.data(), 1, array_byte_length, debug_ipc_file);
  133. if (read != array_byte_length) {
  134. printf("[IPCDBG] Could not read enough bytes. Error: %s\n", strerror(errno));
  135. exit(-1);
  136. }
  137. assert(reinterpret_cast<uint64_t>(buffer.data()) % 8 == 0); // Make sure the array is properly aligned
  138. T *local_array = value.data();
  139. T *other_array = reinterpret_cast<T *>(buffer.data());
  140. bool items_equal = true;
  141. for (size_t i = 0; i < value.size(); ++i) {
  142. if (local_array[i] != other_array[i]) {
  143. std::cout << "[IPCDBG] Assertion failed in vector at index " << i
  144. << ". Root has " << local_array[i] << " but client has " << other_array[i] << std::endl;
  145. print_backtrace();
  146. items_equal = false;
  147. break;
  148. }
  149. }
  150. handle_assertion(items_equal);
  151. } else {
  152. fwrite(value.data(), 1, array_byte_length, debug_ipc_file);
  153. handle_assertion();
  154. }
  155. }
  156. // Explicit template instantiation
  157. template void debug_ipc_assert_equal<double>(double);
  158. template void debug_ipc_assert_equal<float>(float);
  159. template void debug_ipc_assert_equal<uint32_t>(uint32_t);
  160. template void debug_ipc_assert_equal<int32_t>(int32_t);
  161. template void debug_ipc_assert_equal<uint64_t>(uint64_t);
  162. template void debug_ipc_assert_equal<int64_t>(int64_t);
  163. template void debug_ipc_assert_equal<bool>(bool);
  164. template void debug_ipc_assert_equal<std::string>(std::string);
  165. void debug_ipc_assert_equal_double(double value) {
  166. debug_ipc_assert_equal<double>(value);
  167. }
  168. void debug_ipc_assert_equal_uint(uint32_t value) {
  169. debug_ipc_assert_equal<uint32_t>(value);
  170. }
  171. void debug_ipc_assert_equal_int(int32_t value) {
  172. debug_ipc_assert_equal<int32_t>(value);
  173. }
  174. void debug_ipc_assert_equal_int64(int64_t value) {
  175. debug_ipc_assert_equal<int64_t>(value);
  176. }
  177. void debug_ipc_assert_equal_array(void *value, size_t size) {
  178. static_assert(sizeof(char) == 1);
  179. if (debug_ipc_file == nullptr) {
  180. return;
  181. }
  182. debug_ipc_assert_equal(size); // Make sure arrays are the same size
  183. char *array = reinterpret_cast<char *>(value);
  184. if (debug_ipc_is_root) {
  185. buffer.resize(size);
  186. char *other_array = buffer.data();
  187. size_t read = fread(other_array, 1, size, debug_ipc_file);
  188. if (read != size) {
  189. printf("[IPCDBG] Could not read enough bytes. Error: %s\n", strerror(errno));
  190. exit(-1);
  191. }
  192. bool items_equal = true;
  193. for (size_t i = 0; i < size; i++) {
  194. if (array[i] != other_array[i]) {
  195. printf("[IPCDBG] Assertion failed in byte %lu!\n", i);
  196. print_backtrace();
  197. items_equal = false;
  198. break;
  199. }
  200. }
  201. handle_assertion(items_equal);
  202. } else {
  203. size_t written = fwrite(value, 1, size, debug_ipc_file);
  204. if (written != size) {
  205. printf("[IPCDBG] Could not write enough bytes. Error: %s\n", strerror(errno));
  206. exit(-1);
  207. }
  208. handle_assertion();
  209. }
  210. }
  211. void debug_ipc_assert_source_location(const char *source_file, const long int line_number) {
  212. std::string fname = source_file;
  213. debug_ipc_assert_equal_array(fname.data(), fname.size());
  214. debug_ipc_assert_equal(line_number);
  215. }
  216. #include <backward.hpp>
  217. void print_backtrace()
  218. {
  219. backward::StackTrace st;
  220. st.load_here(32);
  221. backward::Printer p;
  222. p.print(st);
  223. }
  224. void debug_ipc_mpi_set_data_distribution(int start_index) {
  225. if (rank == 0) {
  226. start_indices_map.clear();
  227. start_indices_list.resize(cluster_size + 1);
  228. }
  229. int my_start_index = start_index;
  230. MPI_Gather(&my_start_index, 1, MPI_INT,
  231. start_indices_list.data(), 1, MPI_INT,
  232. 0, MPI_COMM_WORLD);
  233. if (rank == 0) {
  234. for (int i = 0; i < cluster_size; i++) {
  235. start_indices_map[start_indices_list[i]] = i;
  236. }
  237. }
  238. }
  239. void debug_ipc_assert_equal_mpi_double_array(double *array, size_t array_length) {
  240. std::vector<int> recv_counts(cluster_size);
  241. std::vector<int> displacements(cluster_size);
  242. int local_array_length = static_cast<int>(array_length);
  243. // Gather local lengths
  244. MPI_Gather(&local_array_length, 1, MPI_INT,
  245. recv_counts.data(), 1, MPI_INT,
  246. 0, MPI_COMM_WORLD);
  247. if (rank == 0) {
  248. // Setup displacements and receive counts
  249. int global_index = 0;
  250. // Iterate over start indices from small to large
  251. for (const auto& [start_index, m_rank] : start_indices_map) {
  252. displacements[m_rank] = global_index;
  253. global_index += recv_counts[m_rank];
  254. }
  255. float_buffer.resize(global_index); // Make sure buffer is appropriately sized.
  256. }
  257. MPI_Gatherv(array, array_length, MPI_DOUBLE,
  258. float_buffer.data(), // recvbuf
  259. recv_counts.data(), // recv counts
  260. displacements.data(), // displacements
  261. MPI_DOUBLE, 0, MPI_COMM_WORLD);
  262. if (rank == 0) {
  263. debug_ipc_assert_equal_vector(float_buffer);
  264. }
  265. MPI_Barrier(MPI_COMM_WORLD); // make sure other ranks do not carry on until the root has also left the assertion sub-routine
  266. }