From 83e097c0fb828aa504935a065c94f7d9425454c5 Mon Sep 17 00:00:00 2001
From: Mikael Djurfeldt <mikael@djurfeldt.com>
Date: Wed, 15 Jul 2020 17:35:29 +0200
Subject: [PATCH] Add and use runtime.finalize()

---
 pymusic/examples/eventlogger.py   | 2 ++
 pymusic/examples/eventsource.py   | 2 ++
 pymusic/examples/messagelogger.py | 2 ++
 pymusic/examples/messagesource.py | 2 ++
 pymusic/examples/receivers.py     | 2 ++
 pymusic/examples/senders.py       | 2 ++
 pymusic/examples/testsink.py      | 2 ++
 pymusic/examples/testsource.py    | 2 ++
 pymusic/pymusic.pxd               | 1 +
 pymusic/pymusic.pyx               | 9 ++++++++-
 10 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/pymusic/examples/eventlogger.py b/pymusic/examples/eventlogger.py
index eba8716..5b9475f 100755
--- a/pymusic/examples/eventlogger.py
+++ b/pymusic/examples/eventlogger.py
@@ -47,3 +47,5 @@ runtime = music.Runtime(setup, timestep)
 times = takewhile(lambda t: t <= stoptime, runtime)
 for time in times:
     pass
+
+runtime.finalize()
diff --git a/pymusic/examples/eventsource.py b/pymusic/examples/eventsource.py
index 6461bb8..04a9442 100755
--- a/pymusic/examples/eventsource.py
+++ b/pymusic/examples/eventsource.py
@@ -47,3 +47,5 @@ for t in times:
         out.insertEvent(when, index, music.Index.GLOBAL)
 
     nextStep, nextEvents = step()
+
+runtime.finalize()
diff --git a/pymusic/examples/messagelogger.py b/pymusic/examples/messagelogger.py
index cea1146..1c97376 100755
--- a/pymusic/examples/messagelogger.py
+++ b/pymusic/examples/messagelogger.py
@@ -35,3 +35,5 @@ runtime = setup.runtime(timestep)
 times = takewhile(lambda t: t <= stoptime, runtime)
 for time in times:
     pass
+
+runtime.finalize()
diff --git a/pymusic/examples/messagesource.py b/pymusic/examples/messagesource.py
index 442e89f..86db1e7 100755
--- a/pymusic/examples/messagesource.py
+++ b/pymusic/examples/messagesource.py
@@ -36,3 +36,5 @@ for t in times:
         out.insertMessage(when, msg)
 
     nextStep, nextEvents = step()
+
+runtime.finalize()
diff --git a/pymusic/examples/receivers.py b/pymusic/examples/receivers.py
index 2a796bd..a724a60 100755
--- a/pymusic/examples/receivers.py
+++ b/pymusic/examples/receivers.py
@@ -27,3 +27,5 @@ for time in times:
     sys.stdout.write(
         "t={}\treceiver {}: received Hello from sender {}\n".
         format(time, rank, srank))
+
+runtime.finalize()
diff --git a/pymusic/examples/senders.py b/pymusic/examples/senders.py
index 2ea85ea..8efb206 100755
--- a/pymusic/examples/senders.py
+++ b/pymusic/examples/senders.py
@@ -22,3 +22,5 @@ times = takewhile(lambda t: t < stoptime, runtime)
 for time in times:
     data[0] = rank
     sys.stdout.write("t={}\tsender {}: Hello!\n".format(time, rank))
+
+runtime.finalize()
diff --git a/pymusic/examples/testsink.py b/pymusic/examples/testsink.py
index bc9f4e1..0fd312a 100755
--- a/pymusic/examples/testsink.py
+++ b/pymusic/examples/testsink.py
@@ -44,3 +44,5 @@ runtime = music.Runtime(setup, timestep)
 times = takewhile(lambda t: t <= stoptime, runtime)
 for time in times:
     pass
+
+runtime.finalize()
diff --git a/pymusic/examples/testsource.py b/pymusic/examples/testsource.py
index fc80248..d8daf36 100755
--- a/pymusic/examples/testsource.py
+++ b/pymusic/examples/testsource.py
@@ -54,3 +54,5 @@ for t in times:
         out.insertEvent(when, i, index)
 
     nextStep, nextEvents = step()
+
+runtime.finalize()
diff --git a/pymusic/pymusic.pxd b/pymusic/pymusic.pxd
index 92aa5c6..d1d7d68 100644
--- a/pymusic/pymusic.pxd
+++ b/pymusic/pymusic.pxd
@@ -186,6 +186,7 @@ cdef class Runtime(object):
     cdef CRuntime* ptr
     cdef readonly MPI.Intracomm comm
     cdef set ports
+    cdef int isFinalized
 
 ###########################################################
 
diff --git a/pymusic/pymusic.pyx b/pymusic/pymusic.pyx
index 63accac..ac8a98b 100644
--- a/pymusic/pymusic.pyx
+++ b/pymusic/pymusic.pyx
@@ -518,6 +518,7 @@ cdef class Runtime(object):
     def __cinit__(self, Setup setup, double h):
         self.ptr = new CRuntime(setup.ptr, h)
         self.ports = setup.ports
+        self.isFinalized = 0
         setup.null()
 
         cdef MPI.Intracomm comm = MPI.Intracomm()
@@ -525,7 +526,8 @@ cdef class Runtime(object):
         self.comm = comm
 
     def __dealloc__(self):
-        self.ptr.finalize()
+        if not self.isFinalized:
+           self.ptr.finalize()
 
         for p in self.ports:
             p.null()
@@ -533,8 +535,13 @@ cdef class Runtime(object):
         del self.ptr
 
     def time(self): return self.ptr.time()
+    
     def tick(self): tick(self.ptr)
 
+    def finalize(self):
+        self.isFinalized = 1
+        self.ptr.finalize()
+
     def __iter__(self):
         cdef CRuntime* ptr = self.ptr
         while True:
-- 
GitLab