Browse Source

Add checkpoint function and make mpi optional

DEBUG_IPC_CHECKPOINT() allows to assert that root and client are
executing the same source line.

If MPI is not used (and therefore not initialized), the init function
will no longer call MPI_Comm_rank, preventing a crash.
Christoph Stelz 3 months ago
parent
commit
258ae774f1
3 changed files with 25 additions and 3 deletions
  1. 10 0
      include/ipc_debug.h
  2. 12 3
      src/ipc_debug.cpp
  3. 3 0
      src/test.cpp

+ 10 - 0
include/ipc_debug.h

@@ -13,6 +13,8 @@ void debug_ipc_assert_equal(T value);
 extern "C" {
 #endif
 
+#define DEBUG_IPC_CHECKPOINT() debug_ipc_assert_source_location(__FILE__, __LINE__)
+
 
 void debug_ipc_init();
 
@@ -23,6 +25,14 @@ void debug_ipc_assert_equal_int64(int64_t value);
 
 void debug_ipc_assert_equal_array(void *value, size_t size);
 
+/**
+ * Check that root and client are executing the same source line.
+ * Should typically be called like this:
+ * debug_ipc_assert_source_location(__FILE__, __LINE__)
+ */
+void debug_ipc_assert_source_location(const char *source_file, const long int line_number);
+
+
 void debug_ipc_mpi_set_data_distribution(int start_index, uint64_t local_length);
 
 /**

+ 12 - 3
src/ipc_debug.cpp

@@ -13,6 +13,7 @@
 #include <map>
 #include <iostream>
 #include <fstream>
+#include <string>
 
 #include <ipc_debug.h>
 
@@ -32,8 +33,12 @@ constexpr size_t INITIAL_BUFFER_SIZE = 5 * 1024 * 1024; // 5 MiB
 void print_backtrace();
 
 void debug_ipc_init() {
-    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
-    MPI_Comm_size(MPI_COMM_WORLD, &cluster_size);
+    int mpi_initialized;
+    MPI_Initialized(&mpi_initialized);
+    if (mpi_initialized) {
+        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
+        MPI_Comm_size(MPI_COMM_WORLD, &cluster_size);
+    }
 
     if (rank == 0) {
         const char *root_env = std::getenv("IPC_DEBUG_ROOT");
@@ -154,6 +159,7 @@ template void debug_ipc_assert_equal<int32_t>(int32_t);
 template void debug_ipc_assert_equal<uint64_t>(uint64_t);
 template void debug_ipc_assert_equal<int64_t>(int64_t);
 template void debug_ipc_assert_equal<bool>(bool);
+template void debug_ipc_assert_equal<std::string>(std::string);
 
 void debug_ipc_assert_equal_double(double value) {
     debug_ipc_assert_equal<double>(value);
@@ -208,7 +214,10 @@ void debug_ipc_assert_equal_array(void *value, size_t size) {
     }
 }
 
-
+void debug_ipc_assert_source_location(const char *source_file, const long int line_number) {
+    debug_ipc_assert_equal(std::string(source_file));
+    debug_ipc_assert_equal(line_number);
+}
 
 #include <backward.hpp>
 

+ 3 - 0
src/test.cpp

@@ -3,7 +3,10 @@
 #include <cstring>
 
 
+
 int main(int argc, char **argv) {
+    DEBUG_IPC_CHECKPOINT();
+
     if (argc != 2) {
         fprintf(stderr, "Usage: %s <string>\n", argv[0]);
         return -1;