Commits

Jacob Hinkle committed 20001c4

Make Parallel.py fail gracefully when missing mpi4py

Comments (0)

Files changed (1)

Code/Python/Parallel/PyCAParallel.py

 
 import numpy as np
 
-from mpi4py import MPI
+try:
+   from mpi4py import MPI
+   hasMPI = True
+except:
+   hasMPI = False
 
-def Reduce(A, hA, op=MPI.SUM):
+def Reduce(A, hA, op=None):
     """Reduce PyCA Image3D or Field3D over MPI
 
     A can live anywhere but hA needs to be of mType MEM_HOST
 
     """
+    if not hasMPI:
+      raise Exception("mpi4py required for Reduce operations: not found")
+
+    if op is None:
+      op = MPI.SUM
+
     if A.memType() == ca.MEM_HOST:
         # can do this in place without using hA
         if isinstance(A, ca.Image3D):
         ca.Copy(A, hA)  # upload
 
 
-def ReduceFloat(f, op=MPI.SUM):
+def ReduceFloat(f, op=None):
     """Reduce a single float value over MPI"""
+    if not hasMPI:
+      raise Exception("mpi4py required for Reduce operations: not found")
+
+    if op is None:
+      op = MPI.SUM
+
     fa = np.array([f])  # can only reduce over numpy arrays
     MPI.COMM_WORLD.Allreduce(MPI.IN_PLACE,
                              fa,