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

WIP: implementation of fixed-size arrays #7568

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export
LAPACK,

# Types
AbstractFixedArray,
AbstractMatrix,
AbstractSparseArray,
AbstractSparseMatrix,
Expand Down Expand Up @@ -48,6 +49,8 @@ export
FileMonitor,
FileOffset,
Filter,
FixedArrayI,
FixedArrayM,
FloatRange,
Hermitian,
UniformScaling,
Expand Down
105 changes: 105 additions & 0 deletions base/fixedarrays.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
module FixedArrays

export AbstractFixedArray, FixedArrayI, FixedArrayM

using Base.Cartesian
import Base: (*), (==), A_mul_B!, eltype, getindex, length, ndims, size

## Types

abstract AbstractFixedArray{T,N,SZ} <: DenseArray{T,N}

immutable FixedArrayI{T,N,SZ,L} <: AbstractFixedArray{T,N,SZ}
data::NTuple{L,T}
end

immutable FixedArrayM{T,N,SZ} <: AbstractFixedArray{T,N,SZ}
data::Array{T,N}
end

## Constructors
# Note these can't be type-stable, since the size of A is unknown to the type system

function FixedArrayI(A::AbstractArray)
L = length(A)
FixedArrayI{eltype(A), ndims(A), size(A), L}(ntuple(L, i->A[i]))
end

toarray(A) = convert(Array, A)
toarray(A::Array) = copy(A)

FixedArrayM(A::AbstractArray) = FixedArrayM{eltype(A), ndims(A), size(A)}(toarray(A))


## Utility functions
eltype{T,N,SZ}(A::AbstractFixedArray{T,N,SZ}) = T
length{T,N,SZ}(A::AbstractFixedArray{T,N,SZ}) = prod(SZ)
ndims{T,N,SZ}(A::AbstractFixedArray{T,N,SZ}) = N
size{T,N,SZ}(A::AbstractFixedArray{T,N,SZ}) = SZ
size{T,N,SZ}(A::AbstractFixedArray{T,N,SZ}, d::Integer) = SZ[d]

getindex(A::AbstractFixedArray, i::Real) = A.data[i]
@nsplat N getindex(A::FixedArrayM, I::NTuple{N,Real}...) = getindex(A.data, I...)

getindex{T,SZ}(A::FixedArrayI{T,2,SZ}, i::Real, j::Real) = A.data[i+(j-1)*SZ[1]]

## Comparisons

for (TA, TB) in ((AbstractArray,AbstractFixedArray), (AbstractArray,AbstractFixedArray))
if TA == AbstractArray && TB == AbstractArray
continue
end
@eval begin
function (==)(A::$TA, B::$TB)
size(A) == size(B) || return false
for i = 1:length(B)
if A[i] != B[i]
return false
end
end
true
end
end
end

## Linear algebra

for N=1:4, K=1:4, M=1:4
L = M*N
@eval begin
function (*){TA,TB}(A::FixedArrayI{TA,2,($M,$K)}, B::FixedArrayI{TB,2,($K,$N)})
TP = typeof(one(TA)*one(TB) + one(TA)*one(TB))
@nexprs $K d2->(@nexprs $M d1->A_d1_d2 = A.data[(d2-1)*$M+d1])
@nexprs $N d2->(@nexprs $K d1->B_d1_d2 = B.data[(d2-1)*$K+d1])
@nexprs $N n->(@nexprs $M m->begin
tmp = zero(TP)
@nexprs $K k->(tmp += A_m_k * B_k_n)
dest_{m+(n-1)*$K} = tmp
end)
FixedArrayI{TP,2,($M,$N),$L}(@ntuple $L d->dest_d)
end
end
end

function (*){TA,TB,M,K,N}(A::FixedArrayM{TA,2,(M,K)}, B::FixedArrayM{TB,2,(K,N)})
TP = typeof(one(TA)*one(TB) + one(TA)*one(TB))
A_mul_B!(FixedArrayM{TP,2,(M,N)}(Array(TP, M, N)), A, B)
end

for N=1:4, K=1:4, M=1:4
@eval begin
function A_mul_B!{TC,TA,TB}(out::FixedArrayM{TC,2,($M,$N)}, A::AbstractFixedArray{TA,2,($M,$K)}, B::AbstractFixedArray{TB,2,($K,$N)})
TP = typeof(one(TA)*one(TB) + one(TA)*one(TB))
@nexprs $K d2->(@nexprs $M d1->@inbounds A_d1_d2 = A.data[(d2-1)*$M+d1])
@nexprs $N d2->(@nexprs $K d1->@inbounds B_d1_d2 = B.data[(d2-1)*$K+d1])
@nexprs $N n->(@nexprs $M m->begin
tmp = zero(TP)
@nexprs $K k->(tmp += A_m_k * B_k_n)
@inbounds out.data[m,n] = tmp
end)
out
end
end
end

end
4 changes: 4 additions & 0 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ const × = cross
include("broadcast.jl")
importall .Broadcast

# fixed arrays
include("fixedarrays.jl")
using .FixedArrays

# statistics
include("statistics.jl")

Expand Down
15 changes: 15 additions & 0 deletions test/fixedarrays.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Base.Test

let
A = rand(2,2)
B = rand(2,3)
C = A*B
for Constr in (FixedArrayI, FixedArrayM)
AI = Constr(A)
BI = Constr(B)
CI = AI*BI
@test_approx_eq CI C
CI = Constr(C)
@test CI == C
end
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
testnames = [
"linalg", "core", "keywordargs", "numbers", "strings",
"collections", "hashing", "remote", "iobuffer", "arrayops", "reduce", "reducedim",
"simdloop", "blas", "fft", "dsp", "sparse", "bitarray", "random", "math",
"simdloop", "blas", "fft", "dsp", "sparse", "bitarray", "fixedarrays",
"random", "math",
"functional", "bigint", "sorting", "statistics", "spawn",
"backtrace", "priorityqueue", "arpack", "file", "suitesparse", "version",
"resolve", "pollfd", "mpfr", "broadcast", "complex", "socket",
Expand Down