ipc_debug.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. void print_backtrace();
  28. void debug_ipc_init() {
  29. int mpi_initialized;
  30. MPI_Initialized(&mpi_initialized);
  31. if (mpi_initialized) {
  32. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  33. MPI_Comm_size(MPI_COMM_WORLD, &cluster_size);
  34. }
  35. if (rank == 0) {
  36. const char *root_env = std::getenv("IPC_DEBUG_ROOT");
  37. const char *file_env = std::getenv("IPC_DEBUG_FILE");
  38. if (file_env == nullptr) {
  39. return;
  40. }
  41. if (root_env != nullptr && 0 == strcmp(root_env, "1")) {
  42. debug_ipc_is_root = true;
  43. printf("[IPCDBG] I am host\n");
  44. } else {
  45. printf("[IPCDBG] I am client\n");
  46. }
  47. if (debug_ipc_is_root) {
  48. debug_ipc_file = fopen(file_env, "r");
  49. } else {
  50. debug_ipc_file = fopen(file_env, "w");
  51. }
  52. if (debug_ipc_file == nullptr) {
  53. printf("[IPCDBG] Error, could not open named pipe: %s", strerror(errno));
  54. exit(-1);
  55. }
  56. buffer.resize(INITIAL_BUFFER_SIZE);
  57. }
  58. }
  59. template<typename T>
  60. void debug_ipc_assert_equal(T value) {
  61. if (debug_ipc_file == nullptr) {
  62. return;
  63. }
  64. const size_t expected_size = sizeof(T);
  65. if (debug_ipc_is_root) {
  66. T other_value;
  67. size_t read = fread(&other_value, expected_size, 1, debug_ipc_file);
  68. if (read != 1) {
  69. printf("[IPCDBG] Could not read enough bytes. Error: %s\n", strerror(errno));
  70. exit(-1);
  71. }
  72. if (other_value != value) {
  73. std::cout << "[IPCDBG] Assertion failed!"
  74. << " Master has " << value << " but client has " << other_value;
  75. print_backtrace();
  76. std::cout << "Entering endless loop, attach debugger to PID " << getpid();
  77. fflush(stdout);
  78. while (1) {
  79. sleep(1);
  80. }
  81. } else {
  82. #ifdef TRACE
  83. std::cout << "[IPCDBG] Assertion passed, value = " << value << std::endl;
  84. #endif
  85. }
  86. } else {
  87. size_t written = fwrite(&value, expected_size, 1, debug_ipc_file);
  88. if (written != 1) {
  89. printf("[IPCDBG] Could not write enough bytes. Error: %s\n", strerror(errno));
  90. exit(-1);
  91. }
  92. }
  93. }
  94. template<typename T>
  95. void debug_ipc_assert_equal_vector(std::vector<T> value) {
  96. if (debug_ipc_file == nullptr) {
  97. return;
  98. }
  99. debug_ipc_assert_equal(value.size());
  100. const size_t array_byte_length = sizeof(T) * value.size();
  101. if (debug_ipc_is_root) {
  102. buffer.resize(array_byte_length);
  103. size_t read = fread(buffer.data(), 1, array_byte_length, debug_ipc_file);
  104. if (read != array_byte_length) {
  105. printf("[IPCDBG] Could not read enough bytes. Error: %s\n", strerror(errno));
  106. exit(-1);
  107. }
  108. assert(reinterpret_cast<uint64_t>(buffer.data()) % 8 == 0); // Make sure the array is properly aligned
  109. T *local_array = value.data();
  110. T *other_array = reinterpret_cast<T *>(buffer.data());
  111. for (size_t i = 0; i < value.size(); ++i) {
  112. if (local_array[i] != other_array[i]) {
  113. std::cout << "[IPCDBG] Assertion failed in vector at index " << i
  114. << ". Root has " << local_array[i] << " but client has " << other_array[i] << std::endl;
  115. print_backtrace();
  116. printf("Entering endless loop, attach debugger to PID %i \n", getpid());
  117. fflush(stdout);
  118. while (1) {
  119. sleep(1);
  120. }
  121. }
  122. }
  123. } else {
  124. fwrite(value.data(), 1, array_byte_length, debug_ipc_file);
  125. }
  126. }
  127. // Explicit template instantiation
  128. template void debug_ipc_assert_equal<double>(double);
  129. template void debug_ipc_assert_equal<float>(float);
  130. template void debug_ipc_assert_equal<uint32_t>(uint32_t);
  131. template void debug_ipc_assert_equal<int32_t>(int32_t);
  132. template void debug_ipc_assert_equal<uint64_t>(uint64_t);
  133. template void debug_ipc_assert_equal<int64_t>(int64_t);
  134. template void debug_ipc_assert_equal<bool>(bool);
  135. template void debug_ipc_assert_equal<std::string>(std::string);
  136. void debug_ipc_assert_equal_double(double value) {
  137. debug_ipc_assert_equal<double>(value);
  138. }
  139. void debug_ipc_assert_equal_uint(uint32_t value) {
  140. debug_ipc_assert_equal<uint32_t>(value);
  141. }
  142. void debug_ipc_assert_equal_int(int32_t value) {
  143. debug_ipc_assert_equal<int32_t>(value);
  144. }
  145. void debug_ipc_assert_equal_int64(int64_t value) {
  146. debug_ipc_assert_equal<int64_t>(value);
  147. }
  148. void debug_ipc_assert_equal_array(void *value, size_t size) {
  149. static_assert(sizeof(char) == 1);
  150. if (debug_ipc_file == nullptr) {
  151. return;
  152. }
  153. debug_ipc_assert_equal(size); // Make sure arrays are the same size
  154. char *array = reinterpret_cast<char *>(value);
  155. if (debug_ipc_is_root) {
  156. buffer.resize(size);
  157. char *other_array = buffer.data();
  158. size_t read = fread(other_array, 1, size, debug_ipc_file);
  159. if (read != size) {
  160. printf("[IPCDBG] Could not read enough bytes. Error: %s\n", strerror(errno));
  161. exit(-1);
  162. }
  163. for (size_t i = 0; i < size; i++) {
  164. if (array[i] != other_array[i]) {
  165. printf("[IPCDBG] Assertion failed in byte %lu!\n", i);
  166. print_backtrace();
  167. printf("Entering endless loop, attach debugger to PID %i \n", getpid());
  168. while (1) {
  169. sleep(1);
  170. }
  171. }
  172. }
  173. } else {
  174. size_t written = fwrite(value, 1, size, debug_ipc_file);
  175. if (written != size) {
  176. printf("[IPCDBG] Could not write enough bytes. Error: %s\n", strerror(errno));
  177. exit(-1);
  178. }
  179. }
  180. }
  181. void debug_ipc_assert_source_location(const char *source_file, const long int line_number) {
  182. std::string fname = source_file;
  183. debug_ipc_assert_equal_array(fname.data(), fname.size());
  184. debug_ipc_assert_equal(line_number);
  185. }
  186. #include <backward.hpp>
  187. void print_backtrace()
  188. {
  189. backward::StackTrace st;
  190. st.load_here(32);
  191. backward::Printer p;
  192. p.print(st);
  193. }
  194. void debug_ipc_mpi_set_data_distribution(int start_index, uint64_t local_length) {
  195. if (rank == 0) {
  196. start_indices_map.clear();
  197. start_indices_list.resize(cluster_size + 1);
  198. recv_counts.resize(cluster_size);
  199. }
  200. unsigned long total_length = 0UL;
  201. unsigned long length = local_length;
  202. MPI_Reduce(&length, &total_length, 1, MPI_UNSIGNED_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
  203. MPI_Gather(&start_index, 1, MPI_INT,
  204. start_indices_list.data(), 1, MPI_INT, 0,
  205. MPI_COMM_WORLD);
  206. if (rank != 0) {
  207. // Only root needs to now the sendcounts and displacements
  208. return;
  209. }
  210. start_indices_map[total_length] = -1; // sentinel element
  211. start_indices_list[cluster_size] = total_length;
  212. for (int i = 0; i < cluster_size; i++) {
  213. start_indices_map[start_indices_list[i]] = i;
  214. }
  215. // Calculate recv_counts
  216. for (const auto& [start_index, m_rank] : start_indices_map) {
  217. // Exit loop on sentinel element
  218. if (m_rank == -1) {
  219. break;
  220. }
  221. const uint64_t next_index = start_indices_map.upper_bound(start_index)->first;
  222. const uint64_t segment_length = next_index - start_index;
  223. recv_counts[m_rank] = segment_length;
  224. printf("Recv counts [%i] = %lu\n", m_rank, segment_length);
  225. }
  226. uint64_t total_recvs = std::accumulate(recv_counts.begin(), recv_counts.end(), 0);
  227. printf("total recv = %lu, total length = %lu\n", total_recvs, total_length);
  228. assert(total_length == total_recvs);
  229. debug_ipc_assert_equal<uint64_t>(total_length);
  230. }
  231. void debug_ipc_assert_equal_mpi_double_array(double *array, size_t array_length, int span) {
  232. if (rank == 0) {
  233. std::vector<int> actual_recv_counts(recv_counts);
  234. std::vector<int> displacements(recv_counts);
  235. size_t total_length = span * start_indices_list[cluster_size];
  236. float_buffer.resize(total_length); // Make sure we have enough space for all values
  237. assert(actual_recv_counts.size() == static_cast<unsigned>(cluster_size));
  238. assert(start_indices_map.size() == static_cast<unsigned>(cluster_size + 1));
  239. for (unsigned int i = 0; i < recv_counts.size(); i++) {
  240. actual_recv_counts[i] *= span;
  241. }
  242. for (unsigned int i = 0; i < recv_counts.size(); i++) {
  243. displacements[i] *= span;
  244. }
  245. MPI_Gatherv(array, array_length * span, MPI_DOUBLE,
  246. float_buffer.data(), // recvbuf
  247. actual_recv_counts.data(), // recv counts
  248. start_indices_list.data(), // displacements
  249. MPI_DOUBLE, 0, MPI_COMM_WORLD);
  250. debug_ipc_assert_equal_vector(float_buffer);
  251. } else {
  252. // Gather all data at the root.
  253. MPI_Gatherv(array, array_length * span, MPI_DOUBLE,
  254. nullptr, // recvbuf
  255. nullptr, // recv counts
  256. nullptr, // displacements
  257. MPI_DOUBLE, 0, MPI_COMM_WORLD);
  258. }
  259. }