Exemple #1
0
 def create_iter(self, shape=None, backward_broadcast=False):
     if shape is not None and \
             support.product(shape) > support.product(self.get_shape()):
         r = calculate_broadcast_strides(self.get_strides(),
                                         self.get_backstrides(),
                                         self.get_shape(), shape,
                                         backward_broadcast)
         i = ArrayIter(self, support.product(shape), shape, r[0], r[1])
     else:
         i = ArrayIter(self, self.get_size(), self.shape, self.strides,
                       self.backstrides)
     return i, i.reset()
Exemple #2
0
 def create_iter(self, shape=None, backward_broadcast=False):
     if shape is not None and \
             support.product(shape) > support.product(self.get_shape()):
         r = calculate_broadcast_strides(self.get_strides(),
                                         self.get_backstrides(),
                                         self.get_shape(), shape,
                                         backward_broadcast)
         i = ArrayIter(self, support.product(shape), shape, r[0], r[1])
     else:
         i = ArrayIter(self, self.get_size(), self.shape,
                       self.strides, self.backstrides)
     return i, i.reset()
Exemple #3
0
 def test_iterator_goto(self):
     shape = [3, 5]
     strides = [1, 3]
     backstrides = [x * (y - 1) for x, y in zip(strides, shape)]
     assert backstrides == [2, 12]
     a = MockArray(shape, strides, 42)
     i = ArrayIter(a, support.product(shape), shape, strides, backstrides)
     assert not i.contiguous
     s = i.reset()
     assert s.index == 0
     assert s._indices == [0, 0]
     assert s.offset == a.start
     s = i.goto(11)
     assert s.index == 11
     assert s._indices is None
     assert s.offset == a.start + 5
Exemple #4
0
 def test_iterator_goto(self):
     shape = [3, 5]
     strides = [1, 3]
     backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
     assert backstrides == [2, 12]
     a = MockArray(shape, strides, 42)
     i = ArrayIter(a, support.product(shape), shape,
                   strides, backstrides)
     assert not i.contiguous
     s = i.reset()
     assert s.index == 0
     assert s._indices == [0, 0]
     assert s.offset == a.start
     s = i.goto(11)
     assert s.index == 11
     assert s._indices is None
     assert s.offset == a.start + 5
Exemple #5
0
def split_iter(arr, axis_flags):
    """Prepare 2 iterators for nested iteration over `arr`.

    Arguments:
        arr: instance of BaseConcreteArray
        axis_flags: list of bools, one for each dimension of `arr`.The inner
        iterator operates over the dimensions for which the flag is True
    """
    shape = arr.get_shape()
    strides = arr.get_strides()
    backstrides = arr.get_backstrides()
    shapelen = len(shape)
    assert len(axis_flags) == shapelen
    inner_shape = [-1] * shapelen
    inner_strides = [-1] * shapelen
    inner_backstrides = [-1] * shapelen
    outer_shape = [-1] * shapelen
    outer_strides = [-1] * shapelen
    outer_backstrides = [-1] * shapelen
    for i in range(len(shape)):
        if axis_flags[i]:
            inner_shape[i] = shape[i]
            inner_strides[i] = strides[i]
            inner_backstrides[i] = backstrides[i]
            outer_shape[i] = 1
            outer_strides[i] = 0
            outer_backstrides[i] = 0
        else:
            outer_shape[i] = shape[i]
            outer_strides[i] = strides[i]
            outer_backstrides[i] = backstrides[i]
            inner_shape[i] = 1
            inner_strides[i] = 0
            inner_backstrides[i] = 0
    inner_iter = ArrayIter(arr, support.product(inner_shape), inner_shape,
                           inner_strides, inner_backstrides)
    outer_iter = ArrayIter(arr, support.product(outer_shape), outer_shape,
                           outer_strides, outer_backstrides)
    return inner_iter, outer_iter
Exemple #6
0
 def test_one_in_shape(self):
     strides = [16, 4, 8]
     shape = [3, 4, 1]
     backstrides = [x * (y - 1) for x, y in zip(strides, shape)]
     assert backstrides == [32, 12, 0]
     i = ArrayIter(MockArray(shape, strides), support.product(shape), shape,
                   strides, backstrides)
     assert not i.contiguous
     s = i.reset()
     for j in range(3):
         s = i.next(s)
     assert s.offset == 12
     assert not i.done(s)
     assert s._indices == [0, 3, 0]
     while not i.done(s):
         old_indices = s._indices[:]
         old_offset = s.offset
         s = i.next(s)
     assert s.offset == 0
     assert s._indices == [0, 0, 0]
     assert old_indices == [2, 3, 0]
     assert old_offset == 44
