Skip to content
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

RFC: A systematic array view framework #5513

Closed
lindahua opened this issue Jan 24, 2014 · 8 comments
Closed

RFC: A systematic array view framework #5513

lindahua opened this issue Jan 24, 2014 · 8 comments

Comments

@lindahua
Copy link
Contributor

Efficient array views have been a long standing issue in Julia. Recently, I decided to tackle this problem again, and for this purpose, created a package ArrayViews.jl to explore a generic & efficient approach.

There remains some work to be done, but the framework is largely there. While I will continue to work on this package, I would like to solicit comments here.

The ultimate goal is to move this to the Base when it is ready, to take the place of the current subarray system.

An important feature of this approach is that it has two view types ContiguousView and StridedView. Each strided view is associated with a static number called contiguous rank, which can be used to determine (statically) the contiguousness of a subview thereof.

A contiguous view (with more compact representation and faster indexing) will be returned whenever the contiguousness of a view can be determined statically, thus substantially improving indexing efficiency in a lot of common situations.

@ViralBShah
Copy link
Member

This looks really systematic. How does the performance on your benchmarks compare currently?

@tknopp
Copy link
Contributor

tknopp commented Jan 25, 2014

@lindahua: This looks fantastic! hopefully this gets in for 0.3 and replaces getindex/setindex! for regular arrays

@lindahua
Copy link
Contributor Author

@ViralBShah: Here is a brief summary of my benchmark results obtained so far:

  1. Indexing over contiguous views are considerably faster than subarrays. For example, traversing view(a, :, i) is about 2x - 3x faster than traversing sub(a, :, i). The speed of traversing view(a, :, i) is comparable to traversing an ordinary array.
  2. Linear indexing over contiguous views are remarkably faster than linear indexing over subarrays (up to 50x - 100x faster) for 2D, 3D and multidimensional cases.
  3. Since view(a, :, i), view(a, i:j), view(a, i:j, k), view(:, i:j, k), view(:,:,k) and many more cases are statically detected as contiguous (thanks to the systematical approach), many practical use cases can benefit from the performance improvement over contiguous views.
  4. For non-contiguous views, indexing performance is similar to subarrays. (For such cases, the codes for indexing are similar).
  5. Constructor of views are much more light-weighted. A specific benchmark on view construction shows that for array views, it takes about 0.15 sec (on average) to construct 1 million views, while it takes 1.5 - 1.9 sec to construct the same number of sub-arrays. The overhead of view construction is substantially reduced, and therefore use cases that require constructing a lot of small subviews can also benefit from this.
  6. Type stability is carefully maintained.

cc: @StefanKarpinski

@mschauer
Copy link
Contributor

Very nice. Could ArrayViews allow to iterate over the contiguous r dimensional subarrays of a dimension n matrix in a efficient manner?

@lindahua
Copy link
Contributor Author

The package is basically ready. Now it supports view composition (i.e. views over views) and works well with linear algebra functions. I think it is ready to be migrated to the Base.

@mschauer The interface of view is nearly the same as sub, but with a more efficient internal representation & implementation.

To iterate over sub slices, one may do

a = rand(m, n, k)
for i = 1 : size(a, 3)
    do_something(view(a,:,:,i))
end

This is more efficient than using a[:,:,i] or sub(a,:,:,i).

@mschauer
Copy link
Contributor

Ah, that is good, these contiguous subarrays were a bit underprivileged in Julia compared to APL and J, where operations on those are very well integrated... I guess you know by your usage of the term [rank](http:https://en.wikipedia.org/wiki/Rank_(J_programming_language%29) . Here also #5405 applies because the syntax changes with the number of dimensions which makes it a bit inflexible.

@lindahua
Copy link
Contributor Author

For #5405, I like the ellipsis notation. Until that lands, I am not able to support the notation like view(a, ..., i).

However, it is possible to add something like view(a, ellipsis(), i). It is ugly, but it does the job.

@lindahua
Copy link
Contributor Author

Close in favor of the PR at #5556. Please move the discussions there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants