Add array type parameter to DefaultArrayInterface
#44
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
EDIT: This description is a bit outdated since it is all handled by just adding a new type parameter to
DefaultArrayInterface
but the idea is still relevant:This adds another
AbstractArrayInterface
subtype calledArrayInterface
. It is analogous toBase.Broadcast.ArrayStyle
. Specifically, it can act as the array style for any array type by storing the array type as a type parameter.In the current PR, when you call
interface(A::AbstractArray)
, it has the following behavior:typeof(A)
.Array
or an abstract array type likeAbstractArray
, it constructs aDefaultArrayInterface
.ArrayStyle
with the unwrapped array type ofA
as a type parameter, though to canonicalize the type it strips off all type parameters.Then,
similar(::ArrayStyle, T::Type, ax::Tuple)
sets the element type of the stored array type (usingTypeParameterAccessors.set_eltype
) and tries to construct the array withsimilar(A{T}, ax)
. This of course can go wrong, in which case that array type should define a custom interface. However, this works for a lot of cases automatically. The motivation is ITensor/BlockSparseArrays.jl#138, where I want the interface system to automatically work for GPU array types without having to make a custom interface for each one, this PR handles that case. We may want custom GPU array interfaces anyway at some point, for example to store information about the device, storage mode like unified memory, etc., but for now I think it is nice to have something that "just works".