| 1 |
""" This is a mini-tutorial on iterators, strides, and |
| 2 |
memory layout. It assumes you are familiar with the terms, see |
| 3 |
http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html |
| 4 |
for a more gentle introduction. |
| 5 |
|
| 6 |
Given an array x: x.shape == [5,6], where each element occupies one byte |
| 7 |
|
| 8 |
At which byte in x.data does the item x[3,4] begin? |
| 9 |
if x.strides==[1,5]: |
| 10 |
pData = x.pData + (x.start + 3*1 + 4*5)*sizeof(x.pData[0]) |
| 11 |
pData = x.pData + (x.start + 23) * sizeof(x.pData[0]) |
| 12 |
so the offset of the element is 23 elements after the first |
| 13 |
|
| 14 |
What is the next element in x after coordinates [3,4]? |
| 15 |
if x.order =='C': |
| 16 |
next == [3,5] => offset is 28 |
| 17 |
if x.order =='F': |
| 18 |
next == [4,4] => offset is 24 |
| 19 |
so for the strides [1,5] x is 'F' contiguous |
| 20 |
likewise, for the strides [6,1] x would be 'C' contiguous. |
| 21 |
|
| 22 |
Iterators have an internal representation of the current coordinates |
| 23 |
(indices), the array, strides, and backstrides. A short digression to |
| 24 |
explain backstrides: what is the coordinate and offset after [3,5] in |
| 25 |
the example above? |
| 26 |
if x.order == 'C': |
| 27 |
next == [4,0] => offset is 4 |
| 28 |
if x.order == 'F': |
| 29 |
next == [4,5] => offset is 25 |
| 30 |
Note that in 'C' order we stepped BACKWARDS 24 while 'overflowing' a |
| 31 |
shape dimension |
| 32 |
which is back 25 and forward 1, |
| 33 |
which is x.strides[1] * (x.shape[1] - 1) + x.strides[0] |
| 34 |
so if we precalculate the overflow backstride as |
| 35 |
[x.strides[i] * (x.shape[i] - 1) for i in range(len(x.shape))] |
| 36 |
we can do only addition while iterating |
| 37 |
All the calculations happen in next() |
| 38 |
""" |
| 39 |
from rpython.rlib import jit |
| 40 |
from pypy.module.micronumpy import support, constants as NPY |
| 41 |
from pypy.module.micronumpy.base import W_NDimArray |
| 42 |
|
| 43 |
class PureShapeIter(object): |
| 44 |
def __init__(self, shape, idx_w): |
| 45 |
self.shape = shape |
| 46 |
self.shapelen = len(shape) |
| 47 |
self.indexes = [0] * len(shape) |
| 48 |
self._done = False |
| 49 |
self.idx_w_i = [None] * len(idx_w) |
| 50 |
self.idx_w_s = [None] * len(idx_w) |
| 51 |
for i, w_idx in enumerate(idx_w): |
| 52 |
if isinstance(w_idx, W_NDimArray): |
| 53 |
self.idx_w_i[i], self.idx_w_s[i] = w_idx.create_iter(shape) |
| 54 |
|
| 55 |
def done(self): |
| 56 |
return self._done |
| 57 |
|
| 58 |
@jit.unroll_safe |
| 59 |
def next(self): |
| 60 |
for i, idx_w_i in enumerate(self.idx_w_i): |
| 61 |
if idx_w_i is not None: |
| 62 |
self.idx_w_s[i] = idx_w_i.next(self.idx_w_s[i]) |
| 63 |
for i in range(self.shapelen - 1, -1, -1): |
| 64 |
if self.indexes[i] < self.shape[i] - 1: |
| 65 |
self.indexes[i] += 1 |
| 66 |
break |
| 67 |
else: |
| 68 |
self.indexes[i] = 0 |
| 69 |
else: |
| 70 |
self._done = True |
| 71 |
|
| 72 |
@jit.unroll_safe |
| 73 |
def get_index(self, space, shapelen): |
| 74 |
return [space.newint(self.indexes[i]) for i in range(shapelen)] |
| 75 |
|
| 76 |
|
| 77 |
class IterState(object): |
| 78 |
_immutable_fields_ = ['iterator', '_indices'] |
| 79 |
|
| 80 |
def __init__(self, iterator, index, indices, offset): |
| 81 |
self.iterator = iterator |
| 82 |
self.index = index |
| 83 |
self._indices = indices |
| 84 |
self.offset = offset |
| 85 |
|
| 86 |
def same(self, other): |
| 87 |
if self.offset == other.offset and \ |
| 88 |
self.index == other.index and \ |
| 89 |
self._indices == other._indices: |
| 90 |
return self.iterator.same_shape(other.iterator) |
| 91 |
return False |
| 92 |
|
| 93 |
class ArrayIter(object): |
| 94 |
_immutable_fields_ = ['contiguous', 'array', 'size', 'ndim_m1', 'shape_m1[*]', |
| 95 |
'strides[*]', 'backstrides[*]', 'factors[*]', |
| 96 |
'track_index'] |
| 97 |
|
| 98 |
track_index = True |
| 99 |
|
| 100 |
@jit.unroll_safe |
| 101 |
def __init__(self, array, size, shape, strides, backstrides): |
| 102 |
assert len(shape) == len(strides) == len(backstrides) |
| 103 |
self.contiguous = (array.flags & NPY.ARRAY_C_CONTIGUOUS and |
| 104 |
array.shape == shape and array.strides == strides) |
| 105 |
|
| 106 |
self.array = array |
| 107 |
self.size = size |
| 108 |
self.ndim_m1 = len(shape) - 1 |
| 109 |
|
| 110 |
self.shape_m1 = [s - 1 for s in shape] |
| 111 |
self.strides = strides |
| 112 |
self.backstrides = backstrides |
| 113 |
|
| 114 |
ndim = len(shape) |
| 115 |
factors = [0] * ndim |
| 116 |
for i in xrange(ndim): |
| 117 |
if i == 0: |
| 118 |
factors[ndim-1] = 1 |
| 119 |
else: |
| 120 |
factors[ndim-i-1] = factors[ndim-i] * shape[ndim-i] |
| 121 |
self.factors = factors |
| 122 |
|
| 123 |
def same_shape(self, other): |
| 124 |
""" Iterating over the same element """ |
| 125 |
if not self.contiguous or not other.contiguous: |
| 126 |
return False |
| 127 |
return (self.contiguous == other.contiguous and |
| 128 |
self.array.dtype is other.array.dtype and |
| 129 |
self.shape_m1 == other.shape_m1 and |
| 130 |
self.strides == other.strides and |
| 131 |
self.backstrides == other.backstrides and |
| 132 |
self.factors == other.factors) |
| 133 |
|
| 134 |
@jit.unroll_safe |
| 135 |
def reset(self, state=None, mutate=False): |
| 136 |
index = 0 |
| 137 |
if state is None: |
| 138 |
indices = [0] * len(self.shape_m1) |
| 139 |
else: |
| 140 |
assert state.iterator is self |
| 141 |
indices = state._indices |
| 142 |
for i in xrange(self.ndim_m1, -1, -1): |
| 143 |
indices[i] = 0 |
| 144 |
offset = self.array.start |
| 145 |
if not mutate: |
| 146 |
return IterState(self, index, indices, offset) |
| 147 |
state.index = index |
| 148 |
state.offset = offset |
| 149 |
|
| 150 |
@jit.unroll_safe |
| 151 |
def next(self, state, mutate=False): |
| 152 |
assert state.iterator is self |
| 153 |
index = state.index |
| 154 |
if self.track_index: |
| 155 |
index += 1 |
| 156 |
indices = state._indices |
| 157 |
offset = state.offset |
| 158 |
if self.contiguous: |
| 159 |
elsize = self.array.dtype.elsize |
| 160 |
jit.promote(elsize) |
| 161 |
offset += elsize |
| 162 |
elif self.ndim_m1 == 0: |
| 163 |
stride = self.strides[0] |
| 164 |
jit.promote(stride) |
| 165 |
offset += stride |
| 166 |
else: |
| 167 |
for i in xrange(self.ndim_m1, -1, -1): |
| 168 |
idx = indices[i] |
| 169 |
if idx < self.shape_m1[i]: |
| 170 |
indices[i] = idx + 1 |
| 171 |
offset += self.strides[i] |
| 172 |
break |
| 173 |
else: |
| 174 |
indices[i] = 0 |
| 175 |
offset -= self.backstrides[i] |
| 176 |
if not mutate: |
| 177 |
return IterState(self, index, indices, offset) |
| 178 |
state.index = index |
| 179 |
state.offset = offset |
| 180 |
|
| 181 |
@jit.unroll_safe |
| 182 |
def goto(self, index): |
| 183 |
offset = self.array.start |
| 184 |
if self.contiguous: |
| 185 |
offset += index * self.array.dtype.elsize |
| 186 |
elif self.ndim_m1 == 0: |
| 187 |
offset += index * self.strides[0] |
| 188 |
else: |
| 189 |
current = index |
| 190 |
for i in xrange(len(self.shape_m1)): |
| 191 |
offset += (current / self.factors[i]) * self.strides[i] |
| 192 |
current %= self.factors[i] |
| 193 |
return IterState(self, index, None, offset) |
| 194 |
|
| 195 |
@jit.unroll_safe |
| 196 |
def indices(self, state): |
| 197 |
assert state.iterator is self |
| 198 |
assert self.track_index |
| 199 |
indices = state._indices |
| 200 |
if not (self.contiguous or self.ndim_m1 == 0): |
| 201 |
return indices |
| 202 |
current = state.index |
| 203 |
for i in xrange(len(self.shape_m1)): |
| 204 |
if self.factors[i] != 0: |
| 205 |
indices[i] = current / self.factors[i] |
| 206 |
current %= self.factors[i] |
| 207 |
else: |
| 208 |
indices[i] = 0 |
| 209 |
return indices |
| 210 |
|
| 211 |
def done(self, state): |
| 212 |
assert state.iterator is self |
| 213 |
assert self.track_index |
| 214 |
return state.index >= self.size |
| 215 |
|
| 216 |
def getitem(self, state): |
| 217 |
|
| 218 |
return self.array.getitem(state.offset) |
| 219 |
|
| 220 |
def getitem_bool(self, state): |
| 221 |
assert state.iterator is self |
| 222 |
return self.array.getitem_bool(state.offset) |
| 223 |
|
| 224 |
def setitem(self, state, elem): |
| 225 |
assert state.iterator is self |
| 226 |
self.array.setitem(state.offset, elem) |
| 227 |
|
| 228 |
def AxisIter(array, shape, axis): |
| 229 |
strides = array.get_strides() |
| 230 |
backstrides = array.get_backstrides() |
| 231 |
if len(shape) == len(strides): |
| 232 |
|
| 233 |
strides = strides[:axis] + [0] + strides[axis + 1:] |
| 234 |
backstrides = backstrides[:axis] + [0] + backstrides[axis + 1:] |
| 235 |
else: |
| 236 |
strides = strides[:axis] + [0] + strides[axis:] |
| 237 |
backstrides = backstrides[:axis] + [0] + backstrides[axis:] |
| 238 |
return ArrayIter(array, support.product(shape), shape, strides, backstrides) |
| 239 |
|
| 240 |
|
| 241 |
def AllButAxisIter(array, axis): |
| 242 |
size = array.get_size() |
| 243 |
shape = array.get_shape()[:] |
| 244 |
backstrides = array.backstrides[:] |
| 245 |
if size: |
| 246 |
size /= shape[axis] |
| 247 |
shape[axis] = backstrides[axis] = 0 |
| 248 |
return ArrayIter(array, size, shape, array.strides, backstrides) |