Skip to content

Commit

Permalink
Create PETSc comms
Browse files Browse the repository at this point in the history
Based on trilinos' comms

Addresses usnistgov#644
  • Loading branch information
guyer committed Jul 3, 2019
1 parent 23a9371 commit c9ba707
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 0 deletions.
Empty file.
74 changes: 74 additions & 0 deletions fipy/solvers/petsc/comms/parallelPETScCommWrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python

## -*-Pyth-*-
# ###################################################################
# FiPy - Python-based finite volume PDE solver
#
# FILE: "parallelEpetraCommWrapper.py"
#
# Author: Jonathan Guyer <guyer@nist.gov>
# Author: Daniel Wheeler <daniel.wheeler@nist.gov>
# Author: James Warren <jwarren@nist.gov>
# mail: NIST
# www: http://www.ctcms.nist.gov/fipy/
#
# ========================================================================
# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
# of their official duties. Pursuant to title 17 Section 105 of the
# United States Code this software is not subject to copyright
# protection and is in the public domain. FiPy is an experimental
# system. NIST assumes no responsibility whatsoever for its use by
# other parties, and makes no guarantees, expressed or implied, about
# its quality, reliability, or any other characteristic. We would
# appreciate acknowledgement if the software is used.
#
# This software can be redistributed and/or modified freely
# provided that any derivative works bear some notice that they are
# derived from it, and any modified versions bear some notice that
# they have been modified.
# ========================================================================
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# ###################################################################
##

from PyTrilinos import Epetra
from mpi4py import MPI

from fipy.tools import numerix
from fipy.solvers.trilinos.comms.epetraCommWrapper import EpetraCommWrapper

__all__ = ["ParallelEpetraCommWrapper"]

class ParallelEpetraCommWrapper(EpetraCommWrapper):
"""MPI Communicator wrapper
Encapsulates capabilities needed for both Epetra and mpi4py.
"""

def __init__(self):
self.mpi4py_comm = MPI.COMM_WORLD
super(ParallelEpetraCommWrapper, self).__init__()

def __setstate__(self, dict):
self.__init__()

def all(self, a, axis=None):
return self.mpi4py_comm.allreduce(a.all(axis=axis), op=MPI.LAND)

def any(self, a, axis=None):
return self.mpi4py_comm.allreduce(a.any(axis=axis), op=MPI.LOR)

def allclose(self, a, b, rtol=1.e-5, atol=1.e-8):
return self.mpi4py_comm.allreduce(numerix.allclose(a, b, rtol=rtol, atol=atol), op=MPI.LAND)

def allequal(self, a, b):
return self.mpi4py_comm.allreduce(numerix.allequal(a, b), op=MPI.LAND)

def bcast(self, obj=None, root=0):
return self.mpi4py_comm.bcast(obj=obj, root=root)

def allgather(self, sendobj=None, recvobj=None):
return self.mpi4py_comm.allgather(sendobj=sendobj, recvobj=recvobj)
88 changes: 88 additions & 0 deletions fipy/solvers/petsc/comms/petscCommWrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env python

## -*-Pyth-*-
# ###################################################################
# FiPy - Python-based finite volume PDE solver
#
# FILE: "epetraCommWrapper.py"
#
# Author: Jonathan Guyer <guyer@nist.gov>
# Author: Daniel Wheeler <daniel.wheeler@nist.gov>
# Author: James Warren <jwarren@nist.gov>
# mail: NIST
# www: http://www.ctcms.nist.gov/fipy/
#
# ========================================================================
# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
# of their official duties. Pursuant to title 17 Section 105 of the
# United States Code this software is not subject to copyright
# protection and is in the public domain. FiPy is an experimental
# system. NIST assumes no responsibility whatsoever for its use by
# other parties, and makes no guarantees, expressed or implied, about
# its quality, reliability, or any other characteristic. We would
# appreciate acknowledgement if the software is used.
#
# This software can be redistributed and/or modified freely
# provided that any derivative works bear some notice that they are
# derived from it, and any modified versions bear some notice that
# they have been modified.
# ========================================================================
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# ###################################################################
##

__docformat__ = 'restructuredtext'

from PyTrilinos import Epetra

from fipy.tools import numerix
from fipy.tools.comms.abstractCommWrapper import AbstractCommWrapper

__all__ = ["EpetraCommWrapper"]

class EpetraCommWrapper(AbstractCommWrapper):
"""MPI Communicator wrapper
Encapsulates capabilities needed for Epetra.
Some capabilities are not parallel.
"""

def __init__(self):
self.epetra_comm = Epetra.PyComm()
super(EpetraCommWrapper, self).__init__()

@property
def procID(self):
return self.epetra_comm.MyPID()

@property
def Nproc(self):
return self.epetra_comm.NumProc()

def Barrier(self):
self.epetra_comm.Barrier()

def sum(self, a, axis=None):
summed = numerix.array(a).sum(axis=axis)
shape = summed.shape
if shape == ():
summed = summed.reshape((1,))
parallelSummed = self.epetra_comm.SumAll(summed)
if shape == ():
parallelSummed = parallelSummed.reshape(())
return parallelSummed

def __setstate__(self, dict):
self.__init__()

def Norm2(self, vec):
return vec.Norm2()

def MaxAll(self, vec):
return self.epetra_comm.MaxAll(numerix.array(vec))

def MinAll(self, vec):
return self.epetra_comm.MinAll(numerix.array(vec))
53 changes: 53 additions & 0 deletions fipy/solvers/petsc/comms/serialPETScCommWrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python

## -*-Pyth-*-
# ###################################################################
# FiPy - Python-based finite volume PDE solver
#
# FILE: "serialEpetraCommWrapper.py"
#
# Author: Jonathan Guyer <guyer@nist.gov>
# Author: Daniel Wheeler <daniel.wheeler@nist.gov>
# Author: James Warren <jwarren@nist.gov>
# mail: NIST
# www: http://www.ctcms.nist.gov/fipy/
#
# ========================================================================
# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
# of their official duties. Pursuant to title 17 Section 105 of the
# United States Code this software is not subject to copyright
# protection and is in the public domain. FiPy is an experimental
# system. NIST assumes no responsibility whatsoever for its use by
# other parties, and makes no guarantees, expressed or implied, about
# its quality, reliability, or any other characteristic. We would
# appreciate acknowledgement if the software is used.
#
# This software can be redistributed and/or modified freely
# provided that any derivative works bear some notice that they are
# derived from it, and any modified versions bear some notice that
# they have been modified.
# ========================================================================
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# ###################################################################
##

from fipy.tools import numerix
from fipy.solvers.trilinos.comms.epetraCommWrapper import EpetraCommWrapper

__all__ = ["SerialEpetraCommWrapper"]

class SerialEpetraCommWrapper(EpetraCommWrapper):
@property
def procID(self):
return 0

@property
def Nproc(self):
return 1

def Norm2(self, vec):
return numerix.L2norm(vec)

0 comments on commit c9ba707

Please sign in to comment.