From 563efddeb5349541c29a91484cafd083f550c755 Mon Sep 17 00:00:00 2001
From: Rafael Ravedutti <rafaelravedutti@gmail.com>
Date: Tue, 9 Jan 2024 12:52:05 +0100
Subject: [PATCH] Print stats on number of local/ghost particles

Signed-off-by: Rafael Ravedutti <rafaelravedutti@gmail.com>
---
 runtime/stats.hpp          | 33 +++++++++++++++++++++++++++++++++
 src/pairs/code_gen/cgen.py |  2 ++
 2 files changed, 35 insertions(+)
 create mode 100644 runtime/stats.hpp

diff --git a/runtime/stats.hpp b/runtime/stats.hpp
new file mode 100644
index 0000000..413ffab
--- /dev/null
+++ b/runtime/stats.hpp
@@ -0,0 +1,33 @@
+#include "pairs.hpp"
+
+#pragma once
+
+using namespace std;
+
+namespace pairs {
+
+void print_stats(PairsSimulation *ps, int nlocal, int nghost) {
+    int min_nlocal = nlocal;
+    int max_nlocal = nlocal;
+    int min_nghost = nghost;
+    int max_nghost = nghost;
+    int nglobal;
+
+    if(ps->getDomainPartitioner()->getWorldSize() > 1) {
+        MPI_Allreduce(&nlocal, &nglobal, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
+        min_nlocal = nglobal;
+        MPI_Allreduce(&nlocal, &nglobal, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
+        max_nlocal = nglobal;
+        MPI_Allreduce(&nghost, &nglobal, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
+        min_nghost = nglobal;
+        MPI_Allreduce(&nghost, &nglobal, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
+        max_nghost = nglobal;
+    }
+
+    if(ps->getDomainPartitioner()->getRank() == 0) {
+        std::cout << "Number of local particles: " << min_nlocal << " / " << max_nlocal << std::endl;
+        std::cout << "Number of ghost particles: " << min_nghost << " / " << max_nghost << std::endl;
+    }
+}
+
+}
diff --git a/src/pairs/code_gen/cgen.py b/src/pairs/code_gen/cgen.py
index 03c5299..a053d94 100644
--- a/src/pairs/code_gen/cgen.py
+++ b/src/pairs/code_gen/cgen.py
@@ -77,6 +77,7 @@ class CGen:
         self.print("#include \"runtime/dem_sc_grid.hpp\"")
         self.print("#include \"runtime/pairs.hpp\"")
         self.print("#include \"runtime/read_from_file.hpp\"")
+        self.print("#include \"runtime/stats.hpp\"")
         self.print("#include \"runtime/timing.hpp\"")
         self.print("#include \"runtime/thermo.hpp\"")
         self.print("#include \"runtime/vtk.hpp\"")
@@ -135,6 +136,7 @@ class CGen:
                 self.print("    LIKWID_MARKER_CLOSE;")
 
             self.print("    pairs::print_timers(pairs);")
+            self.print("    pairs::print_stats(pairs, nlocal, nghost);")
             self.print("    delete pairs;")
             self.print("    return 0;")
             self.print("}")
-- 
GitLab