ipc_debug.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. 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<char> buffer;
  25. std::vector<double> float_buffer;
  26. constexpr size_t INITIAL_BUFFER_SIZE = 5 * 1024 * 1024; // 5 MiB
  27. /**
  28. * Additionally to the data necessary to compare between the two processes,
  29. * the client also sends a single OK_FLAG to signal that it can continue
  30. * execution. If the assertion on the root process does not pass, it will enter
  31. * an endless loop, thus causing the write to block on client side as well.
  32. *
  33. * This is useful to make sure the client stops execution as well if the
  34. * assertion does not pass.
  35. */
  36. const char OK_FLAG = 0x42;
  37. void print_backtrace();
  38. void debug_ipc_init() {
  39. int mpi_initialized;
  40. MPI_Initialized(&mpi_initialized);
  41. if (mpi_initialized) {
  42. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  43. MPI_Comm_size(MPI_COMM_WORLD, &cluster_size);
  44. }
  45. if (rank == 0) {
  46. const char *root_env = std::getenv("IPC_DEBUG_ROOT");
  47. const char *file_env = std::getenv("IPC_DEBUG_FILE");
  48. if (file_env == nullptr) {
  49. return;
  50. }
  51. if (root_env != nullptr && 0 == strcmp(root_env, "1")) {
  52. debug_ipc_is_root = true;
  53. printf("[IPCDBG] I am host\n");
  54. } else {
  55. printf("[IPCDBG] I am client\n");
  56. }
  57. if (debug_ipc_is_root) {
  58. debug_ipc_file = fopen(file_env, "rb");
  59. } else {
  60. debug_ipc_file = fopen(file_env, "wb");
  61. }
  62. if (debug_ipc_file == nullptr) {
  63. printf("[IPCDBG] Error, could not open named pipe: %s", strerror(errno));
  64. exit(-1);
  65. }
  66. setvbuf(debug_ipc_file, nullptr, _IONBF, 0);
  67. buffer.resize(INITIAL_BUFFER_SIZE);
  68. }
  69. }
  70. template<typename T>
  71. void debug_ipc_assert_equal(T value) {
  72. if (debug_ipc_file == nullptr) {
  73. return;
  74. }
  75. const size_t expected_size = sizeof(T);
  76. if (debug_ipc_is_root) {
  77. T other_value;
  78. size_t read = fread(&other_value, expected_size, 1, debug_ipc_file);
  79. if (read != 1) {
  80. printf("[IPCDBG] Could not read enough bytes. Error: %s\n", strerror(errno));
  81. exit(-1);
  82. }
  83. if (other_value != value) {
  84. std::cout << "[IPCDBG] Assertion failed!"
  85. << " Root has " << value << " but client has " << other_value;
  86. print_backtrace();
  87. std::cout << "Entering endless loop, attach debugger to PID " << getpid();
  88. fflush(stdout);
  89. while (1) {
  90. sleep(1);
  91. }
  92. } else {
  93. #ifdef TRACE
  94. std::cout << "[IPCDBG] Assertion passed, value = " << value << std::endl;
  95. #endif
  96. }
  97. // Read ok flag so that client can continue
  98. char status; fread(&status, 1, 1, debug_ipc_file); assert(status == OK_FLAG);
  99. } else {
  100. size_t written = fwrite(&value, expected_size, 1, debug_ipc_file);
  101. if (written != 1) {
  102. printf("[IPCDBG] Could not write enough bytes. Error: %s\n", strerror(errno));
  103. exit(-1);
  104. }
  105. fflush(debug_ipc_file);
  106. fwrite(&OK_FLAG, 1, 1, debug_ipc_file);
  107. fflush(debug_ipc_file);
  108. }
  109. }
  110. template<typename T>
  111. void debug_ipc_assert_equal_vector(std::vector<T> value) {
  112. if (debug_ipc_file == nullptr) {
  113. return;
  114. }
  115. debug_ipc_assert_equal(value.size());
  116. const size_t array_byte_length = sizeof(T) * value.size();
  117. if (debug_ipc_is_root) {
  118. buffer.resize(array_byte_length);
  119. size_t read = fread(buffer.data(), 1, array_byte_length, debug_ipc_file);
  120. if (read != array_byte_length) {
  121. printf("[IPCDBG] Could not read enough bytes. Error: %s\n", strerror(errno));
  122. exit(-1);
  123. }
  124. assert(reinterpret_cast<uint64_t>(buffer.data()) % 8 == 0); // Make sure the array is properly aligned
  125. T *local_array = value.data();
  126. T *other_array = reinterpret_cast<T *>(buffer.data());
  127. for (size_t i = 0; i < value.size(); ++i) {
  128. if (local_array[i] != other_array[i]) {
  129. std::cout << "[IPCDBG] Assertion failed in vector at index " << i
  130. << ". Root has " << local_array[i] << " but client has " << other_array[i] << std::endl;
  131. print_backtrace();
  132. printf("Entering endless loop, attach debugger to PID %i \n", getpid());
  133. fflush(stdout);
  134. while (1) {
  135. sleep(1);
  136. }
  137. }
  138. }
  139. // Read ok flag so that client can continue
  140. char status; fread(&status, 1, 1, debug_ipc_file); assert(status == OK_FLAG);
  141. } else {
  142. fwrite(value.data(), 1, array_byte_length, debug_ipc_file);
  143. fflush(debug_ipc_file);
  144. size_t result = fwrite(&OK_FLAG, 1, 1, debug_ipc_file);
  145. printf("Result: %lu\n", result);
  146. }
  147. }
  148. // Explicit template instantiation
  149. template void debug_ipc_assert_equal<double>(double);
  150. template void debug_ipc_assert_equal<float>(float);
  151. template void debug_ipc_assert_equal<uint32_t>(uint32_t);
  152. template void debug_ipc_assert_equal<int32_t>(int32_t);
  153. template void debug_ipc_assert_equal<uint64_t>(uint64_t);
  154. template void debug_ipc_assert_equal<int64_t>(int64_t);
  155. template void debug_ipc_assert_equal<bool>(bool);
  156. template void debug_ipc_assert_equal<std::string>(std::string);
  157. void debug_ipc_assert_equal_double(double value) {
  158. debug_ipc_assert_equal<double>(value);
  159. }
  160. void debug_ipc_assert_equal_uint(uint32_t value) {
  161. debug_ipc_assert_equal<uint32_t>(value);
  162. }
  163. void debug_ipc_assert_equal_int(int32_t value) {
  164. debug_ipc_assert_equal<int32_t>(value);
  165. }
  166. void debug_ipc_assert_equal_int64(int64_t value) {
  167. debug_ipc_assert_equal<int64_t>(value);
  168. }
  169. void debug_ipc_assert_equal_array(void *value, size_t size) {
  170. static_assert(sizeof(char) == 1);
  171. if (debug_ipc_file == nullptr) {
  172. return;
  173. }
  174. debug_ipc_assert_equal(size); // Make sure arrays are the same size
  175. char *array = reinterpret_cast<char *>(value);
  176. if (debug_ipc_is_root) {
  177. buffer.resize(size);
  178. char *other_array = buffer.data();
  179. size_t read = fread(other_array, 1, size, debug_ipc_file);
  180. if (read != size) {
  181. printf("[IPCDBG] Could not read enough bytes. Error: %s\n", strerror(errno));
  182. exit(-1);
  183. }
  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. printf("Entering endless loop, attach debugger to PID %i \n", getpid());
  189. while (1) {
  190. sleep(1);
  191. }
  192. }
  193. }
  194. // Read ok flag so that client can continue
  195. char status; fread(&status, 1, 1, debug_ipc_file); assert(status == OK_FLAG);
  196. } else {
  197. size_t written = fwrite(value, 1, size, debug_ipc_file);
  198. if (written != size) {
  199. printf("[IPCDBG] Could not write enough bytes. Error: %s\n", strerror(errno));
  200. exit(-1);
  201. }
  202. fflush(debug_ipc_file);
  203. fwrite(&OK_FLAG, 1, 1, debug_ipc_file);
  204. }
  205. }
  206. void debug_ipc_assert_source_location(const char *source_file, const long int line_number) {
  207. std::string fname = source_file;
  208. debug_ipc_assert_equal_array(fname.data(), fname.size());
  209. debug_ipc_assert_equal(line_number);
  210. }
  211. #include <backward.hpp>
  212. void print_backtrace()
  213. {
  214. backward::StackTrace st;
  215. st.load_here(32);
  216. backward::Printer p;
  217. p.print(st);
  218. }
  219. void debug_ipc_mpi_set_data_distribution(int start_index) {
  220. if (rank == 0) {
  221. start_indices_map.clear();
  222. start_indices_list.resize(cluster_size + 1);
  223. }
  224. int my_start_index = start_index;
  225. MPI_Gather(&my_start_index, 1, MPI_INT,
  226. start_indices_list.data(), 1, MPI_INT,
  227. 0, MPI_COMM_WORLD);
  228. if (rank == 0) {
  229. for (int i = 0; i < cluster_size; i++) {
  230. start_indices_map[start_indices_list[i]] = i;
  231. }
  232. }
  233. }
  234. void debug_ipc_assert_equal_mpi_double_array(double *array, size_t array_length) {
  235. std::vector<int> recv_counts(cluster_size);
  236. std::vector<int> displacements(cluster_size);
  237. int local_array_length = static_cast<int>(array_length);
  238. // Gather local lengths
  239. MPI_Gather(&local_array_length, 1, MPI_INT,
  240. recv_counts.data(), 1, MPI_INT,
  241. 0, MPI_COMM_WORLD);
  242. if (rank == 0) {
  243. // Setup displacements and receive counts
  244. int global_index = 0;
  245. // Iterate over start indices from small to large
  246. for (const auto& [start_index, m_rank] : start_indices_map) {
  247. displacements[m_rank] = global_index;
  248. global_index += recv_counts[m_rank];
  249. }
  250. float_buffer.resize(global_index); // Make sure buffer is appropriately sized.
  251. }
  252. MPI_Gatherv(array, array_length, MPI_DOUBLE,
  253. float_buffer.data(), // recvbuf
  254. recv_counts.data(), // recv counts
  255. displacements.data(), // displacements
  256. MPI_DOUBLE, 0, MPI_COMM_WORLD);
  257. if (rank == 0) {
  258. debug_ipc_assert_equal_vector(float_buffer);
  259. }
  260. }