-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Toward defining the interface of AbstractArrays #10064
Comments
Ordinary indexing means looking up the column pointer of the current and next |
Bless you for opening this, Jiahao. |
By "rank" do you mean dimensionality? "rank" has a different meaning in linear algebra, of course, and that meaning of rank does not influence the cost of indexing. |
Updated with clarifications and corrections. @timholy it is quite unfortunate that "rank of an array" means something very different from "rank of a matrix", and that both meanings are quite widely used. It is rather unpretty. One might even say it is a rank situation. |
Yes, thank you for posting that link! |
Here's a very quick summary of the current state of the API proposal in the gist:
Arrays can have the ability to return multiple index iterators. For example SparseMatrixCSC should be able to return an iterator over the indices of its nonzeros. Or you might want to return an iterator that provides faster access. It's possible that dimensions need to be ordered, such that |
It would be great with some thoughts on distributed arrays. |
Here's one dirt-simple thought, that's possible to implement now: the local chunks should have indices that correspond to their "piece" of the whole array. That seems like it might make a number of things easier (fewer index gymastics to worry about). |
That does sound compelling. I also have a half-baked idea that the outer container managing the distribution could somehow be an array mapping bounding boxes to chunks with the data, e.g. for easily identifying which chunks are involved in a computation. |
This is exactly the idea promoted in ZPL ("regions") and Chapel ("distributions" (index set mapping global index view to individual chunks), and "layouts" (local index view of underlying buffer, aka Jeff's proposal). Lots of previous work to draw on here. |
PETSc also has the notions of local to global mapping index sets, views of local data, and fast access to local data (not quite iteration, but pretty close considering it is written in C), and it has worked out really well for them in terms of usability. |
There are also Fortran co-arrays. Aside, those use two sets of parenthesis for local and global indexing. Maybe something to consider for |
Not sure what's actually the action item here, but @mbauman, I'll leave that up to you. |
This issue is a bit vague for a 1.0 milestone issue at this point. Let's discuss on the call next week. |
Too vague to be actionable for 1.0; everything actionable is traits stuff which can be done in 1.x. |
Is this still an open issue? My understanding is that the |
What is an AbstractArray?
This issue follows up from #987 (and complements #9586), where the question was raised about what it means to be an
AbstractArray
. My understanding of where this issue left off was thatAbstractArray{T,N}
has the following interface:size
is defined and returns a tuple of lengthN
,getindex
is defined and returns data of typeT
"cheaply", where cheaply means "in O(1) time preferably but we will allow some small growth bounded very loosely above by O(length(n)) to accommodate sparse matrices".This interface is incompletely defined because Julia arrays support nontrivial indexing semantics and not all of which have the same computational complexity.
The question
This issue is about spelling out the methods supported by
AbstractArray
s, and in particular:AbstractArray
s must support is, andIndexing semantics
Julia arrays support several indexing semantics:
:
, UnitRange or Range (slice indexing)For arrays of rank
N>1
we have to further distinguish between indexing with asingle entity (linear indexing) vs indexing with
N
entities (Cartesianindexing). So this gives us a matrix of 4 x 2 = 8 different indexing behaviors.
Some complexity computations
@andreasnoack @jakebolewski and I sat down this afternoon to work out the complexity of indexing semantics of various kinds of arrays. The goal of these calculations is to help clarify the importance of complexity bounds on the interface of
AbstractArray
s.We considered the following types of arrays:
vectors containing shape
information or its equivalent.
The corresponding Julia type is
StridedArray
, which is the union ofSubArray
andDenseArray
(which in turn includes ordinaryArray
).information and layout can be represented by
Iliffe vectors.
Each dimension need not be stored contiguously, but in general will be stored in
M
distinct chunks of contiguous memory.For simplicity we consider only N-dimensional arrays produced by ragged arrays of
ragged arrays, nested N-1 times.
We also distinguish between sorted and unsorted variants.
Ragged arrays are the default in Java.
Ragged arrays don't really exist in Julia for
M>1
. ForM=1
chunk per dimension,they correspond to the recursive family of types
Vector{T}
,Vector{Vector{T}}
,Vector{Vector{Vector{T}}}
,... Also,DArray
s withcustomizable chunking schemes support some form of chunking where each chunk
must live on a different process.
matrices, which many people in array type hierarchy #987 want to be
AbstractArray
s.Base Julia has
SparseMatrixCSC
.Where necessary the analysis was simplified by assuming a uniform prior distribution of matrix elements in each column and each row.
The results
Let
N
be the rank of an array (i.e., the number of dimensions, or the length of the index tuple associated with a matrix element),M
be the number of chunks in a single ragged array dimension,k
be the length of a Range, Colon or iterable (=k1 * k2 * ... * kN
inthe case of using
N
of these entities),ncol
be the number of columns,NNZ
be the number of stored elements.Considering only linear indexing and Cartesian indexing using the same type of
entity in each dimension, we can write the computational complexity of each
indexing behavior as
In no case did I find that the cost of the Cartesian to linear offset computation dominates the complexity.
Note that we have different results depending on whether slicing returns
or
Breakdown of the computations
Strided array
dimension.
Ragged sorted array
ordinary indexing is a sorted search problem in each dimension. We get log(M)
from binary search.
is a nonunit stride in the slice which does not dominate) because at every
dimension you have to compute the intersection of two intervals, namely the
slice and the index set of that chunk
matter
Ragged unsorted array (indexes for each chunk are not sorted)
each chunk. On average we expect something like (NNZ/M^N) entries in each
index set (per dimension), assuming a uniform prior on the distribution
of entries in chunks. We expect on average k/M matches for each chunk.
So we expect to do O((k/M) * (M^N)) work. The work is the same to compute the
index set for a view as it is to copy.
to be mapped to a chunk by linear search each time.
CSC
columns, identifying the corresponding row indexes and values in the resulting
half-open interval. Again assuming for simplicity a uniform prior distribution
of entries, the expected number of entries in any given column is O(NNZ/ncol).
Since each entry is sorted by row index, we can use a sorted search so the
complexity of lookup is O(log (NNZ/ncol)).
so the cost is O(log (NNZ/ncol)). When the stride of the slice is nonunit there
is extra work that needs to be done but I haven't checked the cost of this yet.
The probability to match one element is ~ncol/NNZ, so we expect to retrieve
k * ncol/NNZ stored elements.
Cartesian indexing behaviors mixing different indexing types in each dimension
can get more complicated. CSC is an interesting one to look at:
A[:,5]
returning a view costsO(1)
since the answer is given immediately bylooking up the start and end of the column in the list of column offsets.
A[:,5]
returning a copy costs~O(NNZ/ncol)
, the expected number of entries inthe column.
A[5,:]
returning a view costsO(ncol log(NNZ/ncol))
, since you have to do asorted search for each column.
A[5,:]
returning a copy costsO(NNZ log(NNZ/ncol))
, since you have to computethe index set then copy the elements, which on average we expect to have
NNZ/ncol
of them.The text was updated successfully, but these errors were encountered: