Changeset 3095

Show
Ignore:
Timestamp:
04/04/08 15:56:02 (8 months ago)
Author:
bgranger
Message:

Adding simple routines for plotting the dist_matrix.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ipythondistarray/trunk/ipythondistarray/__init__.py

    r3094 r3095  
    44from ipythondistarray.mpi import * 
    55from ipythondistarray import random 
     6from ipythondistarray.random import rand, randn 
    67 
    78__all__ = [] 
    89__all__ += core.__all__ 
    910__all__ += mpi.__all__ 
    10 __all__ += [random.rand, random.randn
     11__all__ += ['rand', 'randn'
  • ipythondistarray/trunk/ipythondistarray/core/construct.py

    r3094 r3095  
    111111def init_local_shape_and_maps(shape, grid_shape, distdims, map_classes): 
    112112    maps = [] 
    113     local_shape = [] 
     113    local_shape = list(shape) 
    114114    for i, distdim in enumerate(distdims): 
    115115        minst = map_classes[i](shape[distdim], grid_shape[i]) 
    116         local_shape.append(minst.local_shape) 
     116        local_shape[distdim]= minst.local_shape 
    117117        maps.append(minst) 
    118118    return tuple(local_shape), tuple(maps) 
  • ipythondistarray/trunk/ipythondistarray/core/distarray.py

    r3094 r3095  
    44 
    55import sys 
     6import math 
    67 
    78import numpy as np 
     
    5758    'mintypecode', 
    5859    'finfo', 
     60    'sum', 
    5961    'add', 
    6062    'subtract', 
     
    238240            raise DistMatrixError("The dist matrix can only be created for a 2d array")         
    239241     
     242    def plot_dist_matrix(self): 
     243        try: 
     244            dm = self.get_dist_matrix() 
     245        except DistMatrixError: 
     246            pass 
     247        else: 
     248            if self.comm_rank==0: 
     249                try: 
     250                    import pylab 
     251                except ImportError: 
     252                    print "Matplotlib is not installed so the dist_matrix cannot be plotted" 
     253                else: 
     254                    pylab.ion() 
     255                    pylab.matshow(dm) 
     256                    pylab.colorbar() 
     257                    pylab.xlabel('columns') 
     258                    pylab.ylabel('rows') 
     259                    pylab.title('Memory Distribution Plot') 
     260                    pylab.draw()  
     261                    pylab.show() 
     262                     
     263     
    240264    #---------------------------------------------------------------------------- 
    241265    # 3.2 ndarray methods 
     
    261285     
    262286    def local_view(self, dtype=None): 
    263         return self.local_array.view(dtype) 
    264      
     287        if dtype is None: 
     288            return self.local_array.view() 
     289        else: 
     290            return self.local_array.view(dtype) 
     291             
    265292    def view(self, dtype=None): 
    266         new_da = DistArray(self.shape, self.dtype, self.dist, 
    267             self.grid_shape, self.base_comm, buf=self.data) 
     293        if dtype is None: 
     294            new_da = DistArray(self.shape, self.dtype, self.dist, 
     295                self.grid_shape, self.base_comm, buf=self.data) 
     296        else: 
     297            new_da = DistArray(self.shape, dtype, self.dist, 
     298                self.grid_shape, self.base_comm, buf=self.data) 
    268299        return new_da 
    269300     
     
    446477        _raise_nie() 
    447478     
     479    # def sum(self, axis=None, dtype=None, out=None):     
    448480    def sum(self, axis=None, dtype=None, out=None): 
    449         _raise_nie(
     481        return sum(self, dtype
    450482     
    451483    def cumsum(self, axis=None, dtype=None, out=None):         
     
    453485     
    454486    def mean(self, axis=None, dtype=None, out=None): 
    455         _raise_nie() 
     487        return self.sum(dtype=dtype)/self.size 
    456488     
    457489    def var(self, axis=None, dtype=None, out=None): 
    458         _raise_nie() 
     490        mu = self.mean() 
     491        temp = (self - mu)**2 
     492        return temp.mean() 
    459493      
    460494    def std(self, axis=None, dtype=None, out=None): 
    461         _raise_nie(
     495        return math.sqrt(self.var()
    462496     
    463497    def prod(self, axis=None, dtype=None, out=None): 
     
    753787 
    754788 
    755 def empty(shape, dtype=int, dist={0:'b'}, grid_shape=None, comm=None): 
     789def empty(shape, dtype=float, dist={0:'b'}, grid_shape=None, comm=None): 
    756790    return DistArray(shape, dtype, dist, grid_shape, comm) 
    757791 
     
    764798 
    765799 
    766 def zeros(shape, dtype=int, dist={0:'b'}, grid_shape=None, comm=None): 
     800def zeros(shape, dtype=float, dist={0:'b'}, grid_shape=None, comm=None): 
    767801    base_comm = init_base_comm(comm) 
    768802    local_shape = find_local_shape(shape, dist, grid_shape, base_comm.Get_size()) 
     
    778812 
    779813 
    780 def ones(shape, dtype=int, dist={0:'b'}, grid_shape=None, comm=None): 
     814def ones(shape, dtype=float, dist={0:'b'}, grid_shape=None, comm=None): 
    781815    base_comm = init_base_comm(comm) 
    782816    local_shape = find_local_shape(shape, dist, grid_shape, base_comm.Get_size()) 
     
    785819 
    786820 
    787 def fromfunction(function, **kwargs): 
     821def fromfunction(function, shape, **kwargs): 
    788822    dtype = kwargs.pop('dtype', int) 
    789823    dist = kwargs.pop('dist', {0:'b'}) 
     
    793827    local_view = da.local_view() 
    794828    for local_inds, x in np.ndenumerate(local_view): 
    795         global_inds = da.global_inds(*local_inds) 
     829        global_inds = da.global_ind(da.comm_rank, *local_inds) 
    796830        local_view[local_inds] = function(*global_inds, **kwargs) 
    797831    return da 
     
    904938#---------------------------------------------------------------------------- 
    905939 
     940 
     941def sum(a, dtype=None): 
     942    local_sum = a.local_array.sum(dtype) 
     943    global_sum = a.comm.Allreduce(local_sum, op=MPI.SUM) 
     944    return global_sum 
    906945 
    907946def average(a, axis=None, weights=None, returned=0): 
  • ipythondistarray/trunk/ipythondistarray/core/tests/test_distarray.py

    r3094 r3095  
    4646                self.assertEquals(da.maps[0].shape, 16) 
    4747                self.assertEquals(da.maps[0].grid_shape, 4) 
    48                 self.assertEquals(da.local_shape, (4,)) 
     48                self.assertEquals(da.local_shape, (4,16)) 
    4949                self.assertEquals(da.local_array.shape, da.local_shape) 
    5050                self.assertEquals(da.local_array.dtype, da.dtype) 
     
    147147                da = distarray.DistArray((4,4),comm=comm) 
    148148            except NullCommError: 
     149                pass 
     150            else: 
    149151                self.assertEquals(da.shape,(4,4)) 
    150152                self.assertEquals(da.grid_shape,(4,)) 
  • ipythondistarray/trunk/ipythondistarray/random/__init__.py

    r3094 r3095  
    1 from ipythondistarray.random import rand 
    2 from ipythondistarray.random.rand import * 
     1from ipythondistarray.random import nprand 
     2from ipythondistarray.random.nprand import * 
    33 
    44__all__ = [] 
    5 __all__ += rand.__all__ 
     5__all__ += nprand.__all__ 
  • ipythondistarray/trunk/ipythondistarray/random/nprand.py

    r3094 r3095  
    1414    'normal', 
    1515    'rand', 
     16    'randint', 
    1617    'randn'] 
    1718 
     
    2728        return distarray.DistArray(size, local_result.dtype, dist, grid_shape, comm, buf=local_result) 
    2829 
     30 
    2931def normal(loc=0.0, scale=1.0, size=None, dist={0:'b'}, grid_shape=None, comm=None): 
    3032    if size is None: 
     
    3638        local_result = np.random.normal(loc, scale, size=local_shape) 
    3739        return distarray.DistArray(size, local_result.dtype, dist, grid_shape, comm, buf=local_result) 
     40 
    3841 
    3942def rand(size=None, dist={0:'b'}, grid_shape=None, comm=None): 
     
    4750        return distarray.DistArray(size, local_result.dtype, dist, grid_shape, comm, buf=local_result) 
    4851 
     52 
     53def randint(low, high=None, size=None, dist={0:'b'}, grid_shape=None, comm=None): 
     54    if size is None: 
     55        return np.random.randint(low, high) 
     56    else: 
     57        base_comm = init_base_comm(comm) 
     58        comm_size = base_comm.Get_size() 
     59        local_shape = find_local_shape(size, dist=dist, grid_shape=grid_shape, comm_size=comm_size) 
     60        local_result = np.random.randint(low, high, size=local_shape) 
     61        return distarray.DistArray(size, local_result.dtype, dist, grid_shape, comm, buf=local_result) 
     62 
     63 
    4964def randn(size=None, dist={0:'b'}, grid_shape=None, comm=None): 
    5065    if size is None: