Ejemplo n.º 1
0
    def test_validate_shape_called(self):
        shape_int = 1
        with mock.patch('mpids.MPInumpy.array_creation._validate_shape'
                        ) as mock_obj_int:
            mpi_np.zeros(shape_int)
        mock_obj_int.assert_called_with(shape_int)

        shape_tuple = (1, 2)
        with mock.patch('mpids.MPInumpy.array_creation._validate_shape'
                        ) as mock_obj_tuple:
            mpi_np.zeros(shape_tuple)
        mock_obj_tuple.assert_called_with(shape_tuple)
Ejemplo n.º 2
0
def _process_observations(observations, comm):
    """ Helper method to distribute provided observations if necessary.

    Returns
    -------
    observations : Block Distributed MPIArray
        Array of cluster centroids generated from provided set of observations.
        Format
    num_features : int
        Number of features in observation vector.
    labels : Block Distributed MPIArray
        Array of centroid indexes that classify a given observation to its
        closest cluster centroid.
    """
    if not isinstance(observations, Block):
        observations = mpi_np.array(observations, comm=comm, dist='b')

    if observations.globalndim > 2:
        raise ValueError('only 1/2-Dimensional observation' +
                         'vector/matrices supported.')

    num_observations = observations.globalshape[0]
    num_features = \
        observations.globalshape[1] if observations.globalndim == 2 else 1

    labels = mpi_np.zeros(num_observations,
                          dtype=np.int64,
                          comm=comm,
                          dist=observations.dist)

    return observations, num_features, labels
Ejemplo n.º 3
0
def __centroids_from_int(k, num_features, observations, comm):
    centroids = mpi_np.zeros((k, num_features),
                             dtype=observations.dtype,
                             comm=comm,
                             dist='r')
    #Pick initial centroids
    num_observations = observations.globalshape[0]
    for j in range(k):
        i = j * (num_observations // k)
        centroids[j] = observations[i]

    return centroids
Ejemplo n.º 4
0
 def test_return_behavior_from_all_ranks_with_tuple_shape(self):
     for root in range(self.size):
         shape = None
         self.assertTrue(shape is None)
         if self.rank == root:
             shape = self.tuple_shape
         mpi_np_zeros = mpi_np.zeros(shape,
                                     comm=self.comm,
                                     root=root,
                                     dist=self.dist)
         self.assertTrue(isinstance(mpi_np_zeros, mpi_np.MPIArray))
         self.assertTrue(isinstance(mpi_np_zeros, self.dist_class))
         self.assertEqual(mpi_np_zeros.comm, self.comm)
         self.assertEqual(mpi_np_zeros.dist, self.dist)
         self.assertTrue(np.alltrue((mpi_np_zeros) == (0)))
Ejemplo n.º 5
0
def _process_centroids(k, num_features, observations, comm):
    """ Helper method to distribute provided k if necessary and resolve whether
        or not the input is seeded.

    Returns
    -------
    centroids : Replicated MPIArray
        Array of cluster centroids generated from provided set of observations.
    num_centroids : int
        Number of centroids.
    temp_centroids : Replicated MPIArray
        Intermediate centroid locations prior to computing distributed result.
    """
    def __unsupported_type(*args):
        raise TypeError('only number of clusters(int) or ' + \
        'centroid seeds(ndarray or MPIArray) should be k.')

    __process_centroid_map = {
        int: __centroids_from_int,
        np.ndarray: __centroids_from_ndarray,
        Block: __centroids_from_mpinp_block,
        Replicated: __centroids_from_mpinp_undist
    }
    centroids = \
        __process_centroid_map.get(type(k), __unsupported_type)(k,
                                                                num_features,
                                                                observations,
                                                                comm)

    num_centroids = centroids.shape[0]
    if num_features != centroids.shape[-1] and centroids.ndim != 1:
        raise ValueError('expected {} '.format(num_features) + \
                         'number of features in seeded cluster centroids.')
    temp_centroids = mpi_np.zeros((num_centroids, num_features),
                                  dtype=observations.dtype,
                                  comm=comm,
                                  dist='r')

    return centroids, num_centroids, temp_centroids
Ejemplo n.º 6
0
 def test_validate_shape_errors_propegated(self):
     with mock.patch('mpids.MPInumpy.array_creation._validate_shape',
                     side_effect=Exception('Mock Execption')) as mock_obj:
         with self.assertRaises(Exception):
             mpi_np.zeros(1)
Ejemplo n.º 7
0
from mpi4py import MPI
import numpy as np

import mpids.MPInumpy as mpi_np

if __name__ == "__main__":

    #Capture default communicator, MPI process rank, and number of MPI processes
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    size = comm.Get_size()

    note = "Note: creation routines are using their default MPI related kwargs."
    note += "\nDefault kwargs:"
    note += " routine(..., comm=MPI.COMM_WORLD, root=0, dist='b')\n"
    print(note) if rank == 0 else None

    #Zeros, shape based distributed array initialized with zeros
    print('From zeros(array_shape) Routine:') if rank == 0 else None
    array_shape = (size, size)
    mpi_zeros_array = mpi_np.zeros(array_shape)
    print('Local Array Result Rank {}:\n{}'.format(rank, mpi_zeros_array))
    print() if rank == 0 else None