Changeset 3017

Show
Ignore:
Timestamp:
02/03/12 22:29:15 (4 months ago)
Author:
thesamovar
Message:

Updated DynamicArray? to use numpy.resize when possible

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/brian/utils/dynamicarray.py

    r3011 r3017  
    2828        The resizing factor (see notes below). Larger values tend to lead to 
    2929        more wasted memory, but more computationally efficient code. 
     30    ``use_numpy_resize``, ``refcheck`` 
     31        Normally, when you resize the array it creates a new array and copies 
     32        the data. Sometimes, it is possible to resize an array without a copy, 
     33        and if this option is set it will attempt to do this. However, this can 
     34        cause memory problems if you are not careful so the option is off by 
     35        default. You need to ensure that you do not create slices of the array 
     36        so that no references to the memory exist other than the main array 
     37        object. If you are sure you know what you're doing, you can switch this 
     38        reference check off. Note that resizing in this way is only done if you 
     39        resize in the first dimension. 
    3040         
    3141    The array is initialised with zeros. The data is stored in the attribute 
     
    105115            minnewshapearr[resizedimensions] = newdims 
    106116            newshapearr = maximum(newshapearr, minnewshapearr) 
    107             if self.use_numpy_resize: 
     117            do_resize = False 
     118            if self.use_numpy_resize and self._data.flags['C_CONTIGUOUS']: 
     119                if sum(resizedimensions)==resizedimensions[0]: 
     120                    do_resize = True 
     121            if do_resize: 
    108122                self.data = None 
    109123                self._data.resize(tuple(newshapearr), refcheck=self.refcheck) 
     
    160174if __name__=='__main__': 
    161175    if 1: 
     176        x = DynamicArray((2, 2), use_numpy_resize=True) 
     177        x[0, 0] = 0 
     178        x[0, 1] = 1 
     179        x[1, 0] = 2 
     180        x[1, 1] = 3 
     181        print x 
     182        x.resize((3, 2)) 
     183        print x 
     184    if 0: 
    162185        import time, gc 
    163186        # speed comparison between numpy resize and not numpy resize