Changeset 3095
- Timestamp:
- 04/04/08 15:56:02 (8 months ago)
- Files:
-
- ipythondistarray/trunk/ipythondistarray/__init__.py (modified) (1 diff)
- ipythondistarray/trunk/ipythondistarray/core/construct.py (modified) (1 diff)
- ipythondistarray/trunk/ipythondistarray/core/distarray.py (modified) (12 diffs)
- ipythondistarray/trunk/ipythondistarray/core/tests/test_distarray.py (modified) (2 diffs)
- ipythondistarray/trunk/ipythondistarray/proxy.py (added)
- ipythondistarray/trunk/ipythondistarray/random/__init__.py (modified) (1 diff)
- ipythondistarray/trunk/ipythondistarray/random/nprand.py (moved) (moved from ipythondistarray/trunk/ipythondistarray/random/rand.py) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
ipythondistarray/trunk/ipythondistarray/__init__.py
r3094 r3095 4 4 from ipythondistarray.mpi import * 5 5 from ipythondistarray import random 6 from ipythondistarray.random import rand, randn 6 7 7 8 __all__ = [] 8 9 __all__ += core.__all__ 9 10 __all__ += mpi.__all__ 10 __all__ += [ random.rand, random.randn]11 __all__ += ['rand', 'randn'] ipythondistarray/trunk/ipythondistarray/core/construct.py
r3094 r3095 111 111 def init_local_shape_and_maps(shape, grid_shape, distdims, map_classes): 112 112 maps = [] 113 local_shape = []113 local_shape = list(shape) 114 114 for i, distdim in enumerate(distdims): 115 115 minst = map_classes[i](shape[distdim], grid_shape[i]) 116 local_shape .append(minst.local_shape)116 local_shape[distdim]= minst.local_shape 117 117 maps.append(minst) 118 118 return tuple(local_shape), tuple(maps) ipythondistarray/trunk/ipythondistarray/core/distarray.py
r3094 r3095 4 4 5 5 import sys 6 import math 6 7 7 8 import numpy as np … … 57 58 'mintypecode', 58 59 'finfo', 60 'sum', 59 61 'add', 60 62 'subtract', … … 238 240 raise DistMatrixError("The dist matrix can only be created for a 2d array") 239 241 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 240 264 #---------------------------------------------------------------------------- 241 265 # 3.2 ndarray methods … … 261 285 262 286 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 265 292 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) 268 299 return new_da 269 300 … … 446 477 _raise_nie() 447 478 479 # def sum(self, axis=None, dtype=None, out=None): 448 480 def sum(self, axis=None, dtype=None, out=None): 449 _raise_nie()481 return sum(self, dtype) 450 482 451 483 def cumsum(self, axis=None, dtype=None, out=None): … … 453 485 454 486 def mean(self, axis=None, dtype=None, out=None): 455 _raise_nie()487 return self.sum(dtype=dtype)/self.size 456 488 457 489 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() 459 493 460 494 def std(self, axis=None, dtype=None, out=None): 461 _raise_nie()495 return math.sqrt(self.var()) 462 496 463 497 def prod(self, axis=None, dtype=None, out=None): … … 753 787 754 788 755 def empty(shape, dtype= int, dist={0:'b'}, grid_shape=None, comm=None):789 def empty(shape, dtype=float, dist={0:'b'}, grid_shape=None, comm=None): 756 790 return DistArray(shape, dtype, dist, grid_shape, comm) 757 791 … … 764 798 765 799 766 def zeros(shape, dtype= int, dist={0:'b'}, grid_shape=None, comm=None):800 def zeros(shape, dtype=float, dist={0:'b'}, grid_shape=None, comm=None): 767 801 base_comm = init_base_comm(comm) 768 802 local_shape = find_local_shape(shape, dist, grid_shape, base_comm.Get_size()) … … 778 812 779 813 780 def ones(shape, dtype= int, dist={0:'b'}, grid_shape=None, comm=None):814 def ones(shape, dtype=float, dist={0:'b'}, grid_shape=None, comm=None): 781 815 base_comm = init_base_comm(comm) 782 816 local_shape = find_local_shape(shape, dist, grid_shape, base_comm.Get_size()) … … 785 819 786 820 787 def fromfunction(function, **kwargs):821 def fromfunction(function, shape, **kwargs): 788 822 dtype = kwargs.pop('dtype', int) 789 823 dist = kwargs.pop('dist', {0:'b'}) … … 793 827 local_view = da.local_view() 794 828 for local_inds, x in np.ndenumerate(local_view): 795 global_inds = da.global_ind s(*local_inds)829 global_inds = da.global_ind(da.comm_rank, *local_inds) 796 830 local_view[local_inds] = function(*global_inds, **kwargs) 797 831 return da … … 904 938 #---------------------------------------------------------------------------- 905 939 940 941 def 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 906 945 907 946 def average(a, axis=None, weights=None, returned=0): ipythondistarray/trunk/ipythondistarray/core/tests/test_distarray.py
r3094 r3095 46 46 self.assertEquals(da.maps[0].shape, 16) 47 47 self.assertEquals(da.maps[0].grid_shape, 4) 48 self.assertEquals(da.local_shape, (4, ))48 self.assertEquals(da.local_shape, (4,16)) 49 49 self.assertEquals(da.local_array.shape, da.local_shape) 50 50 self.assertEquals(da.local_array.dtype, da.dtype) … … 147 147 da = distarray.DistArray((4,4),comm=comm) 148 148 except NullCommError: 149 pass 150 else: 149 151 self.assertEquals(da.shape,(4,4)) 150 152 self.assertEquals(da.grid_shape,(4,)) ipythondistarray/trunk/ipythondistarray/random/__init__.py
r3094 r3095 1 from ipythondistarray.random import rand2 from ipythondistarray.random. rand import *1 from ipythondistarray.random import nprand 2 from ipythondistarray.random.nprand import * 3 3 4 4 __all__ = [] 5 __all__ += rand.__all__5 __all__ += nprand.__all__ ipythondistarray/trunk/ipythondistarray/random/nprand.py
r3094 r3095 14 14 'normal', 15 15 'rand', 16 'randint', 16 17 'randn'] 17 18 … … 27 28 return distarray.DistArray(size, local_result.dtype, dist, grid_shape, comm, buf=local_result) 28 29 30 29 31 def normal(loc=0.0, scale=1.0, size=None, dist={0:'b'}, grid_shape=None, comm=None): 30 32 if size is None: … … 36 38 local_result = np.random.normal(loc, scale, size=local_shape) 37 39 return distarray.DistArray(size, local_result.dtype, dist, grid_shape, comm, buf=local_result) 40 38 41 39 42 def rand(size=None, dist={0:'b'}, grid_shape=None, comm=None): … … 47 50 return distarray.DistArray(size, local_result.dtype, dist, grid_shape, comm, buf=local_result) 48 51 52 53 def 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 49 64 def randn(size=None, dist={0:'b'}, grid_shape=None, comm=None): 50 65 if size is None:
