ipc_debug.cpp 8.4 KB

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