/usr/lib/python2.7/dist-packages/PySPH-1.0a4.dev0-py2.7-linux-x86_64.egg/pysph/base/reduce_array.py is in python-pysph 0~20160514.git91867dc-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | """Functions to reduce array data in serial or parallel.
"""
import numpy as np
from pyzoltan.core.carray import BaseArray
def _check_operation(op):
"""Raise an exception if the wrong operation is given.
"""
valid_ops = ('sum', 'max', 'min', 'prod')
msg = "Unsupported operation %s, must be one of %s."%(op, valid_ops)
if op not in valid_ops:
raise RuntimeError(msg)
def _get_npy_array(array_or_carray):
"""Return a numpy array from given carray or numpy array.
"""
if isinstance(array_or_carray, BaseArray):
return array_or_carray.get_npy_array()
else:
return array_or_carray
def serial_reduce_array(array, op='sum'):
"""Reduce an array given an array and a suitable reduction operation.
Currently, only 'sum', 'max', 'min' and 'prod' are supported.
**Parameters**
- array: numpy.ndarray: Any numpy array (1D).
- op: str: reduction operation, one of ('sum', 'prod', 'min', 'max')
"""
_check_operation(op)
ops = {'sum': np.sum, 'prod': np.prod,
'max': np.max, 'min': np.min}
np_array = _get_npy_array(array)
return ops[op](np_array)
def dummy_reduce_array(array, op='sum'):
"""Simply returns the array for the serial case.
"""
return _get_npy_array(array)
def mpi_reduce_array(array, op='sum'):
"""Reduce an array given an array and a suitable reduction operation.
Currently, only 'sum', 'max', 'min' and 'prod' are supported.
**Parameters**
- array: numpy.ndarray: Any numpy array (1D).
- op: str: reduction operation, one of ('sum', 'prod', 'min', 'max')
"""
np_array = _get_npy_array(array)
from mpi4py import MPI
ops = {'sum': MPI.SUM, 'prod': MPI.PROD,
'max': MPI.MAX, 'min': MPI.MIN}
return MPI.COMM_WORLD.allreduce(np_array, op=ops[op])
# This is just to keep syntax highlighters happy in editors while writing
# equations.
parallel_reduce_array = mpi_reduce_array
|