Open rainwoodman opened 7 years ago
I thought about this and we should maybe design the current iterator to know the number of contiguous entry and skip chunks.
There's a ravel
utility in NumPy which flattens an array into a contiguous 1D vector. If the Iterator
class could know, looking at the strides, how many contiguous elements are coming and its memory layout, that would be great.
var iter = array.ravel ().iterator ();
int num_elements; // number of contigous elements
ssize_t stride; // how memory is layed out
while (iter.next ()) {
if (iter.contigous_stride == arrary.scalar_size && iter.contiguous_elements >= 4) {
var double_vec = (double_v16) iter.get (); // cast into vector
} else {
// make copy
}
}
I don't see why we would need more than two iterator (multi and single) if we can perform most iterating strategy my changing the view.
Also, if we need some buffering strategy, we might as well derive Array
once we will extract a proper interface and reuse existing iterators.
the flat iterator looks useful by itself as it provides a guaranteed sequence of looping. Ravel creates a copy if the array is not C-contiguous, thus cannot substitute flat iterator.
I take it back. The optimization shall not happen in a iterator. For clarity, any iterator shall always loop over the last dimension first. The caller to the iterator can pass in an optimized view. In this sense the iterator is always flat, then there is no need to add Flat to the name.
I agree flat iter is a subset of the full iterator and it appears there is no need to have two classes. The iterator also need to deal with write backs, so the pointers we shall always access with set() and get() but modifying the result of get() will not necessarily update the array.
We can call it Iterator(group, alignment)
and when group==1, alignment==1 the behavior is the same as the current Iterator. No need for a new class.
An Optimized View of an array shall have the as many as possible number of last axes trivially iterable.
OptimizedIterator will
for partially trivially iterable arrays, N can include a full step of the entire trivally iterable subspace.