[cig-commits] r13933 - in short/3D/PyLith/branches/pylith-swig: modulesrc/mpi pylith/mpi pylith/topology unittests/pytests/topology
brad at geodynamics.org
brad at geodynamics.org
Fri Jan 23 08:43:29 PST 2009
Author: brad
Date: 2009-01-23 08:43:29 -0800 (Fri, 23 Jan 2009)
New Revision: 13933
Modified:
short/3D/PyLith/branches/pylith-swig/modulesrc/mpi/mpi_comm.i
short/3D/PyLith/branches/pylith-swig/pylith/mpi/Communicator.py
short/3D/PyLith/branches/pylith-swig/pylith/topology/Mesh.py
short/3D/PyLith/branches/pylith-swig/unittests/pytests/topology/TestMesh.py
Log:
Fixed memory management of MPI_Comm* handle in Python Communicator.
Modified: short/3D/PyLith/branches/pylith-swig/modulesrc/mpi/mpi_comm.i
===================================================================
--- short/3D/PyLith/branches/pylith-swig/modulesrc/mpi/mpi_comm.i 2009-01-23 05:51:16 UTC (rev 13932)
+++ short/3D/PyLith/branches/pylith-swig/modulesrc/mpi/mpi_comm.i 2009-01-23 16:43:29 UTC (rev 13933)
@@ -10,12 +10,14 @@
// ======================================================================
//
-
// ----------------------------------------------------------------------
-// destructor
-%typemap(newfree) MPI_Comm* {
- delete $1
-}
+// destroy_comm
+%inline %{
+ void
+ destroy_comm(MPI_Comm* comm) {
+ delete comm;
+ } // destroy_comm
+%}
// ----------------------------------------------------------------------
// PETSC_COMM_WORLD
Modified: short/3D/PyLith/branches/pylith-swig/pylith/mpi/Communicator.py
===================================================================
--- short/3D/PyLith/branches/pylith-swig/pylith/mpi/Communicator.py 2009-01-23 05:51:16 UTC (rev 13932)
+++ short/3D/PyLith/branches/pylith-swig/pylith/mpi/Communicator.py 2009-01-23 16:43:29 UTC (rev 13933)
@@ -15,7 +15,13 @@
## @brief Python MPI communicator object.
##
## Provides SWIG friendly interface to MPI communicator object.
+##
+## The communicator requires special treatment because we do not have
+## a normal SWIG interface definition. SWIG treats the communicator in
+## the same way it treats a pointer to an object, even if it is an
+## integer.
+
import pylith.mpi.mpi as mpimodule
# Communicator class
@@ -26,16 +32,28 @@
# PUBLIC METHODS /////////////////////////////////////////////////////
- def __init__(self, cppComm):
+ def __init__(self, handle):
"""
Constructor.
"""
- self.handle = cppComm
+ # Transfer responsibility of memory management from module to this
+ # class.
+ handle.disown()
+
+ self.handle = handle
self.rank = mpimodule.rank(self.handle)
self.size = mpimodule.size(self.handle)
return
+ def __del__(self):
+ del self.rank
+ del self.size
+ mpimodule.destroy_comm(self.handle)
+ del self.handle
+ return
+
+
def barrier(self):
"""
MPI Barrier.
Modified: short/3D/PyLith/branches/pylith-swig/pylith/topology/Mesh.py
===================================================================
--- short/3D/PyLith/branches/pylith-swig/pylith/topology/Mesh.py 2009-01-23 05:51:16 UTC (rev 13932)
+++ short/3D/PyLith/branches/pylith-swig/pylith/topology/Mesh.py 2009-01-23 16:43:29 UTC (rev 13933)
@@ -39,4 +39,14 @@
return
+ def comm(self):
+ """
+ Get communicator.
+ """
+ # Use Communicator object to wrap C++ MPI_Comm* returned by
+ # module.
+ from pylith.mpi.Communicator import Communicator
+ return Communicator(ModuleMesh.comm(self))
+
+
# End of file
Modified: short/3D/PyLith/branches/pylith-swig/unittests/pytests/topology/TestMesh.py
===================================================================
--- short/3D/PyLith/branches/pylith-swig/unittests/pytests/topology/TestMesh.py 2009-01-23 05:51:16 UTC (rev 13932)
+++ short/3D/PyLith/branches/pylith-swig/unittests/pytests/topology/TestMesh.py 2009-01-23 16:43:29 UTC (rev 13933)
@@ -89,6 +89,18 @@
return
+ def test_comm(self):
+ """
+ Test comm().
+ """
+ from pylith.mpi.Communicator import petsc_comm_world
+ mesh = Mesh(comm=petsc_comm_world())
+ comm = mesh.comm()
+ self.assertEqual(0, comm.rank)
+ self.assertEqual(1, comm.size)
+ return
+
+
def test_initialize(self):
"""
Test initialize().
More information about the CIG-COMMITS
mailing list