ipc_debug.cpp 9.5 KB

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