ipc_debug.cpp 8.9 KB

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