From aa9128daee5062d77cb24b401259cd9de515f0d7 Mon Sep 17 00:00:00 2001
From: Alexis GAMELIN <alexis.gamelin@synchrotron-soleil.fr>
Date: Fri, 6 Dec 2024 10:57:07 +0100
Subject: [PATCH] Fix monitor flush.

Only print version information for one process.
---
 mbtrack2/__init__.py                   | 40 +++++++++++++++++---------
 mbtrack2/tracking/monitors/monitors.py |  3 --
 2 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/mbtrack2/__init__.py b/mbtrack2/__init__.py
index 77657f5..83082c2 100644
--- a/mbtrack2/__init__.py
+++ b/mbtrack2/__init__.py
@@ -32,17 +32,29 @@ except:
 
     dirty = False
 
-print(("mbtrack2 version " + __version__))
-if dirty:
-    print("(dirty git work tree)")
-print(50 * '-')
-print(
-    "If used in a publication, please cite mbtrack2 paper and the zenodo archive for the corresponding code version (and other papers for more specific features)."
-)
-print(
-    "[1] A. Gamelin, W. Foosang, N. Yamamoto, V. Gubaidulin and R. Nagaoka, “mbtrack2”. Zenodo, Jull. 17, 2024. doi: 10.5281/zenodo.12749990."
-)
-print(
-    "[2] A. Gamelin, W. Foosang, and R. Nagaoka, “mbtrack2, a Collective Effect Library in Python”, presented at the 12th Int. Particle Accelerator Conf. (IPAC'21), Campinas, Brazil, May 2021, paper MOPAB070."
-)
-print("\n")
+
+def print_version():
+    print(("mbtrack2 version " + __version__))
+    if dirty:
+        print("(dirty git work tree)")
+    print(50 * '-')
+    print(
+        "If used in a publication, please cite mbtrack2 paper and the zenodo archive for the corresponding code version (and other papers for more specific features)."
+    )
+    print(
+        "[1] A. Gamelin, W. Foosang, N. Yamamoto, V. Gubaidulin and R. Nagaoka, “mbtrack2”. Zenodo, Jull. 17, 2024. doi: 10.5281/zenodo.12749990."
+    )
+    print(
+        "[2] A. Gamelin, W. Foosang, and R. Nagaoka, “mbtrack2, a Collective Effect Library in Python”, presented at the 12th Int. Particle Accelerator Conf. (IPAC'21), Campinas, Brazil, May 2021, paper MOPAB070."
+    )
+    print("\n")
+
+
+try:
+    from mpi4py import MPI
+    comm = MPI.COMM_WORLD
+    rank = comm.Get_rank()
+    if rank == 0:
+        print_version()
+except:
+    print_version()
diff --git a/mbtrack2/tracking/monitors/monitors.py b/mbtrack2/tracking/monitors/monitors.py
index ee16f66..029c21c 100644
--- a/mbtrack2/tracking/monitors/monitors.py
+++ b/mbtrack2/tracking/monitors/monitors.py
@@ -197,7 +197,6 @@ class Monitor(Element, metaclass=ABCMeta):
             self.file[
                 self.group_name][key][slice_tuple] = self.__getattribute__(key)
 
-        self.file.flush()
         self.write_count += 1
 
     def to_buffer(self, object_to_save):
@@ -944,7 +943,6 @@ class WakePotentialMonitor(Monitor):
                          self.buffer_size] = self.__getattribute__("dipole_" +
                                                                    dim)
 
-        self.file.flush()
         self.write_count += 1
 
     def track(self, object_to_save, wake_potential_to_save):
@@ -1304,7 +1302,6 @@ class BunchSpectrumMonitor(Monitor):
                               self.buffer_size:(self.write_count + 1) *
                               self.buffer_size] = self.coherent_o
 
-        self.file.flush()
         self.write_count += 1
 
     def get_incoherent_spectrum(self, positions):
-- 
GitLab