From 9d0c2875a6710806e35dada1ae4771252d673c2c Mon Sep 17 00:00:00 2001
From: Robert Izzard <r.izzard@surrey.ac.uk>
Date: Sat, 23 Oct 2021 10:27:01 +0100
Subject: [PATCH] add max memory use to end-of-run data that's output to the
 screen

---
 binarycpython/utils/grid.py | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py
index 7df7d8785..94d06289d 100644
--- a/binarycpython/utils/grid.py
+++ b/binarycpython/utils/grid.py
@@ -979,11 +979,14 @@ class Population:
             "i", [0] * self.grid_options["n_logging_stats"]
         )
 
-        # array to store memory use per-thread
+        # arrays to store memory and max memory use per-thread
         mem = 1.0 * mem_use()
         self.shared_memory["memory_use_per_thread"] = multiprocessing.Array(
             "d", [mem] * self.grid_options["num_cores"]
         )
+        self.shared_memory["max_memory_use_per_thread"] = multiprocessing.Array(
+            "d", [mem] * self.grid_options["num_cores"]
+        )
 
     def clean(self) -> None:
         """
@@ -1108,13 +1111,15 @@ class Population:
 
         # Log and print some information
         dtsecs = self.grid_options["_end_time_evolution"] - self.grid_options["_start_time_evolution"]
-        string1 = "Population-{} finished!\nThe total probability is {:g}.".format(self.grid_options["_population_id"],
-                                                                                  self.grid_options["_probtot"],)
-        string2 = "It took a total of {} to run {} systems on {} cores\n = {} of CPU time".format(
-            timedelta(dtsecs),
-            self.grid_options["_total_starcount"],
-            self.grid_options["num_cores"],
-            timedelta(dtsecs * self.grid_options["num_cores"])
+        string1 = "Population-{} finished!\nThe total probability is {:g}.".format(
+            self.grid_options["_population_id"],
+            self.grid_options["_probtot"])
+        string2 = "It took a total of {dtsecs} to run {starcount} systems on {ncores} cores\n = {totaldtsecs} of CPU time.\nMaximum memory use {memuse:.3f} MB".format(
+            dtsecs=timedelta(dtsecs),
+            starcount=self.grid_options["_total_starcount"],
+            ncores=self.grid_options["num_cores"],
+            totaldtsecs=timedelta(dtsecs * self.grid_options["num_cores"]),
+            memuse=sum(self.shared_memory["max_memory_use_per_thread"])
         )
         verbose_print(self._boxed(string1,string2),
                       self.grid_options["verbosity"],
@@ -1562,8 +1567,11 @@ class Population:
 
             # update memory use stats every log_dt seconds (not every time, this is likely a bit expensive)
             if now > next_mem_update_time:
-                self.shared_memory["memory_use_per_thread"][ID] = mem_use()
+                m = mem_use()
+                self.shared_memory["memory_use_per_thread"][ID] = m
                 next_mem_update_time = now + self.grid_options["log_dt"]
+                if m > self.shared_memory["max_memory_use_per_thread"][ID]:
+                    self.shared_memory["max_memory_use_per_thread"][ID] = m
 
             # calculate the next logging time
             next_log_time = (
@@ -2049,7 +2057,7 @@ class Population:
                 verbose_print(
                     self._boxed(
                         "Total starcount for this run is {starcount}".format(starcount=self.grid_options["_total_starcount"]),
-                        "Total probability is {probtot}".format(probtot=self.grid_options["_probtot"])
+                        "Total probability is {probtot:g}".format(probtot=self.grid_options["_probtot"])
                     ),
                     self.grid_options["verbosity"],
                     0)
@@ -2706,7 +2714,7 @@ class Population:
         self._increment_indent_depth(+1)
         self._add_code("\n", "#" * 40 + "\n", "if print_results:\n")
         self._add_code(
-            "print('Grid has handled {} stars with a total probability of {}'.format(_total_starcount,self.grid_options['_probtot']))\n",
+            "print('Grid has handled {} stars with a total probability of {:g}'.format(_total_starcount,self.grid_options['_probtot']))\n",
             indent=1,
         )
 
-- 
GitLab