Exemple #7
0
 def test_one_in_shape(self):
     strides = [16, 4, 8]
     shape   = [3,  4, 1]
     backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
     assert backstrides == [32, 12, 0]
     i = ArrayIter(MockArray(shape, strides), support.product(shape), shape,
                   strides, backstrides)
     assert not i.contiguous
     s = i.reset()
     for j in range(3):
         s = i.next(s)
     assert s.offset == 12
     assert not i.done(s)
     assert s._indices == [0, 3, 0]
     while not i.done(s):
         old_indices = s._indices[:]
         old_offset = s.offset
         s = i.next(s)
     assert s.offset == 0
     assert s._indices == [0, 0, 0]
     assert old_indices == [2, 3, 0]
     assert old_offset == 44
Exemple #8
0
    def test_iterator_basic(self):
        #Let's get started, simple iteration in C order with
        #contiguous layout => strides[-1] is 1
        shape = [3, 5]
        strides = [5, 1]
        backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
        assert backstrides == [10, 4]
        i = ArrayIter(MockArray(shape, strides), support.product(shape), shape,
                      strides, backstrides)
        assert i.contiguous
        s = i.reset()
        s = i.next(s)
        s = i.next(s)
        s = i.next(s)
        assert s.offset == 3
        assert not i.done(s)
        assert s._indices == [0,0]
        assert i.indices(s) == [0,3]
        #cause a dimension overflow
        s = i.next(s)
        s = i.next(s)
        assert s.offset == 5
        assert s._indices == [0,3]
        assert i.indices(s) == [1,0]

        #Now what happens if the array is transposed? strides[-1] != 1
        # therefore layout is non-contiguous
        strides = [1, 3]
        backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
        assert backstrides == [2, 12]
        i = ArrayIter(MockArray(shape, strides), support.product(shape), shape,
                      strides, backstrides)
        assert not i.contiguous
        s = i.reset()
        s = i.next(s)
        s = i.next(s)
        s = i.next(s)
        assert s.offset == 9
        assert not i.done(s)
        assert s._indices == [0,3]
        #cause a dimension overflow
        s = i.next(s)
        s = i.next(s)
        assert s.offset == 1
        assert s._indices == [1,0]
Exemple #9
0
    def test_iterator_step(self):
        #iteration in C order with #contiguous layout => strides[-1] is 1
        #skip less than the shape
        shape = [3, 5]
        strides = [5, 1]
        backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
        assert backstrides == [10, 4]
        i = ArrayIter(MockArray, support.product(shape), shape,
                      strides, backstrides)
        s = i.reset()
        s = i.next_skip_x(s, 2)
        s = i.next_skip_x(s, 2)
        s = i.next_skip_x(s, 2)
        assert s.offset == 6
        assert not i.done(s)
        assert s.indices == [1,1]
        #And for some big skips
        s = i.next_skip_x(s, 5)
        assert s.offset == 11
        assert s.indices == [2,1]
        s = i.next_skip_x(s, 5)
        # Note: the offset does not overflow but recycles,
        # this is good for broadcast
        assert s.offset == 1
        assert s.indices == [0,1]
        assert i.done(s)

        #Now what happens if the array is transposed? strides[-1] != 1
        # therefore layout is non-contiguous
        strides = [1, 3]
        backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
        assert backstrides == [2, 12]
        i = ArrayIter(MockArray, support.product(shape), shape,
                      strides, backstrides)
        s = i.reset()
        s = i.next_skip_x(s, 2)
        s = i.next_skip_x(s, 2)
        s = i.next_skip_x(s, 2)
        assert s.offset == 4
        assert s.indices == [1,1]
        assert not i.done(s)
        s = i.next_skip_x(s, 5)
        assert s.offset == 5
        assert s.indices == [2,1]
        assert not i.done(s)
        s = i.next_skip_x(s, 5)
        assert s.indices == [0,1]
        assert s.offset == 3
        assert i.done(s)
Exemple #10
0
    def test_iterator_basic(self):
        #Let's get started, simple iteration in C order with
        #contiguous layout => strides[-1] is 1
        shape = [3, 5]
        strides = [5, 1]
        backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
        assert backstrides == [10, 4]
        i = ArrayIter(MockArray(shape, strides), support.product(shape), shape,
                      strides, backstrides)
        assert i.contiguous
        s = i.reset()
        s = i.next(s)
        s = i.next(s)
        s = i.next(s)
        assert s.offset == 3
        assert not i.done(s)
        assert s._indices == [0,0]
        assert i.indices(s) == [0,3]
        #cause a dimension overflow
        s = i.next(s)
        s = i.next(s)
        assert s.offset == 5
        assert s._indices == [0,3]
        assert i.indices(s) == [1,0]

        #Now what happens if the array is transposed? strides[-1] != 1
        # therefore layout is non-contiguous
        strides = [1, 3]
        backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
        assert backstrides == [2, 12]
        i = ArrayIter(MockArray(shape, strides), support.product(shape), shape,
                      strides, backstrides)
        assert not i.contiguous
        s = i.reset()
        s = i.next(s)
        s = i.next(s)
        s = i.next(s)
        assert s.offset == 9
        assert not i.done(s)
        assert s._indices == [0,3]
        #cause a dimension overflow
        s = i.next(s)
        s = i.next(s)
        assert s.offset == 1
        assert s._indices == [1,0]