From 7453e040651173b98b2493de31b6923e71024365 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 18 Oct 2023 14:18:13 -0400 Subject: [PATCH 001/156] Add FieldType module with Unallocated and Unspecified Types --- NDTensors/Project.toml | 1 + NDTensors/src/FieldTypes/README.md | 3 + NDTensors/src/FieldTypes/src/FieldTypes.jl | 23 +++ .../src/unallocatedarray/unallocatedfill.jl | 2 + .../src/unallocatedarray/unallocatedzeros.jl | 144 ++++++++++++++++++ .../src/unspecifiedarray/unspecifiedarray.jl | 1 + .../unspecifiednumber/unspecifiednumber.jl | 5 + .../src/unspecifiednumber/unspecifiedzero.jl | 40 +++++ NDTensors/src/NDTensors.jl | 2 + 9 files changed, 221 insertions(+) create mode 100644 NDTensors/src/FieldTypes/README.md create mode 100644 NDTensors/src/FieldTypes/src/FieldTypes.jl create mode 100644 NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl create mode 100644 NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl create mode 100644 NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl create mode 100644 NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiednumber.jl create mode 100644 NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl diff --git a/NDTensors/Project.toml b/NDTensors/Project.toml index 061c527156..7770f99947 100644 --- a/NDTensors/Project.toml +++ b/NDTensors/Project.toml @@ -8,6 +8,7 @@ Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" +FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" FLoops = "cc61a311-1640-44b5-9fba-1b764f453329" Folds = "41a02a25-b8f0-4f67-bc48-60067656b558" Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196" diff --git a/NDTensors/src/FieldTypes/README.md b/NDTensors/src/FieldTypes/README.md new file mode 100644 index 0000000000..52cc6a0a35 --- /dev/null +++ b/NDTensors/src/FieldTypes/README.md @@ -0,0 +1,3 @@ +# FieldTypes + +A module defining a series of representative field types which are by nature lazy and unallocated. diff --git a/NDTensors/src/FieldTypes/src/FieldTypes.jl b/NDTensors/src/FieldTypes/src/FieldTypes.jl new file mode 100644 index 0000000000..dbbdda2077 --- /dev/null +++ b/NDTensors/src/FieldTypes/src/FieldTypes.jl @@ -0,0 +1,23 @@ +module FieldTypes + +using FillArrays +using LinearAlgebra + +#include("SetParameters/src/SetParameters.jl") +#using ..SetParameters + +include("unspecifiednumber/unspecifiednumber.jl") +include("unspecifiednumber/unspecifiedzero.jl") + +include("unallocatedarray/unallocatedfill.jl") +include("unallocatedarray/unallocatedzeros.jl") + +include("unspecifiedarray/unspecifiedarray.jl") + + +export UnallocatedFill, + UnallocatedZeros, + UnspecifiedArray, + UnspecifiedNumber, + UnspecifiedZero +end \ No newline at end of file diff --git a/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl b/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl new file mode 100644 index 0000000000..7173ebe4f5 --- /dev/null +++ b/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl @@ -0,0 +1,2 @@ +struct UnallocatedFill{ElT, N, Axes} <: FillArrays.AbstractFill{ElT, N, Axes} +end \ No newline at end of file diff --git a/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl b/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl new file mode 100644 index 0000000000..372e366b74 --- /dev/null +++ b/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl @@ -0,0 +1,144 @@ +struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: + FillArrays.AbstractFill{ElT,N,Axes} +z::FillArrays.Zeros{ElT,N,Axes} +function UnallocatedZeros{ElT,N,Alloc}(inds::Tuple) where {ElT,N,Alloc} + z = FillArrays.Zeros{ElT,N}(inds) + Axes = typeof(FillArrays.axes(z)) + return new{ElT,N,Axes,Alloc}(z) +end + + +function UnallocatedZeros{ElT,N,Alloc}(::Tuple{}) where {ElT,N,Alloc} + @assert N == 1 + z = FillArrays.Zeros{ElT,N}(1) + Axes = typeof(FillArrays.axes(z)) + return new{ElT,N,Axes,Alloc}(z) +end + +function UnallocatedZeros{ElT,N,Axes,Alloc}(inds::Tuple) where {ElT,N,Axes,Alloc} + @assert Axes == typeof(Base.axes(inds)) + z = FillArrays.Zeros{ElT,N}(inds) + return new{ElT,N,Axes,Alloc}(z) +end +end + +function UnallocatedZeros{ElT,N,Axes,Alloc}() where {ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} +return UnallocatedZeros{ElT,N,Axes,Alloc}[] +end +function UnallocatedZeros(alloc::Type{<:AbstractArray}, inds...) +@assert ndims(alloc) == length(inds) +alloc = specify_eltype(alloc) +return UnallocatedZeros{eltype(alloc),ndims(alloc),alloc}(Tuple(inds)) +end + +function UnallocatedZeros{ElT}(alloc::Type{<:AbstractArray}, inds...) where {ElT} +alloc = set_eltype(alloc, ElT) +N = length(Tuple(inds)) +alloc = set_ndims(alloc, N) +return UnallocatedZeros(alloc, inds...) +end + +Base.ndims(::UnallocatedZeros{ElT,N}) where {ElT,N} = N +ndims(::UnallocatedZeros{ElT,N}) where {ElT,N} = N +Base.eltype(::UnallocatedZeros{ElT}) where {ElT} = ElT +alloctype(::UnallocatedZeros{ElT,N,Axes,Alloc}) where {ElT,N,Axes,Alloc} = Alloc +function alloctype(::Type{<:UnallocatedZeros{ElT,N,Axes,Alloc}}) where {ElT,N,Axes,Alloc} +return Alloc +end +axes(::Type{<:UnallocatedZeros{ElT,N,Axes}}) where {ElT,N,Axes} = Axes + +Base.size(zero::UnallocatedZeros) = Base.size(zero.z) + +Base.print_array(io::IO, X::UnallocatedZeros) = Base.print_array(io, X.z) + +data(zero::UnallocatedZeros) = zero.z +getindex(zero::UnallocatedZeros) = getindex(zero.z) + +array(zero::UnallocatedZeros) = alloctype(zero)(zero.z) +Array(zero::UnallocatedZeros) = array(zero) +axes(z::UnallocatedZeros) = axes(z.z) +dims(z::UnallocatedZeros) = Tuple(size(z.z)) +dim(z::UnallocatedZeros) = dim(size(z.z)) +copy(z::UnallocatedZeros) = UnallocatedZeros{eltype(z),ndims(z),alloctype(z)}(dims(z)) + +Base.vec(z::Type{<:UnallocatedZeros}) = z +Base.vec(z::UnallocatedZeros) = z + +function Base.convert(x::Type{T}, z::UnallocatedZeros) where {T<:Base.Array} +return Base.convert(x, z.z) +end + +function complex(z::UnallocatedZeros) +ElT = complex(eltype(z)) +N = ndims(z) +AllocT = similartype(alloctype(z), ElT) +return UnallocatedZeros{ElT,N,AllocT}(dims(z)) +end + +Base.sum(z::UnallocatedZeros) = sum(z.z) +LinearAlgebra.norm(z::UnallocatedZeros) = norm(z.z) + +# function (arraytype::Type{<:UnallocatedZeros})(::AllowAlias, A::UnallocatedZeros) +# return A +# end + +# function (arraytype::Type{<:UnallocatedZeros})(::NeverAlias, A::UnallocatedZeros) +# return copy(A) +# end + +function to_shape(::Type{<:UnallocatedZeros}, dims::Tuple) +return NDTensors.to_shape(dims) +end + +function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:UnallocatedZeros}) +ElT = promote_type(eltype(z1), eltype(z2)) +@assert ndims(z1) == ndims(z2) +Axs = axes(z1) +Alloc = promote_type(alloctype(z1), alloctype(z2)) +set_eltype(Alloc, ElT) +return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} +end + +function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:AbstractArray}) +ElT = promote_type(eltype(z1), eltype(z2)) +@assert ndims(z1) == ndims(z2) +Axs = axes(z1) +Alloc = promote_type(alloctype(z1), z2) +set_eltype(Alloc, ElT) +return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} +end + +function promote_rule(z1::Type{<:AbstractArray}, z2::Type{<:UnallocatedZeros}) +return promote_rule(z2, z1) +end + +## Check datatypes to see if underlying storage is a +## UnallocatedZeros +is_unallocated_zeros(a) = data_isa(a, UnallocatedZeros) + +FillArrays.getindex_value(Z::UnallocatedZeros) = FillArrays.getindex_value(Z.z) + +function generic_zeros(::Type{<:UnallocatedZeros}, inds::Integer) +elt = default_eltype() +datat = default_datatype(elt) +N = ndims(datat) +return UnallocatedZeros{elt,N,datat}(Tuple(dim)) +end + +function generic_zeros(::Type{<:UnallocatedZeros{ElT}}, inds::Integer) where {ElT} +datat = default_datatype(ElT) +N = ndims(datat) +return UnallocatedZeros{ElT,N,datat}(Tuple(dim)) +end + +function generic_zeros( +::Type{<:UnallocatedZeros{ElT,N,DataT}}, dim::Integer +) where {ElT,N,DataT<:AbstractArray{ElT,N}} +return UnallocatedZeros{ElT,N,DataT}(Tuple(dim)) +end + +function generic_zeros( +::Type{<:UnallocatedZeros{ElT,N,Axes,DataT}}, dim::Integer +) where {ElT,N,Axes,DataT<:AbstractArray{ElT,N}} +return UnallocatedZeros{ElT,N,DataT}(Tuple(dim)) +end \ No newline at end of file diff --git a/NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl b/NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl new file mode 100644 index 0000000000..3c722e5c60 --- /dev/null +++ b/NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl @@ -0,0 +1 @@ +struct UnspecifiedArray{ElT, N} <: AbstractArray{ElT, N} end \ No newline at end of file diff --git a/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiednumber.jl b/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiednumber.jl new file mode 100644 index 0000000000..3de49b00bd --- /dev/null +++ b/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiednumber.jl @@ -0,0 +1,5 @@ +abstract type AbstractUnspecifiedNumber <: Number end + +struct UnspecifiedNumber{T} <: AbstractUnspecifiedNumber + value::T +end \ No newline at end of file diff --git a/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl b/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl new file mode 100644 index 0000000000..eb86c7579e --- /dev/null +++ b/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl @@ -0,0 +1,40 @@ +struct UnspecifiedZero <: AbstractUnspecifiedNumber +end + +# Base.Complex{UnspecifiedZero}() = complex(UnspecifiedZero()) +# function Base.Complex{UnspecifiedZero}(z::Real) +# return (iszero(z) ? complex(UnspecifiedZero()) : throw(ErrorException)) +# end + +zero(::Type{UnspecifiedZero}) = UnspecifiedZero() +zero(n::UnspecifiedZero) = zero(typeof(n)) + +# This helps handle a lot of basic algebra, like: +# UnspecifiedZero() + 2.3 == 2.3 +convert(::Type{T}, x::UnspecifiedZero) where {T<:Number} = T(zero(T)) + +#convert(::Type{Complex{UnspecifiedZero}}, x::UnspecifiedZero) = complex(x) + +# TODO: Should this be implemented? +#Complex(x::Real, ::UnspecifiedZero) = x + +# This is to help define `float(::UnspecifiedZero) = 0.0`. +# This helps with defining `norm` of `UnallocatedZeros{UnspecifiedZero}`. +AbstractFloat(::UnspecifiedZero) = zero(AbstractFloat) + +# Basic arithmetic +(::UnspecifiedZero + ::UnspecifiedZero) = UnspecifiedZero() +(::UnspecifiedZero - ::UnspecifiedZero) = UnspecifiedZero() +(::Number * ::UnspecifiedZero) = UnspecifiedZero() +(::UnspecifiedZero * ::Number) = UnspecifiedZero() +(::UnspecifiedZero * ::UnspecifiedZero) = UnspecifiedZero() +(::UnspecifiedZero / ::Number) = UnspecifiedZero() +(::Number / ::UnspecifiedZero) = throw(DivideError()) +(::UnspecifiedZero / ::UnspecifiedZero) = throw(DivideError()) +-(::UnspecifiedZero) = UnspecifiedZero() + +Base.promote_type(z::Type{<:UnspecifiedZero}, ElT::Type) = Base.promote_type(ElT, z) + +Base.promote_type(ElT::Type, ::Type{<:UnspecifiedZero}) = ElT +Base.promote_type(::Type{<:UnspecifiedZero}, ::Type{<:UnspecifiedZero}) = UnspecifiedZero +Base.promote_type(ElT::Type, ::Type{<:Complex{<:UnspecifiedZero}}) = Complex{real(ElT)} \ No newline at end of file diff --git a/NDTensors/src/NDTensors.jl b/NDTensors/src/NDTensors.jl index 641253dec0..8322fc8c4f 100644 --- a/NDTensors/src/NDTensors.jl +++ b/NDTensors/src/NDTensors.jl @@ -20,6 +20,8 @@ using TupleTools include("SetParameters/src/SetParameters.jl") using .SetParameters +include("FieldTypes/src/FieldTypes.jl") +using .FieldTypes include("BlockSparseArrays/src/BlockSparseArrays.jl") using .BlockSparseArrays include("SmallVectors/src/SmallVectors.jl") From af065e778822041eb4ca3cc1e7c224a3cee3d49a Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 18 Oct 2023 14:18:39 -0400 Subject: [PATCH 002/156] format --- NDTensors/src/FieldTypes/src/FieldTypes.jl | 8 +- .../src/unallocatedarray/unallocatedfill.jl | 3 +- .../src/unallocatedarray/unallocatedzeros.jl | 121 +++++++++--------- .../src/unspecifiedarray/unspecifiedarray.jl | 2 +- .../unspecifiednumber/unspecifiednumber.jl | 4 +- .../src/unspecifiednumber/unspecifiedzero.jl | 5 +- 6 files changed, 68 insertions(+), 75 deletions(-) diff --git a/NDTensors/src/FieldTypes/src/FieldTypes.jl b/NDTensors/src/FieldTypes/src/FieldTypes.jl index dbbdda2077..ba914b5373 100644 --- a/NDTensors/src/FieldTypes/src/FieldTypes.jl +++ b/NDTensors/src/FieldTypes/src/FieldTypes.jl @@ -14,10 +14,6 @@ include("unallocatedarray/unallocatedzeros.jl") include("unspecifiedarray/unspecifiedarray.jl") - export UnallocatedFill, - UnallocatedZeros, - UnspecifiedArray, - UnspecifiedNumber, - UnspecifiedZero -end \ No newline at end of file + UnallocatedZeros, UnspecifiedArray, UnspecifiedNumber, UnspecifiedZero +end diff --git a/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl b/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl index 7173ebe4f5..70247a1cd6 100644 --- a/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl +++ b/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl @@ -1,2 +1 @@ -struct UnallocatedFill{ElT, N, Axes} <: FillArrays.AbstractFill{ElT, N, Axes} -end \ No newline at end of file +struct UnallocatedFill{ElT,N,Axes} <: FillArrays.AbstractFill{ElT,N,Axes} end diff --git a/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl b/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl index 372e366b74..b23b396bf7 100644 --- a/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl +++ b/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl @@ -1,41 +1,40 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: - FillArrays.AbstractFill{ElT,N,Axes} -z::FillArrays.Zeros{ElT,N,Axes} -function UnallocatedZeros{ElT,N,Alloc}(inds::Tuple) where {ElT,N,Alloc} - z = FillArrays.Zeros{ElT,N}(inds) - Axes = typeof(FillArrays.axes(z)) - return new{ElT,N,Axes,Alloc}(z) -end - - -function UnallocatedZeros{ElT,N,Alloc}(::Tuple{}) where {ElT,N,Alloc} - @assert N == 1 - z = FillArrays.Zeros{ElT,N}(1) - Axes = typeof(FillArrays.axes(z)) - return new{ElT,N,Axes,Alloc}(z) -end - -function UnallocatedZeros{ElT,N,Axes,Alloc}(inds::Tuple) where {ElT,N,Axes,Alloc} - @assert Axes == typeof(Base.axes(inds)) - z = FillArrays.Zeros{ElT,N}(inds) - return new{ElT,N,Axes,Alloc}(z) -end + FillArrays.AbstractFill{ElT,N,Axes} + z::FillArrays.Zeros{ElT,N,Axes} + function UnallocatedZeros{ElT,N,Alloc}(inds::Tuple) where {ElT,N,Alloc} + z = FillArrays.Zeros{ElT,N}(inds) + Axes = typeof(FillArrays.axes(z)) + return new{ElT,N,Axes,Alloc}(z) + end + + function UnallocatedZeros{ElT,N,Alloc}(::Tuple{}) where {ElT,N,Alloc} + @assert N == 1 + z = FillArrays.Zeros{ElT,N}(1) + Axes = typeof(FillArrays.axes(z)) + return new{ElT,N,Axes,Alloc}(z) + end + + function UnallocatedZeros{ElT,N,Axes,Alloc}(inds::Tuple) where {ElT,N,Axes,Alloc} + @assert Axes == typeof(Base.axes(inds)) + z = FillArrays.Zeros{ElT,N}(inds) + return new{ElT,N,Axes,Alloc}(z) + end end function UnallocatedZeros{ElT,N,Axes,Alloc}() where {ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} -return UnallocatedZeros{ElT,N,Axes,Alloc}[] + return UnallocatedZeros{ElT,N,Axes,Alloc}[] end function UnallocatedZeros(alloc::Type{<:AbstractArray}, inds...) -@assert ndims(alloc) == length(inds) -alloc = specify_eltype(alloc) -return UnallocatedZeros{eltype(alloc),ndims(alloc),alloc}(Tuple(inds)) + @assert ndims(alloc) == length(inds) + alloc = specify_eltype(alloc) + return UnallocatedZeros{eltype(alloc),ndims(alloc),alloc}(Tuple(inds)) end function UnallocatedZeros{ElT}(alloc::Type{<:AbstractArray}, inds...) where {ElT} -alloc = set_eltype(alloc, ElT) -N = length(Tuple(inds)) -alloc = set_ndims(alloc, N) -return UnallocatedZeros(alloc, inds...) + alloc = set_eltype(alloc, ElT) + N = length(Tuple(inds)) + alloc = set_ndims(alloc, N) + return UnallocatedZeros(alloc, inds...) end Base.ndims(::UnallocatedZeros{ElT,N}) where {ElT,N} = N @@ -43,7 +42,7 @@ ndims(::UnallocatedZeros{ElT,N}) where {ElT,N} = N Base.eltype(::UnallocatedZeros{ElT}) where {ElT} = ElT alloctype(::UnallocatedZeros{ElT,N,Axes,Alloc}) where {ElT,N,Axes,Alloc} = Alloc function alloctype(::Type{<:UnallocatedZeros{ElT,N,Axes,Alloc}}) where {ElT,N,Axes,Alloc} -return Alloc + return Alloc end axes(::Type{<:UnallocatedZeros{ElT,N,Axes}}) where {ElT,N,Axes} = Axes @@ -65,14 +64,14 @@ Base.vec(z::Type{<:UnallocatedZeros}) = z Base.vec(z::UnallocatedZeros) = z function Base.convert(x::Type{T}, z::UnallocatedZeros) where {T<:Base.Array} -return Base.convert(x, z.z) + return Base.convert(x, z.z) end function complex(z::UnallocatedZeros) -ElT = complex(eltype(z)) -N = ndims(z) -AllocT = similartype(alloctype(z), ElT) -return UnallocatedZeros{ElT,N,AllocT}(dims(z)) + ElT = complex(eltype(z)) + N = ndims(z) + AllocT = similartype(alloctype(z), ElT) + return UnallocatedZeros{ElT,N,AllocT}(dims(z)) end Base.sum(z::UnallocatedZeros) = sum(z.z) @@ -87,29 +86,29 @@ LinearAlgebra.norm(z::UnallocatedZeros) = norm(z.z) # end function to_shape(::Type{<:UnallocatedZeros}, dims::Tuple) -return NDTensors.to_shape(dims) + return NDTensors.to_shape(dims) end function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:UnallocatedZeros}) -ElT = promote_type(eltype(z1), eltype(z2)) -@assert ndims(z1) == ndims(z2) -Axs = axes(z1) -Alloc = promote_type(alloctype(z1), alloctype(z2)) -set_eltype(Alloc, ElT) -return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} + ElT = promote_type(eltype(z1), eltype(z2)) + @assert ndims(z1) == ndims(z2) + Axs = axes(z1) + Alloc = promote_type(alloctype(z1), alloctype(z2)) + set_eltype(Alloc, ElT) + return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} end function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:AbstractArray}) -ElT = promote_type(eltype(z1), eltype(z2)) -@assert ndims(z1) == ndims(z2) -Axs = axes(z1) -Alloc = promote_type(alloctype(z1), z2) -set_eltype(Alloc, ElT) -return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} + ElT = promote_type(eltype(z1), eltype(z2)) + @assert ndims(z1) == ndims(z2) + Axs = axes(z1) + Alloc = promote_type(alloctype(z1), z2) + set_eltype(Alloc, ElT) + return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} end function promote_rule(z1::Type{<:AbstractArray}, z2::Type{<:UnallocatedZeros}) -return promote_rule(z2, z1) + return promote_rule(z2, z1) end ## Check datatypes to see if underlying storage is a @@ -119,26 +118,26 @@ is_unallocated_zeros(a) = data_isa(a, UnallocatedZeros) FillArrays.getindex_value(Z::UnallocatedZeros) = FillArrays.getindex_value(Z.z) function generic_zeros(::Type{<:UnallocatedZeros}, inds::Integer) -elt = default_eltype() -datat = default_datatype(elt) -N = ndims(datat) -return UnallocatedZeros{elt,N,datat}(Tuple(dim)) + elt = default_eltype() + datat = default_datatype(elt) + N = ndims(datat) + return UnallocatedZeros{elt,N,datat}(Tuple(dim)) end function generic_zeros(::Type{<:UnallocatedZeros{ElT}}, inds::Integer) where {ElT} -datat = default_datatype(ElT) -N = ndims(datat) -return UnallocatedZeros{ElT,N,datat}(Tuple(dim)) + datat = default_datatype(ElT) + N = ndims(datat) + return UnallocatedZeros{ElT,N,datat}(Tuple(dim)) end function generic_zeros( -::Type{<:UnallocatedZeros{ElT,N,DataT}}, dim::Integer + ::Type{<:UnallocatedZeros{ElT,N,DataT}}, dim::Integer ) where {ElT,N,DataT<:AbstractArray{ElT,N}} -return UnallocatedZeros{ElT,N,DataT}(Tuple(dim)) + return UnallocatedZeros{ElT,N,DataT}(Tuple(dim)) end function generic_zeros( -::Type{<:UnallocatedZeros{ElT,N,Axes,DataT}}, dim::Integer + ::Type{<:UnallocatedZeros{ElT,N,Axes,DataT}}, dim::Integer ) where {ElT,N,Axes,DataT<:AbstractArray{ElT,N}} -return UnallocatedZeros{ElT,N,DataT}(Tuple(dim)) -end \ No newline at end of file + return UnallocatedZeros{ElT,N,DataT}(Tuple(dim)) +end diff --git a/NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl b/NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl index 3c722e5c60..e47e9832e5 100644 --- a/NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl +++ b/NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl @@ -1 +1 @@ -struct UnspecifiedArray{ElT, N} <: AbstractArray{ElT, N} end \ No newline at end of file +struct UnspecifiedArray{ElT,N} <: AbstractArray{ElT,N} end diff --git a/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiednumber.jl b/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiednumber.jl index 3de49b00bd..c44587b376 100644 --- a/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiednumber.jl +++ b/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiednumber.jl @@ -1,5 +1,5 @@ abstract type AbstractUnspecifiedNumber <: Number end struct UnspecifiedNumber{T} <: AbstractUnspecifiedNumber - value::T -end \ No newline at end of file + value::T +end diff --git a/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl b/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl index eb86c7579e..4ca1858218 100644 --- a/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl +++ b/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl @@ -1,5 +1,4 @@ -struct UnspecifiedZero <: AbstractUnspecifiedNumber -end +struct UnspecifiedZero <: AbstractUnspecifiedNumber end # Base.Complex{UnspecifiedZero}() = complex(UnspecifiedZero()) # function Base.Complex{UnspecifiedZero}(z::Real) @@ -37,4 +36,4 @@ Base.promote_type(z::Type{<:UnspecifiedZero}, ElT::Type) = Base.promote_type(ElT Base.promote_type(ElT::Type, ::Type{<:UnspecifiedZero}) = ElT Base.promote_type(::Type{<:UnspecifiedZero}, ::Type{<:UnspecifiedZero}) = UnspecifiedZero -Base.promote_type(ElT::Type, ::Type{<:Complex{<:UnspecifiedZero}}) = Complex{real(ElT)} \ No newline at end of file +Base.promote_type(ElT::Type, ::Type{<:Complex{<:UnspecifiedZero}}) = Complex{real(ElT)} From 86872e1ccff69805c16a41cf6c380bc2259aedcc Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Thu, 19 Oct 2023 16:41:23 -0400 Subject: [PATCH 003/156] Split up fieldtypes into Unallocated and Unspecified modules --- NDTensors/src/FieldTypes/README.md | 3 --- NDTensors/src/FieldTypes/src/FieldTypes.jl | 19 ------------------- NDTensors/src/NDTensors.jl | 6 ++++-- NDTensors/src/UnallocatedArrays/README.md | 4 ++++ .../src/UnallocatedArrays.jl | 10 ++++++++++ .../src}/unallocatedfill.jl | 0 .../src}/unallocatedzeros.jl | 2 +- NDTensors/src/UnspecifiedTypes/README.md | 3 +++ .../UnspecifiedTypes/src/UnspecifiedTypes.jl | 11 +++++++++++ .../src}/unspecifiedarray.jl | 0 .../src}/unspecifiednumber.jl | 0 .../src}/unspecifiedzero.jl | 0 12 files changed, 33 insertions(+), 25 deletions(-) delete mode 100644 NDTensors/src/FieldTypes/README.md delete mode 100644 NDTensors/src/FieldTypes/src/FieldTypes.jl create mode 100644 NDTensors/src/UnallocatedArrays/README.md create mode 100644 NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl rename NDTensors/src/{FieldTypes/src/unallocatedarray => UnallocatedArrays/src}/unallocatedfill.jl (100%) rename NDTensors/src/{FieldTypes/src/unallocatedarray => UnallocatedArrays/src}/unallocatedzeros.jl (99%) create mode 100644 NDTensors/src/UnspecifiedTypes/README.md create mode 100644 NDTensors/src/UnspecifiedTypes/src/UnspecifiedTypes.jl rename NDTensors/src/{FieldTypes/src/unspecifiedarray => UnspecifiedTypes/src}/unspecifiedarray.jl (100%) rename NDTensors/src/{FieldTypes/src/unspecifiednumber => UnspecifiedTypes/src}/unspecifiednumber.jl (100%) rename NDTensors/src/{FieldTypes/src/unspecifiednumber => UnspecifiedTypes/src}/unspecifiedzero.jl (100%) diff --git a/NDTensors/src/FieldTypes/README.md b/NDTensors/src/FieldTypes/README.md deleted file mode 100644 index 52cc6a0a35..0000000000 --- a/NDTensors/src/FieldTypes/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# FieldTypes - -A module defining a series of representative field types which are by nature lazy and unallocated. diff --git a/NDTensors/src/FieldTypes/src/FieldTypes.jl b/NDTensors/src/FieldTypes/src/FieldTypes.jl deleted file mode 100644 index ba914b5373..0000000000 --- a/NDTensors/src/FieldTypes/src/FieldTypes.jl +++ /dev/null @@ -1,19 +0,0 @@ -module FieldTypes - -using FillArrays -using LinearAlgebra - -#include("SetParameters/src/SetParameters.jl") -#using ..SetParameters - -include("unspecifiednumber/unspecifiednumber.jl") -include("unspecifiednumber/unspecifiedzero.jl") - -include("unallocatedarray/unallocatedfill.jl") -include("unallocatedarray/unallocatedzeros.jl") - -include("unspecifiedarray/unspecifiedarray.jl") - -export UnallocatedFill, - UnallocatedZeros, UnspecifiedArray, UnspecifiedNumber, UnspecifiedZero -end diff --git a/NDTensors/src/NDTensors.jl b/NDTensors/src/NDTensors.jl index 8322fc8c4f..bae9797b8c 100644 --- a/NDTensors/src/NDTensors.jl +++ b/NDTensors/src/NDTensors.jl @@ -20,8 +20,10 @@ using TupleTools include("SetParameters/src/SetParameters.jl") using .SetParameters -include("FieldTypes/src/FieldTypes.jl") -using .FieldTypes +include("UnspecifiedTypes/src/UnspecifiedTypes.jl") +using .UnspecifiedTypes +include("UnallocatedArrays/src/UnallocatedArrays.jl") +using .UnallocatedArrays include("BlockSparseArrays/src/BlockSparseArrays.jl") using .BlockSparseArrays include("SmallVectors/src/SmallVectors.jl") diff --git a/NDTensors/src/UnallocatedArrays/README.md b/NDTensors/src/UnallocatedArrays/README.md new file mode 100644 index 0000000000..f0e962a928 --- /dev/null +++ b/NDTensors/src/UnallocatedArrays/README.md @@ -0,0 +1,4 @@ +# UnallocatedArrays + +A module defining a set of unallocated immutable lazy arrays which will be used to quickly construct +tensors and allocating as little data as possible. diff --git a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl new file mode 100644 index 0000000000..0dad9a680c --- /dev/null +++ b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl @@ -0,0 +1,10 @@ +module UnallocatedArrays + +using FillArrays +using LinearAlgebra + +include("unallocatedfill.jl") +include("unallocatedzeros.jl") + +export UnallocatedFill, UnallocatedZeros +end diff --git a/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl similarity index 100% rename from NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl rename to NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl diff --git a/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl similarity index 99% rename from NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl rename to NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl index b23b396bf7..59df5f4cba 100644 --- a/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl @@ -1,5 +1,5 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: - FillArrays.AbstractFill{ElT,N,Axes} + FillArrays.AbstractZeros{ElT,N,Axes} z::FillArrays.Zeros{ElT,N,Axes} function UnallocatedZeros{ElT,N,Alloc}(inds::Tuple) where {ElT,N,Alloc} z = FillArrays.Zeros{ElT,N}(inds) diff --git a/NDTensors/src/UnspecifiedTypes/README.md b/NDTensors/src/UnspecifiedTypes/README.md new file mode 100644 index 0000000000..15ad85367d --- /dev/null +++ b/NDTensors/src/UnspecifiedTypes/README.md @@ -0,0 +1,3 @@ +# UnspecifiedTypes + +A module defining a set of basic types which are place holders for allocated bit-wise representable types. diff --git a/NDTensors/src/UnspecifiedTypes/src/UnspecifiedTypes.jl b/NDTensors/src/UnspecifiedTypes/src/UnspecifiedTypes.jl new file mode 100644 index 0000000000..f94a63a14c --- /dev/null +++ b/NDTensors/src/UnspecifiedTypes/src/UnspecifiedTypes.jl @@ -0,0 +1,11 @@ +module UnspecifiedTypes + +using LinearAlgebra + +include("unspecifiednumber.jl") +include("unspecifiedzero.jl") + +include("unspecifiedarray.jl") + +export UnspecifiedArray, UnspecifiedNumber, UnspecifiedZero +end diff --git a/NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl b/NDTensors/src/UnspecifiedTypes/src/unspecifiedarray.jl similarity index 100% rename from NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl rename to NDTensors/src/UnspecifiedTypes/src/unspecifiedarray.jl diff --git a/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiednumber.jl b/NDTensors/src/UnspecifiedTypes/src/unspecifiednumber.jl similarity index 100% rename from NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiednumber.jl rename to NDTensors/src/UnspecifiedTypes/src/unspecifiednumber.jl diff --git a/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl b/NDTensors/src/UnspecifiedTypes/src/unspecifiedzero.jl similarity index 100% rename from NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl rename to NDTensors/src/UnspecifiedTypes/src/unspecifiedzero.jl From 071ae9279da960747c20ece8141c2db1edcc77e4 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 23 Oct 2023 11:10:42 -0400 Subject: [PATCH 004/156] More UnallocatedFill implementation. --- .../UnallocatedArrays/src/unallocatedfill.jl | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl index 70247a1cd6..fb3949eb35 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl @@ -1 +1,29 @@ -struct UnallocatedFill{ElT,N,Axes} <: FillArrays.AbstractFill{ElT,N,Axes} end +struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray} <: FillArrays.AbstractFill{ElT,N,Axes} + f::FillArrays.Fill{ElT, N, Axes} + + function UnallocatedFill{ElT,N,Axes,Alloc}(x::ElT,inds::Tuple)where{ElT,N,Axes,Alloc} + f = FillArrays.Fill(x, inds) + ax = typeof(FillArrays.axes(f)) + new{ElT,N,ax,Alloc}(f) + end + + function UnallocatedFill{ElT,0,Tuple{},Alloc}(x::ElT, inds::Tuple{}) where{ElT,Alloc} + f = FillArrays.Fill{ElT,0,Tuple{}}(x,inds) + new{ElT,0,Tuple{},Alloc}(f) + end +end + +function UnallocatedFill{ElT, Alloc}(x::ElT, inds::Tuple) where {ElT<:Number, Alloc<:AbstractArray} + N = length(inds) + Ax = Base.axes(inds) + return UnallocatedFill{ElT, N, Ax,Alloc}(x, inds) +end + +alloctype(::UnallocatedFill{ElT,N,Axes,Alloc}) where {ElT,N,Axes,Alloc} = Alloc +alloctype(::Type{<:UnallocatedFill{ElT,N,Axes,Alloc}}) where {ElT,N,Axes,Alloc} = Alloc + +Base.axes(F::UnallocatedFill) = Base.axes(F.f) +Base.size(F::UnallocatedFill) = Base.size(F.f) +Base.length(F::UnallocatedFill) = Base.length(F.f) + +Base.print_array(io::IO, X::UnallocatedFill) = Base.print_array(io, X.f) From 1a849c9ddb3d8a6f6a496c25c496845b294f166d Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 23 Oct 2023 11:12:54 -0400 Subject: [PATCH 005/156] Start simplifying the UnallocatedZeros file --- .../UnallocatedArrays/src/unallocatedfill.jl | 2 + .../UnallocatedArrays/src/unallocatedzeros.jl | 45 +++++-------------- 2 files changed, 12 insertions(+), 35 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl index fb3949eb35..1c06b0518c 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl @@ -1,3 +1,5 @@ +## TODO All constructors not fully implemented but so far it matches the +## constructors found in `FillArrays`. Need to fix io struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray} <: FillArrays.AbstractFill{ElT,N,Axes} f::FillArrays.Fill{ElT, N, Axes} diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl index 59df5f4cba..39430a886c 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl @@ -1,52 +1,28 @@ +## TODO still working to make this implementation simplified struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: FillArrays.AbstractZeros{ElT,N,Axes} z::FillArrays.Zeros{ElT,N,Axes} - function UnallocatedZeros{ElT,N,Alloc}(inds::Tuple) where {ElT,N,Alloc} - z = FillArrays.Zeros{ElT,N}(inds) - Axes = typeof(FillArrays.axes(z)) - return new{ElT,N,Axes,Alloc}(z) - end - function UnallocatedZeros{ElT,N,Alloc}(::Tuple{}) where {ElT,N,Alloc} - @assert N == 1 - z = FillArrays.Zeros{ElT,N}(1) - Axes = typeof(FillArrays.axes(z)) - return new{ElT,N,Axes,Alloc}(z) + function UnallocatedZeros{ElT,N,Axes,Alloc}(inds::Tuple) where{ElT,N,Axes,Alloc} + z = FillArrays.Zeros(inds) + ax = typeof(FillArrays.axes(z)) + new{ElT,N,ax,Alloc}(z) end - function UnallocatedZeros{ElT,N,Axes,Alloc}(inds::Tuple) where {ElT,N,Axes,Alloc} - @assert Axes == typeof(Base.axes(inds)) - z = FillArrays.Zeros{ElT,N}(inds) - return new{ElT,N,Axes,Alloc}(z) + function UnallcoatedZeros{ElT,0,Tuple{},Alloc}(inds::Tuple{}) where{ElT,Alloc} + z = FillArrays.Zeros(inds) + new{ElT,0,Tuple{},Alloc}(z) end end -function UnallocatedZeros{ElT,N,Axes,Alloc}() where {ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} - return UnallocatedZeros{ElT,N,Axes,Alloc}[] -end -function UnallocatedZeros(alloc::Type{<:AbstractArray}, inds...) - @assert ndims(alloc) == length(inds) - alloc = specify_eltype(alloc) - return UnallocatedZeros{eltype(alloc),ndims(alloc),alloc}(Tuple(inds)) -end - -function UnallocatedZeros{ElT}(alloc::Type{<:AbstractArray}, inds...) where {ElT} - alloc = set_eltype(alloc, ElT) - N = length(Tuple(inds)) - alloc = set_ndims(alloc, N) - return UnallocatedZeros(alloc, inds...) -end -Base.ndims(::UnallocatedZeros{ElT,N}) where {ElT,N} = N -ndims(::UnallocatedZeros{ElT,N}) where {ElT,N} = N -Base.eltype(::UnallocatedZeros{ElT}) where {ElT} = ElT alloctype(::UnallocatedZeros{ElT,N,Axes,Alloc}) where {ElT,N,Axes,Alloc} = Alloc function alloctype(::Type{<:UnallocatedZeros{ElT,N,Axes,Alloc}}) where {ElT,N,Axes,Alloc} return Alloc end -axes(::Type{<:UnallocatedZeros{ElT,N,Axes}}) where {ElT,N,Axes} = Axes -Base.size(zero::UnallocatedZeros) = Base.size(zero.z) +Base.axes(::Type{<:UnallocatedZeros{ElT,N,Axes}}) where {ElT,N,Axes} = Axes +Base.size(Z::UnallocatedZeros) = Base.size(Z.z) Base.print_array(io::IO, X::UnallocatedZeros) = Base.print_array(io, X.z) @@ -60,7 +36,6 @@ dims(z::UnallocatedZeros) = Tuple(size(z.z)) dim(z::UnallocatedZeros) = dim(size(z.z)) copy(z::UnallocatedZeros) = UnallocatedZeros{eltype(z),ndims(z),alloctype(z)}(dims(z)) -Base.vec(z::Type{<:UnallocatedZeros}) = z Base.vec(z::UnallocatedZeros) = z function Base.convert(x::Type{T}, z::UnallocatedZeros) where {T<:Base.Array} From 297aaa2c179f972d70680107f48154d2e3c468f5 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 23 Oct 2023 11:20:48 -0400 Subject: [PATCH 006/156] Fix typo --- NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl index 39430a886c..6f2ece41f1 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl @@ -9,7 +9,7 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: new{ElT,N,ax,Alloc}(z) end - function UnallcoatedZeros{ElT,0,Tuple{},Alloc}(inds::Tuple{}) where{ElT,Alloc} + function UnallocatedZeros{ElT,0,Tuple{},Alloc}(inds::Tuple{}) where{ElT,Alloc} z = FillArrays.Zeros(inds) new{ElT,0,Tuple{},Alloc}(z) end From 3fc526c6c32637d3e923a3a319b857d0765582e6 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 23 Oct 2023 11:24:36 -0400 Subject: [PATCH 007/156] format --- .../UnallocatedArrays/src/unallocatedfill.jl | 21 +++++++++++-------- .../UnallocatedArrays/src/unallocatedzeros.jl | 9 ++++---- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl index 1c06b0518c..83a31475c4 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl @@ -1,24 +1,27 @@ ## TODO All constructors not fully implemented but so far it matches the ## constructors found in `FillArrays`. Need to fix io -struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray} <: FillArrays.AbstractFill{ElT,N,Axes} - f::FillArrays.Fill{ElT, N, Axes} +struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray} <: + FillArrays.AbstractFill{ElT,N,Axes} + f::FillArrays.Fill{ElT,N,Axes} - function UnallocatedFill{ElT,N,Axes,Alloc}(x::ElT,inds::Tuple)where{ElT,N,Axes,Alloc} + function UnallocatedFill{ElT,N,Axes,Alloc}(x::ElT, inds::Tuple) where {ElT,N,Axes,Alloc} f = FillArrays.Fill(x, inds) ax = typeof(FillArrays.axes(f)) - new{ElT,N,ax,Alloc}(f) + return new{ElT,N,ax,Alloc}(f) end - function UnallocatedFill{ElT,0,Tuple{},Alloc}(x::ElT, inds::Tuple{}) where{ElT,Alloc} - f = FillArrays.Fill{ElT,0,Tuple{}}(x,inds) - new{ElT,0,Tuple{},Alloc}(f) + function UnallocatedFill{ElT,0,Tuple{},Alloc}(x::ElT, inds::Tuple{}) where {ElT,Alloc} + f = FillArrays.Fill{ElT,0,Tuple{}}(x, inds) + return new{ElT,0,Tuple{},Alloc}(f) end end -function UnallocatedFill{ElT, Alloc}(x::ElT, inds::Tuple) where {ElT<:Number, Alloc<:AbstractArray} +function UnallocatedFill{ElT,Alloc}( + x::ElT, inds::Tuple +) where {ElT<:Number,Alloc<:AbstractArray} N = length(inds) Ax = Base.axes(inds) - return UnallocatedFill{ElT, N, Ax,Alloc}(x, inds) + return UnallocatedFill{ElT,N,Ax,Alloc}(x, inds) end alloctype(::UnallocatedFill{ElT,N,Axes,Alloc}) where {ElT,N,Axes,Alloc} = Alloc diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl index 6f2ece41f1..d96aca447e 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl @@ -3,19 +3,18 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: FillArrays.AbstractZeros{ElT,N,Axes} z::FillArrays.Zeros{ElT,N,Axes} - function UnallocatedZeros{ElT,N,Axes,Alloc}(inds::Tuple) where{ElT,N,Axes,Alloc} + function UnallocatedZeros{ElT,N,Axes,Alloc}(inds::Tuple) where {ElT,N,Axes,Alloc} z = FillArrays.Zeros(inds) ax = typeof(FillArrays.axes(z)) - new{ElT,N,ax,Alloc}(z) + return new{ElT,N,ax,Alloc}(z) end - function UnallocatedZeros{ElT,0,Tuple{},Alloc}(inds::Tuple{}) where{ElT,Alloc} + function UnallocatedZeros{ElT,0,Tuple{},Alloc}(inds::Tuple{}) where {ElT,Alloc} z = FillArrays.Zeros(inds) - new{ElT,0,Tuple{},Alloc}(z) + return new{ElT,0,Tuple{},Alloc}(z) end end - alloctype(::UnallocatedZeros{ElT,N,Axes,Alloc}) where {ElT,N,Axes,Alloc} = Alloc function alloctype(::Type{<:UnallocatedZeros{ElT,N,Axes,Alloc}}) where {ElT,N,Axes,Alloc} return Alloc From c9fb989737d3f1c10407ceb3ee4c2f3d8a66203f Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 23 Oct 2023 16:18:03 -0400 Subject: [PATCH 008/156] Update to the Unallocatedfill/zero code --- .../src/UnallocatedArrays.jl | 3 + NDTensors/src/UnallocatedArrays/src/import.jl | 6 ++ .../UnallocatedArrays/src/unallocated_impl.jl | 29 ++++++++ .../UnallocatedArrays/src/unallocatedfill.jl | 22 +----- .../UnallocatedArrays/src/unallocatedzeros.jl | 74 +------------------ 5 files changed, 43 insertions(+), 91 deletions(-) create mode 100644 NDTensors/src/UnallocatedArrays/src/import.jl create mode 100644 NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl diff --git a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl index 0dad9a680c..06e80d1814 100644 --- a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl @@ -3,8 +3,11 @@ module UnallocatedArrays using FillArrays using LinearAlgebra +include("import.jl") + include("unallocatedfill.jl") include("unallocatedzeros.jl") +include("unallocated_impl.jl") export UnallocatedFill, UnallocatedZeros end diff --git a/NDTensors/src/UnallocatedArrays/src/import.jl b/NDTensors/src/UnallocatedArrays/src/import.jl new file mode 100644 index 0000000000..ce278dadaf --- /dev/null +++ b/NDTensors/src/UnallocatedArrays/src/import.jl @@ -0,0 +1,6 @@ +import FillArrays: + axes, + convert, + getindex_value, + size, + sum \ No newline at end of file diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl new file mode 100644 index 0000000000..daad254420 --- /dev/null +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -0,0 +1,29 @@ +for Typ in (:UnallocatedFill, :UnallocatedZeros) + ## Here are functions specifically defined for UnallocatedArrays + ## not implemented by FillArrays + @eval begin + alloctype(A::$Typ) = alloctype(typeof(A)) + alloctype(::Type{<:$Typ{ElT,N,Axes,Alloc}}) where {ElT,N,Axes,Alloc} = Alloc + + get_index(A::$Typ) = getindex(data(A)) + + array(A::$Typ) = alloctype(typeof(A))(data(A)) + Array(A::$Typ) = array(A) + ## A function for NDTensors to launch functions off of + is_immutable(A::$Typ) = is_immutable(typeof(A)) + is_immutable(::Type{<:$Typ}) = true + + end + ## TODO I don't think this is the correct way to call + ## functions which are defined in `FillArrays` + for fun in (:size, :length, :convert, :sum, :getindex_value) + @eval FillArrays.$fun(A::$Typ) = $fun(data(A)) + end + ## TODO Here I am defining LinearAlgebra functions in one sweep + for fun in (:norm,) + @eval LinearAlgebra.$fun(A::$Typ) = $fun(data(A)) + end + +end + +copy(F::UnallocatedFill) = UnallocatedFill{eltype(F), ndims(F), axes(F), alloctype(F)}(data(F)[1], size(F)) \ No newline at end of file diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl index 83a31475c4..bf1ee4b976 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl @@ -9,26 +9,6 @@ struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray} <: ax = typeof(FillArrays.axes(f)) return new{ElT,N,ax,Alloc}(f) end - - function UnallocatedFill{ElT,0,Tuple{},Alloc}(x::ElT, inds::Tuple{}) where {ElT,Alloc} - f = FillArrays.Fill{ElT,0,Tuple{}}(x, inds) - return new{ElT,0,Tuple{},Alloc}(f) - end -end - -function UnallocatedFill{ElT,Alloc}( - x::ElT, inds::Tuple -) where {ElT<:Number,Alloc<:AbstractArray} - N = length(inds) - Ax = Base.axes(inds) - return UnallocatedFill{ElT,N,Ax,Alloc}(x, inds) end -alloctype(::UnallocatedFill{ElT,N,Axes,Alloc}) where {ElT,N,Axes,Alloc} = Alloc -alloctype(::Type{<:UnallocatedFill{ElT,N,Axes,Alloc}}) where {ElT,N,Axes,Alloc} = Alloc - -Base.axes(F::UnallocatedFill) = Base.axes(F.f) -Base.size(F::UnallocatedFill) = Base.size(F.f) -Base.length(F::UnallocatedFill) = Base.length(F.f) - -Base.print_array(io::IO, X::UnallocatedFill) = Base.print_array(io, X.f) +data(F::UnallocatedFill) = F.f diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl index d96aca447e..f794508a73 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl @@ -8,38 +8,11 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: ax = typeof(FillArrays.axes(z)) return new{ElT,N,ax,Alloc}(z) end - - function UnallocatedZeros{ElT,0,Tuple{},Alloc}(inds::Tuple{}) where {ElT,Alloc} - z = FillArrays.Zeros(inds) - return new{ElT,0,Tuple{},Alloc}(z) - end end -alloctype(::UnallocatedZeros{ElT,N,Axes,Alloc}) where {ElT,N,Axes,Alloc} = Alloc -function alloctype(::Type{<:UnallocatedZeros{ElT,N,Axes,Alloc}}) where {ElT,N,Axes,Alloc} - return Alloc -end - -Base.axes(::Type{<:UnallocatedZeros{ElT,N,Axes}}) where {ElT,N,Axes} = Axes -Base.size(Z::UnallocatedZeros) = Base.size(Z.z) - -Base.print_array(io::IO, X::UnallocatedZeros) = Base.print_array(io, X.z) - -data(zero::UnallocatedZeros) = zero.z -getindex(zero::UnallocatedZeros) = getindex(zero.z) - -array(zero::UnallocatedZeros) = alloctype(zero)(zero.z) -Array(zero::UnallocatedZeros) = array(zero) -axes(z::UnallocatedZeros) = axes(z.z) -dims(z::UnallocatedZeros) = Tuple(size(z.z)) -dim(z::UnallocatedZeros) = dim(size(z.z)) -copy(z::UnallocatedZeros) = UnallocatedZeros{eltype(z),ndims(z),alloctype(z)}(dims(z)) - -Base.vec(z::UnallocatedZeros) = z - -function Base.convert(x::Type{T}, z::UnallocatedZeros) where {T<:Base.Array} - return Base.convert(x, z.z) -end +data(Z::UnallocatedZeros) = Z.z +copy(Z::UnallocatedZeros) = typeof(Z)(size(Z)) +Base.vec(Z::UnallocatedZeros) = typeof(Z)(length(Z)) function complex(z::UnallocatedZeros) ElT = complex(eltype(z)) @@ -48,48 +21,9 @@ function complex(z::UnallocatedZeros) return UnallocatedZeros{ElT,N,AllocT}(dims(z)) end -Base.sum(z::UnallocatedZeros) = sum(z.z) -LinearAlgebra.norm(z::UnallocatedZeros) = norm(z.z) - -# function (arraytype::Type{<:UnallocatedZeros})(::AllowAlias, A::UnallocatedZeros) -# return A -# end - -# function (arraytype::Type{<:UnallocatedZeros})(::NeverAlias, A::UnallocatedZeros) -# return copy(A) -# end - -function to_shape(::Type{<:UnallocatedZeros}, dims::Tuple) - return NDTensors.to_shape(dims) -end - -function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:UnallocatedZeros}) - ElT = promote_type(eltype(z1), eltype(z2)) - @assert ndims(z1) == ndims(z2) - Axs = axes(z1) - Alloc = promote_type(alloctype(z1), alloctype(z2)) - set_eltype(Alloc, ElT) - return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} -end - -function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:AbstractArray}) - ElT = promote_type(eltype(z1), eltype(z2)) - @assert ndims(z1) == ndims(z2) - Axs = axes(z1) - Alloc = promote_type(alloctype(z1), z2) - set_eltype(Alloc, ElT) - return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} -end - -function promote_rule(z1::Type{<:AbstractArray}, z2::Type{<:UnallocatedZeros}) - return promote_rule(z2, z1) -end - ## Check datatypes to see if underlying storage is a ## UnallocatedZeros -is_unallocated_zeros(a) = data_isa(a, UnallocatedZeros) - -FillArrays.getindex_value(Z::UnallocatedZeros) = FillArrays.getindex_value(Z.z) +#is_unallocated_zeros(a) = data_isa(a, UnallocatedZeros) function generic_zeros(::Type{<:UnallocatedZeros}, inds::Integer) elt = default_eltype() From e0e365276a329ff017d8b2f093e2014b10c2f006 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 23 Oct 2023 16:18:56 -0400 Subject: [PATCH 009/156] format --- NDTensors/src/UnallocatedArrays/src/import.jl | 7 +------ NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl | 6 +++--- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/import.jl b/NDTensors/src/UnallocatedArrays/src/import.jl index ce278dadaf..2f39355bc6 100644 --- a/NDTensors/src/UnallocatedArrays/src/import.jl +++ b/NDTensors/src/UnallocatedArrays/src/import.jl @@ -1,6 +1 @@ -import FillArrays: - axes, - convert, - getindex_value, - size, - sum \ No newline at end of file +import FillArrays: axes, convert, getindex_value, size, sum diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index daad254420..cab9eb1298 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -12,7 +12,6 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) ## A function for NDTensors to launch functions off of is_immutable(A::$Typ) = is_immutable(typeof(A)) is_immutable(::Type{<:$Typ}) = true - end ## TODO I don't think this is the correct way to call ## functions which are defined in `FillArrays` @@ -23,7 +22,8 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) for fun in (:norm,) @eval LinearAlgebra.$fun(A::$Typ) = $fun(data(A)) end - end -copy(F::UnallocatedFill) = UnallocatedFill{eltype(F), ndims(F), axes(F), alloctype(F)}(data(F)[1], size(F)) \ No newline at end of file +function copy(F::UnallocatedFill) + return UnallocatedFill{eltype(F),ndims(F),axes(F),alloctype(F)}(data(F)[1], size(F)) +end From ae13d96937aa8d98906e039a31e5e8108ae6aa56 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 24 Oct 2023 13:25:48 -0400 Subject: [PATCH 010/156] Updates to code. Still working on all of these --- .../UnallocatedArrays/src/unallocated_impl.jl | 27 +++++----- .../UnallocatedArrays/src/unallocatedfill.jl | 9 +--- .../UnallocatedArrays/src/unallocatedzeros.jl | 52 +++---------------- 3 files changed, 23 insertions(+), 65 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index cab9eb1298..962aafaa33 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -1,29 +1,28 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) ## Here are functions specifically defined for UnallocatedArrays ## not implemented by FillArrays + ## TODO use set_parameters/get_parameters instead of alloctype and other + ## type info functions. + ## TODO determine min number of functions needed to be forwarded @eval begin alloctype(A::$Typ) = alloctype(typeof(A)) alloctype(::Type{<:$Typ{ElT,N,Axes,Alloc}}) where {ElT,N,Axes,Alloc} = Alloc - get_index(A::$Typ) = getindex(data(A)) + getindex(A::$Typ, i...) = getindex(parent(A), i...) - array(A::$Typ) = alloctype(typeof(A))(data(A)) - Array(A::$Typ) = array(A) - ## A function for NDTensors to launch functions off of - is_immutable(A::$Typ) = is_immutable(typeof(A)) - is_immutable(::Type{<:$Typ}) = true + Array(A::$Typ) = alloctype(typeof(A)) + + Base.copy(A::$Typ) = A + ## TODO Implement vec + # Base.vec(Z::UnallocatedZeros) = typeof(Z)(length(Z)) end - ## TODO I don't think this is the correct way to call - ## functions which are defined in `FillArrays` + + ## TODO forwarding functions to fillarrays for fun in (:size, :length, :convert, :sum, :getindex_value) - @eval FillArrays.$fun(A::$Typ) = $fun(data(A)) + @eval FillArrays.$fun(A::$Typ) = $fun(parent(A)) end ## TODO Here I am defining LinearAlgebra functions in one sweep for fun in (:norm,) - @eval LinearAlgebra.$fun(A::$Typ) = $fun(data(A)) + @eval LinearAlgebra.$fun(A::$Typ) = $fun(parent(A)) end end - -function copy(F::UnallocatedFill) - return UnallocatedFill{eltype(F),ndims(F),axes(F),alloctype(F)}(data(F)[1], size(F)) -end diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl index bf1ee4b976..7bef529533 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl @@ -3,12 +3,7 @@ struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray} <: FillArrays.AbstractFill{ElT,N,Axes} f::FillArrays.Fill{ElT,N,Axes} - - function UnallocatedFill{ElT,N,Axes,Alloc}(x::ElT, inds::Tuple) where {ElT,N,Axes,Alloc} - f = FillArrays.Fill(x, inds) - ax = typeof(FillArrays.axes(f)) - return new{ElT,N,ax,Alloc}(f) - end + ## TODO use `set_parameters` as constructor to these types end -data(F::UnallocatedFill) = F.f +parent(F::UnallocatedFill) = F.f diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl index f794508a73..b5ff051144 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl @@ -2,50 +2,14 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: FillArrays.AbstractZeros{ElT,N,Axes} z::FillArrays.Zeros{ElT,N,Axes} - - function UnallocatedZeros{ElT,N,Axes,Alloc}(inds::Tuple) where {ElT,N,Axes,Alloc} - z = FillArrays.Zeros(inds) - ax = typeof(FillArrays.axes(z)) - return new{ElT,N,ax,Alloc}(z) - end -end - -data(Z::UnallocatedZeros) = Z.z -copy(Z::UnallocatedZeros) = typeof(Z)(size(Z)) -Base.vec(Z::UnallocatedZeros) = typeof(Z)(length(Z)) - -function complex(z::UnallocatedZeros) - ElT = complex(eltype(z)) - N = ndims(z) - AllocT = similartype(alloctype(z), ElT) - return UnallocatedZeros{ElT,N,AllocT}(dims(z)) + ## TODO use `set_parameters` as constructor to these types end -## Check datatypes to see if underlying storage is a -## UnallocatedZeros -#is_unallocated_zeros(a) = data_isa(a, UnallocatedZeros) - -function generic_zeros(::Type{<:UnallocatedZeros}, inds::Integer) - elt = default_eltype() - datat = default_datatype(elt) - N = ndims(datat) - return UnallocatedZeros{elt,N,datat}(Tuple(dim)) -end +parent(Z::UnallocatedZeros) = Z.z -function generic_zeros(::Type{<:UnallocatedZeros{ElT}}, inds::Integer) where {ElT} - datat = default_datatype(ElT) - N = ndims(datat) - return UnallocatedZeros{ElT,N,datat}(Tuple(dim)) -end - -function generic_zeros( - ::Type{<:UnallocatedZeros{ElT,N,DataT}}, dim::Integer -) where {ElT,N,DataT<:AbstractArray{ElT,N}} - return UnallocatedZeros{ElT,N,DataT}(Tuple(dim)) -end - -function generic_zeros( - ::Type{<:UnallocatedZeros{ElT,N,Axes,DataT}}, dim::Integer -) where {ElT,N,Axes,DataT<:AbstractArray{ElT,N}} - return UnallocatedZeros{ElT,N,DataT}(Tuple(dim)) -end +# function complex(z::UnallocatedZeros) +# ElT = complex(eltype(z)) +# N = ndims(z) +# AllocT = similartype(alloctype(z), ElT) +# return UnallocatedZeros{ElT,N,AllocT}(dims(z)) +# end From 3839f88ec5cf16566e39f3b1ae491c023dd699ab Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 24 Oct 2023 13:25:59 -0400 Subject: [PATCH 011/156] Add some promote rules. Still working here --- .../UnallocatedArrays/src/promote_rules.jl | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 NDTensors/src/UnallocatedArrays/src/promote_rules.jl diff --git a/NDTensors/src/UnallocatedArrays/src/promote_rules.jl b/NDTensors/src/UnallocatedArrays/src/promote_rules.jl new file mode 100644 index 0000000000..0e8e776516 --- /dev/null +++ b/NDTensors/src/UnallocatedArrays/src/promote_rules.jl @@ -0,0 +1,23 @@ +## TODO this section is not finished. I just pasted this from the previous PR. +## Still working here +function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:UnallocatedZeros}) + ElT = promote_type(eltype(z1), eltype(z2)) + @assert ndims(z1) == ndims(z2) + Axs = axes(z1) + Alloc = promote_type(alloctype(z1), alloctype(z2)) + set_eltype(Alloc, ElT) + return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} +end + +function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:AbstractArray}) + ElT = promote_type(eltype(z1), eltype(z2)) + @assert ndims(z1) == ndims(z2) + Axs = axes(z1) + Alloc = promote_type(alloctype(z1), z2) + set_eltype(Alloc, ElT) + return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} +end + +function promote_rule(z1::Type{<:AbstractArray}, z2::Type{<:UnallocatedZeros}) + return promote_rule(z2, z1) +end From c1b834fa8f51d9bae91f272524d45f884c0630f7 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 24 Oct 2023 13:26:16 -0400 Subject: [PATCH 012/156] format --- NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index 962aafaa33..f20cccb659 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -10,7 +10,7 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) getindex(A::$Typ, i...) = getindex(parent(A), i...) - Array(A::$Typ) = alloctype(typeof(A)) + Array(A::$Typ) = alloctype(typeof(A)) Base.copy(A::$Typ) = A ## TODO Implement vec From e2c59cbe289966c3d983688b21e7afba4f40ceb2 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 2 Nov 2023 17:54:17 -0400 Subject: [PATCH 013/156] Start working on Unallocated settype functions --- NDTensors/src/UnallocatedArrays/src/set_types.jl | 15 +++++++++++++++ .../src/UnallocatedArrays/src/unallocated_impl.jl | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 NDTensors/src/UnallocatedArrays/src/set_types.jl diff --git a/NDTensors/src/UnallocatedArrays/src/set_types.jl b/NDTensors/src/UnallocatedArrays/src/set_types.jl new file mode 100644 index 0000000000..54ea015236 --- /dev/null +++ b/NDTensors/src/UnallocatedArrays/src/set_types.jl @@ -0,0 +1,15 @@ +## TODO I have just started working on this, it needs more work still. +for Typ in (:UnallocatedFill, :UnallocatedZeros) + # `SetParameters.jl` overloads. + get_parameter(::Type{<:$Typ{P1}}, ::Position{1}) where {P1} = P1 + get_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{2}) where {P2} = P2 + get_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 + get_parameter(::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{4}) where {P4} = P4 + + default_parameter(::Type{<:$Typ}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros + default_parameter(::Type{<:$Typ}, ::Position{2}) = 0 + default_parameter(::Type{<:$Typ}, ::Position{3}) = Tuple{} + default_parameter(::Type{<:$Typ}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray + + nparameters(::Type{<:$Typ}) = Val(4) +end \ No newline at end of file diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index f20cccb659..b453ad9691 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -5,8 +5,8 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) ## type info functions. ## TODO determine min number of functions needed to be forwarded @eval begin - alloctype(A::$Typ) = alloctype(typeof(A)) - alloctype(::Type{<:$Typ{ElT,N,Axes,Alloc}}) where {ElT,N,Axes,Alloc} = Alloc + alloctype(A::$Typ) = alloctype($Typ) + alloctype(::Type{<:$Typ}) = get_parameter($Typ, Position{4}) getindex(A::$Typ, i...) = getindex(parent(A), i...) From 145882e171a198bd8f8700268e3dd6e6f998a2c2 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 2 Nov 2023 17:55:16 -0400 Subject: [PATCH 014/156] format --- .../src/UnallocatedArrays/src/set_types.jl | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/set_types.jl b/NDTensors/src/UnallocatedArrays/src/set_types.jl index 54ea015236..6ffdd3fc3a 100644 --- a/NDTensors/src/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/UnallocatedArrays/src/set_types.jl @@ -1,15 +1,15 @@ ## TODO I have just started working on this, it needs more work still. for Typ in (:UnallocatedFill, :UnallocatedZeros) - # `SetParameters.jl` overloads. - get_parameter(::Type{<:$Typ{P1}}, ::Position{1}) where {P1} = P1 - get_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{2}) where {P2} = P2 - get_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 - get_parameter(::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{4}) where {P4} = P4 + # `SetParameters.jl` overloads. + get_parameter(::Type{<:$Typ{P1}}, ::Position{1}) where {P1} = P1 + get_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{2}) where {P2} = P2 + get_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 + get_parameter(::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{4}) where {P4} = P4 - default_parameter(::Type{<:$Typ}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros - default_parameter(::Type{<:$Typ}, ::Position{2}) = 0 - default_parameter(::Type{<:$Typ}, ::Position{3}) = Tuple{} - default_parameter(::Type{<:$Typ}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray + default_parameter(::Type{<:$Typ}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros + default_parameter(::Type{<:$Typ}, ::Position{2}) = 0 + default_parameter(::Type{<:$Typ}, ::Position{3}) = Tuple{} + default_parameter(::Type{<:$Typ}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray - nparameters(::Type{<:$Typ}) = Val(4) -end \ No newline at end of file + nparameters(::Type{<:$Typ}) = Val(4) +end From b5ac4cebffed6d55c19490a1390b69bed0f4ad96 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 8 Nov 2023 15:39:54 -0500 Subject: [PATCH 015/156] Fix set_types --- .../src/UnallocatedArrays/src/set_types.jl | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/set_types.jl b/NDTensors/src/UnallocatedArrays/src/set_types.jl index 6ffdd3fc3a..392d03f0c9 100644 --- a/NDTensors/src/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/UnallocatedArrays/src/set_types.jl @@ -1,15 +1,17 @@ ## TODO I have just started working on this, it needs more work still. for Typ in (:UnallocatedFill, :UnallocatedZeros) - # `SetParameters.jl` overloads. - get_parameter(::Type{<:$Typ{P1}}, ::Position{1}) where {P1} = P1 - get_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{2}) where {P2} = P2 - get_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 - get_parameter(::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{4}) where {P4} = P4 + @eval begin + # `SetParameters.jl` overloads. + SetParameters.get_parameter(::Type{<:$Typ{P1}}, ::Position{1}) where {P1} = P1 + SetParameters.get_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{2}) where {P2} = P2 + SetParameters.get_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 + SetParameters.get_parameter(::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{4}) where {P4} = P4 - default_parameter(::Type{<:$Typ}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros - default_parameter(::Type{<:$Typ}, ::Position{2}) = 0 - default_parameter(::Type{<:$Typ}, ::Position{3}) = Tuple{} - default_parameter(::Type{<:$Typ}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray + SetParameters.default_parameter(::Type{<:$Typ}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros + SetParameters.default_parameter(::Type{<:$Typ}, ::Position{2}) = 0 + SetParameters.default_parameter(::Type{<:$Typ}, ::Position{3}) = Tuple{} + SetParameters.default_parameter(::Type{<:$Typ}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray - nparameters(::Type{<:$Typ}) = Val(4) + nparameters(::Type{<:$Typ}) = Val(4) + end end From c1366858edf2027a89402808c194d07e7659bafc Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 8 Nov 2023 15:40:14 -0500 Subject: [PATCH 016/156] formatting --- .../UnallocatedArrays/src/UnallocatedArrays.jl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl index 06e80d1814..4c3ee117b3 100644 --- a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,13 +1,14 @@ module UnallocatedArrays + using FillArrays + using LinearAlgebra + using NDTensors.SetParameters -using FillArrays -using LinearAlgebra + include("import.jl") -include("import.jl") + include("unallocatedfill.jl") + include("unallocatedzeros.jl") + include("set_types.jl") + include("unallocated_impl.jl") -include("unallocatedfill.jl") -include("unallocatedzeros.jl") -include("unallocated_impl.jl") - -export UnallocatedFill, UnallocatedZeros + export UnallocatedFill, UnallocatedZeros end From 4d1b976ca7c570bc27df9016281a7397d2fb6679 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 8 Nov 2023 15:40:52 -0500 Subject: [PATCH 017/156] format --- .../UnallocatedArrays/src/UnallocatedArrays.jl | 18 +++++++++--------- .../src/UnallocatedArrays/src/set_types.jl | 13 +++++++++---- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl index 4c3ee117b3..094d7b2a15 100644 --- a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,14 +1,14 @@ module UnallocatedArrays - using FillArrays - using LinearAlgebra - using NDTensors.SetParameters +using FillArrays +using LinearAlgebra +using NDTensors.SetParameters - include("import.jl") +include("import.jl") - include("unallocatedfill.jl") - include("unallocatedzeros.jl") - include("set_types.jl") - include("unallocated_impl.jl") +include("unallocatedfill.jl") +include("unallocatedzeros.jl") +include("set_types.jl") +include("unallocated_impl.jl") - export UnallocatedFill, UnallocatedZeros +export UnallocatedFill, UnallocatedZeros end diff --git a/NDTensors/src/UnallocatedArrays/src/set_types.jl b/NDTensors/src/UnallocatedArrays/src/set_types.jl index 392d03f0c9..cedb71cf50 100644 --- a/NDTensors/src/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/UnallocatedArrays/src/set_types.jl @@ -4,13 +4,18 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) # `SetParameters.jl` overloads. SetParameters.get_parameter(::Type{<:$Typ{P1}}, ::Position{1}) where {P1} = P1 SetParameters.get_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{2}) where {P2} = P2 - SetParameters.get_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 - SetParameters.get_parameter(::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{4}) where {P4} = P4 + SetParameters.get_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = + P3 + SetParameters.get_parameter( + ::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{4} + ) where {P4} = P4 - SetParameters.default_parameter(::Type{<:$Typ}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros + SetParameters.default_parameter(::Type{<:$Typ}, ::Position{1}) = + UnspecifiedTypes.UnallocatedZeros SetParameters.default_parameter(::Type{<:$Typ}, ::Position{2}) = 0 SetParameters.default_parameter(::Type{<:$Typ}, ::Position{3}) = Tuple{} - SetParameters.default_parameter(::Type{<:$Typ}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray + SetParameters.default_parameter(::Type{<:$Typ}, ::Position{4}) = + UnspecifiedTypes.UnspecifiedArray nparameters(::Type{<:$Typ}) = Val(4) end From 4e265ce4ba359f8085c3ca58398b9d373a943e57 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 8 Nov 2023 15:55:06 -0500 Subject: [PATCH 018/156] Update UnallocatedArrays --- NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl | 2 +- NDTensors/src/UnallocatedArrays/src/import.jl | 1 + NDTensors/src/UnallocatedArrays/src/set_types.jl | 2 +- NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl | 2 +- NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl | 2 +- NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl | 2 +- 6 files changed, 6 insertions(+), 5 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl index 094d7b2a15..7d9670e9bc 100644 --- a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl @@ -10,5 +10,5 @@ include("unallocatedzeros.jl") include("set_types.jl") include("unallocated_impl.jl") -export UnallocatedFill, UnallocatedZeros +export UnallocatedFill, UnallocatedZeros, alloctype end diff --git a/NDTensors/src/UnallocatedArrays/src/import.jl b/NDTensors/src/UnallocatedArrays/src/import.jl index 2f39355bc6..594f863d1c 100644 --- a/NDTensors/src/UnallocatedArrays/src/import.jl +++ b/NDTensors/src/UnallocatedArrays/src/import.jl @@ -1 +1,2 @@ import FillArrays: axes, convert, getindex_value, size, sum +import Base: getindex, copy \ No newline at end of file diff --git a/NDTensors/src/UnallocatedArrays/src/set_types.jl b/NDTensors/src/UnallocatedArrays/src/set_types.jl index cedb71cf50..5182608b35 100644 --- a/NDTensors/src/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/UnallocatedArrays/src/set_types.jl @@ -17,6 +17,6 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) SetParameters.default_parameter(::Type{<:$Typ}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray - nparameters(::Type{<:$Typ}) = Val(4) + SetParameters.nparameters(::Type{<:$Typ}) = Val(4) end end diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index b453ad9691..f238b93351 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -12,7 +12,7 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) Array(A::$Typ) = alloctype(typeof(A)) - Base.copy(A::$Typ) = A + copy(A::$Typ) = A ## TODO Implement vec # Base.vec(Z::UnallocatedZeros) = typeof(Z)(length(Z)) end diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl index 7bef529533..b114ca3502 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl @@ -6,4 +6,4 @@ struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray} <: ## TODO use `set_parameters` as constructor to these types end -parent(F::UnallocatedFill) = F.f +Base.parent(F::UnallocatedFill) = F.f diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl index b5ff051144..8ec9a8b8f2 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl @@ -5,7 +5,7 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: ## TODO use `set_parameters` as constructor to these types end -parent(Z::UnallocatedZeros) = Z.z +Base.parent(Z::UnallocatedZeros) = Z.z # function complex(z::UnallocatedZeros) # ElT = complex(eltype(z)) From 2b2a6b284641946f78920ba2bb47eec76fab3014 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 8 Nov 2023 15:55:29 -0500 Subject: [PATCH 019/156] format --- NDTensors/src/UnallocatedArrays/src/import.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/UnallocatedArrays/src/import.jl b/NDTensors/src/UnallocatedArrays/src/import.jl index 594f863d1c..1d354e7cde 100644 --- a/NDTensors/src/UnallocatedArrays/src/import.jl +++ b/NDTensors/src/UnallocatedArrays/src/import.jl @@ -1,2 +1,2 @@ import FillArrays: axes, convert, getindex_value, size, sum -import Base: getindex, copy \ No newline at end of file +import Base: getindex, copy From 305c47be81f7685b21afb431447008a6ad58c74a Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 8 Nov 2023 16:38:17 -0500 Subject: [PATCH 020/156] CuArray name change, fix warning --- NDTensors/ext/NDTensorsCUDAExt/imports.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NDTensors/ext/NDTensorsCUDAExt/imports.jl b/NDTensors/ext/NDTensorsCUDAExt/imports.jl index 96b8383c74..eb525ed2d3 100644 --- a/NDTensors/ext/NDTensorsCUDAExt/imports.jl +++ b/NDTensors/ext/NDTensorsCUDAExt/imports.jl @@ -3,4 +3,5 @@ import NDTensors: ContractionProperties, _contract!, GemmBackend, auto_select_backend, _gemm!, iscu import NDTensors.SetParameters: nparameters, get_parameter, set_parameter, default_parameter -import .CUDA: CuArrayAdaptor +## CUDA changed names from CuArrayAdaptor to this name but we shouldn't need either +#import .CUDA: CuArrayKernelAdaptor From 1468c690e1edccbeb7f83adeb9e5e5bf00c247d4 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 8 Nov 2023 16:39:38 -0500 Subject: [PATCH 021/156] Some fixes --- NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl | 8 +++++--- NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index f238b93351..ef695afc89 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -5,10 +5,12 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) ## type info functions. ## TODO determine min number of functions needed to be forwarded @eval begin - alloctype(A::$Typ) = alloctype($Typ) - alloctype(::Type{<:$Typ}) = get_parameter($Typ, Position{4}) + alloctype(A::$Typ) = alloctype(typeof(A)) + alloctype(Atype::Type{<:$Typ}) = get_parameter(Atype, Position{4}()) - getindex(A::$Typ, i...) = getindex(parent(A), i...) + # function getindex(A::$Typ, i...) + # parent(A)[i...] + # end Array(A::$Typ) = alloctype(typeof(A)) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl index 8ec9a8b8f2..85e958f599 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl @@ -1,5 +1,6 @@ -## TODO still working to make this implementation simplified -struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: +## TODO Should Alloc also be of ElT and N or should there be +## More freedom there? +struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray} <: FillArrays.AbstractZeros{ElT,N,Axes} z::FillArrays.Zeros{ElT,N,Axes} ## TODO use `set_parameters` as constructor to these types From a25ec05302e53529e2ada98646d578f74e384afe Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 8 Nov 2023 16:40:10 -0500 Subject: [PATCH 022/156] Add comment --- NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index ef695afc89..81bc5f4e51 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -8,6 +8,8 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) alloctype(A::$Typ) = alloctype(typeof(A)) alloctype(Atype::Type{<:$Typ}) = get_parameter(Atype, Position{4}()) + ## something is wrong when this function is + ## called an error is thrown. # function getindex(A::$Typ, i...) # parent(A)[i...] # end From a824fabc9e8035e366f5f86980e535506a9e4a86 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 8 Nov 2023 16:47:00 -0500 Subject: [PATCH 023/156] I don't need getindex function and `[I...]` still works --- NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl | 6 ------ 1 file changed, 6 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index 81bc5f4e51..0fd9398816 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -8,12 +8,6 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) alloctype(A::$Typ) = alloctype(typeof(A)) alloctype(Atype::Type{<:$Typ}) = get_parameter(Atype, Position{4}()) - ## something is wrong when this function is - ## called an error is thrown. - # function getindex(A::$Typ, i...) - # parent(A)[i...] - # end - Array(A::$Typ) = alloctype(typeof(A)) copy(A::$Typ) = A From 0fdb0a48bb19cb18871360235175770089c5019c Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 8 Nov 2023 16:50:15 -0500 Subject: [PATCH 024/156] Convert is implemented wrong --- NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index 0fd9398816..f5eec90ee1 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -16,7 +16,8 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) end ## TODO forwarding functions to fillarrays - for fun in (:size, :length, :convert, :sum, :getindex_value) + ## convert doesn't work + for fun in (:size, :length, :sum, :getindex_value) @eval FillArrays.$fun(A::$Typ) = $fun(parent(A)) end ## TODO Here I am defining LinearAlgebra functions in one sweep From fe051c53cba0cd4417b84c9ed7f89519f4367daf Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 09:51:38 -0500 Subject: [PATCH 025/156] Import setparameters --- NDTensors/src/UnallocatedArrays/src/import.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/NDTensors/src/UnallocatedArrays/src/import.jl b/NDTensors/src/UnallocatedArrays/src/import.jl index 1d354e7cde..02fe2cd875 100644 --- a/NDTensors/src/UnallocatedArrays/src/import.jl +++ b/NDTensors/src/UnallocatedArrays/src/import.jl @@ -1,2 +1,3 @@ import FillArrays: axes, convert, getindex_value, size, sum import Base: getindex, copy +import NDTensors.SetParameters: nparameters, get_parameter, set_parameter, default_parameter From 547fcd7944fe03268d51126cdb9d50d262b06c9e Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 09:51:52 -0500 Subject: [PATCH 026/156] Working on set_parameter --- .../src/UnallocatedArrays/src/set_types.jl | 67 ++++++++++++++++--- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/set_types.jl b/NDTensors/src/UnallocatedArrays/src/set_types.jl index 5182608b35..13bf33a8c7 100644 --- a/NDTensors/src/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/UnallocatedArrays/src/set_types.jl @@ -2,21 +2,68 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) @eval begin # `SetParameters.jl` overloads. - SetParameters.get_parameter(::Type{<:$Typ{P1}}, ::Position{1}) where {P1} = P1 - SetParameters.get_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{2}) where {P2} = P2 - SetParameters.get_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = - P3 - SetParameters.get_parameter( + get_parameter(::Type{<:$Typ{P1}}, ::Position{1}) where {P1} = P1 + get_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{2}) where {P2} = P2 + get_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 + get_parameter( ::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{4} ) where {P4} = P4 - SetParameters.default_parameter(::Type{<:$Typ}, ::Position{1}) = + ## Setting paramaters + # Set parameter 1 + set_parameter(::Type{<:$Typ}, ::Position{1}, P1) = $Typ{P1} + set_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{1}, P1) where {P2} = $Typ{P1,P2} + function set_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{1}, P1) where {P3} + return $Typ{P1,<:Any,P3} + end + function set_parameter(::Type{<:$Typ{<:Any,P2,P3}}, ::Position{1}, P1) where {P2,P3} + return $Typ{P1,P2,P3} + end + function set_parameter(::Type{<:$Typ{<:Any,P2,P3,P4}}, ::Position{1}, P1) where {P2,P3,P4} + return $Typ{P1,P2,P3,P4} + end + function set_parameter(::Type{<:$Typ{<:Any,<:Any,P3,P4}}, ::Position{1}, P1) where {P3,P4} + return $Typ{P1,<:Any,P3,P4} + end + function set_parameter(::Type{<:$Typ{<:Any,P2,<:Any,P4}}, ::Position{1}, P1) where {P2,P4} + return $Typ{P1,P2,<:Any,P4} + end + function set_parameter(::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{1}, P1) where {P4} + return $Typ{P1,<:Any,<:Any,P4} + end + + # Set parameter 2 + set_parameter(::Type{<:$Typ}, ::Position{2}, P2) = $Typ{<:Any,P2} + set_parameter(::Type{<:$Typ{P1}}, ::Position{2}, P2) where {P1} = $Typ{P1,P2} + function set_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{2}, P2) where {P3} + return $Typ{<:Any,P2,P3} + end + function set_parameter(::Type{<:$Typ{P1,<:Any,P3}}, ::Position{2}, P2) where {P1,P3} + return $Typ{P1,P2,P3} + end + + # Set parameter 3 + set_parameter(::Type{<:$Typ}, ::Position{3}, P3) = $Typ{<:Any,<:Any,P3} + set_parameter(::Type{<:$Typ{P1}}, ::Position{3}, P3) where {P1} = $Typ{P1,<:Any,P3} + function set_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{3}, P3) where {P2} + return $Typ{<:Any,P2,P3} + end + set_parameter(::Type{<:$Typ{P1,P2}}, ::Position{3}, P3) where {P1,P2} = $Typ{P1,P2,P3} + + # Set paramter 4 + set_parameter(::Type{<:$Typ}, ::Position{4}, P4) = $Typ{<:Any,<:Any,<:Any,P4} + set_parameter(::Type{<:$Typ{P1}}, ::Position{4}, P4) where {P1} = $Typ{P1,<:Any,<:Any,P4} + set_parameter(::Type{<:$Typ{P1,P2}}, ::Position{4}, P4) where{P1,P2} = $Typ{P1,P2,<:Any,P4} + set_parameter(::Type{<:$Typ{P1,P2,P3}}, ::Position{4}, P4) where{P1,P2,P3} = $Typ{P1,P2,P3,P4} + +## default parameters + default_parameter(::Type{<:$Typ}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros - SetParameters.default_parameter(::Type{<:$Typ}, ::Position{2}) = 0 - SetParameters.default_parameter(::Type{<:$Typ}, ::Position{3}) = Tuple{} - SetParameters.default_parameter(::Type{<:$Typ}, ::Position{4}) = + default_parameter(::Type{<:$Typ}, ::Position{2}) = 0 + default_parameter(::Type{<:$Typ}, ::Position{3}) = Tuple{} + default_parameter(::Type{<:$Typ}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray - SetParameters.nparameters(::Type{<:$Typ}) = Val(4) + nparameters(::Type{<:$Typ}) = Val(4) end end From cf1dff45844e7cc5380e1e8bd8cbbe5f2d0ba46c Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 09:57:28 -0500 Subject: [PATCH 027/156] format --- .../src/UnallocatedArrays/src/set_types.jl | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/set_types.jl b/NDTensors/src/UnallocatedArrays/src/set_types.jl index 13bf33a8c7..578a230637 100644 --- a/NDTensors/src/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/UnallocatedArrays/src/set_types.jl @@ -5,9 +5,7 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) get_parameter(::Type{<:$Typ{P1}}, ::Position{1}) where {P1} = P1 get_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{2}) where {P2} = P2 get_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 - get_parameter( - ::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{4} - ) where {P4} = P4 + get_parameter(::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{4}) where {P4} = P4 ## Setting paramaters # Set parameter 1 @@ -19,16 +17,24 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) function set_parameter(::Type{<:$Typ{<:Any,P2,P3}}, ::Position{1}, P1) where {P2,P3} return $Typ{P1,P2,P3} end - function set_parameter(::Type{<:$Typ{<:Any,P2,P3,P4}}, ::Position{1}, P1) where {P2,P3,P4} + function set_parameter( + ::Type{<:$Typ{<:Any,P2,P3,P4}}, ::Position{1}, P1 + ) where {P2,P3,P4} return $Typ{P1,P2,P3,P4} end - function set_parameter(::Type{<:$Typ{<:Any,<:Any,P3,P4}}, ::Position{1}, P1) where {P3,P4} + function set_parameter( + ::Type{<:$Typ{<:Any,<:Any,P3,P4}}, ::Position{1}, P1 + ) where {P3,P4} return $Typ{P1,<:Any,P3,P4} end - function set_parameter(::Type{<:$Typ{<:Any,P2,<:Any,P4}}, ::Position{1}, P1) where {P2,P4} + function set_parameter( + ::Type{<:$Typ{<:Any,P2,<:Any,P4}}, ::Position{1}, P1 + ) where {P2,P4} return $Typ{P1,P2,<:Any,P4} end - function set_parameter(::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{1}, P1) where {P4} + function set_parameter( + ::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{1}, P1 + ) where {P4} return $Typ{P1,<:Any,<:Any,P4} end @@ -52,17 +58,18 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) # Set paramter 4 set_parameter(::Type{<:$Typ}, ::Position{4}, P4) = $Typ{<:Any,<:Any,<:Any,P4} - set_parameter(::Type{<:$Typ{P1}}, ::Position{4}, P4) where {P1} = $Typ{P1,<:Any,<:Any,P4} - set_parameter(::Type{<:$Typ{P1,P2}}, ::Position{4}, P4) where{P1,P2} = $Typ{P1,P2,<:Any,P4} - set_parameter(::Type{<:$Typ{P1,P2,P3}}, ::Position{4}, P4) where{P1,P2,P3} = $Typ{P1,P2,P3,P4} + set_parameter(::Type{<:$Typ{P1}}, ::Position{4}, P4) where {P1} = + $Typ{P1,<:Any,<:Any,P4} + set_parameter(::Type{<:$Typ{P1,P2}}, ::Position{4}, P4) where {P1,P2} = + $Typ{P1,P2,<:Any,P4} + set_parameter(::Type{<:$Typ{P1,P2,P3}}, ::Position{4}, P4) where {P1,P2,P3} = + $Typ{P1,P2,P3,P4} -## default parameters - default_parameter(::Type{<:$Typ}, ::Position{1}) = - UnspecifiedTypes.UnallocatedZeros + ## default parameters + default_parameter(::Type{<:$Typ}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros default_parameter(::Type{<:$Typ}, ::Position{2}) = 0 default_parameter(::Type{<:$Typ}, ::Position{3}) = Tuple{} - default_parameter(::Type{<:$Typ}, ::Position{4}) = - UnspecifiedTypes.UnspecifiedArray + default_parameter(::Type{<:$Typ}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray nparameters(::Type{<:$Typ}) = Val(4) end From 568fe7d7f9758792ebfc479f5e2aed4205fa2407 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 10:11:59 -0500 Subject: [PATCH 028/156] Create set_alloctype constructor for UnallocatedArrays --- NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl | 2 +- NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl index 7d9670e9bc..0cf31821bb 100644 --- a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl @@ -10,5 +10,5 @@ include("unallocatedzeros.jl") include("set_types.jl") include("unallocated_impl.jl") -export UnallocatedFill, UnallocatedZeros, alloctype +export UnallocatedFill, UnallocatedZeros, alloctype, set_alloctype end diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index f5eec90ee1..979503db89 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -25,3 +25,6 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) @eval LinearAlgebra.$fun(A::$Typ) = $fun(parent(A)) end end + +set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) = UnallocatedFill{eltype(f), ndims(f), typeof(axes(f)), alloc}(f) +set_alloctype(z::Zeros, alloc::Type{<:AbstractArray}) = UnallocatedZeros{eltype(z), ndims(z), typeof(axes(z)), alloc}(z) From 8d0135d6c1e9d42b68719f856a8c8c423115fe9c Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 10:58:50 -0500 Subject: [PATCH 029/156] Yes we should force Alloc to be the same dimensions as the `Fill` --- NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl | 2 +- NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl index b114ca3502..3b2fb52a4f 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl @@ -1,6 +1,6 @@ ## TODO All constructors not fully implemented but so far it matches the ## constructors found in `FillArrays`. Need to fix io -struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray} <: +struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray{ElT, N}} <: FillArrays.AbstractFill{ElT,N,Axes} f::FillArrays.Fill{ElT,N,Axes} ## TODO use `set_parameters` as constructor to these types diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl index 85e958f599..0c8007fb26 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl @@ -1,6 +1,6 @@ ## TODO Should Alloc also be of ElT and N or should there be ## More freedom there? -struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray} <: +struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT, N}} <: FillArrays.AbstractZeros{ElT,N,Axes} z::FillArrays.Zeros{ElT,N,Axes} ## TODO use `set_parameters` as constructor to these types From 9056599e2d8722086f592a4aafb40b098223639b Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 10:59:43 -0500 Subject: [PATCH 030/156] Add set_alloctype and complex --- NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index 979503db89..9ecb8507fc 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -13,6 +13,10 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) copy(A::$Typ) = A ## TODO Implement vec # Base.vec(Z::UnallocatedZeros) = typeof(Z)(length(Z)) + ## TODO Still working here I am not sure these functions and the + ## Set parameter functions are working properly + set_alloctype(F::Type{<:$Typ}, alloc::Type{<:AbstractArray}) = set_parameter(F, Position{4}(), alloc) + Base.complex(A::$Typ) = set_alloctype(complex(parent(A)), set_eltype(alloctype(A), complex(eltype(alloctype(A))))) end ## TODO forwarding functions to fillarrays From c6465987c97c943041061349da8405fb2fb63ed7 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 11:00:05 -0500 Subject: [PATCH 031/156] format --- .../src/UnallocatedArrays/src/unallocated_impl.jl | 15 +++++++++++---- .../src/UnallocatedArrays/src/unallocatedfill.jl | 2 +- .../src/UnallocatedArrays/src/unallocatedzeros.jl | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index 9ecb8507fc..41afd62f96 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -15,8 +15,11 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) # Base.vec(Z::UnallocatedZeros) = typeof(Z)(length(Z)) ## TODO Still working here I am not sure these functions and the ## Set parameter functions are working properly - set_alloctype(F::Type{<:$Typ}, alloc::Type{<:AbstractArray}) = set_parameter(F, Position{4}(), alloc) - Base.complex(A::$Typ) = set_alloctype(complex(parent(A)), set_eltype(alloctype(A), complex(eltype(alloctype(A))))) + set_alloctype(F::Type{<:$Typ}, alloc::Type{<:AbstractArray}) = + set_parameter(F, Position{4}(), alloc) + Base.complex(A::$Typ) = set_alloctype( + complex(parent(A)), set_eltype(alloctype(A), complex(eltype(alloctype(A)))) + ) end ## TODO forwarding functions to fillarrays @@ -30,5 +33,9 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) end end -set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) = UnallocatedFill{eltype(f), ndims(f), typeof(axes(f)), alloc}(f) -set_alloctype(z::Zeros, alloc::Type{<:AbstractArray}) = UnallocatedZeros{eltype(z), ndims(z), typeof(axes(z)), alloc}(z) +function set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) + return UnallocatedFill{eltype(f),ndims(f),typeof(axes(f)),alloc}(f) +end +function set_alloctype(z::Zeros, alloc::Type{<:AbstractArray}) + return UnallocatedZeros{eltype(z),ndims(z),typeof(axes(z)),alloc}(z) +end diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl index 3b2fb52a4f..36474febd3 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl @@ -1,6 +1,6 @@ ## TODO All constructors not fully implemented but so far it matches the ## constructors found in `FillArrays`. Need to fix io -struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray{ElT, N}} <: +struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: FillArrays.AbstractFill{ElT,N,Axes} f::FillArrays.Fill{ElT,N,Axes} ## TODO use `set_parameters` as constructor to these types diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl index 0c8007fb26..d05fcbde51 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl @@ -1,6 +1,6 @@ ## TODO Should Alloc also be of ElT and N or should there be ## More freedom there? -struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT, N}} <: +struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: FillArrays.AbstractZeros{ElT,N,Axes} z::FillArrays.Zeros{ElT,N,Axes} ## TODO use `set_parameters` as constructor to these types From 2a9bc4f2bbf91689b85faf781a31244723807f62 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 11:02:19 -0500 Subject: [PATCH 032/156] We don't need to define norm --- NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index 41afd62f96..48161882bc 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -27,10 +27,6 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) for fun in (:size, :length, :sum, :getindex_value) @eval FillArrays.$fun(A::$Typ) = $fun(parent(A)) end - ## TODO Here I am defining LinearAlgebra functions in one sweep - for fun in (:norm,) - @eval LinearAlgebra.$fun(A::$Typ) = $fun(parent(A)) - end end function set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) From 25b1ef49dbc54c57babfe6d961e171cdfbf4bf2f Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 11:54:19 -0500 Subject: [PATCH 033/156] Remove unecessary imports --- NDTensors/src/UnallocatedArrays/src/import.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/NDTensors/src/UnallocatedArrays/src/import.jl b/NDTensors/src/UnallocatedArrays/src/import.jl index 02fe2cd875..c8747f39c6 100644 --- a/NDTensors/src/UnallocatedArrays/src/import.jl +++ b/NDTensors/src/UnallocatedArrays/src/import.jl @@ -1,3 +1,2 @@ -import FillArrays: axes, convert, getindex_value, size, sum import Base: getindex, copy import NDTensors.SetParameters: nparameters, get_parameter, set_parameter, default_parameter From c0de9dc2f7bc62808b4c9ea3652e66f20ab4acac Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 12:01:37 -0500 Subject: [PATCH 034/156] Remove set_eltype --- NDTensors/src/UnallocatedArrays/src/promote_rules.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/NDTensors/src/UnallocatedArrays/src/promote_rules.jl b/NDTensors/src/UnallocatedArrays/src/promote_rules.jl index 0e8e776516..a73773e56e 100644 --- a/NDTensors/src/UnallocatedArrays/src/promote_rules.jl +++ b/NDTensors/src/UnallocatedArrays/src/promote_rules.jl @@ -14,7 +14,6 @@ function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:AbstractArray}) @assert ndims(z1) == ndims(z2) Axs = axes(z1) Alloc = promote_type(alloctype(z1), z2) - set_eltype(Alloc, ElT) return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} end From 2ad44e912c0959e007f76f7e764547bbda5a9c64 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 12:02:43 -0500 Subject: [PATCH 035/156] Remove line per matts comment --- NDTensors/src/UnallocatedArrays/src/promote_rules.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/UnallocatedArrays/src/promote_rules.jl b/NDTensors/src/UnallocatedArrays/src/promote_rules.jl index a73773e56e..9982b6ced1 100644 --- a/NDTensors/src/UnallocatedArrays/src/promote_rules.jl +++ b/NDTensors/src/UnallocatedArrays/src/promote_rules.jl @@ -5,7 +5,6 @@ function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:UnallocatedZeros} @assert ndims(z1) == ndims(z2) Axs = axes(z1) Alloc = promote_type(alloctype(z1), alloctype(z2)) - set_eltype(Alloc, ElT) return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} end @@ -14,6 +13,7 @@ function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:AbstractArray}) @assert ndims(z1) == ndims(z2) Axs = axes(z1) Alloc = promote_type(alloctype(z1), z2) + set_eltype(Alloc, ElT) return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} end From c56b3ac5652e5d6290eb5c8d13167c9382a231db Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 12:05:02 -0500 Subject: [PATCH 036/156] Move set_alloctype constructors to their own files --- .../src/UnallocatedArrays/src/unallocated_impl.jl | 12 ------------ .../src/UnallocatedArrays/src/unallocatedfill.jl | 4 ++++ .../src/UnallocatedArrays/src/unallocatedzeros.jl | 11 ++++------- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index 48161882bc..d5c040d2ab 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -22,16 +22,4 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) ) end - ## TODO forwarding functions to fillarrays - ## convert doesn't work - for fun in (:size, :length, :sum, :getindex_value) - @eval FillArrays.$fun(A::$Typ) = $fun(parent(A)) - end -end - -function set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) - return UnallocatedFill{eltype(f),ndims(f),typeof(axes(f)),alloc}(f) -end -function set_alloctype(z::Zeros, alloc::Type{<:AbstractArray}) - return UnallocatedZeros{eltype(z),ndims(z),typeof(axes(z)),alloc}(z) end diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl index 36474febd3..e5d8988faa 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl @@ -6,4 +6,8 @@ struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: ## TODO use `set_parameters` as constructor to these types end +function set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) + return UnallocatedFill{eltype(f),ndims(f),typeof(axes(f)),alloc}(f) +end + Base.parent(F::UnallocatedFill) = F.f diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl index d05fcbde51..b41c7b5d8a 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl @@ -6,11 +6,8 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: ## TODO use `set_parameters` as constructor to these types end -Base.parent(Z::UnallocatedZeros) = Z.z +function set_alloctype(z::Zeros, alloc::Type{<:AbstractArray}) + return UnallocatedZeros{eltype(z),ndims(z),typeof(axes(z)),alloc}(z) +end -# function complex(z::UnallocatedZeros) -# ElT = complex(eltype(z)) -# N = ndims(z) -# AllocT = similartype(alloctype(z), ElT) -# return UnallocatedZeros{ElT,N,AllocT}(dims(z)) -# end +Base.parent(Z::UnallocatedZeros) = Z.z From fcd199767ff3843e2064ce17bb971adf499048e3 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 12:48:40 -0500 Subject: [PATCH 037/156] Some updates --- NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl | 2 +- NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl index 0cf31821bb..b0f899a504 100644 --- a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl @@ -10,5 +10,5 @@ include("unallocatedzeros.jl") include("set_types.jl") include("unallocated_impl.jl") -export UnallocatedFill, UnallocatedZeros, alloctype, set_alloctype +export UnallocatedFill, UnallocatedZeros, alloctype, set_alloctype, allocate end diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index d5c040d2ab..d3f4b19cd9 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -8,8 +8,13 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) alloctype(A::$Typ) = alloctype(typeof(A)) alloctype(Atype::Type{<:$Typ}) = get_parameter(Atype, Position{4}()) - Array(A::$Typ) = alloctype(typeof(A)) + allocate(A::$Typ) = alloctype(A)(parent(A)) + ## With these functions defined I can print UnallocatedArrays + ## compute things like sum and norm, compute the size and length + @inline Base.axes(A::$Typ) = axes(parent(A)) + Base.size(A::$Typ) = size(parent(A)) + FillArrays.getindex_value(A::$Typ) = FillArrays.getindex_value(parent(A)) copy(A::$Typ) = A ## TODO Implement vec # Base.vec(Z::UnallocatedZeros) = typeof(Z)(length(Z)) @@ -17,9 +22,9 @@ for Typ in (:UnallocatedFill, :UnallocatedZeros) ## Set parameter functions are working properly set_alloctype(F::Type{<:$Typ}, alloc::Type{<:AbstractArray}) = set_parameter(F, Position{4}(), alloc) + Base.complex(A::$Typ) = set_alloctype( complex(parent(A)), set_eltype(alloctype(A), complex(eltype(alloctype(A)))) ) end - end From ab7e910964a8c6a6f8b2f960501e0ca209bf1217 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 12:48:53 -0500 Subject: [PATCH 038/156] Add a unittest --- .../src/UnallocatedArrays/test/runtests.jl | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 NDTensors/src/UnallocatedArrays/test/runtests.jl diff --git a/NDTensors/src/UnallocatedArrays/test/runtests.jl b/NDTensors/src/UnallocatedArrays/test/runtests.jl new file mode 100644 index 0000000000..b67584f388 --- /dev/null +++ b/NDTensors/src/UnallocatedArrays/test/runtests.jl @@ -0,0 +1,36 @@ +using FillArrays +using NDTensors.UnallocatedArrays +using LinearAlgebra +using Test + +begin + z = Zeros{Float64}((2,3)) + Z = UnallocatedZeros{eltype(z), ndims(z), typeof(axes(z)), Matrix{eltype(z)}}(z) + + @test size(Z) == (2,3) + @test length(Z) == 6 + @test sum(Z) == 0 + @test norm(Z) == 0 + @test Z[2,3] == 0 + @test allocate(Z) isa Matrix{eltype(z)} + Zp = set_alloctype(z, Matrix{eltype(z)}) + @test Zp == Z + Zc = copy(Z) + @test Zc == Z + + f = Fill{Float64}(3., (2,3,4)) + F = UnallocatedFill{eltype(f), ndims(f), typeof(axes(f)), Array{eltype(f), ndims(f)}}(f) + + @test size(F) == (2,3,4) + @test length(F) == 24 + @test sum(F) ≈ 3 * 24 + @test norm(F) ≈ sqrt(3^2 * 24) + @test F[2,3,1] == 3. + @test allocate(F) isa Array{eltype(z), 3} + Fp = allocate(F) + @test norm(Fp) ≈ norm(F) + Fp = set_alloctype(f, Array{eltype(f), ndims(f)}) + @test Fp == F + Fc = copy(F) + @test Fc == F +end \ No newline at end of file From e007458674f1c9172c67892d7e20286ade366197 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 9 Nov 2023 12:59:07 -0500 Subject: [PATCH 039/156] format --- .../src/UnallocatedArrays/test/runtests.jl | 63 +++++++++++-------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/test/runtests.jl b/NDTensors/src/UnallocatedArrays/test/runtests.jl index b67584f388..385e0cf0d2 100644 --- a/NDTensors/src/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/UnallocatedArrays/test/runtests.jl @@ -4,33 +4,42 @@ using LinearAlgebra using Test begin - z = Zeros{Float64}((2,3)) - Z = UnallocatedZeros{eltype(z), ndims(z), typeof(axes(z)), Matrix{eltype(z)}}(z) + z = Zeros{Float64}((2, 3)) + Z = UnallocatedZeros{eltype(z),ndims(z),typeof(axes(z)),Matrix{eltype(z)}}(z) - @test size(Z) == (2,3) - @test length(Z) == 6 - @test sum(Z) == 0 - @test norm(Z) == 0 - @test Z[2,3] == 0 - @test allocate(Z) isa Matrix{eltype(z)} - Zp = set_alloctype(z, Matrix{eltype(z)}) - @test Zp == Z - Zc = copy(Z) - @test Zc == Z + @test size(Z) == (2, 3) + @test length(Z) == 6 + @test sum(Z) == 0 + @test norm(Z) == 0 + @test Z[2, 3] == 0 + @test allocate(Z) isa Matrix{eltype(z)} + Zp = set_alloctype(z, Matrix{eltype(z)}) + @test Zp == Z + Zc = copy(Z) + @test Zc == Z - f = Fill{Float64}(3., (2,3,4)) - F = UnallocatedFill{eltype(f), ndims(f), typeof(axes(f)), Array{eltype(f), ndims(f)}}(f) + # z = Zeros(()) + # Z = UnallocatedZeros{Float32, ndims(z), typeof(axes(z)), Vector{Float32}}(z) + # @test size(Z) == () + # @test length(Z) == 1 + # @test Z[] == 0 + # Z = set_alloctype(z, Matrix{Float64}) + # m = allocate(Z) + # @test length(m) == 1 + # m[] == 0 - @test size(F) == (2,3,4) - @test length(F) == 24 - @test sum(F) ≈ 3 * 24 - @test norm(F) ≈ sqrt(3^2 * 24) - @test F[2,3,1] == 3. - @test allocate(F) isa Array{eltype(z), 3} - Fp = allocate(F) - @test norm(Fp) ≈ norm(F) - Fp = set_alloctype(f, Array{eltype(f), ndims(f)}) - @test Fp == F - Fc = copy(F) - @test Fc == F -end \ No newline at end of file + f = Fill{Float64}(3.0, (2, 3, 4)) + F = UnallocatedFill{eltype(f),ndims(f),typeof(axes(f)),Array{eltype(f),ndims(f)}}(f) + @test size(F) == (2, 3, 4) + @test length(F) == 24 + @test sum(F) ≈ 3 * 24 + @test norm(F) ≈ sqrt(3^2 * 24) + @test F[2, 3, 1] == 3.0 + @test allocate(F) isa Array{eltype(z),3} + Fp = allocate(F) + @test norm(Fp) ≈ norm(F) + Fp = set_alloctype(f, Array{eltype(f),ndims(f)}) + @test Fp == F + Fc = copy(F) + @test Fc == F +end From df752fe66a9f0d2eb8136fafbfe3455141252f9f Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Thu, 9 Nov 2023 15:01:24 -0500 Subject: [PATCH 040/156] Remove @eval and fix the complex function --- .../UnallocatedArrays/src/unallocated_impl.jl | 46 +++++++++---------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index d3f4b19cd9..3713d10e4f 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -1,30 +1,26 @@ -for Typ in (:UnallocatedFill, :UnallocatedZeros) - ## Here are functions specifically defined for UnallocatedArrays +## Here are functions specifically defined for UnallocatedArrays ## not implemented by FillArrays - ## TODO use set_parameters/get_parameters instead of alloctype and other - ## type info functions. ## TODO determine min number of functions needed to be forwarded - @eval begin - alloctype(A::$Typ) = alloctype(typeof(A)) - alloctype(Atype::Type{<:$Typ}) = get_parameter(Atype, Position{4}()) - allocate(A::$Typ) = alloctype(A)(parent(A)) +alloctype(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = alloctype(typeof(A)) +alloctype(Atype::Type{<:Union{<:UnallocatedFill, <:UnallocatedZeros}}) = get_parameter(Atype, Position{4}()) - ## With these functions defined I can print UnallocatedArrays - ## compute things like sum and norm, compute the size and length - @inline Base.axes(A::$Typ) = axes(parent(A)) - Base.size(A::$Typ) = size(parent(A)) - FillArrays.getindex_value(A::$Typ) = FillArrays.getindex_value(parent(A)) - copy(A::$Typ) = A - ## TODO Implement vec - # Base.vec(Z::UnallocatedZeros) = typeof(Z)(length(Z)) - ## TODO Still working here I am not sure these functions and the - ## Set parameter functions are working properly - set_alloctype(F::Type{<:$Typ}, alloc::Type{<:AbstractArray}) = - set_parameter(F, Position{4}(), alloc) +allocate(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = alloctype(A)(parent(A)) - Base.complex(A::$Typ) = set_alloctype( - complex(parent(A)), set_eltype(alloctype(A), complex(eltype(alloctype(A)))) - ) - end -end +## TODO Still working here I am not sure these functions and the +## Set parameter functions are working properly +set_alloctype(F::Type{<:Union{<:UnallocatedFill,<:UnallocatedZeros}}, alloc::Type{<:AbstractArray}) = + set_parameter(F, Position{4}(), alloc) +set_eltype(F::Type{<:Union{<:UnallocatedFill,<:UnallocatedZeros}}, elt) + +## With these functions defined I can print UnallocatedArrays +## compute things like sum and norm, compute the size and length +@inline Base.axes(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = axes(parent(A)) +Base.size(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = size(parent(A)) +FillArrays.getindex_value(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = FillArrays.getindex_value(parent(A)) +Base.copy(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = A +## Can't actually use NDTensors.set_eltype because it doesn't +## exist yet in thie area +Base.complex(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = set_alloctype(complex(parent(A)), set_parameter(alloctype(A), Position{1}(), complex(eltype(A)))) +# ## TODO Implement vec +# # Base.vec(Z::UnallocatedZeros) = typeof(Z)(length(Z)) From d254db87be5b6a3276883bd8d0acb8d4676c1372 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Thu, 9 Nov 2023 15:05:16 -0500 Subject: [PATCH 041/156] Add complex testing --- NDTensors/src/UnallocatedArrays/test/runtests.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/UnallocatedArrays/test/runtests.jl b/NDTensors/src/UnallocatedArrays/test/runtests.jl index 385e0cf0d2..ab370a9ecb 100644 --- a/NDTensors/src/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/UnallocatedArrays/test/runtests.jl @@ -3,7 +3,7 @@ using NDTensors.UnallocatedArrays using LinearAlgebra using Test -begin +@testset "Testing UnallocatedArrays" begin z = Zeros{Float64}((2, 3)) Z = UnallocatedZeros{eltype(z),ndims(z),typeof(axes(z)),Matrix{eltype(z)}}(z) @@ -17,6 +17,9 @@ begin @test Zp == Z Zc = copy(Z) @test Zc == Z + Zc = complex(Z) + @test eltype(Zc) == complex(eltype(z)) + @test Zc[1,2] == 0.0 + 0.0im # z = Zeros(()) # Z = UnallocatedZeros{Float32, ndims(z), typeof(axes(z)), Vector{Float32}}(z) @@ -42,4 +45,8 @@ begin @test Fp == F Fc = copy(F) @test Fc == F + Fc = allocate(complex(F)) + @test eltype(Fc) == complex(eltype(F)) + Fc[2,3,4] = 4.0 + 3.0im + @test Fc[2,3,4] == 4.0 + 3.0im end From c4dc2af6e3ca80d94dddc4607c77f32848c3d1af Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Thu, 9 Nov 2023 15:05:31 -0500 Subject: [PATCH 042/156] format --- .../UnallocatedArrays/src/unallocated_impl.jl | 25 +++++++++++++------ .../src/UnallocatedArrays/test/runtests.jl | 6 ++--- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index 3713d10e4f..0f190a97f3 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -1,26 +1,37 @@ ## Here are functions specifically defined for UnallocatedArrays - ## not implemented by FillArrays - ## TODO determine min number of functions needed to be forwarded +## not implemented by FillArrays +## TODO determine min number of functions needed to be forwarded alloctype(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = alloctype(typeof(A)) -alloctype(Atype::Type{<:Union{<:UnallocatedFill, <:UnallocatedZeros}}) = get_parameter(Atype, Position{4}()) +function alloctype(Atype::Type{<:Union{<:UnallocatedFill,<:UnallocatedZeros}}) + return get_parameter(Atype, Position{4}()) +end allocate(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = alloctype(A)(parent(A)) ## TODO Still working here I am not sure these functions and the ## Set parameter functions are working properly -set_alloctype(F::Type{<:Union{<:UnallocatedFill,<:UnallocatedZeros}}, alloc::Type{<:AbstractArray}) = - set_parameter(F, Position{4}(), alloc) +function set_alloctype( + F::Type{<:Union{<:UnallocatedFill,<:UnallocatedZeros}}, alloc::Type{<:AbstractArray} +) + return set_parameter(F, Position{4}(), alloc) +end set_eltype(F::Type{<:Union{<:UnallocatedFill,<:UnallocatedZeros}}, elt) ## With these functions defined I can print UnallocatedArrays ## compute things like sum and norm, compute the size and length @inline Base.axes(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = axes(parent(A)) Base.size(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = size(parent(A)) -FillArrays.getindex_value(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = FillArrays.getindex_value(parent(A)) +function FillArrays.getindex_value(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) + return FillArrays.getindex_value(parent(A)) +end Base.copy(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = A ## Can't actually use NDTensors.set_eltype because it doesn't ## exist yet in thie area -Base.complex(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = set_alloctype(complex(parent(A)), set_parameter(alloctype(A), Position{1}(), complex(eltype(A)))) +function Base.complex(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) + return set_alloctype( + complex(parent(A)), set_parameter(alloctype(A), Position{1}(), complex(eltype(A))) + ) +end # ## TODO Implement vec # # Base.vec(Z::UnallocatedZeros) = typeof(Z)(length(Z)) diff --git a/NDTensors/src/UnallocatedArrays/test/runtests.jl b/NDTensors/src/UnallocatedArrays/test/runtests.jl index ab370a9ecb..697829139f 100644 --- a/NDTensors/src/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/UnallocatedArrays/test/runtests.jl @@ -19,7 +19,7 @@ using Test @test Zc == Z Zc = complex(Z) @test eltype(Zc) == complex(eltype(z)) - @test Zc[1,2] == 0.0 + 0.0im + @test Zc[1, 2] == 0.0 + 0.0im # z = Zeros(()) # Z = UnallocatedZeros{Float32, ndims(z), typeof(axes(z)), Vector{Float32}}(z) @@ -47,6 +47,6 @@ using Test @test Fc == F Fc = allocate(complex(F)) @test eltype(Fc) == complex(eltype(F)) - Fc[2,3,4] = 4.0 + 3.0im - @test Fc[2,3,4] == 4.0 + 3.0im + Fc[2, 3, 4] = 4.0 + 3.0im + @test Fc[2, 3, 4] == 4.0 + 3.0im end From 4e6036bf0a5ed3778c88759594d5a37a3350c037 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Thu, 9 Nov 2023 15:47:33 -0500 Subject: [PATCH 043/156] Make a UnallocFillOrZero union variable --- .../UnallocatedArrays/src/unallocated_impl.jl | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index 0f190a97f3..093edc704a 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -1,37 +1,52 @@ ## Here are functions specifically defined for UnallocatedArrays ## not implemented by FillArrays ## TODO determine min number of functions needed to be forwarded +const UnallocFillOrZero = Union{<:UnallocatedFill, <:UnallocatedZeros} -alloctype(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = alloctype(typeof(A)) -function alloctype(Atype::Type{<:Union{<:UnallocatedFill,<:UnallocatedZeros}}) +alloctype(A::UnallocFillOrZero) = alloctype(typeof(A)) +function alloctype(Atype::Type{UnallocFillOrZero}) return get_parameter(Atype, Position{4}()) end -allocate(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = alloctype(A)(parent(A)) +allocate(A::Union{UnallocFillOrZero}) = alloctype(A)(parent(A)) ## TODO Still working here I am not sure these functions and the ## Set parameter functions are working properly function set_alloctype( - F::Type{<:Union{<:UnallocatedFill,<:UnallocatedZeros}}, alloc::Type{<:AbstractArray} + F::Type{UnallocFillOrZero}, alloc::Type{<:AbstractArray} ) return set_parameter(F, Position{4}(), alloc) end -set_eltype(F::Type{<:Union{<:UnallocatedFill,<:UnallocatedZeros}}, elt) +## TODO this is broken +set_eltype(F::Type{UnallocFillOrZero}, elt::Type) = set_alloctype(set_eltype(parent(z), elt), set_eltype(alloctype(F), elt)) ## With these functions defined I can print UnallocatedArrays ## compute things like sum and norm, compute the size and length -@inline Base.axes(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = axes(parent(A)) -Base.size(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = size(parent(A)) -function FillArrays.getindex_value(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) +@inline Base.axes(A::UnallocFillOrZero) = axes(parent(A)) +Base.size(A::UnallocFillOrZero) = size(parent(A)) +function FillArrays.getindex_value(A::UnallocFillOrZero) return FillArrays.getindex_value(parent(A)) end -Base.copy(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = A +Base.copy(A::UnallocFillOrZero) = A ## Can't actually use NDTensors.set_eltype because it doesn't ## exist yet in thie area -function Base.complex(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) +function Base.complex(A::UnallocFillOrZero) return set_alloctype( complex(parent(A)), set_parameter(alloctype(A), Position{1}(), complex(eltype(A))) ) end -# ## TODO Implement vec -# # Base.vec(Z::UnallocatedZeros) = typeof(Z)(length(Z)) + +mult_fill(a, b, val, ax) = Fill(val, ax) +mult_zeros(a, b, elt, ax) = Zeros{elt}(ax) +mult_ones(a, b, elt, ax) = Ones{elt}(ax) + +broadcasted_fill(f, a, val, ax) = Fill(val, ax) +broadcasted_fill(f, a, b, val, ax) = Fill(val, ax) +broadcasted_zeros(f, a, elt, ax) = Zeros{elt}(ax) +broadcasted_zeros(f, a, b, elt, ax) = Zeros{elt}(ax) +broadcasted_ones(f, a, elt, ax) = Ones{elt}(ax) +broadcasted_ones(f, a, b, elt, ax) = Ones{elt}(ax) + +kron_fill(a, b, val, ax) = Fill(val, ax) +kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) +kron_ones(a, b, elt, ax) = Ones{elt}(ax) \ No newline at end of file From f87c2d37b1236cef4e26d4795b277502ea20ff9f Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Thu, 9 Nov 2023 15:48:19 -0500 Subject: [PATCH 044/156] format --- .../src/UnallocatedArrays/src/unallocated_impl.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl index 093edc704a..d62819f4bb 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl @@ -1,7 +1,7 @@ ## Here are functions specifically defined for UnallocatedArrays ## not implemented by FillArrays ## TODO determine min number of functions needed to be forwarded -const UnallocFillOrZero = Union{<:UnallocatedFill, <:UnallocatedZeros} +const UnallocFillOrZero = Union{<:UnallocatedFill,<:UnallocatedZeros} alloctype(A::UnallocFillOrZero) = alloctype(typeof(A)) function alloctype(Atype::Type{UnallocFillOrZero}) @@ -12,13 +12,13 @@ allocate(A::Union{UnallocFillOrZero}) = alloctype(A)(parent(A)) ## TODO Still working here I am not sure these functions and the ## Set parameter functions are working properly -function set_alloctype( - F::Type{UnallocFillOrZero}, alloc::Type{<:AbstractArray} -) +function set_alloctype(F::Type{UnallocFillOrZero}, alloc::Type{<:AbstractArray}) return set_parameter(F, Position{4}(), alloc) end ## TODO this is broken -set_eltype(F::Type{UnallocFillOrZero}, elt::Type) = set_alloctype(set_eltype(parent(z), elt), set_eltype(alloctype(F), elt)) +function set_eltype(F::Type{UnallocFillOrZero}, elt::Type) + return set_alloctype(set_eltype(parent(z), elt), set_eltype(alloctype(F), elt)) +end ## With these functions defined I can print UnallocatedArrays ## compute things like sum and norm, compute the size and length @@ -49,4 +49,4 @@ broadcasted_ones(f, a, b, elt, ax) = Ones{elt}(ax) kron_fill(a, b, val, ax) = Fill(val, ax) kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) -kron_ones(a, b, elt, ax) = Ones{elt}(ax) \ No newline at end of file +kron_ones(a, b, elt, ax) = Ones{elt}(ax) From a0ceca43f469890220f81acf19520f7305d9df49 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 20 Nov 2023 09:55:38 -0500 Subject: [PATCH 045/156] Move abstractUnallocatedArray functions to file --- ...ed_impl.jl => abstractunallocatedarray.jl} | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) rename NDTensors/src/UnallocatedArrays/src/{unallocated_impl.jl => abstractunallocatedarray.jl} (71%) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl b/NDTensors/src/UnallocatedArrays/src/abstractunallocatedarray.jl similarity index 71% rename from NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl rename to NDTensors/src/UnallocatedArrays/src/abstractunallocatedarray.jl index d62819f4bb..155e0cfd54 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocated_impl.jl +++ b/NDTensors/src/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -1,36 +1,37 @@ +abstract type AbstractUnallocatedArray +end + ## Here are functions specifically defined for UnallocatedArrays ## not implemented by FillArrays ## TODO determine min number of functions needed to be forwarded -const UnallocFillOrZero = Union{<:UnallocatedFill,<:UnallocatedZeros} - -alloctype(A::UnallocFillOrZero) = alloctype(typeof(A)) +alloctype(A::AbstractUnallocatedArray) = alloctype(typeof(A)) function alloctype(Atype::Type{UnallocFillOrZero}) return get_parameter(Atype, Position{4}()) end -allocate(A::Union{UnallocFillOrZero}) = alloctype(A)(parent(A)) +allocate(A::AbstractUnallocatedArray) = alloctype(A)(parent(A)) ## TODO Still working here I am not sure these functions and the ## Set parameter functions are working properly -function set_alloctype(F::Type{UnallocFillOrZero}, alloc::Type{<:AbstractArray}) +function set_alloctype(F::Type{AbstractUnallocatedArray}, alloc::Type{<:AbstractArray}) return set_parameter(F, Position{4}(), alloc) end ## TODO this is broken -function set_eltype(F::Type{UnallocFillOrZero}, elt::Type) +function set_eltype(F::Type{AbstractUnallocatedArray}, elt::Type) return set_alloctype(set_eltype(parent(z), elt), set_eltype(alloctype(F), elt)) end ## With these functions defined I can print UnallocatedArrays ## compute things like sum and norm, compute the size and length -@inline Base.axes(A::UnallocFillOrZero) = axes(parent(A)) -Base.size(A::UnallocFillOrZero) = size(parent(A)) -function FillArrays.getindex_value(A::UnallocFillOrZero) +@inline Base.axes(A::AbstractUnallocatedArray) = axes(parent(A)) +Base.size(A::AbstractUnallocatedArray) = size(parent(A)) +function FillArrays.getindex_value(A::AbstractUnallocatedArray) return FillArrays.getindex_value(parent(A)) end -Base.copy(A::UnallocFillOrZero) = A +Base.copy(A::AbstractUnallocatedArray) = A ## Can't actually use NDTensors.set_eltype because it doesn't ## exist yet in thie area -function Base.complex(A::UnallocFillOrZero) +function Base.complex(A::AbstractUnallocatedArray) return set_alloctype( complex(parent(A)), set_parameter(alloctype(A), Position{1}(), complex(eltype(A))) ) From 8925888002e8b315f4f2abe2ec5890857a64f44f Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 20 Nov 2023 09:56:01 -0500 Subject: [PATCH 046/156] Add UnallocatedArray.jl --- NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl index b0f899a504..6f3c63354a 100644 --- a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl @@ -5,10 +5,10 @@ using NDTensors.SetParameters include("import.jl") +include("abstractunallocatedarray.jl") include("unallocatedfill.jl") include("unallocatedzeros.jl") include("set_types.jl") -include("unallocated_impl.jl") export UnallocatedFill, UnallocatedZeros, alloctype, set_alloctype, allocate end From 2a694c0ccd642069d11df7461965e56859abfb12 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 20 Nov 2023 09:56:25 -0500 Subject: [PATCH 047/156] Update set_types to use AbstractUnallocatedArrays --- .../src/UnallocatedArrays/src/set_types.jl | 136 +++++++++--------- 1 file changed, 66 insertions(+), 70 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/set_types.jl b/NDTensors/src/UnallocatedArrays/src/set_types.jl index 578a230637..1902f7a70a 100644 --- a/NDTensors/src/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/UnallocatedArrays/src/set_types.jl @@ -1,76 +1,72 @@ -## TODO I have just started working on this, it needs more work still. -for Typ in (:UnallocatedFill, :UnallocatedZeros) - @eval begin - # `SetParameters.jl` overloads. - get_parameter(::Type{<:$Typ{P1}}, ::Position{1}) where {P1} = P1 - get_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{2}) where {P2} = P2 - get_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 - get_parameter(::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{4}) where {P4} = P4 +## TODO make unit tests for all of these functions +# `SetParameters.jl` overloads. +get_parameter(::Type{<:AbstractUnallocatedArray{P1}}, ::Position{1}) where {P1} = P1 +get_parameter(::Type{<:AbstractUnallocatedArray{<:Any,P2}}, ::Position{2}) where {P2} = P2 +get_parameter(::Type{<:AbstractUnallocatedArray{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 +get_parameter(::Type{<:AbstractUnallocatedArray{<:Any,<:Any,<:Any,P4}}, ::Position{4}) where {P4} = P4 - ## Setting paramaters - # Set parameter 1 - set_parameter(::Type{<:$Typ}, ::Position{1}, P1) = $Typ{P1} - set_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{1}, P1) where {P2} = $Typ{P1,P2} - function set_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{1}, P1) where {P3} - return $Typ{P1,<:Any,P3} - end - function set_parameter(::Type{<:$Typ{<:Any,P2,P3}}, ::Position{1}, P1) where {P2,P3} - return $Typ{P1,P2,P3} - end - function set_parameter( - ::Type{<:$Typ{<:Any,P2,P3,P4}}, ::Position{1}, P1 - ) where {P2,P3,P4} - return $Typ{P1,P2,P3,P4} - end - function set_parameter( - ::Type{<:$Typ{<:Any,<:Any,P3,P4}}, ::Position{1}, P1 - ) where {P3,P4} - return $Typ{P1,<:Any,P3,P4} - end - function set_parameter( - ::Type{<:$Typ{<:Any,P2,<:Any,P4}}, ::Position{1}, P1 - ) where {P2,P4} - return $Typ{P1,P2,<:Any,P4} - end - function set_parameter( - ::Type{<:$Typ{<:Any,<:Any,<:Any,P4}}, ::Position{1}, P1 - ) where {P4} - return $Typ{P1,<:Any,<:Any,P4} - end +## Setting paramaters +# Set parameter 1 +set_parameter(T::Type{<:AbstractUnallocatedArray}, ::Position{1}, P1) = T{P1} +set_parameter(T::Type{<:AbstractUnallocatedArray{<:Any,P2}}, ::Position{1}, P1) where {P2} = T{P1,P2} +function set_parameter(T::Type{<:AbstractUnallocatedArray{<:Any,<:Any,P3}}, ::Position{1}, P1) where {P3} + return T{P1,<:Any,P3} +end +function set_parameter(T::Type{<:AbstractUnallocatedArray{<:Any,P2,P3}}, ::Position{1}, P1) where {P2,P3} + return T{P1,P2,P3} +end +function set_parameter( + T::Type{<:AbstractUnallocatedArray{<:Any,P2,P3,P4}}, ::Position{1}, P1 +) where {P2,P3,P4} + return T{P1,P2,P3,P4} +end +function set_parameter( + T::Type{<:AbstractUnallocatedArray{<:Any,<:Any,P3,P4}}, ::Position{1}, P1 +) where {P3,P4} + return T{P1,<:Any,P3,P4} +end +function set_parameter( + T::Type{<:AbstractUnallocatedArray{<:Any,P2,<:Any,P4}}, ::Position{1}, P1 +) where {P2,P4} + return T{P1,P2,<:Any,P4} +end +function set_parameter( + T::Type{<:AbstractUnallocatedArray{<:Any,<:Any,<:Any,P4}}, ::Position{1}, P1 +) where {P4} + return T{P1,<:Any,<:Any,P4} +end - # Set parameter 2 - set_parameter(::Type{<:$Typ}, ::Position{2}, P2) = $Typ{<:Any,P2} - set_parameter(::Type{<:$Typ{P1}}, ::Position{2}, P2) where {P1} = $Typ{P1,P2} - function set_parameter(::Type{<:$Typ{<:Any,<:Any,P3}}, ::Position{2}, P2) where {P3} - return $Typ{<:Any,P2,P3} - end - function set_parameter(::Type{<:$Typ{P1,<:Any,P3}}, ::Position{2}, P2) where {P1,P3} - return $Typ{P1,P2,P3} - end +# Set parameter 2 +set_parameter(T::Type{<:AbstractUnallocatedArray}, ::Position{2}, P2) = T{<:Any,P2} +set_parameter(T::Type{<:AbstractUnallocatedArray{P1}}, ::Position{2}, P2) where {P1} = T{P1,P2} +function set_parameter(T::Type{<:AbstractUnallocatedArray{<:Any,<:Any,P3}}, ::Position{2}, P2) where {P3} + return T{<:Any,P2,P3} +end +function set_parameter(T::Type{<:AbstractUnallocatedArray{P1,<:Any,P3}}, ::Position{2}, P2) where {P1,P3} + return T{P1,P2,P3} +end - # Set parameter 3 - set_parameter(::Type{<:$Typ}, ::Position{3}, P3) = $Typ{<:Any,<:Any,P3} - set_parameter(::Type{<:$Typ{P1}}, ::Position{3}, P3) where {P1} = $Typ{P1,<:Any,P3} - function set_parameter(::Type{<:$Typ{<:Any,P2}}, ::Position{3}, P3) where {P2} - return $Typ{<:Any,P2,P3} - end - set_parameter(::Type{<:$Typ{P1,P2}}, ::Position{3}, P3) where {P1,P2} = $Typ{P1,P2,P3} +# Set parameter 3 +set_parameter(T::Type{<:AbstractUnallocatedArray}, ::Position{3}, P3) = T{<:Any,<:Any,P3} +set_parameter(T::Type{<:AbstractUnallocatedArray{P1}}, ::Position{3}, P3) where {P1} = T{P1,<:Any,P3} +function set_parameter(T::Type{<:AbstractUnallocatedArray{<:Any,P2}}, ::Position{3}, P3) where {P2} + return T{<:Any,P2,P3} +end +set_parameter(T::Type{<:AbstractUnallocatedArray{P1,P2}}, ::Position{3}, P3) where {P1,P2} = T{P1,P2,P3} - # Set paramter 4 - set_parameter(::Type{<:$Typ}, ::Position{4}, P4) = $Typ{<:Any,<:Any,<:Any,P4} - set_parameter(::Type{<:$Typ{P1}}, ::Position{4}, P4) where {P1} = - $Typ{P1,<:Any,<:Any,P4} - set_parameter(::Type{<:$Typ{P1,P2}}, ::Position{4}, P4) where {P1,P2} = - $Typ{P1,P2,<:Any,P4} - set_parameter(::Type{<:$Typ{P1,P2,P3}}, ::Position{4}, P4) where {P1,P2,P3} = - $Typ{P1,P2,P3,P4} +# Set paramter 4 +set_parameter(T::Type{<:AbstractUnallocatedArray}, ::Position{4}, P4) = T{<:Any,<:Any,<:Any,P4} +set_parameter(T::Type{<:AbstractUnallocatedArray{P1}}, ::Position{4}, P4) where {P1} = + T{P1,<:Any,<:Any,P4} +set_parameter(T::Type{<:AbstractUnallocatedArray{P1,P2}}, ::Position{4}, P4) where {P1,P2} = + T{P1,P2,<:Any,P4} +set_parameter(T::Type{<:AbstractUnallocatedArray{P1,P2,P3}}, ::Position{4}, P4) where {P1,P2,P3} = + T{P1,P2,P3,P4} - ## default parameters - default_parameter(::Type{<:$Typ}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros - default_parameter(::Type{<:$Typ}, ::Position{2}) = 0 - default_parameter(::Type{<:$Typ}, ::Position{3}) = Tuple{} - default_parameter(::Type{<:$Typ}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray +## default parameters +default_parameter(::Type{<:AbstractUnallocatedArray}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros +default_parameter(::Type{<:AbstractUnallocatedArray}, ::Position{2}) = 0 +default_parameter(::Type{<:AbstractUnallocatedArray}, ::Position{3}) = Tuple{} +default_parameter(::Type{<:AbstractUnallocatedArray}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray - nparameters(::Type{<:$Typ}) = Val(4) - end -end +nparameters(::Type{<:AbstractUnallocatedArray}) = Val(4) \ No newline at end of file From 3027afa7531ba6c83fe8910f6c2e6c063c175e56 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 20 Nov 2023 09:56:43 -0500 Subject: [PATCH 048/156] Make other objects subtype of AbstractUnallocatedArray --- NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl | 2 +- NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl index e5d8988faa..362f0bb635 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl @@ -1,7 +1,7 @@ ## TODO All constructors not fully implemented but so far it matches the ## constructors found in `FillArrays`. Need to fix io struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: - FillArrays.AbstractFill{ElT,N,Axes} + FillArrays.AbstractFill{ElT,N,Axes} <: AbstractUnallocatedArray f::FillArrays.Fill{ElT,N,Axes} ## TODO use `set_parameters` as constructor to these types end diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl index b41c7b5d8a..beb36652c9 100644 --- a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl @@ -1,7 +1,7 @@ ## TODO Should Alloc also be of ElT and N or should there be ## More freedom there? struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: - FillArrays.AbstractZeros{ElT,N,Axes} + FillArrays.AbstractZeros{ElT,N,Axes} <: AbstractUnallocatedArray z::FillArrays.Zeros{ElT,N,Axes} ## TODO use `set_parameters` as constructor to these types end From 85a9837d4d223aeed68ee4979295efa2fadd1ab9 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 20 Nov 2023 09:57:34 -0500 Subject: [PATCH 049/156] Add norm --- NDTensors/src/UnallocatedArrays/test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/UnallocatedArrays/test/runtests.jl b/NDTensors/src/UnallocatedArrays/test/runtests.jl index 697829139f..400e89dcf6 100644 --- a/NDTensors/src/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/UnallocatedArrays/test/runtests.jl @@ -1,6 +1,6 @@ using FillArrays using NDTensors.UnallocatedArrays -using LinearAlgebra +using LinearAlgebra : norm using Test @testset "Testing UnallocatedArrays" begin From be31025f8234613f317f69214f42dc5e43b438f4 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 30 Nov 2023 15:14:14 -0500 Subject: [PATCH 050/156] Move unallocatedArrays and UnspecifiedTypes to lib folder --- NDTensors/src/NDTensors.jl | 9 +++------ NDTensors/src/{ => lib}/UnallocatedArrays/README.md | 0 .../{ => lib}/UnallocatedArrays/src/UnallocatedArrays.jl | 0 .../UnallocatedArrays/src/abstractunallocatedarray.jl | 0 NDTensors/src/{ => lib}/UnallocatedArrays/src/import.jl | 0 .../src/{ => lib}/UnallocatedArrays/src/promote_rules.jl | 0 .../src/{ => lib}/UnallocatedArrays/src/set_types.jl | 0 .../{ => lib}/UnallocatedArrays/src/unallocatedfill.jl | 0 .../{ => lib}/UnallocatedArrays/src/unallocatedzeros.jl | 0 .../src/{ => lib}/UnallocatedArrays/test/runtests.jl | 0 NDTensors/src/{ => lib}/UnspecifiedTypes/README.md | 0 .../{ => lib}/UnspecifiedTypes/src/UnspecifiedTypes.jl | 0 .../{ => lib}/UnspecifiedTypes/src/unspecifiedarray.jl | 0 .../{ => lib}/UnspecifiedTypes/src/unspecifiednumber.jl | 0 .../{ => lib}/UnspecifiedTypes/src/unspecifiedzero.jl | 0 15 files changed, 3 insertions(+), 6 deletions(-) rename NDTensors/src/{ => lib}/UnallocatedArrays/README.md (100%) rename NDTensors/src/{ => lib}/UnallocatedArrays/src/UnallocatedArrays.jl (100%) rename NDTensors/src/{ => lib}/UnallocatedArrays/src/abstractunallocatedarray.jl (100%) rename NDTensors/src/{ => lib}/UnallocatedArrays/src/import.jl (100%) rename NDTensors/src/{ => lib}/UnallocatedArrays/src/promote_rules.jl (100%) rename NDTensors/src/{ => lib}/UnallocatedArrays/src/set_types.jl (100%) rename NDTensors/src/{ => lib}/UnallocatedArrays/src/unallocatedfill.jl (100%) rename NDTensors/src/{ => lib}/UnallocatedArrays/src/unallocatedzeros.jl (100%) rename NDTensors/src/{ => lib}/UnallocatedArrays/test/runtests.jl (100%) rename NDTensors/src/{ => lib}/UnspecifiedTypes/README.md (100%) rename NDTensors/src/{ => lib}/UnspecifiedTypes/src/UnspecifiedTypes.jl (100%) rename NDTensors/src/{ => lib}/UnspecifiedTypes/src/unspecifiedarray.jl (100%) rename NDTensors/src/{ => lib}/UnspecifiedTypes/src/unspecifiednumber.jl (100%) rename NDTensors/src/{ => lib}/UnspecifiedTypes/src/unspecifiedzero.jl (100%) diff --git a/NDTensors/src/NDTensors.jl b/NDTensors/src/NDTensors.jl index 3a7d052f89..84f02b3977 100644 --- a/NDTensors/src/NDTensors.jl +++ b/NDTensors/src/NDTensors.jl @@ -18,12 +18,7 @@ using SplitApplyCombine using Strided using TimerOutputs using TupleTools -# TODO move these folders to `lib` -using .UnspecifiedTypes -include("UnallocatedArrays/src/UnallocatedArrays.jl") -using .UnallocatedArrays -include("DiagonalArrays/src/DiagonalArrays.jl") -======= + for lib in [ :AlgorithmSelection, :BaseExtensions, @@ -41,6 +36,8 @@ for lib in [ :SmallVectors, :SortedSets, :TagSets, + :UnspecifiedTypes, + :UnallocatedArrays, ] include("lib/$(lib)/src/$(lib).jl") @eval using .$lib: $lib diff --git a/NDTensors/src/UnallocatedArrays/README.md b/NDTensors/src/lib/UnallocatedArrays/README.md similarity index 100% rename from NDTensors/src/UnallocatedArrays/README.md rename to NDTensors/src/lib/UnallocatedArrays/README.md diff --git a/NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl similarity index 100% rename from NDTensors/src/UnallocatedArrays/src/UnallocatedArrays.jl rename to NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl diff --git a/NDTensors/src/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl similarity index 100% rename from NDTensors/src/UnallocatedArrays/src/abstractunallocatedarray.jl rename to NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl diff --git a/NDTensors/src/UnallocatedArrays/src/import.jl b/NDTensors/src/lib/UnallocatedArrays/src/import.jl similarity index 100% rename from NDTensors/src/UnallocatedArrays/src/import.jl rename to NDTensors/src/lib/UnallocatedArrays/src/import.jl diff --git a/NDTensors/src/UnallocatedArrays/src/promote_rules.jl b/NDTensors/src/lib/UnallocatedArrays/src/promote_rules.jl similarity index 100% rename from NDTensors/src/UnallocatedArrays/src/promote_rules.jl rename to NDTensors/src/lib/UnallocatedArrays/src/promote_rules.jl diff --git a/NDTensors/src/UnallocatedArrays/src/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl similarity index 100% rename from NDTensors/src/UnallocatedArrays/src/set_types.jl rename to NDTensors/src/lib/UnallocatedArrays/src/set_types.jl diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl similarity index 100% rename from NDTensors/src/UnallocatedArrays/src/unallocatedfill.jl rename to NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl diff --git a/NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl similarity index 100% rename from NDTensors/src/UnallocatedArrays/src/unallocatedzeros.jl rename to NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl diff --git a/NDTensors/src/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl similarity index 100% rename from NDTensors/src/UnallocatedArrays/test/runtests.jl rename to NDTensors/src/lib/UnallocatedArrays/test/runtests.jl diff --git a/NDTensors/src/UnspecifiedTypes/README.md b/NDTensors/src/lib/UnspecifiedTypes/README.md similarity index 100% rename from NDTensors/src/UnspecifiedTypes/README.md rename to NDTensors/src/lib/UnspecifiedTypes/README.md diff --git a/NDTensors/src/UnspecifiedTypes/src/UnspecifiedTypes.jl b/NDTensors/src/lib/UnspecifiedTypes/src/UnspecifiedTypes.jl similarity index 100% rename from NDTensors/src/UnspecifiedTypes/src/UnspecifiedTypes.jl rename to NDTensors/src/lib/UnspecifiedTypes/src/UnspecifiedTypes.jl diff --git a/NDTensors/src/UnspecifiedTypes/src/unspecifiedarray.jl b/NDTensors/src/lib/UnspecifiedTypes/src/unspecifiedarray.jl similarity index 100% rename from NDTensors/src/UnspecifiedTypes/src/unspecifiedarray.jl rename to NDTensors/src/lib/UnspecifiedTypes/src/unspecifiedarray.jl diff --git a/NDTensors/src/UnspecifiedTypes/src/unspecifiednumber.jl b/NDTensors/src/lib/UnspecifiedTypes/src/unspecifiednumber.jl similarity index 100% rename from NDTensors/src/UnspecifiedTypes/src/unspecifiednumber.jl rename to NDTensors/src/lib/UnspecifiedTypes/src/unspecifiednumber.jl diff --git a/NDTensors/src/UnspecifiedTypes/src/unspecifiedzero.jl b/NDTensors/src/lib/UnspecifiedTypes/src/unspecifiedzero.jl similarity index 100% rename from NDTensors/src/UnspecifiedTypes/src/unspecifiedzero.jl rename to NDTensors/src/lib/UnspecifiedTypes/src/unspecifiedzero.jl From d64f3dee5f710b77cee539531a69eac30b22ab0f Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Sun, 3 Dec 2023 10:35:13 -0500 Subject: [PATCH 051/156] Working on UnallocatedArrays --- .../src/abstractfill/abstractfill.jl | 50 +++++++++++++ .../src/abstractfill/set_types.jl | 70 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl create mode 100644 NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl new file mode 100644 index 0000000000..cc35ecb517 --- /dev/null +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl @@ -0,0 +1,50 @@ +## Here are functions specifically defined for UnallocatedArrays +## not implemented by FillArrays +## TODO determine min number of functions needed to be forwarded +alloctype(A::AbstractFill) = alloctype(typeof(A)) +function alloctype(Atype::Type{AbstractFill}) + return get_parameter(Atype, Position{4}()) +end + +allocate(A::AbstractFill) = alloctype(A)(parent(A)) + +## TODO Still working here I am not sure these functions and the +## Set parameter functions are working properly +function set_alloctype(F::Type{AbstractFill}, alloc::Type{<:AbstractArray}) + return set_parameter(F, Position{4}(), alloc) +end +## TODO this is broken +function set_eltype(F::Type{AbstractFill}, elt::Type) + return set_alloctype(set_eltype(parent(z), elt), set_eltype(alloctype(F), elt)) +end + +## With these functions defined I can print UnallocatedArrays +## compute things like sum and norm, compute the size and length +@inline Base.axes(A::AbstractFill) = axes(parent(A)) +Base.size(A::AbstractFill) = size(parent(A)) +function FillArrays.getindex_value(A::AbstractFill) + return FillArrays.getindex_value(parent(A)) +end +Base.copy(A::AbstractFill) = A +## Can't actually use NDTensors.set_eltype because it doesn't +## exist yet in thie area +function Base.complex(A::AbstractFill) + return set_alloctype( + complex(parent(A)), set_parameter(alloctype(A), Position{1}(), complex(eltype(A))) + ) +end + +mult_fill(a, b, val, ax) = Fill(val, ax) +mult_zeros(a, b, elt, ax) = Zeros{elt}(ax) +mult_ones(a, b, elt, ax) = Ones{elt}(ax) + +broadcasted_fill(f, a, val, ax) = Fill(val, ax) +broadcasted_fill(f, a, b, val, ax) = Fill(val, ax) +broadcasted_zeros(f, a, elt, ax) = Zeros{elt}(ax) +broadcasted_zeros(f, a, b, elt, ax) = Zeros{elt}(ax) +broadcasted_ones(f, a, elt, ax) = Ones{elt}(ax) +broadcasted_ones(f, a, b, elt, ax) = Ones{elt}(ax) + +kron_fill(a, b, val, ax) = Fill(val, ax) +kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) +kron_ones(a, b, elt, ax) = Ones{elt}(ax) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl new file mode 100644 index 0000000000..5fd641b0e8 --- /dev/null +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl @@ -0,0 +1,70 @@ +## TODO make unit tests for all of these functions +## TODO remove P4 +# `SetParameters.jl` overloads. +get_parameter(::Type{<:AbstractFill{P1}}, ::Position{1}) where {P1} = P1 +get_parameter(::Type{<:AbstractFill{<:Any,P2}}, ::Position{2}) where {P2} = P2 +get_parameter(::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 + +## Setting paramaters +# Set parameter 1 +set_parameter(T::Type{<:AbstractFill}, ::Position{1}, P1) = T{P1} +set_parameter(T::Type{<:AbstractFill{<:Any,P2}}, ::Position{1}, P1) where {P2} = T{P1,P2} +function set_parameter( + T::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{1}, P1 +) where {P3} + return T{P1,<:Any,P3} +end +function set_parameter( + T::Type{<:AbstractFill{<:Any,P2,P3}}, ::Position{1}, P1 +) where {P2,P3} + return T{P1,P2,P3} +end +function set_parameter( + T::Type{<:AbstractFill{<:Any,P2,P3,P4}}, ::Position{1}, P1 +) where {P2,P3,P4} + return T{P1,P2,P3,P4} +end +function set_parameter( + T::Type{<:AbstractFill{<:Any,<:Any,P3,P4}}, ::Position{1}, P1 +) where {P3,P4} + return T{P1,<:Any,P3,P4} +end +function set_parameter( + T::Type{<:AbstractFill{<:Any,P2,<:Any,P4}}, ::Position{1}, P1 +) where {P2,P4} + return T{P1,P2,<:Any,P4} +end +function set_parameter( + T::Type{<:AbstractFill{<:Any,<:Any,<:Any,P4}}, ::Position{1}, P1 +) where {P4} + return T{P1,<:Any,<:Any,P4} +end + +# Set parameter 2 +set_parameter(T::Type{<:AbstractFill}, ::Position{2}, P2) = T{<:Any,P2} +set_parameter(T::Type{<:AbstractFill{P1}}, ::Position{2}, P2) where {P1} = T{P1,P2} +function set_parameter( + T::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{2}, P2 +) where {P3} + return T{<:Any,P2,P3} +end +function set_parameter( + T::Type{<:AbstractFill{P1,<:Any,P3}}, ::Position{2}, P2 +) where {P1,P3} + return T{P1,P2,P3} +end + +# Set parameter 3 +set_parameter(T::Type{<:AbstractFill}, ::Position{3}, P3) = T{<:Any,<:Any,P3} +set_parameter(T::Type{<:AbstractFill{P1}}, ::Position{3}, P3) where {P1} = T{P1,<:Any,P3} +function set_parameter(T::Type{<:AbstractFill{<:Any,P2}}, ::Position{3}, P3) where {P2} + return T{<:Any,P2,P3} +end +set_parameter(T::Type{<:AbstractFill{P1,P2}}, ::Position{3}, P3) where {P1,P2} = T{P1,P2,P3} + +## default parameters +default_parameter(::Type{<:AbstractFill}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros +default_parameter(::Type{<:AbstractFill}, ::Position{2}) = 0 +default_parameter(::Type{<:AbstractFill}, ::Position{3}) = Tuple{} + +nparameters(::Type{<:AbstractFill}) = Val(3) From ac5cb591a29088bddfe1df3f6b941d6f65b6fe82 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 10:43:38 -0500 Subject: [PATCH 052/156] Remove old file --- .../src/abstractunallocatedarray.jl | 53 ------------------- 1 file changed, 53 deletions(-) delete mode 100644 NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl deleted file mode 100644 index 155e0cfd54..0000000000 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ /dev/null @@ -1,53 +0,0 @@ -abstract type AbstractUnallocatedArray -end - -## Here are functions specifically defined for UnallocatedArrays -## not implemented by FillArrays -## TODO determine min number of functions needed to be forwarded -alloctype(A::AbstractUnallocatedArray) = alloctype(typeof(A)) -function alloctype(Atype::Type{UnallocFillOrZero}) - return get_parameter(Atype, Position{4}()) -end - -allocate(A::AbstractUnallocatedArray) = alloctype(A)(parent(A)) - -## TODO Still working here I am not sure these functions and the -## Set parameter functions are working properly -function set_alloctype(F::Type{AbstractUnallocatedArray}, alloc::Type{<:AbstractArray}) - return set_parameter(F, Position{4}(), alloc) -end -## TODO this is broken -function set_eltype(F::Type{AbstractUnallocatedArray}, elt::Type) - return set_alloctype(set_eltype(parent(z), elt), set_eltype(alloctype(F), elt)) -end - -## With these functions defined I can print UnallocatedArrays -## compute things like sum and norm, compute the size and length -@inline Base.axes(A::AbstractUnallocatedArray) = axes(parent(A)) -Base.size(A::AbstractUnallocatedArray) = size(parent(A)) -function FillArrays.getindex_value(A::AbstractUnallocatedArray) - return FillArrays.getindex_value(parent(A)) -end -Base.copy(A::AbstractUnallocatedArray) = A -## Can't actually use NDTensors.set_eltype because it doesn't -## exist yet in thie area -function Base.complex(A::AbstractUnallocatedArray) - return set_alloctype( - complex(parent(A)), set_parameter(alloctype(A), Position{1}(), complex(eltype(A))) - ) -end - -mult_fill(a, b, val, ax) = Fill(val, ax) -mult_zeros(a, b, elt, ax) = Zeros{elt}(ax) -mult_ones(a, b, elt, ax) = Ones{elt}(ax) - -broadcasted_fill(f, a, val, ax) = Fill(val, ax) -broadcasted_fill(f, a, b, val, ax) = Fill(val, ax) -broadcasted_zeros(f, a, elt, ax) = Zeros{elt}(ax) -broadcasted_zeros(f, a, b, elt, ax) = Zeros{elt}(ax) -broadcasted_ones(f, a, elt, ax) = Ones{elt}(ax) -broadcasted_ones(f, a, b, elt, ax) = Ones{elt}(ax) - -kron_fill(a, b, val, ax) = Fill(val, ax) -kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) -kron_ones(a, b, elt, ax) = Ones{elt}(ax) From 834287ad9c58277afb439ed9b47654c8acc7b565 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 10:48:20 -0500 Subject: [PATCH 053/156] Updates to UnallocatedArrays system --- .../src/UnallocatedArrays.jl | 6 +- .../src/abstractfill/abstractfill.jl | 37 +++--- .../src/abstractfill/set_types.jl | 20 ---- .../lib/UnallocatedArrays/src/set_types.jl | 112 ++++++++---------- .../UnallocatedArrays/src/unallocatedfill.jl | 11 +- .../UnallocatedArrays/src/unallocatedzeros.jl | 11 +- .../lib/UnallocatedArrays/test/runtests.jl | 57 +++++---- .../src/lib/UnspecifiedTypes/test/runtests.jl | 9 ++ 8 files changed, 133 insertions(+), 130 deletions(-) create mode 100644 NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index 6f3c63354a..e28b2b9334 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,11 +1,13 @@ module UnallocatedArrays -using FillArrays +using FillArrays: FillArrays, AbstractFill, Fill, Zeros using LinearAlgebra using NDTensors.SetParameters include("import.jl") -include("abstractunallocatedarray.jl") +include("abstractfill/abstractfill.jl") +include("abstractfill/set_types.jl") + include("unallocatedfill.jl") include("unallocatedzeros.jl") include("set_types.jl") diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl index cc35ecb517..d35654fb05 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl @@ -2,7 +2,7 @@ ## not implemented by FillArrays ## TODO determine min number of functions needed to be forwarded alloctype(A::AbstractFill) = alloctype(typeof(A)) -function alloctype(Atype::Type{AbstractFill}) +function alloctype(Atype::Type{<:AbstractFill}) return get_parameter(Atype, Position{4}()) end @@ -10,11 +10,11 @@ allocate(A::AbstractFill) = alloctype(A)(parent(A)) ## TODO Still working here I am not sure these functions and the ## Set parameter functions are working properly -function set_alloctype(F::Type{AbstractFill}, alloc::Type{<:AbstractArray}) +function set_alloctype(F::Type{<:AbstractFill}, alloc::Type{<:AbstractArray}) return set_parameter(F, Position{4}(), alloc) end ## TODO this is broken -function set_eltype(F::Type{AbstractFill}, elt::Type) +function set_eltype(F::Type{<:AbstractFill}, elt::Type) return set_alloctype(set_eltype(parent(z), elt), set_eltype(alloctype(F), elt)) end @@ -26,25 +26,18 @@ function FillArrays.getindex_value(A::AbstractFill) return FillArrays.getindex_value(parent(A)) end Base.copy(A::AbstractFill) = A -## Can't actually use NDTensors.set_eltype because it doesn't -## exist yet in thie area -function Base.complex(A::AbstractFill) - return set_alloctype( - complex(parent(A)), set_parameter(alloctype(A), Position{1}(), complex(eltype(A))) - ) -end -mult_fill(a, b, val, ax) = Fill(val, ax) -mult_zeros(a, b, elt, ax) = Zeros{elt}(ax) -mult_ones(a, b, elt, ax) = Ones{elt}(ax) +# mult_fill(a, b, val, ax) = Fill(val, ax) +# mult_zeros(a, b, elt, ax) = Zeros{elt}(ax) +# mult_ones(a, b, elt, ax) = Ones{elt}(ax) -broadcasted_fill(f, a, val, ax) = Fill(val, ax) -broadcasted_fill(f, a, b, val, ax) = Fill(val, ax) -broadcasted_zeros(f, a, elt, ax) = Zeros{elt}(ax) -broadcasted_zeros(f, a, b, elt, ax) = Zeros{elt}(ax) -broadcasted_ones(f, a, elt, ax) = Ones{elt}(ax) -broadcasted_ones(f, a, b, elt, ax) = Ones{elt}(ax) +# broadcasted_fill(f, a, val, ax) = Fill(val, ax) +# broadcasted_fill(f, a, b, val, ax) = Fill(val, ax) +# broadcasted_zeros(f, a, elt, ax) = Zeros{elt}(ax) +# broadcasted_zeros(f, a, b, elt, ax) = Zeros{elt}(ax) +# broadcasted_ones(f, a, elt, ax) = Ones{elt}(ax) +# broadcasted_ones(f, a, b, elt, ax) = Ones{elt}(ax) -kron_fill(a, b, val, ax) = Fill(val, ax) -kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) -kron_ones(a, b, elt, ax) = Ones{elt}(ax) +# kron_fill(a, b, val, ax) = Fill(val, ax) +# kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) +# kron_ones(a, b, elt, ax) = Ones{elt}(ax) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl index 5fd641b0e8..e5eaa5e32d 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl @@ -19,26 +19,6 @@ function set_parameter( ) where {P2,P3} return T{P1,P2,P3} end -function set_parameter( - T::Type{<:AbstractFill{<:Any,P2,P3,P4}}, ::Position{1}, P1 -) where {P2,P3,P4} - return T{P1,P2,P3,P4} -end -function set_parameter( - T::Type{<:AbstractFill{<:Any,<:Any,P3,P4}}, ::Position{1}, P1 -) where {P3,P4} - return T{P1,<:Any,P3,P4} -end -function set_parameter( - T::Type{<:AbstractFill{<:Any,P2,<:Any,P4}}, ::Position{1}, P1 -) where {P2,P4} - return T{P1,P2,<:Any,P4} -end -function set_parameter( - T::Type{<:AbstractFill{<:Any,<:Any,<:Any,P4}}, ::Position{1}, P1 -) where {P4} - return T{P1,<:Any,<:Any,P4} -end # Set parameter 2 set_parameter(T::Type{<:AbstractFill}, ::Position{2}, P2) = T{<:Any,P2} diff --git a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl index 1902f7a70a..3735522369 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl @@ -1,72 +1,58 @@ -## TODO make unit tests for all of these functions +# ## TODO make unit tests for all of these functions +## TODO All I need to do is overload AbstractFill functions with 4 parameters # `SetParameters.jl` overloads. -get_parameter(::Type{<:AbstractUnallocatedArray{P1}}, ::Position{1}) where {P1} = P1 -get_parameter(::Type{<:AbstractUnallocatedArray{<:Any,P2}}, ::Position{2}) where {P2} = P2 -get_parameter(::Type{<:AbstractUnallocatedArray{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 -get_parameter(::Type{<:AbstractUnallocatedArray{<:Any,<:Any,<:Any,P4}}, ::Position{4}) where {P4} = P4 - -## Setting paramaters -# Set parameter 1 -set_parameter(T::Type{<:AbstractUnallocatedArray}, ::Position{1}, P1) = T{P1} -set_parameter(T::Type{<:AbstractUnallocatedArray{<:Any,P2}}, ::Position{1}, P1) where {P2} = T{P1,P2} -function set_parameter(T::Type{<:AbstractUnallocatedArray{<:Any,<:Any,P3}}, ::Position{1}, P1) where {P3} - return T{P1,<:Any,P3} -end -function set_parameter(T::Type{<:AbstractUnallocatedArray{<:Any,P2,P3}}, ::Position{1}, P1) where {P2,P3} - return T{P1,P2,P3} -end -function set_parameter( - T::Type{<:AbstractUnallocatedArray{<:Any,P2,P3,P4}}, ::Position{1}, P1 -) where {P2,P3,P4} - return T{P1,P2,P3,P4} -end -function set_parameter( - T::Type{<:AbstractUnallocatedArray{<:Any,<:Any,P3,P4}}, ::Position{1}, P1 -) where {P3,P4} - return T{P1,<:Any,P3,P4} -end -function set_parameter( - T::Type{<:AbstractUnallocatedArray{<:Any,P2,<:Any,P4}}, ::Position{1}, P1 -) where {P2,P4} - return T{P1,P2,<:Any,P4} +function get_parameter( + ::Type{<:UnallocatedFill{<:Any,<:Any,<:Any,P4}}, ::Position{4} +) where {P4} + return P4 end -function set_parameter( - T::Type{<:AbstractUnallocatedArray{<:Any,<:Any,<:Any,P4}}, ::Position{1}, P1 +function get_parameter( + ::Type{<:UnallocatedZeros{<:Any,<:Any,<:Any,P4}}, ::Position{4} ) where {P4} - return T{P1,<:Any,<:Any,P4} + return P4 end -# Set parameter 2 -set_parameter(T::Type{<:AbstractUnallocatedArray}, ::Position{2}, P2) = T{<:Any,P2} -set_parameter(T::Type{<:AbstractUnallocatedArray{P1}}, ::Position{2}, P2) where {P1} = T{P1,P2} -function set_parameter(T::Type{<:AbstractUnallocatedArray{<:Any,<:Any,P3}}, ::Position{2}, P2) where {P3} - return T{<:Any,P2,P3} -end -function set_parameter(T::Type{<:AbstractUnallocatedArray{P1,<:Any,P3}}, ::Position{2}, P2) where {P1,P3} - return T{P1,P2,P3} -end +# ## Setting paramaters +# ## Set parameter 1 +# function set_parameter( +# T::Type{<:AbstractUnallocatedArray{<:Any,P2,P3,P4}}, ::Position{1}, P1 +# ) where {P2,P3,P4} +# return T{P1,P2,P3,P4} +# end +# function set_parameter( +# T::Type{<:AbstractUnallocatedArray{<:Any,<:Any,P3,P4}}, ::Position{1}, P1 +# ) where {P3,P4} +# return T{P1,<:Any,P3,P4} +# end +# function set_parameter( +# T::Type{<:AbstractUnallocatedArray{<:Any,P2,<:Any,P4}}, ::Position{1}, P1 +# ) where {P2,P4} +# return T{P1,P2,<:Any,P4} +# end +# function set_parameter( +# T::Type{<:AbstractUnallocatedArray{<:Any,<:Any,<:Any,P4}}, ::Position{1}, P1 +# ) where {P4} +# return T{P1,<:Any,<:Any,P4} +# end -# Set parameter 3 -set_parameter(T::Type{<:AbstractUnallocatedArray}, ::Position{3}, P3) = T{<:Any,<:Any,P3} -set_parameter(T::Type{<:AbstractUnallocatedArray{P1}}, ::Position{3}, P3) where {P1} = T{P1,<:Any,P3} -function set_parameter(T::Type{<:AbstractUnallocatedArray{<:Any,P2}}, ::Position{3}, P3) where {P2} - return T{<:Any,P2,P3} -end -set_parameter(T::Type{<:AbstractUnallocatedArray{P1,P2}}, ::Position{3}, P3) where {P1,P2} = T{P1,P2,P3} +# ## Set parameter 2 +# function set_parameter(T::Type{<:AbstractUnallocatedArray{P1,<:Any,P3, P4}}, ::Position{2}, P2) where {P1,P3} +# return T{P1,P2,P3, P4} +# end + +# # Set parameter 3 +# set_parameter(T::Type{<:AbstractUnallocatedArray{P1,P2}}, ::Position{3}, P3) where {P1,P2} = T{P1,P2,P3} -# Set paramter 4 -set_parameter(T::Type{<:AbstractUnallocatedArray}, ::Position{4}, P4) = T{<:Any,<:Any,<:Any,P4} -set_parameter(T::Type{<:AbstractUnallocatedArray{P1}}, ::Position{4}, P4) where {P1} = - T{P1,<:Any,<:Any,P4} -set_parameter(T::Type{<:AbstractUnallocatedArray{P1,P2}}, ::Position{4}, P4) where {P1,P2} = - T{P1,P2,<:Any,P4} -set_parameter(T::Type{<:AbstractUnallocatedArray{P1,P2,P3}}, ::Position{4}, P4) where {P1,P2,P3} = - T{P1,P2,P3,P4} +# # Set paramter 4 +# set_parameter(T::Type{<:AbstractUnallocatedArray}, ::Position{4}, P4) = T{<:Any,<:Any,<:Any,P4} +# set_parameter(T::Type{<:AbstractUnallocatedArray{P1}}, ::Position{4}, P4) where {P1} = +# T{P1,<:Any,<:Any,P4} +# set_parameter(T::Type{<:AbstractUnallocatedArray{P1,P2}}, ::Position{4}, P4) where {P1,P2} = +# T{P1,P2,<:Any,P4} +# set_parameter(T::Type{<:AbstractUnallocatedArray{P1,P2,P3}}, ::Position{4}, P4) where {P1,P2,P3} = +# T{P1,P2,P3,P4} -## default parameters -default_parameter(::Type{<:AbstractUnallocatedArray}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros -default_parameter(::Type{<:AbstractUnallocatedArray}, ::Position{2}) = 0 -default_parameter(::Type{<:AbstractUnallocatedArray}, ::Position{3}) = Tuple{} -default_parameter(::Type{<:AbstractUnallocatedArray}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray +# ## default parameters +# default_parameter(::Type{<:AbstractUnallocatedArray}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray -nparameters(::Type{<:AbstractUnallocatedArray}) = Val(4) \ No newline at end of file +# nparameters(::Type{<:AbstractUnallocatedArray}) = Val(4) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 362f0bb635..23e421efaf 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -1,7 +1,7 @@ ## TODO All constructors not fully implemented but so far it matches the ## constructors found in `FillArrays`. Need to fix io struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: - FillArrays.AbstractFill{ElT,N,Axes} <: AbstractUnallocatedArray + FillArrays.AbstractFill{ElT,N,Axes} f::FillArrays.Fill{ElT,N,Axes} ## TODO use `set_parameters` as constructor to these types end @@ -11,3 +11,12 @@ function set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) end Base.parent(F::UnallocatedFill) = F.f + +## Here I can't overload ::AbstractFill because this overwrites Base.complex in +## Fill arrays which creates an infinite loop. Another option would be to write +## UnallocatedArrays.complex(::AbstractFill) which calls Base.complex on the parent +function Base.complex(A::UnallocatedFill) + return set_alloctype( + complex(parent(A)), set_parameter(alloctype(A), Position{1}(), complex(eltype(A))) + ) +end diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index beb36652c9..576458afce 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -1,7 +1,7 @@ ## TODO Should Alloc also be of ElT and N or should there be ## More freedom there? struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: - FillArrays.AbstractZeros{ElT,N,Axes} <: AbstractUnallocatedArray + FillArrays.AbstractZeros{ElT,N,Axes} z::FillArrays.Zeros{ElT,N,Axes} ## TODO use `set_parameters` as constructor to these types end @@ -11,3 +11,12 @@ function set_alloctype(z::Zeros, alloc::Type{<:AbstractArray}) end Base.parent(Z::UnallocatedZeros) = Z.z + +## Here I can't overload ::AbstractFill because this overwrites Base.complex in +## Fill arrays which creates an infinite loop. Another option would be to write +## UnallocatedArrays.complex(::AbstractFill) which calls Base.complex on the parent +function Base.complex(A::UnallocatedZeros) + return set_alloctype( + complex(parent(A)), set_parameter(alloctype(A), Position{1}(), complex(eltype(A))) + ) +end diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 400e89dcf6..10df98fe88 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -1,19 +1,29 @@ -using FillArrays +@eval module $(gensym()) +using FillArrays: FillArrays, AbstractFill, Fill, Zeros +using NDTensors: NDTensors using NDTensors.UnallocatedArrays -using LinearAlgebra : norm -using Test +using LinearAlgebra: norm +using Test: @test, @testset, @test_broken +## Could potentially need this +#using GPUArraysCore: @allowscalar -@testset "Testing UnallocatedArrays" begin - z = Zeros{Float64}((2, 3)) - Z = UnallocatedZeros{eltype(z),ndims(z),typeof(axes(z)),Matrix{eltype(z)}}(z) +include(joinpath(pkgdir(NDTensors), "test", "NDTensorsTestUtils", "NDTensorsTestUtils.jl")) +using .NDTensorsTestUtils: devices_list +@testset "Testing UnallocatedArrays" for dev in devices_list(ARGS), + elt in (Float64, Float32, ComplexF64, ComplexF32) + + z = Zeros{elt}((2, 3)) + Z = UnallocatedZeros{eltype(z),ndims(z),typeof(axes(z)),dev(Matrix{eltype(z)})}(z) + + @test Z isa AbstractFill @test size(Z) == (2, 3) @test length(Z) == 6 @test sum(Z) == 0 @test norm(Z) == 0 @test Z[2, 3] == 0 - @test allocate(Z) isa Matrix{eltype(z)} - Zp = set_alloctype(z, Matrix{eltype(z)}) + @test allocate(Z) isa dev(Matrix{elt}) + Zp = set_alloctype(z, dev(Matrix{elt})) @test Zp == Z Zc = copy(Z) @test Zc == Z @@ -21,32 +31,37 @@ using Test @test eltype(Zc) == complex(eltype(z)) @test Zc[1, 2] == 0.0 + 0.0im - # z = Zeros(()) - # Z = UnallocatedZeros{Float32, ndims(z), typeof(axes(z)), Vector{Float32}}(z) - # @test size(Z) == () - # @test length(Z) == 1 - # @test Z[] == 0 - # Z = set_alloctype(z, Matrix{Float64}) - # m = allocate(Z) - # @test length(m) == 1 - # m[] == 0 + ## Things that are still broken + R = Zc * Zc' + @test_broken R isa UnallocatedZeros + R = Zc + Zc + @test_broken R isa UnallocatedZeros + R = Zc .* Zc + @test_broken R isa UnallocatedZeros - f = Fill{Float64}(3.0, (2, 3, 4)) - F = UnallocatedFill{eltype(f),ndims(f),typeof(axes(f)),Array{eltype(f),ndims(f)}}(f) + ######################################### + # UnallocatedFill + f = Fill{elt}(3.0, (2, 3, 4)) + F = UnallocatedFill{elt,ndims(f),typeof(axes(f)),Array{elt,ndims(f)}}(f) @test size(F) == (2, 3, 4) @test length(F) == 24 @test sum(F) ≈ 3 * 24 @test norm(F) ≈ sqrt(3^2 * 24) @test F[2, 3, 1] == 3.0 - @test allocate(F) isa Array{eltype(z),3} + @test allocate(F) isa Array{elt,3} Fp = allocate(F) @test norm(Fp) ≈ norm(F) - Fp = set_alloctype(f, Array{eltype(f),ndims(f)}) + + Fp = set_alloctype(f, dev(Array{elt,ndims(f)})) + @test allocate(Fp) isa dev(Array{elt,ndims(f)}) @test Fp == F Fc = copy(F) @test Fc == F Fc = allocate(complex(F)) @test eltype(Fc) == complex(eltype(F)) + ## This allocates is this correct? + ## TODO this is broken because it doesn't call allocate Fc[2, 3, 4] = 4.0 + 3.0im @test Fc[2, 3, 4] == 4.0 + 3.0im end +end diff --git a/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl b/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl new file mode 100644 index 0000000000..700ad9fac2 --- /dev/null +++ b/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl @@ -0,0 +1,9 @@ +## TODO This needs work here +@eval module $(gensym()) +using NDTensors.UnspecifiedTypes +using Test: @test, @testset + +@testset "Testing UnspecifiedTypes" begin + UA = UnspecifiedArray{} +end +end From 4c6622492d632a0a344facc35f5301e2c6c11800 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 12:44:12 -0500 Subject: [PATCH 054/156] Remove import to be more explicit --- NDTensors/src/lib/UnallocatedArrays/src/import.jl | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 NDTensors/src/lib/UnallocatedArrays/src/import.jl diff --git a/NDTensors/src/lib/UnallocatedArrays/src/import.jl b/NDTensors/src/lib/UnallocatedArrays/src/import.jl deleted file mode 100644 index c8747f39c6..0000000000 --- a/NDTensors/src/lib/UnallocatedArrays/src/import.jl +++ /dev/null @@ -1,2 +0,0 @@ -import Base: getindex, copy -import NDTensors.SetParameters: nparameters, get_parameter, set_parameter, default_parameter From 19b29df3bf5547ee00539e4f0a8743cd57ea460d Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 12:44:52 -0500 Subject: [PATCH 055/156] Cleanup/fixes --- .../src/UnallocatedArrays.jl | 2 +- .../src/abstractfill/abstractfill.jl | 17 ++-- .../src/abstractfill/set_types.jl | 84 +++++++++++-------- .../UnallocatedArrays/src/promote_rules.jl | 6 +- .../lib/UnallocatedArrays/src/set_types.jl | 58 ++++--------- .../UnallocatedArrays/src/unallocatedfill.jl | 12 ++- .../UnallocatedArrays/src/unallocatedzeros.jl | 10 ++- .../lib/UnallocatedArrays/test/runtests.jl | 5 +- 8 files changed, 91 insertions(+), 103 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index e28b2b9334..8d9bad3811 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,5 +1,5 @@ module UnallocatedArrays -using FillArrays: FillArrays, AbstractFill, Fill, Zeros +using FillArrays: FillArrays, AbstractFill, Fill, Zeros, getindex_value using LinearAlgebra using NDTensors.SetParameters diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl index d35654fb05..2bd05a2a3f 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl @@ -8,25 +8,20 @@ end allocate(A::AbstractFill) = alloctype(A)(parent(A)) -## TODO Still working here I am not sure these functions and the -## Set parameter functions are working properly -function set_alloctype(F::Type{<:AbstractFill}, alloc::Type{<:AbstractArray}) - return set_parameter(F, Position{4}(), alloc) -end -## TODO this is broken -function set_eltype(F::Type{<:AbstractFill}, elt::Type) - return set_alloctype(set_eltype(parent(z), elt), set_eltype(alloctype(F), elt)) -end - +set_eltype(T::Type{<:AbstractFill}, elt::Type) = set_parameters(T, Position{1}(), elt) +set_ndims(T::Type{<:AbstractFill}, n) = set_parameters(T, Position{2}(), n) +set_axes(T::Type{<:AbstractFill}, ax::Type) = set_parameters(T, Position{3}(), ax) + ## With these functions defined I can print UnallocatedArrays ## compute things like sum and norm, compute the size and length @inline Base.axes(A::AbstractFill) = axes(parent(A)) Base.size(A::AbstractFill) = size(parent(A)) function FillArrays.getindex_value(A::AbstractFill) - return FillArrays.getindex_value(parent(A)) + return getindex_value(parent(A)) end Base.copy(A::AbstractFill) = A +## TODO get these working for UnallocatedX # mult_fill(a, b, val, ax) = Fill(val, ax) # mult_zeros(a, b, elt, ax) = Zeros{elt}(ax) # mult_ones(a, b, elt, ax) = Ones{elt}(ax) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl index e5eaa5e32d..d07e2708f0 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl @@ -1,50 +1,60 @@ +using NDTensors.SetParameters: SetParameters ## TODO make unit tests for all of these functions ## TODO remove P4 # `SetParameters.jl` overloads. -get_parameter(::Type{<:AbstractFill{P1}}, ::Position{1}) where {P1} = P1 -get_parameter(::Type{<:AbstractFill{<:Any,P2}}, ::Position{2}) where {P2} = P2 -get_parameter(::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 +SetParameters.get_parameter(::Type{<:AbstractFill{P1}}, ::Position{1}) where {P1} = P1 +SetParameters.get_parameter(::Type{<:AbstractFill{<:Any,P2}}, ::Position{2}) where {P2} = P2 +SetParameters.get_parameter(::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 ## Setting paramaters +# right now I am just defining the necessary ones for my implementation still working on full implementation # Set parameter 1 -set_parameter(T::Type{<:AbstractFill}, ::Position{1}, P1) = T{P1} -set_parameter(T::Type{<:AbstractFill{<:Any,P2}}, ::Position{1}, P1) where {P2} = T{P1,P2} -function set_parameter( - T::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{1}, P1 -) where {P3} - return T{P1,<:Any,P3} -end -function set_parameter( - T::Type{<:AbstractFill{<:Any,P2,P3}}, ::Position{1}, P1 -) where {P2,P3} - return T{P1,P2,P3} -end +SetParameters.set_parameter(T::Type{<:AbstractFill}, ::Position{1}, P1) = T{P1} # Set parameter 2 -set_parameter(T::Type{<:AbstractFill}, ::Position{2}, P2) = T{<:Any,P2} -set_parameter(T::Type{<:AbstractFill{P1}}, ::Position{2}, P2) where {P1} = T{P1,P2} -function set_parameter( - T::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{2}, P2 -) where {P3} - return T{<:Any,P2,P3} -end -function set_parameter( - T::Type{<:AbstractFill{P1,<:Any,P3}}, ::Position{2}, P2 -) where {P1,P3} - return T{P1,P2,P3} -end +SetParameters.set_parameter(T::Type{<:AbstractFill{P1}}, ::Position{2}, P2) where {P1} = T{P2} # Set parameter 3 -set_parameter(T::Type{<:AbstractFill}, ::Position{3}, P3) = T{<:Any,<:Any,P3} -set_parameter(T::Type{<:AbstractFill{P1}}, ::Position{3}, P3) where {P1} = T{P1,<:Any,P3} -function set_parameter(T::Type{<:AbstractFill{<:Any,P2}}, ::Position{3}, P3) where {P2} - return T{<:Any,P2,P3} -end -set_parameter(T::Type{<:AbstractFill{P1,P2}}, ::Position{3}, P3) where {P1,P2} = T{P1,P2,P3} +SetParameters.set_parameter(T::Type{<:AbstractFill{P1,P2}}, ::Position{3}, P3) where {P1,P2} = T{P3} + +## TODO define a specify_parameters function +## To quickly specify P1, P2, and P3 ## default parameters -default_parameter(::Type{<:AbstractFill}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros -default_parameter(::Type{<:AbstractFill}, ::Position{2}) = 0 -default_parameter(::Type{<:AbstractFill}, ::Position{3}) = Tuple{} +SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros +SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{2}) = 0 +SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{3}) = Tuple{} + +SetParameters.nparameters(::Type{<:AbstractFill}) = Val(3) -nparameters(::Type{<:AbstractFill}) = Val(3) +# Set parameter 1 +## Right now using AbstractArray +## TODO These are more difficult because T is technically defined so need some way to strip T of it {} types +# function set_parameter( +# T::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{1}, P1 +# ) where {P3} +# return T{P1,<:Any,P3} +# end +# function set_parameter( +# T::Type{<:AbstractFill{<:Any,P2,P3}}, ::Position{1}, P1 +# ) where {P2,P3} +# return T{P1,P2,P3} +# end +# function set_parameter( +# T::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{2}, P2 +# ) where {P3} +# return T{<:Any,P2,P3} +# end +# function set_parameter( +# T::Type{<:AbstractFill{P1,<:Any,P3}}, ::Position{2}, P2 +# ) where {P1,P3} +# return T +# end +# set_parameter(T::Type{<:AbstractFill}, ::Position{3}, P3) = T{<:Any,<:Any,P3} +# set_parameter(T::Type{<:AbstractFill{P1}}, ::Position{3}, P3) where {P1} = T{<:Any,P3} +# function set_parameter(T::Type{<:AbstractFill{<:Any,P2}}, ::Position{3}, P3) where {P2} +# return T{<:Any,P2,P3} +# end + +# Set parameter 2 +## using AbstractArray diff --git a/NDTensors/src/lib/UnallocatedArrays/src/promote_rules.jl b/NDTensors/src/lib/UnallocatedArrays/src/promote_rules.jl index 9982b6ced1..2ac0bf01ff 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/promote_rules.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/promote_rules.jl @@ -1,6 +1,6 @@ ## TODO this section is not finished. I just pasted this from the previous PR. ## Still working here -function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:UnallocatedZeros}) +function Base.promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:UnallocatedZeros}) ElT = promote_type(eltype(z1), eltype(z2)) @assert ndims(z1) == ndims(z2) Axs = axes(z1) @@ -8,7 +8,7 @@ function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:UnallocatedZeros} return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} end -function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:AbstractArray}) +function Base.promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:AbstractArray}) ElT = promote_type(eltype(z1), eltype(z2)) @assert ndims(z1) == ndims(z2) Axs = axes(z1) @@ -17,6 +17,6 @@ function promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:AbstractArray}) return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} end -function promote_rule(z1::Type{<:AbstractArray}, z2::Type{<:UnallocatedZeros}) +function Base.promote_rule(z1::Type{<:AbstractArray}, z2::Type{<:UnallocatedZeros}) return promote_rule(z2, z1) end diff --git a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl index 3735522369..4dd8af6774 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl @@ -1,58 +1,32 @@ # ## TODO make unit tests for all of these functions ## TODO All I need to do is overload AbstractFill functions with 4 parameters # `SetParameters.jl` overloads. -function get_parameter( +function SetParameters.get_parameter( ::Type{<:UnallocatedFill{<:Any,<:Any,<:Any,P4}}, ::Position{4} ) where {P4} return P4 end -function get_parameter( +function SetParameters.get_parameter( ::Type{<:UnallocatedZeros{<:Any,<:Any,<:Any,P4}}, ::Position{4} ) where {P4} return P4 end # ## Setting paramaters -# ## Set parameter 1 -# function set_parameter( -# T::Type{<:AbstractUnallocatedArray{<:Any,P2,P3,P4}}, ::Position{1}, P1 -# ) where {P2,P3,P4} -# return T{P1,P2,P3,P4} -# end -# function set_parameter( -# T::Type{<:AbstractUnallocatedArray{<:Any,<:Any,P3,P4}}, ::Position{1}, P1 -# ) where {P3,P4} -# return T{P1,<:Any,P3,P4} -# end -# function set_parameter( -# T::Type{<:AbstractUnallocatedArray{<:Any,P2,<:Any,P4}}, ::Position{1}, P1 -# ) where {P2,P4} -# return T{P1,P2,<:Any,P4} -# end -# function set_parameter( -# T::Type{<:AbstractUnallocatedArray{<:Any,<:Any,<:Any,P4}}, ::Position{1}, P1 -# ) where {P4} -# return T{P1,<:Any,<:Any,P4} -# end - -# ## Set parameter 2 -# function set_parameter(T::Type{<:AbstractUnallocatedArray{P1,<:Any,P3, P4}}, ::Position{2}, P2) where {P1,P3} -# return T{P1,P2,P3, P4} -# end - -# # Set parameter 3 -# set_parameter(T::Type{<:AbstractUnallocatedArray{P1,P2}}, ::Position{3}, P3) where {P1,P2} = T{P1,P2,P3} - -# # Set paramter 4 -# set_parameter(T::Type{<:AbstractUnallocatedArray}, ::Position{4}, P4) = T{<:Any,<:Any,<:Any,P4} -# set_parameter(T::Type{<:AbstractUnallocatedArray{P1}}, ::Position{4}, P4) where {P1} = -# T{P1,<:Any,<:Any,P4} -# set_parameter(T::Type{<:AbstractUnallocatedArray{P1,P2}}, ::Position{4}, P4) where {P1,P2} = -# T{P1,P2,<:Any,P4} -# set_parameter(T::Type{<:AbstractUnallocatedArray{P1,P2,P3}}, ::Position{4}, P4) where {P1,P2,P3} = -# T{P1,P2,P3,P4} +function SetParameters.set_parameter( + T::Type{<:UnallocatedFill{P1, P2, P3, <:Any}}, ::Position{4}, P4::Type{<:AbstractArray} +) where {P1,P2,P3} + return T{P4} +end +function SetParameters.set_parameter( + T::Type{<:UnallocatedZeros{P1, P2, P3, <:Any}}, ::Position{4}, P4::Type{<:AbstractArray} +) where {P1,P2,P3} + return T{P4} +end # ## default parameters -# default_parameter(::Type{<:AbstractUnallocatedArray}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray +SetParameters.default_parameter(::Type{<:UnallocatedFill}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray +SetParameters.default_parameter(::Type{<:UnallocatedZeros}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray -# nparameters(::Type{<:AbstractUnallocatedArray}) = Val(4) +nparameters(::Type{<:UnallocatedFill}) = Val(4) +nparameters(::Type{<:UnallocatedZeros}) = Val(4) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 23e421efaf..5f4873bd7a 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -3,13 +3,17 @@ struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: FillArrays.AbstractFill{ElT,N,Axes} f::FillArrays.Fill{ElT,N,Axes} - ## TODO use `set_parameters` as constructor to these types end -function set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) - return UnallocatedFill{eltype(f),ndims(f),typeof(axes(f)),alloc}(f) +## TODO use `set_parameters` as constructor to these types +function UnallocatedFill(f::Fill, alloc::Type{<:AbstractArray}) + return set_alloctype(set_axes(set_ndims(set_eltype(UnallocatedFill, eltype(f)), ndims(f)), typeof(axes(f))), alloc)(f) end +set_alloctype(T::Type{<:UnallocatedFill}, alloc::Type{<:AbstractArray}) = set_parameters(T, Position{4}(), alloc) + +set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) = UnallocatedFill(f, alloc) + Base.parent(F::UnallocatedFill) = F.f ## Here I can't overload ::AbstractFill because this overwrites Base.complex in @@ -17,6 +21,6 @@ Base.parent(F::UnallocatedFill) = F.f ## UnallocatedArrays.complex(::AbstractFill) which calls Base.complex on the parent function Base.complex(A::UnallocatedFill) return set_alloctype( - complex(parent(A)), set_parameter(alloctype(A), Position{1}(), complex(eltype(A))) + complex(parent(A)), set_parameters(alloctype(A), Position{1}(), complex(eltype(A))) ) end diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 576458afce..184d45d8e3 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -6,10 +6,14 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: ## TODO use `set_parameters` as constructor to these types end -function set_alloctype(z::Zeros, alloc::Type{<:AbstractArray}) - return UnallocatedZeros{eltype(z),ndims(z),typeof(axes(z)),alloc}(z) +function UnallocatedZeros(f::Zeros, alloc::Type{<:AbstractArray}) + return set_alloctype(set_axes(set_ndims(set_eltype(UnallocatedZeros, eltype(f)), ndims(f)), typeof(axes(f))), alloc)(f) end +set_alloctype(T::Type{<:UnallocatedZeros}, alloc::Type{<:AbstractArray}) = set_parameters(T, Position{4}(), alloc) + +set_alloctype(f::Zeros, alloc::Type{<:AbstractArray}) = UnallocatedZeros(f, alloc) + Base.parent(Z::UnallocatedZeros) = Z.z ## Here I can't overload ::AbstractFill because this overwrites Base.complex in @@ -17,6 +21,6 @@ Base.parent(Z::UnallocatedZeros) = Z.z ## UnallocatedArrays.complex(::AbstractFill) which calls Base.complex on the parent function Base.complex(A::UnallocatedZeros) return set_alloctype( - complex(parent(A)), set_parameter(alloctype(A), Position{1}(), complex(eltype(A))) + complex(parent(A)), set_parameters(alloctype(A), Position{1}(), complex(eltype(A))) ) end diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 10df98fe88..735ef27371 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -11,10 +11,10 @@ include(joinpath(pkgdir(NDTensors), "test", "NDTensorsTestUtils", "NDTensorsTest using .NDTensorsTestUtils: devices_list @testset "Testing UnallocatedArrays" for dev in devices_list(ARGS), - elt in (Float64, Float32, ComplexF64, ComplexF32) +elt in (Float64, Float32, ComplexF64, ComplexF32) z = Zeros{elt}((2, 3)) - Z = UnallocatedZeros{eltype(z),ndims(z),typeof(axes(z)),dev(Matrix{eltype(z)})}(z) + Z = UnallocatedZeros(z, dev(Matrix{eltype(z)})) @test Z isa AbstractFill @test size(Z) == (2, 3) @@ -43,6 +43,7 @@ using .NDTensorsTestUtils: devices_list # UnallocatedFill f = Fill{elt}(3.0, (2, 3, 4)) F = UnallocatedFill{elt,ndims(f),typeof(axes(f)),Array{elt,ndims(f)}}(f) + @test F isa AbstractFill @test size(F) == (2, 3, 4) @test length(F) == 24 @test sum(F) ≈ 3 * 24 From ba869d3c870e12c80e12e12cf2cbea8d3a90a375 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 12:45:36 -0500 Subject: [PATCH 056/156] Remove import from module --- NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index 8d9bad3811..7b47ae9836 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -3,8 +3,6 @@ using FillArrays: FillArrays, AbstractFill, Fill, Zeros, getindex_value using LinearAlgebra using NDTensors.SetParameters -include("import.jl") - include("abstractfill/abstractfill.jl") include("abstractfill/set_types.jl") From 864c1bec5ad382a2076e501fd7edc539a502dea3 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 12:48:44 -0500 Subject: [PATCH 057/156] Clean up --- NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl | 3 +-- .../src/lib/UnallocatedArrays/src/abstractfill/set_types.jl | 1 - NDTensors/src/lib/UnallocatedArrays/src/set_types.jl | 4 ++-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index 7b47ae9836..b948d81274 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,7 +1,6 @@ module UnallocatedArrays using FillArrays: FillArrays, AbstractFill, Fill, Zeros, getindex_value -using LinearAlgebra -using NDTensors.SetParameters +using NDTensors.SetParameters: SetParameters, Position, default_parameter, get_parameter, nparameters, set_parameter, set_parameters include("abstractfill/abstractfill.jl") include("abstractfill/set_types.jl") diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl index d07e2708f0..65c9408778 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl @@ -1,4 +1,3 @@ -using NDTensors.SetParameters: SetParameters ## TODO make unit tests for all of these functions ## TODO remove P4 # `SetParameters.jl` overloads. diff --git a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl index 4dd8af6774..e688db2098 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl @@ -28,5 +28,5 @@ end SetParameters.default_parameter(::Type{<:UnallocatedFill}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray SetParameters.default_parameter(::Type{<:UnallocatedZeros}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray -nparameters(::Type{<:UnallocatedFill}) = Val(4) -nparameters(::Type{<:UnallocatedZeros}) = Val(4) +SetParameters.nparameters(::Type{<:UnallocatedFill}) = Val(4) +SetParameters.nparameters(::Type{<:UnallocatedZeros}) = Val(4) From 7fe9457ca8fcec9c8cbb2fb46f1b714c6b9eef52 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 12:49:08 -0500 Subject: [PATCH 058/156] Add tests to NDTensors --- NDTensors/test/lib/Project.toml | 1 + NDTensors/test/lib/runtests.jl | 2 ++ 2 files changed, 3 insertions(+) diff --git a/NDTensors/test/lib/Project.toml b/NDTensors/test/lib/Project.toml index b86a01c764..41bb43fa99 100644 --- a/NDTensors/test/lib/Project.toml +++ b/NDTensors/test/lib/Project.toml @@ -4,6 +4,7 @@ BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527" +FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" TensorOperations = "6aa20fa7-93e2-5fca-9bc0-fbd0db3c71a2" diff --git a/NDTensors/test/lib/runtests.jl b/NDTensors/test/lib/runtests.jl index 976f0e601d..f0a5691caf 100644 --- a/NDTensors/test/lib/runtests.jl +++ b/NDTensors/test/lib/runtests.jl @@ -14,6 +14,8 @@ using Test: @testset "SparseArrayDOKs", "TagSets", "TensorAlgebra", + "UnallocatedArrays", + "UnspecifiedTypes", "Unwrap", ] using NDTensors: NDTensors From 01a828fda9443c464d09844482aed1c6e3430a2a Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 12:49:24 -0500 Subject: [PATCH 059/156] format --- .../src/UnallocatedArrays.jl | 9 +++++++- .../src/abstractfill/abstractfill.jl | 2 +- .../src/abstractfill/set_types.jl | 23 +++++++++++++++---- .../lib/UnallocatedArrays/src/set_types.jl | 12 ++++++---- .../UnallocatedArrays/src/unallocatedfill.jl | 11 +++++++-- .../UnallocatedArrays/src/unallocatedzeros.jl | 11 +++++++-- .../lib/UnallocatedArrays/test/runtests.jl | 2 +- 7 files changed, 54 insertions(+), 16 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index b948d81274..c7f6c05619 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,6 +1,13 @@ module UnallocatedArrays using FillArrays: FillArrays, AbstractFill, Fill, Zeros, getindex_value -using NDTensors.SetParameters: SetParameters, Position, default_parameter, get_parameter, nparameters, set_parameter, set_parameters +using NDTensors.SetParameters: + SetParameters, + Position, + default_parameter, + get_parameter, + nparameters, + set_parameter, + set_parameters include("abstractfill/abstractfill.jl") include("abstractfill/set_types.jl") diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl index 2bd05a2a3f..1d484dde76 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl @@ -11,7 +11,7 @@ allocate(A::AbstractFill) = alloctype(A)(parent(A)) set_eltype(T::Type{<:AbstractFill}, elt::Type) = set_parameters(T, Position{1}(), elt) set_ndims(T::Type{<:AbstractFill}, n) = set_parameters(T, Position{2}(), n) set_axes(T::Type{<:AbstractFill}, ax::Type) = set_parameters(T, Position{3}(), ax) - + ## With these functions defined I can print UnallocatedArrays ## compute things like sum and norm, compute the size and length @inline Base.axes(A::AbstractFill) = axes(parent(A)) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl index 65c9408778..8ff9e944de 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl @@ -3,7 +3,11 @@ # `SetParameters.jl` overloads. SetParameters.get_parameter(::Type{<:AbstractFill{P1}}, ::Position{1}) where {P1} = P1 SetParameters.get_parameter(::Type{<:AbstractFill{<:Any,P2}}, ::Position{2}) where {P2} = P2 -SetParameters.get_parameter(::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{3}) where {P3} = P3 +function SetParameters.get_parameter( + ::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{3} +) where {P3} + return P3 +end ## Setting paramaters # right now I am just defining the necessary ones for my implementation still working on full implementation @@ -11,16 +15,25 @@ SetParameters.get_parameter(::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{3 SetParameters.set_parameter(T::Type{<:AbstractFill}, ::Position{1}, P1) = T{P1} # Set parameter 2 -SetParameters.set_parameter(T::Type{<:AbstractFill{P1}}, ::Position{2}, P2) where {P1} = T{P2} +function SetParameters.set_parameter( + T::Type{<:AbstractFill{P1}}, ::Position{2}, P2 +) where {P1} + return T{P2} +end # Set parameter 3 -SetParameters.set_parameter(T::Type{<:AbstractFill{P1,P2}}, ::Position{3}, P3) where {P1,P2} = T{P3} - +function SetParameters.set_parameter( + T::Type{<:AbstractFill{P1,P2}}, ::Position{3}, P3 +) where {P1,P2} + return T{P3} +end ## TODO define a specify_parameters function ## To quickly specify P1, P2, and P3 ## default parameters -SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{1}) = UnspecifiedTypes.UnallocatedZeros +function SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{1}) + return UnspecifiedTypes.UnallocatedZeros +end SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{2}) = 0 SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{3}) = Tuple{} diff --git a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl index e688db2098..b2254bbe82 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl @@ -14,19 +14,23 @@ end # ## Setting paramaters function SetParameters.set_parameter( - T::Type{<:UnallocatedFill{P1, P2, P3, <:Any}}, ::Position{4}, P4::Type{<:AbstractArray} + T::Type{<:UnallocatedFill{P1,P2,P3,<:Any}}, ::Position{4}, P4::Type{<:AbstractArray} ) where {P1,P2,P3} return T{P4} end function SetParameters.set_parameter( - T::Type{<:UnallocatedZeros{P1, P2, P3, <:Any}}, ::Position{4}, P4::Type{<:AbstractArray} + T::Type{<:UnallocatedZeros{P1,P2,P3,<:Any}}, ::Position{4}, P4::Type{<:AbstractArray} ) where {P1,P2,P3} return T{P4} end # ## default parameters -SetParameters.default_parameter(::Type{<:UnallocatedFill}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray -SetParameters.default_parameter(::Type{<:UnallocatedZeros}, ::Position{4}) = UnspecifiedTypes.UnspecifiedArray +function SetParameters.default_parameter(::Type{<:UnallocatedFill}, ::Position{4}) + return UnspecifiedTypes.UnspecifiedArray +end +function SetParameters.default_parameter(::Type{<:UnallocatedZeros}, ::Position{4}) + return UnspecifiedTypes.UnspecifiedArray +end SetParameters.nparameters(::Type{<:UnallocatedFill}) = Val(4) SetParameters.nparameters(::Type{<:UnallocatedZeros}) = Val(4) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 5f4873bd7a..6dc8b45e14 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -7,10 +7,17 @@ end ## TODO use `set_parameters` as constructor to these types function UnallocatedFill(f::Fill, alloc::Type{<:AbstractArray}) - return set_alloctype(set_axes(set_ndims(set_eltype(UnallocatedFill, eltype(f)), ndims(f)), typeof(axes(f))), alloc)(f) + return set_alloctype( + set_axes(set_ndims(set_eltype(UnallocatedFill, eltype(f)), ndims(f)), typeof(axes(f))), + alloc, + )( + f + ) end -set_alloctype(T::Type{<:UnallocatedFill}, alloc::Type{<:AbstractArray}) = set_parameters(T, Position{4}(), alloc) +function set_alloctype(T::Type{<:UnallocatedFill}, alloc::Type{<:AbstractArray}) + return set_parameters(T, Position{4}(), alloc) +end set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) = UnallocatedFill(f, alloc) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 184d45d8e3..22fb8cf523 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -7,10 +7,17 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: end function UnallocatedZeros(f::Zeros, alloc::Type{<:AbstractArray}) - return set_alloctype(set_axes(set_ndims(set_eltype(UnallocatedZeros, eltype(f)), ndims(f)), typeof(axes(f))), alloc)(f) + return set_alloctype( + set_axes(set_ndims(set_eltype(UnallocatedZeros, eltype(f)), ndims(f)), typeof(axes(f))), + alloc, + )( + f + ) end -set_alloctype(T::Type{<:UnallocatedZeros}, alloc::Type{<:AbstractArray}) = set_parameters(T, Position{4}(), alloc) +function set_alloctype(T::Type{<:UnallocatedZeros}, alloc::Type{<:AbstractArray}) + return set_parameters(T, Position{4}(), alloc) +end set_alloctype(f::Zeros, alloc::Type{<:AbstractArray}) = UnallocatedZeros(f, alloc) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 735ef27371..558228c94c 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -11,7 +11,7 @@ include(joinpath(pkgdir(NDTensors), "test", "NDTensorsTestUtils", "NDTensorsTest using .NDTensorsTestUtils: devices_list @testset "Testing UnallocatedArrays" for dev in devices_list(ARGS), -elt in (Float64, Float32, ComplexF64, ComplexF32) + elt in (Float64, Float32, ComplexF64, ComplexF32) z = Zeros{elt}((2, 3)) Z = UnallocatedZeros(z, dev(Matrix{eltype(z)})) From af20c8fddb2459afb54b87b8952d12dab6b362de Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 12:59:49 -0500 Subject: [PATCH 060/156] additional updates --- .../src/lib/UnallocatedArrays/src/UnallocatedArrays.jl | 2 +- NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl | 5 ++--- NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 6 ++---- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index c7f6c05619..3ff93ecb8f 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,5 +1,5 @@ module UnallocatedArrays -using FillArrays: FillArrays, AbstractFill, Fill, Zeros, getindex_value +using FillArrays: FillArrays, AbstractFill, AbstractZeros, Fill, Zeros, getindex_value using NDTensors.SetParameters: SetParameters, Position, diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 6dc8b45e14..22deb6acc4 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -1,8 +1,7 @@ ## TODO All constructors not fully implemented but so far it matches the ## constructors found in `FillArrays`. Need to fix io -struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: - FillArrays.AbstractFill{ElT,N,Axes} - f::FillArrays.Fill{ElT,N,Axes} +struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: AbstractFill{ElT,N,Axes} + f::Fill{ElT,N,Axes} end ## TODO use `set_parameters` as constructor to these types diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 22fb8cf523..2e0076be6c 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -1,9 +1,7 @@ ## TODO Should Alloc also be of ElT and N or should there be ## More freedom there? -struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: - FillArrays.AbstractZeros{ElT,N,Axes} - z::FillArrays.Zeros{ElT,N,Axes} - ## TODO use `set_parameters` as constructor to these types +struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: AbstractZeros{ElT,N,Axes} + z::Zeros{ElT,N,Axes} end function UnallocatedZeros(f::Zeros, alloc::Type{<:AbstractArray}) From 3dc451407f86e2ebaddc5182c6015a74a8b905e8 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 13:00:38 -0500 Subject: [PATCH 061/156] Add FillArrays to NDTensors test --- NDTensors/test/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/NDTensors/test/Project.toml b/NDTensors/test/Project.toml index f47f1f7f21..f6ec19f498 100644 --- a/NDTensors/test/Project.toml +++ b/NDTensors/test/Project.toml @@ -7,6 +7,7 @@ Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527" ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5" +FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" Octavian = "6fd5a793-0b7e-452c-907f-f8bfe9c57db4" From a3dca2b3071aa37d1338f9e7b1ca8835089b4098 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 14:42:42 -0500 Subject: [PATCH 062/156] Remove type piracy code --- .../src/abstractfill/abstractfill.jl | 9 --------- .../src/lib/UnallocatedArrays/src/unallocatedfill.jl | 11 ++++++++--- .../src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 11 ++++++++--- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl index 1d484dde76..105fdbf1aa 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl @@ -12,15 +12,6 @@ set_eltype(T::Type{<:AbstractFill}, elt::Type) = set_parameters(T, Position{1}() set_ndims(T::Type{<:AbstractFill}, n) = set_parameters(T, Position{2}(), n) set_axes(T::Type{<:AbstractFill}, ax::Type) = set_parameters(T, Position{3}(), ax) -## With these functions defined I can print UnallocatedArrays -## compute things like sum and norm, compute the size and length -@inline Base.axes(A::AbstractFill) = axes(parent(A)) -Base.size(A::AbstractFill) = size(parent(A)) -function FillArrays.getindex_value(A::AbstractFill) - return getindex_value(parent(A)) -end -Base.copy(A::AbstractFill) = A - ## TODO get these working for UnallocatedX # mult_fill(a, b, val, ax) = Fill(val, ax) # mult_zeros(a, b, elt, ax) = Zeros{elt}(ax) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 22deb6acc4..16acc196d0 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -22,11 +22,16 @@ set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) = UnallocatedFill(f, alloc) Base.parent(F::UnallocatedFill) = F.f -## Here I can't overload ::AbstractFill because this overwrites Base.complex in -## Fill arrays which creates an infinite loop. Another option would be to write -## UnallocatedArrays.complex(::AbstractFill) which calls Base.complex on the parent +## These functions are the same for UnallocatedX function Base.complex(A::UnallocatedFill) return set_alloctype( complex(parent(A)), set_parameters(alloctype(A), Position{1}(), complex(eltype(A))) ) end + +@inline Base.axes(A::UnallocatedFill) = axes(parent(A)) +Base.size(A::UnallocatedFill) = size(parent(A)) +function FillArrays.getindex_value(A::UnallocatedFill) + return getindex_value(parent(A)) +end +Base.copy(A::UnallocatedFill) = A \ No newline at end of file diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 2e0076be6c..e0868182c4 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -21,11 +21,16 @@ set_alloctype(f::Zeros, alloc::Type{<:AbstractArray}) = UnallocatedZeros(f, allo Base.parent(Z::UnallocatedZeros) = Z.z -## Here I can't overload ::AbstractFill because this overwrites Base.complex in -## Fill arrays which creates an infinite loop. Another option would be to write -## UnallocatedArrays.complex(::AbstractFill) which calls Base.complex on the parent +## These functions are the same for UnallocatedX function Base.complex(A::UnallocatedZeros) return set_alloctype( complex(parent(A)), set_parameters(alloctype(A), Position{1}(), complex(eltype(A))) ) end + +@inline Base.axes(A::UnallocatedZeros) = axes(parent(A)) +Base.size(A::UnallocatedZeros) = size(parent(A)) +function FillArrays.getindex_value(A::UnallocatedZeros) + return getindex_value(parent(A)) +end +Base.copy(A::UnallocatedZeros) = A \ No newline at end of file From 3fcbe11b498f91c20d1120bd3b7b50e20a8a4265 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 14:56:30 -0500 Subject: [PATCH 063/156] Moved shared function to defaultunallocatedarray.jl --- .../src/UnallocatedArrays.jl | 1 + .../src/abstractfill/abstractfill.jl | 16 ------------ .../src/defaultunallocatedarray.jl | 26 +++++++++++++++++++ .../UnallocatedArrays/src/unallocatedfill.jl | 14 ---------- .../UnallocatedArrays/src/unallocatedzeros.jl | 14 ---------- 5 files changed, 27 insertions(+), 44 deletions(-) create mode 100644 NDTensors/src/lib/UnallocatedArrays/src/defaultunallocatedarray.jl diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index 3ff93ecb8f..b4c328caa2 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -14,6 +14,7 @@ include("abstractfill/set_types.jl") include("unallocatedfill.jl") include("unallocatedzeros.jl") +include("defaultunallocatedarray.jl") include("set_types.jl") export UnallocatedFill, UnallocatedZeros, alloctype, set_alloctype, allocate diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl index 105fdbf1aa..0648ec4f46 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl @@ -11,19 +11,3 @@ allocate(A::AbstractFill) = alloctype(A)(parent(A)) set_eltype(T::Type{<:AbstractFill}, elt::Type) = set_parameters(T, Position{1}(), elt) set_ndims(T::Type{<:AbstractFill}, n) = set_parameters(T, Position{2}(), n) set_axes(T::Type{<:AbstractFill}, ax::Type) = set_parameters(T, Position{3}(), ax) - -## TODO get these working for UnallocatedX -# mult_fill(a, b, val, ax) = Fill(val, ax) -# mult_zeros(a, b, elt, ax) = Zeros{elt}(ax) -# mult_ones(a, b, elt, ax) = Ones{elt}(ax) - -# broadcasted_fill(f, a, val, ax) = Fill(val, ax) -# broadcasted_fill(f, a, b, val, ax) = Fill(val, ax) -# broadcasted_zeros(f, a, elt, ax) = Zeros{elt}(ax) -# broadcasted_zeros(f, a, b, elt, ax) = Zeros{elt}(ax) -# broadcasted_ones(f, a, elt, ax) = Ones{elt}(ax) -# broadcasted_ones(f, a, b, elt, ax) = Ones{elt}(ax) - -# kron_fill(a, b, val, ax) = Fill(val, ax) -# kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) -# kron_ones(a, b, elt, ax) = Ones{elt}(ax) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/defaultunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/defaultunallocatedarray.jl new file mode 100644 index 0000000000..419c2d14b8 --- /dev/null +++ b/NDTensors/src/lib/UnallocatedArrays/src/defaultunallocatedarray.jl @@ -0,0 +1,26 @@ +@inline Base.axes(A::Union{<:UnallocatedFill, <:UnallocatedZeros}) = axes(parent(A)) +Base.size(A::Union{<:UnallocatedFill, <:UnallocatedZeros}) = size(parent(A)) +function FillArrays.getindex_value(A::Union{<:UnallocatedFill, <:UnallocatedZeros}) + return getindex_value(parent(A)) +end + +function Base.complex(A::Union{<:UnallocatedFill, <:UnallocatedZeros}) + return set_alloctype( + complex(parent(A)), set_parameters(alloctype(A), Position{1}(), complex(eltype(A))) + ) +end + +# mult_fill(a, b, val, ax) = Fill(val, ax) +# mult_zeros(a, b, elt, ax) = Zeros{elt}(ax) +# mult_ones(a, b, elt, ax) = Ones{elt}(ax) + +# broadcasted_fill(f, a, val, ax) = Fill(val, ax) +# broadcasted_fill(f, a, b, val, ax) = Fill(val, ax) +# broadcasted_zeros(f, a, elt, ax) = Zeros{elt}(ax) +# broadcasted_zeros(f, a, b, elt, ax) = Zeros{elt}(ax) +# broadcasted_ones(f, a, elt, ax) = Ones{elt}(ax) +# broadcasted_ones(f, a, b, elt, ax) = Ones{elt}(ax) + +# kron_fill(a, b, val, ax) = Fill(val, ax) +# kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) +# kron_ones(a, b, elt, ax) = Ones{elt}(ax) \ No newline at end of file diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 16acc196d0..0ff8699ebc 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -21,17 +21,3 @@ end set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) = UnallocatedFill(f, alloc) Base.parent(F::UnallocatedFill) = F.f - -## These functions are the same for UnallocatedX -function Base.complex(A::UnallocatedFill) - return set_alloctype( - complex(parent(A)), set_parameters(alloctype(A), Position{1}(), complex(eltype(A))) - ) -end - -@inline Base.axes(A::UnallocatedFill) = axes(parent(A)) -Base.size(A::UnallocatedFill) = size(parent(A)) -function FillArrays.getindex_value(A::UnallocatedFill) - return getindex_value(parent(A)) -end -Base.copy(A::UnallocatedFill) = A \ No newline at end of file diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index e0868182c4..8b4514dd72 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -20,17 +20,3 @@ end set_alloctype(f::Zeros, alloc::Type{<:AbstractArray}) = UnallocatedZeros(f, alloc) Base.parent(Z::UnallocatedZeros) = Z.z - -## These functions are the same for UnallocatedX -function Base.complex(A::UnallocatedZeros) - return set_alloctype( - complex(parent(A)), set_parameters(alloctype(A), Position{1}(), complex(eltype(A))) - ) -end - -@inline Base.axes(A::UnallocatedZeros) = axes(parent(A)) -Base.size(A::UnallocatedZeros) = size(parent(A)) -function FillArrays.getindex_value(A::UnallocatedZeros) - return getindex_value(parent(A)) -end -Base.copy(A::UnallocatedZeros) = A \ No newline at end of file From f92e4d9c60c69fcf1c2e9490b2e1f130599f4e75 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 4 Dec 2023 14:58:23 -0500 Subject: [PATCH 064/156] format --- .../UnallocatedArrays/src/defaultunallocatedarray.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/defaultunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/defaultunallocatedarray.jl index 419c2d14b8..d9d9347b9c 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/defaultunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/defaultunallocatedarray.jl @@ -1,10 +1,10 @@ -@inline Base.axes(A::Union{<:UnallocatedFill, <:UnallocatedZeros}) = axes(parent(A)) -Base.size(A::Union{<:UnallocatedFill, <:UnallocatedZeros}) = size(parent(A)) -function FillArrays.getindex_value(A::Union{<:UnallocatedFill, <:UnallocatedZeros}) +@inline Base.axes(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = axes(parent(A)) +Base.size(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = size(parent(A)) +function FillArrays.getindex_value(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) return getindex_value(parent(A)) end -function Base.complex(A::Union{<:UnallocatedFill, <:UnallocatedZeros}) +function Base.complex(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) return set_alloctype( complex(parent(A)), set_parameters(alloctype(A), Position{1}(), complex(eltype(A))) ) @@ -23,4 +23,4 @@ end # kron_fill(a, b, val, ax) = Fill(val, ax) # kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) -# kron_ones(a, b, elt, ax) = Ones{elt}(ax) \ No newline at end of file +# kron_ones(a, b, elt, ax) = Ones{elt}(ax) From 71031a3bb839a84c49e9c6776a66f9048e7d88f9 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 4 Dec 2023 15:10:02 -0500 Subject: [PATCH 065/156] force itensors to update packages, using old FillArrays --- .github/workflows/test_itensors_base_ubuntu.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test_itensors_base_ubuntu.yml b/.github/workflows/test_itensors_base_ubuntu.yml index 1314173bba..5dc91f94a3 100644 --- a/.github/workflows/test_itensors_base_ubuntu.yml +++ b/.github/workflows/test_itensors_base_ubuntu.yml @@ -37,6 +37,7 @@ jobs: shell: julia --project=monorepo {0} run: | using Pkg; + Pkg.update(); Pkg.develop(path="."); Pkg.develop(path="./NDTensors"); - name: Run the tests From 9ea0ce54340cc2cbc5fa06ef2d2faaaf9c55082a Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 4 Dec 2023 15:15:06 -0500 Subject: [PATCH 066/156] change order --- .github/workflows/test_itensors_base_ubuntu.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_itensors_base_ubuntu.yml b/.github/workflows/test_itensors_base_ubuntu.yml index 5dc91f94a3..ecb417d87e 100644 --- a/.github/workflows/test_itensors_base_ubuntu.yml +++ b/.github/workflows/test_itensors_base_ubuntu.yml @@ -37,9 +37,9 @@ jobs: shell: julia --project=monorepo {0} run: | using Pkg; - Pkg.update(); Pkg.develop(path="."); Pkg.develop(path="./NDTensors"); + Pkg.update(); - name: Run the tests shell: julia --project=monorepo {0} run: | From 43933b98d97e687795a37d3c65cc5a52aa222cd5 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 4 Dec 2023 15:20:01 -0500 Subject: [PATCH 067/156] Rename --- .../{defaultunallocatedarray.jl => abstractunallocatedarray.jl} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename NDTensors/src/lib/UnallocatedArrays/src/{defaultunallocatedarray.jl => abstractunallocatedarray.jl} (100%) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/defaultunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl similarity index 100% rename from NDTensors/src/lib/UnallocatedArrays/src/defaultunallocatedarray.jl rename to NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl From 9b963fdc573e9176a24dfbc14890ef84e348aa41 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 4 Dec 2023 15:22:51 -0500 Subject: [PATCH 068/156] rename --- NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index b4c328caa2..2b292840cb 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -14,7 +14,7 @@ include("abstractfill/set_types.jl") include("unallocatedfill.jl") include("unallocatedzeros.jl") -include("defaultunallocatedarray.jl") +include("abstractunallocatedarray.jl") include("set_types.jl") export UnallocatedFill, UnallocatedZeros, alloctype, set_alloctype, allocate From c4c4a74448b17e81c321769a6faecd7ca508b3bb Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Wed, 6 Dec 2023 14:01:52 -0500 Subject: [PATCH 069/156] Merge branch 'main' into kmp5/feature/FieldTypes --- NDTensors/Project.toml | 1 + .../src/BlockSparseArrays.jl | 5 ++ .../abstractblocksparsearray.jl | 13 +++- .../abstractblocksparsematrix.jl | 1 + .../abstractblocksparsevector.jl | 1 + .../abstractblocksparsearray/arraylayouts.jl | 28 ++++++++ .../abstractblocksparsearray/linearalgebra.jl | 12 ++++ .../src/blocksparsearray/blocksparsearray.jl | 3 + .../linearalgebra.jl | 12 ++++ .../lib/BlockSparseArrays/test/runtests.jl | 15 +++- .../lib/DiagonalArrays/src/DiagonalArrays.jl | 2 + .../src/abstractdiagonalarray/arraylayouts.jl | 7 ++ .../src/diagonalarray/arraylayouts.jl | 11 +++ .../src/lib/DiagonalArrays/test/runtests.jl | 55 ++++++++++++--- .../SparseArrayDOKs/src/SparseArrayDOKs.jl | 3 + .../lib/SparseArrayDOKs/src/arraylayouts.jl | 14 ++++ .../SparseArrayDOKs/src/sparsematrixdok.jl | 1 + .../SparseArrayDOKs/src/sparsevectordok.jl | 1 + .../src/lib/SparseArrayDOKs/test/runtests.jl | 24 ++++++- .../src/SparseArrayInterface.jl | 3 + .../SparseArrayInterfaceLinearAlgebraExt.jl | 12 ++++ .../abstractsparsearray.jl | 4 +- .../abstractsparsematrix.jl | 1 + .../abstractsparsevector.jl | 1 + .../src/abstractsparsearray/arraylayouts.jl | 9 +++ .../src/abstractsparsearray/map.jl | 8 +++ .../SparseArrayInterfaceLinearAlgebraExt.jl | 46 ++++++++++++- .../SparseArrayInterface/test/Project.toml | 1 + .../AbstractSparseArrays.jl | 11 +++ .../SparseArrays.jl | 3 + .../test/test_abstractsparsearray.jl | 68 ++++++++++++++++++- .../test/test_diagonalarray.jl | 7 ++ .../src/lib/TensorAlgebra/test/Project.toml | 1 + NDTensors/src/lib/Unwrap/README.md | 1 + Project.toml | 1 + 35 files changed, 371 insertions(+), 15 deletions(-) create mode 100644 NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/abstractblocksparsematrix.jl create mode 100644 NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/abstractblocksparsevector.jl create mode 100644 NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/arraylayouts.jl create mode 100644 NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/linearalgebra.jl create mode 100644 NDTensors/src/lib/BlockSparseArrays/src/blocksparsearrayinterface/linearalgebra.jl create mode 100644 NDTensors/src/lib/DiagonalArrays/src/abstractdiagonalarray/arraylayouts.jl create mode 100644 NDTensors/src/lib/DiagonalArrays/src/diagonalarray/arraylayouts.jl create mode 100644 NDTensors/src/lib/SparseArrayDOKs/src/arraylayouts.jl create mode 100644 NDTensors/src/lib/SparseArrayDOKs/src/sparsematrixdok.jl create mode 100644 NDTensors/src/lib/SparseArrayDOKs/src/sparsevectordok.jl create mode 100644 NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/abstractsparsematrix.jl create mode 100644 NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/abstractsparsevector.jl create mode 100644 NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/arraylayouts.jl diff --git a/NDTensors/Project.toml b/NDTensors/Project.toml index 2711c228fd..7806328dff 100644 --- a/NDTensors/Project.toml +++ b/NDTensors/Project.toml @@ -5,6 +5,7 @@ version = "0.2.22" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" +ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" diff --git a/NDTensors/src/lib/BlockSparseArrays/src/BlockSparseArrays.jl b/NDTensors/src/lib/BlockSparseArrays/src/BlockSparseArrays.jl index b0a15209eb..2be0e157a3 100644 --- a/NDTensors/src/lib/BlockSparseArrays/src/BlockSparseArrays.jl +++ b/NDTensors/src/lib/BlockSparseArrays/src/BlockSparseArrays.jl @@ -1,7 +1,12 @@ module BlockSparseArrays include("blocksparsearrayinterface/blocksparsearrayinterface.jl") +include("blocksparsearrayinterface/linearalgebra.jl") include("blocksparsearrayinterface/blockzero.jl") include("abstractblocksparsearray/abstractblocksparsearray.jl") +include("abstractblocksparsearray/abstractblocksparsematrix.jl") +include("abstractblocksparsearray/abstractblocksparsevector.jl") +include("abstractblocksparsearray/arraylayouts.jl") +include("abstractblocksparsearray/linearalgebra.jl") include("blocksparsearray/defaults.jl") include("blocksparsearray/blocksparsearray.jl") include("BlockArraysExtensions/BlockArraysExtensions.jl") diff --git a/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/abstractblocksparsearray.jl b/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/abstractblocksparsearray.jl index f63531179c..adc73343fd 100644 --- a/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/abstractblocksparsearray.jl +++ b/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/abstractblocksparsearray.jl @@ -1,4 +1,4 @@ -using BlockArrays: BlockArrays, AbstractBlockArray, Block, BlockIndex +using BlockArrays: BlockArrays, AbstractBlockArray, Block, BlockIndex, BlockedUnitRange # TODO: Delete this. This function was replaced # by `nstored` but is still used in `NDTensors`. @@ -14,6 +14,9 @@ BlockArrays.blocks(::AbstractBlockSparseArray) = error("Not implemented") blocktype(a::AbstractBlockSparseArray) = eltype(blocks(a)) +blockstype(::Type{<:AbstractBlockSparseArray}) = error("Not implemented") +blocktype(arraytype::Type{<:AbstractBlockSparseArray}) = eltype(blockstype(arraytype)) + # Base `AbstractArray` interface function Base.getindex(a::AbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N}) where {N} return blocksparse_getindex(a, I...) @@ -44,3 +47,11 @@ function BlockArrays.viewblock( ) where {N} return blocksparse_viewblock(a, I) end + +# Needed by `BlockArrays` matrix multiplication interface +function Base.similar( + arraytype::Type{<:AbstractBlockSparseArray{T}}, axes::Tuple{Vararg{BlockedUnitRange}} +) where {T} + # TODO: Make generic for GPU! + return BlockSparseArray{T}(undef, axes) +end diff --git a/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/abstractblocksparsematrix.jl b/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/abstractblocksparsematrix.jl new file mode 100644 index 0000000000..0c2c578781 --- /dev/null +++ b/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/abstractblocksparsematrix.jl @@ -0,0 +1 @@ +const AbstractBlockSparseMatrix{T} = AbstractBlockSparseArray{T,2} diff --git a/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/abstractblocksparsevector.jl b/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/abstractblocksparsevector.jl new file mode 100644 index 0000000000..ae1441c5a8 --- /dev/null +++ b/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/abstractblocksparsevector.jl @@ -0,0 +1 @@ +const AbstractBlockSparseVector{T} = AbstractBlockSparseArray{T,1} diff --git a/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/arraylayouts.jl b/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/arraylayouts.jl new file mode 100644 index 0000000000..e0a420404c --- /dev/null +++ b/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/arraylayouts.jl @@ -0,0 +1,28 @@ +using ArrayLayouts: ArrayLayouts, MemoryLayout, MatMulMatAdd, MulAdd +using BlockArrays: BlockLayout +using ..SparseArrayInterface: SparseLayout +using LinearAlgebra: mul! + +function ArrayLayouts.MemoryLayout(arraytype::Type{<:AbstractBlockSparseArray}) + outer_layout = typeof(MemoryLayout(blockstype(arraytype))) + inner_layout = typeof(MemoryLayout(blocktype(arraytype))) + return BlockLayout{outer_layout,inner_layout}() +end + +function Base.similar( + ::MulAdd{<:BlockLayout{<:SparseLayout},<:BlockLayout{<:SparseLayout}}, elt::Type, axes +) + return similar(BlockSparseArray{elt}, axes) +end + +function ArrayLayouts.materialize!( + m::MatMulMatAdd{ + <:BlockLayout{<:SparseLayout}, + <:BlockLayout{<:SparseLayout}, + <:BlockLayout{<:SparseLayout}, + }, +) + α, a1, a2, β, a_dest = m.α, m.A, m.B, m.β, m.C + mul!(a_dest, a1, a2, α, β) + return a_dest +end diff --git a/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/linearalgebra.jl b/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/linearalgebra.jl new file mode 100644 index 0000000000..47914daaf8 --- /dev/null +++ b/NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/linearalgebra.jl @@ -0,0 +1,12 @@ +using LinearAlgebra: LinearAlgebra, mul! + +function LinearAlgebra.mul!( + a_dest::AbstractMatrix, + a1::AbstractBlockSparseMatrix, + a2::AbstractBlockSparseMatrix, + α::Number=true, + β::Number=false, +) + mul!(blocks(a_dest), blocks(a1), blocks(a2), α, β) + return a_dest +end diff --git a/NDTensors/src/lib/BlockSparseArrays/src/blocksparsearray/blocksparsearray.jl b/NDTensors/src/lib/BlockSparseArrays/src/blocksparsearray/blocksparsearray.jl index d8ba16484d..e3e9f6d3a7 100644 --- a/NDTensors/src/lib/BlockSparseArrays/src/blocksparsearray/blocksparsearray.jl +++ b/NDTensors/src/lib/BlockSparseArrays/src/blocksparsearray/blocksparsearray.jl @@ -99,3 +99,6 @@ Base.axes(a::BlockSparseArray) = a.axes # BlockArrays `AbstractBlockArray` interface BlockArrays.blocks(a::BlockSparseArray) = a.blocks + +# TODO: Use `SetParameters`. +blockstype(::Type{<:BlockSparseArray{<:Any,<:Any,<:Any,B}}) where {B} = B diff --git a/NDTensors/src/lib/BlockSparseArrays/src/blocksparsearrayinterface/linearalgebra.jl b/NDTensors/src/lib/BlockSparseArrays/src/blocksparsearrayinterface/linearalgebra.jl new file mode 100644 index 0000000000..ac7f566a93 --- /dev/null +++ b/NDTensors/src/lib/BlockSparseArrays/src/blocksparsearrayinterface/linearalgebra.jl @@ -0,0 +1,12 @@ +using LinearAlgebra: mul! + +function blocksparse_mul!( + a_dest::AbstractMatrix, + a1::AbstractMatrix, + a2::AbstractMatrix, + α::Number=true, + β::Number=false, +) + mul!(blocks(a_dest), blocks(a1), blocks(a2), α, β) + return a_dest +end diff --git a/NDTensors/src/lib/BlockSparseArrays/test/runtests.jl b/NDTensors/src/lib/BlockSparseArrays/test/runtests.jl index 9cde778ee9..1586c42e47 100644 --- a/NDTensors/src/lib/BlockSparseArrays/test/runtests.jl +++ b/NDTensors/src/lib/BlockSparseArrays/test/runtests.jl @@ -1,7 +1,8 @@ @eval module $(gensym()) -using Test: @test, @testset +using BlockArrays: Block, BlockedUnitRange, blockedrange, blocklength, blocksize +using LinearAlgebra: mul! using NDTensors.BlockSparseArrays: BlockSparseArray, block_nstored -using BlockArrays: BlockedUnitRange, blockedrange, blocklength, blocksize +using Test: @test, @testset include("TestBlockSparseArraysUtils.jl") @testset "BlockSparseArrays (eltype=$elt)" for elt in (Float32, Float64, ComplexF32, ComplexF64) @@ -36,5 +37,15 @@ include("TestBlockSparseArraysUtils.jl") end end end + @testset "LinearAlgebra" begin + a1 = BlockSparseArray{elt}([2, 3], [2, 3]) + a1[Block(1, 1)] = randn(elt, size(@view(a1[Block(1, 1)]))) + a2 = BlockSparseArray{elt}([2, 3], [2, 3]) + a2[Block(1, 1)] = randn(elt, size(@view(a1[Block(1, 1)]))) + a_dest = a1 * a2 + @test Array(a_dest) ≈ Array(a1) * Array(a2) + @test a_dest isa BlockSparseArray{elt} + @test block_nstored(a_dest) == 1 + end end end diff --git a/NDTensors/src/lib/DiagonalArrays/src/DiagonalArrays.jl b/NDTensors/src/lib/DiagonalArrays/src/DiagonalArrays.jl index 3b8e5ff9a4..94566ebd88 100644 --- a/NDTensors/src/lib/DiagonalArrays/src/DiagonalArrays.jl +++ b/NDTensors/src/lib/DiagonalArrays/src/DiagonalArrays.jl @@ -6,7 +6,9 @@ include("diaginterface/diagindices.jl") include("abstractdiagonalarray/abstractdiagonalarray.jl") include("abstractdiagonalarray/sparsearrayinterface.jl") include("abstractdiagonalarray/diagonalarraydiaginterface.jl") +include("abstractdiagonalarray/arraylayouts.jl") include("diagonalarray/diagonalarray.jl") include("diagonalarray/diagonalmatrix.jl") include("diagonalarray/diagonalvector.jl") +include("diagonalarray/arraylayouts.jl") end diff --git a/NDTensors/src/lib/DiagonalArrays/src/abstractdiagonalarray/arraylayouts.jl b/NDTensors/src/lib/DiagonalArrays/src/abstractdiagonalarray/arraylayouts.jl new file mode 100644 index 0000000000..bae6f80e8f --- /dev/null +++ b/NDTensors/src/lib/DiagonalArrays/src/abstractdiagonalarray/arraylayouts.jl @@ -0,0 +1,7 @@ +using ArrayLayouts: ArrayLayouts +using ..SparseArrayInterface: AbstractSparseLayout + +abstract type AbstractDiagonalLayout <: AbstractSparseLayout end +struct DiagonalLayout <: AbstractDiagonalLayout end + +ArrayLayouts.MemoryLayout(::Type{<:AbstractDiagonalArray}) = DiagonalLayout() diff --git a/NDTensors/src/lib/DiagonalArrays/src/diagonalarray/arraylayouts.jl b/NDTensors/src/lib/DiagonalArrays/src/diagonalarray/arraylayouts.jl new file mode 100644 index 0000000000..7f365db0f2 --- /dev/null +++ b/NDTensors/src/lib/DiagonalArrays/src/diagonalarray/arraylayouts.jl @@ -0,0 +1,11 @@ +using ArrayLayouts: MulAdd + +# Default sparse array type for `AbstractDiagonalLayout`. +default_diagonalarraytype(elt::Type) = DiagonalArray{elt} + +# TODO: Preserve GPU memory! Implement `CuSparseArrayLayout`, `MtlSparseLayout`? +function Base.similar( + ::MulAdd{<:AbstractDiagonalLayout,<:AbstractDiagonalLayout}, elt::Type, axes +) + return similar(default_diagonalarraytype(elt), axes) +end diff --git a/NDTensors/src/lib/DiagonalArrays/test/runtests.jl b/NDTensors/src/lib/DiagonalArrays/test/runtests.jl index 1baef0ec29..cd605cf72b 100644 --- a/NDTensors/src/lib/DiagonalArrays/test/runtests.jl +++ b/NDTensors/src/lib/DiagonalArrays/test/runtests.jl @@ -1,6 +1,8 @@ @eval module $(gensym()) -using Test: @test, @testset -using NDTensors.DiagonalArrays: DiagonalArrays +using Test: @test, @testset, @test_broken +using NDTensors.DiagonalArrays: DiagonalArrays, DiagonalArray, DiagonalMatrix, diaglength +using NDTensors.SparseArrayDOKs: SparseArrayDOK +using NDTensors.SparseArrayInterface: nstored @testset "Test NDTensors.DiagonalArrays" begin @testset "README" begin @test include( @@ -9,12 +11,49 @@ using NDTensors.DiagonalArrays: DiagonalArrays ), ) isa Any end - @testset "Basics" begin - using NDTensors.DiagonalArrays: diaglength - a = fill(1.0, 2, 3) - @test diaglength(a) == 2 - a = fill(1.0) - @test diaglength(a) == 1 + @testset "DiagonalArray (eltype=$elt)" for elt in + (Float32, Float64, ComplexF32, ComplexF64) + @testset "Basics" begin + a = fill(one(elt), 2, 3) + @test diaglength(a) == 2 + a = fill(one(elt)) + @test diaglength(a) == 1 + end + @testset "Matrix multiplication" begin + a1 = DiagonalArray{elt}(undef, (2, 3)) + a1[1, 1] = 11 + a1[2, 2] = 22 + a2 = DiagonalArray{elt}(undef, (3, 4)) + a2[1, 1] = 11 + a2[2, 2] = 22 + a2[3, 3] = 33 + a_dest = a1 * a2 + # TODO: Use `densearray` to make generic to GPU. + @test Array(a_dest) ≈ Array(a1) * Array(a2) + # TODO: Make this work with `ArrayLayouts`. + @test nstored(a_dest) == 2 + @test a_dest isa DiagonalMatrix{elt} + + # TODO: Make generic to GPU, use `allocate_randn`? + a2 = randn(elt, (3, 4)) + a_dest = a1 * a2 + # TODO: Use `densearray` to make generic to GPU. + @test Array(a_dest) ≈ Array(a1) * Array(a2) + @test nstored(a_dest) == 8 + @test a_dest isa Matrix{elt} + + a2 = SparseArrayDOK{elt}(3, 4) + a2[1, 1] = 11 + a2[2, 2] = 22 + a2[3, 3] = 33 + a_dest = a1 * a2 + # TODO: Use `densearray` to make generic to GPU. + @test Array(a_dest) ≈ Array(a1) * Array(a2) + # TODO: Define `SparseMatrixDOK`. + # TODO: Make this work with `ArrayLayouts`. + @test nstored(a_dest) == 2 + @test a_dest isa SparseArrayDOK{elt,2} + end end end end diff --git a/NDTensors/src/lib/SparseArrayDOKs/src/SparseArrayDOKs.jl b/NDTensors/src/lib/SparseArrayDOKs/src/SparseArrayDOKs.jl index 2157c59213..24b10cbc0c 100644 --- a/NDTensors/src/lib/SparseArrayDOKs/src/SparseArrayDOKs.jl +++ b/NDTensors/src/lib/SparseArrayDOKs/src/SparseArrayDOKs.jl @@ -1,4 +1,7 @@ module SparseArrayDOKs include("defaults.jl") include("sparsearraydok.jl") +include("sparsematrixdok.jl") +include("sparsevectordok.jl") +include("arraylayouts.jl") end diff --git a/NDTensors/src/lib/SparseArrayDOKs/src/arraylayouts.jl b/NDTensors/src/lib/SparseArrayDOKs/src/arraylayouts.jl new file mode 100644 index 0000000000..1e39fecc50 --- /dev/null +++ b/NDTensors/src/lib/SparseArrayDOKs/src/arraylayouts.jl @@ -0,0 +1,14 @@ +using ArrayLayouts: ArrayLayouts, MemoryLayout, MulAdd +using ..SparseArrayInterface: AbstractSparseLayout, SparseLayout + +ArrayLayouts.MemoryLayout(::Type{<:SparseArrayDOK}) = SparseLayout() + +# Default sparse array type for `AbstractSparseLayout`. +default_sparsearraytype(elt::Type) = SparseArrayDOK{elt} + +# TODO: Preserve GPU memory! Implement `CuSparseArrayLayout`, `MtlSparseLayout`? +function Base.similar( + ::MulAdd{<:AbstractSparseLayout,<:AbstractSparseLayout}, elt::Type, axes +) + return similar(default_sparsearraytype(elt), axes) +end diff --git a/NDTensors/src/lib/SparseArrayDOKs/src/sparsematrixdok.jl b/NDTensors/src/lib/SparseArrayDOKs/src/sparsematrixdok.jl new file mode 100644 index 0000000000..f568760c19 --- /dev/null +++ b/NDTensors/src/lib/SparseArrayDOKs/src/sparsematrixdok.jl @@ -0,0 +1 @@ +const SparseMatrixDOK{T} = SparseArrayDOK{T,2} diff --git a/NDTensors/src/lib/SparseArrayDOKs/src/sparsevectordok.jl b/NDTensors/src/lib/SparseArrayDOKs/src/sparsevectordok.jl new file mode 100644 index 0000000000..7cc7df00d6 --- /dev/null +++ b/NDTensors/src/lib/SparseArrayDOKs/src/sparsevectordok.jl @@ -0,0 +1 @@ +const SparseVectorDOK{T} = SparseArrayDOK{T,1} diff --git a/NDTensors/src/lib/SparseArrayDOKs/test/runtests.jl b/NDTensors/src/lib/SparseArrayDOKs/test/runtests.jl index 0ff841d50c..54cd3e1540 100644 --- a/NDTensors/src/lib/SparseArrayDOKs/test/runtests.jl +++ b/NDTensors/src/lib/SparseArrayDOKs/test/runtests.jl @@ -6,8 +6,8 @@ # Slicing using Test: @test, @testset, @test_broken +using NDTensors.SparseArrayDOKs: SparseArrayDOK, SparseMatrixDOK using NDTensors.SparseArrayInterface: storage_indices, nstored -using NDTensors.SparseArrayDOKs: SparseArrayDOK using SparseArrays: SparseMatrixCSC, nnz @testset "SparseArrayDOK (eltype=$elt)" for elt in (Float32, ComplexF32, Float64, ComplexF64) @@ -57,6 +57,28 @@ using SparseArrays: SparseMatrixCSC, nnz b = reshape(a, 2, 4) @test b[1, 4] == 122 end + @testset "Matrix multiplication" begin + a1 = SparseArrayDOK{elt}(2, 3) + a1[1, 2] = 12 + a1[2, 1] = 21 + a2 = SparseArrayDOK{elt}(3, 4) + a2[1, 1] = 11 + a2[2, 2] = 22 + a2[3, 3] = 33 + a_dest = a1 * a2 + # TODO: Use `densearray` to make generic to GPU. + @test Array(a_dest) ≈ Array(a1) * Array(a2) + # TODO: Make this work with `ArrayLayouts`. + @test nstored(a_dest) == 2 + @test a_dest isa SparseMatrixDOK{elt} + + a2 = randn(elt, (3, 4)) + a_dest = a1 * a2 + # TODO: Use `densearray` to make generic to GPU. + @test Array(a_dest) ≈ Array(a1) * Array(a2) + @test nstored(a_dest) == 8 + @test a_dest isa Matrix{elt} + end @testset "SparseMatrixCSC" begin a = SparseArrayDOK{elt}(2, 2) a[1, 2] = 12 diff --git a/NDTensors/src/lib/SparseArrayInterface/src/SparseArrayInterface.jl b/NDTensors/src/lib/SparseArrayInterface/src/SparseArrayInterface.jl index b2c4b6cf4f..c40db936ba 100644 --- a/NDTensors/src/lib/SparseArrayInterface/src/SparseArrayInterface.jl +++ b/NDTensors/src/lib/SparseArrayInterface/src/SparseArrayInterface.jl @@ -20,5 +20,8 @@ include("abstractsparsearray/map.jl") include("abstractsparsearray/baseinterface.jl") include("abstractsparsearray/convert.jl") include("abstractsparsearray/SparseArrayInterfaceSparseArraysExt.jl") +include("abstractsparsearray/abstractsparsematrix.jl") include("abstractsparsearray/SparseArrayInterfaceLinearAlgebraExt.jl") +include("abstractsparsearray/abstractsparsevector.jl") +include("abstractsparsearray/arraylayouts.jl") end diff --git a/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/SparseArrayInterfaceLinearAlgebraExt.jl b/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/SparseArrayInterfaceLinearAlgebraExt.jl index 6ba11d5a9a..b90dfb5f84 100644 --- a/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/SparseArrayInterfaceLinearAlgebraExt.jl +++ b/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/SparseArrayInterfaceLinearAlgebraExt.jl @@ -1,3 +1,15 @@ using LinearAlgebra: LinearAlgebra LinearAlgebra.norm(a::AbstractSparseArray, p::Real=2) = sparse_norm(a, p) + +# a1 * a2 * α + a_dest * β +function LinearAlgebra.mul!( + a_dest::AbstractMatrix, + a1::AbstractSparseMatrix, + a2::AbstractSparseMatrix, + α::Number=true, + β::Number=false, +) + sparse_mul!(a_dest, a1, a2, α, β) + return a_dest +end diff --git a/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/abstractsparsearray.jl b/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/abstractsparsearray.jl index 5a6ae67596..8c73f7e035 100644 --- a/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/abstractsparsearray.jl +++ b/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/abstractsparsearray.jl @@ -1 +1,3 @@ -abstract type AbstractSparseArray{T,N} <: AbstractArray{T,N} end +using ArrayLayouts: LayoutArray + +abstract type AbstractSparseArray{T,N} <: LayoutArray{T,N} end diff --git a/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/abstractsparsematrix.jl b/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/abstractsparsematrix.jl new file mode 100644 index 0000000000..a545c562e6 --- /dev/null +++ b/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/abstractsparsematrix.jl @@ -0,0 +1 @@ +const AbstractSparseMatrix{T} = AbstractSparseArray{T,2} diff --git a/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/abstractsparsevector.jl b/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/abstractsparsevector.jl new file mode 100644 index 0000000000..033df09d63 --- /dev/null +++ b/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/abstractsparsevector.jl @@ -0,0 +1 @@ +const AbstractSparseVector{T} = AbstractSparseArray{T,1} diff --git a/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/arraylayouts.jl b/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/arraylayouts.jl new file mode 100644 index 0000000000..c73010dd32 --- /dev/null +++ b/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/arraylayouts.jl @@ -0,0 +1,9 @@ +using ArrayLayouts: ArrayLayouts, MemoryLayout, MulAdd + +abstract type AbstractSparseLayout <: MemoryLayout end + +struct SparseLayout <: AbstractSparseLayout end + +function ArrayLayouts.MemoryLayout(arraytype::Type{<:AbstractSparseArray}) + return SparseLayout() +end diff --git a/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/map.jl b/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/map.jl index 86eecc5dd0..9546786648 100644 --- a/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/map.jl +++ b/NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/map.jl @@ -1,3 +1,5 @@ +using ArrayLayouts: LayoutArray + # Map function Base.map!(f, dest::AbstractArray, src::AbstractSparseArray) SparseArrayInterface.sparse_map!(f, dest, src) @@ -14,6 +16,12 @@ function Base.copyto!(dest::AbstractArray, src::AbstractSparseArray) return dest end +# Fix ambiguity error +function Base.copyto!(dest::LayoutArray, src::AbstractSparseArray) + SparseArrayInterface.sparse_copyto!(dest, src) + return dest +end + function Base.permutedims!(dest::AbstractArray, src::AbstractSparseArray, perm) SparseArrayInterface.sparse_permutedims!(dest, src, perm) return dest diff --git a/NDTensors/src/lib/SparseArrayInterface/src/sparsearrayinterface/SparseArrayInterfaceLinearAlgebraExt.jl b/NDTensors/src/lib/SparseArrayInterface/src/sparsearrayinterface/SparseArrayInterfaceLinearAlgebraExt.jl index db9bda209d..7a444297f7 100644 --- a/NDTensors/src/lib/SparseArrayInterface/src/sparsearrayinterface/SparseArrayInterfaceLinearAlgebraExt.jl +++ b/NDTensors/src/lib/SparseArrayInterface/src/sparsearrayinterface/SparseArrayInterfaceLinearAlgebraExt.jl @@ -1,3 +1,47 @@ -using LinearAlgebra: norm +using LinearAlgebra: mul!, norm sparse_norm(a::AbstractArray, p::Real=2) = norm(sparse_storage(a)) + +function mul_indices(I1::CartesianIndex{2}, I2::CartesianIndex{2}) + if I1[2] ≠ I2[1] + return nothing + end + return CartesianIndex(I1[1], I2[2]) +end + +function default_mul!!( + a_dest::AbstractMatrix, + a1::AbstractMatrix, + a2::AbstractMatrix, + α::Number=true, + β::Number=false, +) + mul!(a_dest, a1, a2, α, β) + return a_dest +end + +function default_mul!!( + a_dest::Number, a1::Number, a2::Number, α::Number=true, β::Number=false +) + return a1 * a2 * α + a_dest * β +end + +# a1 * a2 * α + a_dest * β +function sparse_mul!( + a_dest::AbstractMatrix, + a1::AbstractMatrix, + a2::AbstractMatrix, + α::Number=true, + β::Number=false; + (mul!!)=(default_mul!!), +) + for I1 in stored_indices(a1) + for I2 in stored_indices(a2) + I_dest = mul_indices(I1, I2) + if !isnothing(I_dest) + a_dest[I_dest] = mul!!(a_dest[I_dest], a1[I1], a2[I2], α, β) + end + end + end + return a_dest +end diff --git a/NDTensors/src/lib/SparseArrayInterface/test/Project.toml b/NDTensors/src/lib/SparseArrayInterface/test/Project.toml index 4dd438d7ae..3d6b1d6bc8 100644 --- a/NDTensors/src/lib/SparseArrayInterface/test/Project.toml +++ b/NDTensors/src/lib/SparseArrayInterface/test/Project.toml @@ -1,4 +1,5 @@ [deps] +ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" diff --git a/NDTensors/src/lib/SparseArrayInterface/test/SparseArrayInterfaceTestUtils/AbstractSparseArrays.jl b/NDTensors/src/lib/SparseArrayInterface/test/SparseArrayInterfaceTestUtils/AbstractSparseArrays.jl index fa7bf6707c..deb9348d42 100644 --- a/NDTensors/src/lib/SparseArrayInterface/test/SparseArrayInterfaceTestUtils/AbstractSparseArrays.jl +++ b/NDTensors/src/lib/SparseArrayInterface/test/SparseArrayInterfaceTestUtils/AbstractSparseArrays.jl @@ -1,5 +1,6 @@ module AbstractSparseArrays using NDTensors.SparseArrayInterface: SparseArrayInterface, AbstractSparseArray +using ArrayLayouts: ArrayLayouts, MemoryLayout, MulAdd struct SparseArray{T,N} <: AbstractSparseArray{T,N} data::Vector{T} @@ -14,8 +15,18 @@ function SparseArray{T,N}(dims::Tuple{Vararg{Int,N}}) where {T,N} end SparseArray{T,N}(dims::Vararg{Int,N}) where {T,N} = SparseArray{T,N}(dims) SparseArray{T}(dims::Tuple{Vararg{Int}}) where {T} = SparseArray{T,length(dims)}(dims) +function SparseArray{T}(::UndefInitializer, dims::Tuple{Vararg{Int}}) where {T} + return SparseArray{T}(dims) +end SparseArray{T}(dims::Vararg{Int}) where {T} = SparseArray{T}(dims) +# ArrayLayouts interface +struct SparseLayout <: MemoryLayout end +ArrayLayouts.MemoryLayout(::Type{<:SparseArray}) = SparseLayout() +function Base.similar(::MulAdd{<:SparseLayout,<:SparseLayout}, elt::Type, axes) + return similar(SparseArray{elt}, axes) +end + # AbstractArray interface Base.size(a::SparseArray) = a.dims function Base.similar(a::SparseArray, elt::Type, dims::Tuple{Vararg{Int}}) diff --git a/NDTensors/src/lib/SparseArrayInterface/test/SparseArrayInterfaceTestUtils/SparseArrays.jl b/NDTensors/src/lib/SparseArrayInterface/test/SparseArrayInterfaceTestUtils/SparseArrays.jl index 162d0e4a2c..ddf8b2f8cc 100644 --- a/NDTensors/src/lib/SparseArrayInterface/test/SparseArrayInterfaceTestUtils/SparseArrays.jl +++ b/NDTensors/src/lib/SparseArrayInterface/test/SparseArrayInterfaceTestUtils/SparseArrays.jl @@ -14,6 +14,9 @@ function SparseArray{T,N}(dims::Tuple{Vararg{Int,N}}) where {T,N} end SparseArray{T,N}(dims::Vararg{Int,N}) where {T,N} = SparseArray{T,N}(dims) SparseArray{T}(dims::Tuple{Vararg{Int}}) where {T} = SparseArray{T,length(dims)}(dims) +function SparseArray{T}(::UndefInitializer, dims::Tuple{Vararg{Int}}) where {T} + return SparseArray{T}(dims) +end SparseArray{T}(dims::Vararg{Int}) where {T} = SparseArray{T}(dims) # AbstractArray interface diff --git a/NDTensors/src/lib/SparseArrayInterface/test/test_abstractsparsearray.jl b/NDTensors/src/lib/SparseArrayInterface/test/test_abstractsparsearray.jl index 1f7a18295c..1be4d5d8dc 100644 --- a/NDTensors/src/lib/SparseArrayInterface/test/test_abstractsparsearray.jl +++ b/NDTensors/src/lib/SparseArrayInterface/test/test_abstractsparsearray.jl @@ -1,5 +1,5 @@ @eval module $(gensym()) -using LinearAlgebra: norm +using LinearAlgebra: mul!, norm using NDTensors.SparseArrayInterface: SparseArrayInterface include("SparseArrayInterfaceTestUtils/SparseArrayInterfaceTestUtils.jl") using .SparseArrayInterfaceTestUtils.AbstractSparseArrays: AbstractSparseArrays @@ -265,5 +265,71 @@ using Test: @test, @testset a′ .+= b @test a′ == a + b @test SparseArrayInterface.nstored(a′) == 2 + + # Matrix multiplication + a1 = SparseArray{elt}(2, 3) + a1[1, 2] = 12 + a1[2, 1] = 21 + a2 = SparseArray{elt}(3, 4) + a2[1, 1] = 11 + a2[2, 2] = 22 + a_dest = a1 * a2 + @test Array(a_dest) ≈ Array(a1) * Array(a2) + @test a_dest isa SparseArray{elt} + @test SparseArrayInterface.nstored(a_dest) == 2 + + # In-place matrix multiplication + a1 = SparseArray{elt}(2, 3) + a1[1, 2] = 12 + a1[2, 1] = 21 + a2 = SparseArray{elt}(3, 4) + a2[1, 1] = 11 + a2[2, 2] = 22 + a_dest = SparseArray{elt}(2, 4) + mul!(a_dest, a1, a2) + @test Array(a_dest) ≈ Array(a1) * Array(a2) + @test a_dest isa SparseArray{elt} + @test SparseArrayInterface.nstored(a_dest) == 2 + + # In-place matrix multiplication + a1 = SparseArray{elt}(2, 3) + a1[1, 2] = 12 + a1[2, 1] = 21 + a2 = SparseArray{elt}(3, 4) + a2[1, 1] = 11 + a2[2, 2] = 22 + a_dest = SparseArray{elt}(2, 4) + a_dest[1, 2] = 12 + a_dest[2, 1] = 21 + α = elt(2) + β = elt(3) + a_dest′ = copy(a_dest) + mul!(a_dest, a1, a2, α, β) + @test Array(a_dest) ≈ Array(a1) * Array(a2) * α + Array(a_dest′) * β + @test a_dest isa SparseArray{elt} + @test SparseArrayInterface.nstored(a_dest) == 2 + + ## # Sparse matrix of matrix multiplication + ## TODO: Make this work, seems to require + ## a custom zero constructor. + ## a1 = SparseArray{Matrix{elt}}(2, 3) + ## a1[1, 1] = zeros(elt, (2, 3)) + ## a1[1, 2] = randn(elt, (2, 3)) + ## a1[2, 1] = randn(elt, (2, 3)) + ## a1[2, 2] = zeros(elt, (2, 3)) + ## a2 = SparseArray{Matrix{elt}}(3, 4) + ## a2[1, 1] = randn(elt, (3, 4)) + ## a2[1, 2] = zeros(elt, (3, 4)) + ## a2[2, 2] = randn(elt, (3, 4)) + ## a2[2, 2] = zeros(elt, (3, 4)) + ## a_dest = SparseArray{Matrix{elt}}(2, 4) + ## a_dest[1, 1] = zeros(elt, (3, 4)) + ## a_dest[1, 2] = zeros(elt, (3, 4)) + ## a_dest[2, 2] = zeros(elt, (3, 4)) + ## a_dest[2, 2] = zeros(elt, (3, 4)) + ## mul!(a_dest, a1, a2) + ## @test Array(a_dest) ≈ Array(a1) * Array(a2) + ## @test a_dest isa SparseArray{Matrix{elt}} + ## @test SparseArrayInterface.nstored(a_dest) == 2 end end diff --git a/NDTensors/src/lib/SparseArrayInterface/test/test_diagonalarray.jl b/NDTensors/src/lib/SparseArrayInterface/test/test_diagonalarray.jl index 6804647452..257ef385b4 100644 --- a/NDTensors/src/lib/SparseArrayInterface/test/test_diagonalarray.jl +++ b/NDTensors/src/lib/SparseArrayInterface/test/test_diagonalarray.jl @@ -65,5 +65,12 @@ using Test: @test, @testset, @test_throws for I in LinearIndices(a) @test a[I] == a_r[I] end + + # Matrix multiplication! + a1 = DiagonalArray(elt[1, 2], (2, 2)) + a2 = DiagonalArray(elt[2, 3], (2, 2)) + a_dest = a1 * a2 + @test Array(a_dest) ≈ Array(a1) * Array(a2) + @test a_dest isa DiagonalArray{elt} end end diff --git a/NDTensors/src/lib/TensorAlgebra/test/Project.toml b/NDTensors/src/lib/TensorAlgebra/test/Project.toml index 49fd3143c6..098d12c78c 100644 --- a/NDTensors/src/lib/TensorAlgebra/test/Project.toml +++ b/NDTensors/src/lib/TensorAlgebra/test/Project.toml @@ -1,3 +1,4 @@ [deps] Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" +NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" TensorOperations = "6aa20fa7-93e2-5fca-9bc0-fbd0db3c71a2" diff --git a/NDTensors/src/lib/Unwrap/README.md b/NDTensors/src/lib/Unwrap/README.md index d5d1b36d72..9838719572 100644 --- a/NDTensors/src/lib/Unwrap/README.md +++ b/NDTensors/src/lib/Unwrap/README.md @@ -3,6 +3,7 @@ A module to unwrap complex array types to assist in the generic programming of array-type based functions. Related: +- https://github.com/JuliaLinearAlgebra/ArrayLayouts.jl/issues/9 - https://juliaarrays.github.io/ArrayInterface.jl/stable/wrapping/ - https://github.com/JuliaGPU/Adapt.jl - https://github.com/chengchingwen/StructWalk.jl diff --git a/Project.toml b/Project.toml index 6eb16ddb15..3e833daa8e 100644 --- a/Project.toml +++ b/Project.toml @@ -5,6 +5,7 @@ version = "0.3.52" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" +ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" BitIntegers = "c3b6d118-76ef-56ca-8cc7-ebb389d030a1" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" From e3ccfefa1958641b8dadf6f261ea4ec3278fc384 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 7 Dec 2023 15:57:34 -0500 Subject: [PATCH 070/156] Define mult_zero for UnallocatedZeros and make unit tests --- .../UnallocatedArrays/src/UnallocatedArrays.jl | 2 +- .../UnallocatedArrays/src/unallocatedzeros.jl | 12 ++++++++++++ .../src/lib/UnallocatedArrays/test/runtests.jl | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index 2b292840cb..249eeb3b2e 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,5 +1,5 @@ module UnallocatedArrays -using FillArrays: FillArrays, AbstractFill, AbstractZeros, Fill, Zeros, getindex_value +using FillArrays: FillArrays, AbstractFill, AbstractZeros, Fill, Zeros, getindex_value, mult_zeros using NDTensors.SetParameters: SetParameters, Position, diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 8b4514dd72..7d7093b488 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -20,3 +20,15 @@ end set_alloctype(f::Zeros, alloc::Type{<:AbstractArray}) = UnallocatedZeros(f, alloc) Base.parent(Z::UnallocatedZeros) = Z.z + +FillArrays.mult_zeros(a::UnallocatedZeros, b, elt, ax) = UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) +FillArrays.mult_zeros(a, b::UnallocatedZeros, elt, ax) = mult_zeros(b,a, elt, ax) +function FillArrays.mult_zeros(a::UnallocatedZeros, b::UnallocatedZeros, elt, ax) + @assert(alloctype(a) == alloctype(b)) + return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) +end + +# broadcasted_zeros(f, a, elt, ax) = Zeros{elt}(ax) +# broadcasted_zeros(f, a, b, elt, ax) = Zeros{elt}(ax) + +# kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) \ No newline at end of file diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 558228c94c..8c3003aa72 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -33,7 +33,22 @@ using .NDTensorsTestUtils: devices_list ## Things that are still broken R = Zc * Zc' - @test_broken R isa UnallocatedZeros + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Zc) + @test size(R) == (2,2) + M = rand(elt, (3,4)) + R = Zc * M + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Zc) + @test size(R) == (2,4) + R = M' * Zc' + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Zc) + @test size(R) == (4,2) + R = transpose(M) * transpose(Zc) + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Zc) + @test size(R) == (4,2) R = Zc + Zc @test_broken R isa UnallocatedZeros R = Zc .* Zc From ec266f8ff37e3c10978a1afe6b0858cabe37af75 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 7 Dec 2023 15:58:07 -0500 Subject: [PATCH 071/156] Define transpose and adjoint for unallocatedarrays --- .../src/abstractunallocatedarray.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index d9d9347b9c..561a8fc3ca 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -10,17 +10,21 @@ function Base.complex(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) ) end +function Base.transpose(a::Union{<:UnallocatedFill, <:UnallocatedZeros}) + return set_alloctype(transpose(parent(a)), alloctype(a)) +end + +function Base.adjoint(a::Union{<:UnallocatedFill, <:UnallocatedZeros}) + return set_alloctype(adjoint(parent(a)), alloctype(a)) +end + # mult_fill(a, b, val, ax) = Fill(val, ax) -# mult_zeros(a, b, elt, ax) = Zeros{elt}(ax) # mult_ones(a, b, elt, ax) = Ones{elt}(ax) # broadcasted_fill(f, a, val, ax) = Fill(val, ax) # broadcasted_fill(f, a, b, val, ax) = Fill(val, ax) -# broadcasted_zeros(f, a, elt, ax) = Zeros{elt}(ax) -# broadcasted_zeros(f, a, b, elt, ax) = Zeros{elt}(ax) # broadcasted_ones(f, a, elt, ax) = Ones{elt}(ax) # broadcasted_ones(f, a, b, elt, ax) = Ones{elt}(ax) # kron_fill(a, b, val, ax) = Fill(val, ax) -# kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) # kron_ones(a, b, elt, ax) = Ones{elt}(ax) From 57d52e185e23df5f21af5fbdbcdf1b619c036bfa Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 7 Dec 2023 16:36:36 -0500 Subject: [PATCH 072/156] Define broadcasted_zeros --- .../src/lib/UnallocatedArrays/src/UnallocatedArrays.jl | 2 +- .../src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index 249eeb3b2e..567181b79b 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,5 +1,5 @@ module UnallocatedArrays -using FillArrays: FillArrays, AbstractFill, AbstractZeros, Fill, Zeros, getindex_value, mult_zeros +using FillArrays: FillArrays, AbstractFill, AbstractZeros, Fill, Zeros, broadcasted_zeros, getindex_value, mult_zeros using NDTensors.SetParameters: SetParameters, Position, diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 7d7093b488..45b47ca29a 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -28,7 +28,12 @@ function FillArrays.mult_zeros(a::UnallocatedZeros, b::UnallocatedZeros, elt, ax return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end -# broadcasted_zeros(f, a, elt, ax) = Zeros{elt}(ax) -# broadcasted_zeros(f, a, b, elt, ax) = Zeros{elt}(ax) +FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, elt, ax) = UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) +function FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, b::UnallocatedZeros, elt, ax) + @assert(alloctype(a) == alloctype(b)) + return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) +end +FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, b, elt, ax) = UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) +FillArrays.broadcasted_zeros(f, a, b::UnallocatedZeros, elt, ax) = broadcasted_zeros(f, b, a, elt, ax) # kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) \ No newline at end of file From b6a51e7c641d37ba9b145ab7299a9e6d6c0c46c8 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 7 Dec 2023 16:52:59 -0500 Subject: [PATCH 073/156] Make kron_zero functions --- .../src/UnallocatedArrays.jl | 2 +- .../UnallocatedArrays/src/unallocatedzeros.jl | 15 +++++++- .../lib/UnallocatedArrays/test/runtests.jl | 34 +++++++++++++++---- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index 567181b79b..b1e5ec0e84 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,5 +1,5 @@ module UnallocatedArrays -using FillArrays: FillArrays, AbstractFill, AbstractZeros, Fill, Zeros, broadcasted_zeros, getindex_value, mult_zeros +using FillArrays: FillArrays, AbstractFill, AbstractZeros, Fill, Zeros, broadcasted_zeros, getindex_value, kron_zeros, mult_zeros using NDTensors.SetParameters: SetParameters, Position, diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 45b47ca29a..aac0a1a7e7 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -36,4 +36,17 @@ end FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, b, elt, ax) = UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) FillArrays.broadcasted_zeros(f, a, b::UnallocatedZeros, elt, ax) = broadcasted_zeros(f, b, a, elt, ax) -# kron_zeros(a, b, elt, ax) = Zeros{elt}(ax) \ No newline at end of file + +function FillArrays.kron_zeros(a::UnallocatedZeros, b::UnallocatedZeros, elt, ax) + @assert alloctype(a) == alloctype(b) + return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) +end + +function FillArrays.kron_zeros(a::UnallocatedZeros, b::UnallocatedFill, elt, ax) + @assert alloctype(a) == alloctype(b) + return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) +end + +function FillArrays.kron_zeros(a::UnallocatedFill, b::UnallocatedZeros, elt, ax) + kron_zeros(b,a,elt,ax) +end \ No newline at end of file diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 8c3003aa72..79df3cb187 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -1,4 +1,4 @@ -@eval module $(gensym()) +#@eval module $(gensym()) using FillArrays: FillArrays, AbstractFill, Fill, Zeros using NDTensors: NDTensors using NDTensors.UnallocatedArrays @@ -10,8 +10,8 @@ using Test: @test, @testset, @test_broken include(joinpath(pkgdir(NDTensors), "test", "NDTensorsTestUtils", "NDTensorsTestUtils.jl")) using .NDTensorsTestUtils: devices_list -@testset "Testing UnallocatedArrays" for dev in devices_list(ARGS), - elt in (Float64, Float32, ComplexF64, ComplexF32) +#@testset "Testing UnallocatedArrays" for dev in devices_list(ARGS), + # elt in (Float64, Float32, ComplexF64, ComplexF32) z = Zeros{elt}((2, 3)) Z = UnallocatedZeros(z, dev(Matrix{eltype(z)})) @@ -49,11 +49,32 @@ using .NDTensorsTestUtils: devices_list @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Zc) @test size(R) == (4,2) - R = Zc + Zc - @test_broken R isa UnallocatedZeros + + R = eltype(Zc)(2.0) .* Zc + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Zc) + R = Zc .* eltype(Zc)(2.0) + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Zc) + R = Zc .* Zc - @test_broken R isa UnallocatedZeros + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Zc) + A = UnallocatedZeros(Zeros{elt}(2), Vector{Float32}) + B = UnallocatedZeros(Zeros{elt}(2), Vector{Float32}) + C = kron(A, B) + @test C isa UnallocatedZeros + @test alloctype(C) == alloctype(B) + ## TODO make other kron tests + + ## The following two tests don't work properly yet + R = Zc + Zc + @test_broken R isa UnallocatedZeros + @test_broken alloctype(R) == alloctype(Zc) + R = Zc .+ eltype(Zc)(2.0) + @test_broken R isa UnallocatedFill + ######################################### # UnallocatedFill f = Fill{elt}(3.0, (2, 3, 4)) @@ -75,6 +96,7 @@ using .NDTensorsTestUtils: devices_list @test Fc == F Fc = allocate(complex(F)) @test eltype(Fc) == complex(eltype(F)) + @test typeof(Fc) == alloctype(complex(F)) ## This allocates is this correct? ## TODO this is broken because it doesn't call allocate Fc[2, 3, 4] = 4.0 + 3.0im From 0c4c92e7afae9e4590eabc4c9d780ab2d97355d5 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 7 Dec 2023 16:54:50 -0500 Subject: [PATCH 074/156] format --- .../src/UnallocatedArrays.jl | 11 ++++++++- .../src/abstractunallocatedarray.jl | 4 ++-- .../UnallocatedArrays/src/unallocatedzeros.jl | 24 ++++++++++++------- .../lib/UnallocatedArrays/test/runtests.jl | 24 +++++++++---------- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index b1e5ec0e84..7be0496040 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,5 +1,14 @@ module UnallocatedArrays -using FillArrays: FillArrays, AbstractFill, AbstractZeros, Fill, Zeros, broadcasted_zeros, getindex_value, kron_zeros, mult_zeros +using FillArrays: + FillArrays, + AbstractFill, + AbstractZeros, + Fill, + Zeros, + broadcasted_zeros, + getindex_value, + kron_zeros, + mult_zeros using NDTensors.SetParameters: SetParameters, Position, diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 561a8fc3ca..e466227c6a 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -10,11 +10,11 @@ function Base.complex(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) ) end -function Base.transpose(a::Union{<:UnallocatedFill, <:UnallocatedZeros}) +function Base.transpose(a::Union{<:UnallocatedFill,<:UnallocatedZeros}) return set_alloctype(transpose(parent(a)), alloctype(a)) end -function Base.adjoint(a::Union{<:UnallocatedFill, <:UnallocatedZeros}) +function Base.adjoint(a::Union{<:UnallocatedFill,<:UnallocatedZeros}) return set_alloctype(adjoint(parent(a)), alloctype(a)) end diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index aac0a1a7e7..85bb4767b5 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -21,21 +21,29 @@ set_alloctype(f::Zeros, alloc::Type{<:AbstractArray}) = UnallocatedZeros(f, allo Base.parent(Z::UnallocatedZeros) = Z.z -FillArrays.mult_zeros(a::UnallocatedZeros, b, elt, ax) = UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) -FillArrays.mult_zeros(a, b::UnallocatedZeros, elt, ax) = mult_zeros(b,a, elt, ax) +function FillArrays.mult_zeros(a::UnallocatedZeros, b, elt, ax) + return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) +end +FillArrays.mult_zeros(a, b::UnallocatedZeros, elt, ax) = mult_zeros(b, a, elt, ax) function FillArrays.mult_zeros(a::UnallocatedZeros, b::UnallocatedZeros, elt, ax) @assert(alloctype(a) == alloctype(b)) return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end -FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, elt, ax) = UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) +function FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, elt, ax) + return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) +end function FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, b::UnallocatedZeros, elt, ax) - @assert(alloctype(a) == alloctype(b)) + @assert(alloctype(a) == alloctype(b)) return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end -FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, b, elt, ax) = UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) -FillArrays.broadcasted_zeros(f, a, b::UnallocatedZeros, elt, ax) = broadcasted_zeros(f, b, a, elt, ax) +function FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, b, elt, ax) + return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) +end +function FillArrays.broadcasted_zeros(f, a, b::UnallocatedZeros, elt, ax) + return broadcasted_zeros(f, b, a, elt, ax) +end function FillArrays.kron_zeros(a::UnallocatedZeros, b::UnallocatedZeros, elt, ax) @assert alloctype(a) == alloctype(b) @@ -48,5 +56,5 @@ function FillArrays.kron_zeros(a::UnallocatedZeros, b::UnallocatedFill, elt, ax) end function FillArrays.kron_zeros(a::UnallocatedFill, b::UnallocatedZeros, elt, ax) - kron_zeros(b,a,elt,ax) -end \ No newline at end of file + return kron_zeros(b, a, elt, ax) +end diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 79df3cb187..4364b6ef65 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -1,17 +1,15 @@ -#@eval module $(gensym()) +@eval module $(gensym()) using FillArrays: FillArrays, AbstractFill, Fill, Zeros using NDTensors: NDTensors using NDTensors.UnallocatedArrays using LinearAlgebra: norm using Test: @test, @testset, @test_broken -## Could potentially need this -#using GPUArraysCore: @allowscalar include(joinpath(pkgdir(NDTensors), "test", "NDTensorsTestUtils", "NDTensorsTestUtils.jl")) using .NDTensorsTestUtils: devices_list -#@testset "Testing UnallocatedArrays" for dev in devices_list(ARGS), - # elt in (Float64, Float32, ComplexF64, ComplexF32) +@testset "Testing UnallocatedArrays" for dev in devices_list(ARGS), + elt in (Float64, Float32, ComplexF64, ComplexF32) z = Zeros{elt}((2, 3)) Z = UnallocatedZeros(z, dev(Matrix{eltype(z)})) @@ -35,28 +33,28 @@ using .NDTensorsTestUtils: devices_list R = Zc * Zc' @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Zc) - @test size(R) == (2,2) - M = rand(elt, (3,4)) + @test size(R) == (2, 2) + M = rand(elt, (3, 4)) R = Zc * M @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Zc) - @test size(R) == (2,4) + @test size(R) == (2, 4) R = M' * Zc' @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Zc) - @test size(R) == (4,2) + @test size(R) == (4, 2) R = transpose(M) * transpose(Zc) @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Zc) - @test size(R) == (4,2) - + @test size(R) == (4, 2) + R = eltype(Zc)(2.0) .* Zc @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Zc) R = Zc .* eltype(Zc)(2.0) @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Zc) - + R = Zc .* Zc @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Zc) @@ -74,7 +72,7 @@ using .NDTensorsTestUtils: devices_list @test_broken alloctype(R) == alloctype(Zc) R = Zc .+ eltype(Zc)(2.0) @test_broken R isa UnallocatedFill - + ######################################### # UnallocatedFill f = Fill{elt}(3.0, (2, 3, 4)) From 3c1859601bb8f339deee2f30cfafe662bc35b66c Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Thu, 7 Dec 2023 17:05:16 -0500 Subject: [PATCH 075/156] fix typo --- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 4364b6ef65..4a2d5db915 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -59,8 +59,8 @@ using .NDTensorsTestUtils: devices_list @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Zc) - A = UnallocatedZeros(Zeros{elt}(2), Vector{Float32}) - B = UnallocatedZeros(Zeros{elt}(2), Vector{Float32}) + A = UnallocatedZeros(Zeros{elt}(2), Vector{elt}) + B = UnallocatedZeros(Zeros{elt}(2), Vector{elt}) C = kron(A, B) @test C isa UnallocatedZeros @test alloctype(C) == alloctype(B) From 60ebf274959a371f52588a9c500f00b873591262 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 8 Dec 2023 07:52:23 -0500 Subject: [PATCH 076/156] Merge branch 'main' into kmp5/feature/FieldTypes --- jenkins/Jenkinsfile | 6 +- test/ITensorChainRules/test_chainrules_ops.jl | 76 ++++++++++--------- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index ee13f93691..d97c1e0509 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -30,7 +30,7 @@ pipeline { julia -e 'using Pkg; Pkg.add(name="NDTensors", version="0.1"); Pkg.add(name="ITensors"); Pkg.develop(path="./ITensorGPU"); Pkg.instantiate(); Pkg.test("ITensorGPU");' ''' sh ''' - julia --project=NDTensors -e 'using Pkg; Pkg.develop(path="."); Pkg.test(; test_args=["cuda"])' + julia --project=monorepo -e 'using Pkg; Pkg.develop(path="."); Pkg.develop(path="./NDTensors"); Pkg.test("NDTensors"; test_args=["cuda"])' ''' } } @@ -43,7 +43,7 @@ pipeline { label 'gpu&&v100' filename 'Dockerfile' dir 'jenkins' - additionalBuildArgs '--build-arg JULIA=1.8' + additionalBuildArgs '--build-arg JULIA=1.9' args '--gpus "device=1"' } } @@ -57,7 +57,7 @@ pipeline { julia -e 'using Pkg; Pkg.add(name="NDTensors", version="0.1"); Pkg.add(name="ITensors"); Pkg.develop(path="./ITensorGPU"); Pkg.instantiate(); Pkg.test("ITensorGPU");' ''' sh ''' - julia --project=NDTensors -e 'using Pkg; Pkg.develop(path="."); Pkg.test(; test_args=["cuda"])' + julia --project=monorepo -e 'using Pkg; Pkg.develop(path="."); Pkg.develop(path="./NDTensors"); Pkg.test("NDTensors"; test_args=["cuda"])' ''' } } diff --git a/test/ITensorChainRules/test_chainrules_ops.jl b/test/ITensorChainRules/test_chainrules_ops.jl index 297e7e5bd3..3e81585201 100644 --- a/test/ITensorChainRules/test_chainrules_ops.jl +++ b/test/ITensorChainRules/test_chainrules_ops.jl @@ -157,21 +157,23 @@ using Zygote: ZygoteRuleConfig, gradient atol=1.0e-7, ) - f = function (x) - y = ITensor(exp(-x * Op("X", 1) * Op("X", 2)), s) - return norm(y) - end - args = (x,) - test_rrule(ZygoteRuleConfig(), f, args...; rrule_f=rrule_via_ad, check_inferred=false) + if VERSION ≥ v"1.8" + f = function (x) + y = ITensor(exp(-x * Op("X", 1) * Op("X", 2)), s) + return norm(y) + end + args = (x,) + test_rrule(ZygoteRuleConfig(), f, args...; rrule_f=rrule_via_ad, check_inferred=false) - f = function (x) - y = exp(-x * Op("X", 1) * Op("X", 2)) - y *= exp(-x * Op("X", 1) * Op("X", 2)) - U = ITensor(y, s) - return norm(U) + f = function (x) + y = exp(-x * Op("X", 1) * Op("X", 2)) + y *= exp(-x * Op("X", 1) * Op("X", 2)) + U = ITensor(y, s) + return norm(U) + end + args = (x,) + test_rrule(ZygoteRuleConfig(), f, args...; rrule_f=rrule_via_ad, check_inferred=false) end - args = (x,) - test_rrule(ZygoteRuleConfig(), f, args...; rrule_f=rrule_via_ad, check_inferred=false) U1(θ) = Op("Ry", 1; θ) U2(θ) = Op("Ry", 2; θ) @@ -249,30 +251,32 @@ using Zygote: ZygoteRuleConfig, gradient args = (x,) test_rrule(ZygoteRuleConfig(), f, args...; rrule_f=rrule_via_ad, check_inferred=false) - f = function (x) - y = exp(-x * (Op("X", 1) * Op("X", 2) + Op("Z", 1) * Op("Z", 2)); alg=Trotter{1}(1)) - U = ITensor(y, s) - return norm(U * V) - end - args = (x,) - test_rrule(ZygoteRuleConfig(), f, args...; rrule_f=rrule_via_ad, check_inferred=false) + if VERSION ≥ v"1.8" + f = function (x) + y = exp(-x * (Op("X", 1) * Op("X", 2) + Op("Z", 1) * Op("Z", 2)); alg=Trotter{1}(1)) + U = ITensor(y, s) + return norm(U * V) + end + args = (x,) + test_rrule(ZygoteRuleConfig(), f, args...; rrule_f=rrule_via_ad, check_inferred=false) - ## XXX: Fix - f = function (x) - y = exp(-x * Op("X", 1) * Op("X", 2)) - y *= exp(-x * Op("X", 1) * Op("X", 2)) - U = Prod{ITensor}(y, s) - return norm(U(V)) - end - args = (x,) - test_rrule(ZygoteRuleConfig(), f, args...; rrule_f=rrule_via_ad, check_inferred=false) + ## XXX: Fix + f = function (x) + y = exp(-x * Op("X", 1) * Op("X", 2)) + y *= exp(-x * Op("X", 1) * Op("X", 2)) + U = Prod{ITensor}(y, s) + return norm(U(V)) + end + args = (x,) + test_rrule(ZygoteRuleConfig(), f, args...; rrule_f=rrule_via_ad, check_inferred=false) - ## XXX: Fix - f = function (x) - y = exp(-x * (Op("X", 1) + Op("Z", 1) + Op("Z", 1)); alg=Trotter{1}(1)) - U = Prod{ITensor}(y, s) - return norm(U(V)) + ## XXX: Fix + f = function (x) + y = exp(-x * (Op("X", 1) + Op("Z", 1) + Op("Z", 1)); alg=Trotter{1}(1)) + U = Prod{ITensor}(y, s) + return norm(U(V)) + end + args = (x,) + test_rrule(ZygoteRuleConfig(), f, args...; rrule_f=rrule_via_ad, check_inferred=false) end - args = (x,) - test_rrule(ZygoteRuleConfig(), f, args...; rrule_f=rrule_via_ad, check_inferred=false) end From d47e50a7eca60f560734b99aac82600cdc650478 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 8 Dec 2023 12:27:20 -0500 Subject: [PATCH 077/156] Remove update --- .github/workflows/test_itensors_base_ubuntu.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test_itensors_base_ubuntu.yml b/.github/workflows/test_itensors_base_ubuntu.yml index ecb417d87e..1314173bba 100644 --- a/.github/workflows/test_itensors_base_ubuntu.yml +++ b/.github/workflows/test_itensors_base_ubuntu.yml @@ -39,7 +39,6 @@ jobs: using Pkg; Pkg.develop(path="."); Pkg.develop(path="./NDTensors"); - Pkg.update(); - name: Run the tests shell: julia --project=monorepo {0} run: | From 7653d916a11acd5cd5b68ac2dc414348569d9051 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 8 Dec 2023 15:46:14 -0500 Subject: [PATCH 078/156] Add some functions to fix --- .../src/abstractunallocatedarray.jl | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index e466227c6a..96823e31f6 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -18,13 +18,11 @@ function Base.adjoint(a::Union{<:UnallocatedFill,<:UnallocatedZeros}) return set_alloctype(adjoint(parent(a)), alloctype(a)) end -# mult_fill(a, b, val, ax) = Fill(val, ax) -# mult_ones(a, b, elt, ax) = Ones{elt}(ax) +## TODO fix this because reshape loses alloctype +#FillArrays.reshape(a::Union{<:UnallocatedFill, <:UnallocatedZeros}, dims) = set_alloctype(reshape(parent(a), dims), allocate(a)) -# broadcasted_fill(f, a, val, ax) = Fill(val, ax) -# broadcasted_fill(f, a, b, val, ax) = Fill(val, ax) -# broadcasted_ones(f, a, elt, ax) = Ones{elt}(ax) -# broadcasted_ones(f, a, b, elt, ax) = Ones{elt}(ax) +# function Adapt.adapt_storage(to::Type{<:AbstractArray}, x::Union{<:UnallocatedFill, <:UnallocatedZeros}) +# return set_alloctype(parent(x), to) +# end -# kron_fill(a, b, val, ax) = Fill(val, ax) -# kron_ones(a, b, elt, ax) = Ones{elt}(ax) +# function Adapt.adapt_storage(to::Type{<:Number}, x::) \ No newline at end of file From 0699e89c6588dda25d7af97a42a0a343670ba663 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 8 Dec 2023 15:46:33 -0500 Subject: [PATCH 079/156] add fill functions --- .../UnallocatedArrays/src/unallocatedfill.jl | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 0ff8699ebc..433b06e048 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -21,3 +21,33 @@ end set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) = UnallocatedFill(f, alloc) Base.parent(F::UnallocatedFill) = F.f + +# mult_fill(a, b, val, ax) = Fill(val, ax) +function FillArrays.mult_fill(a::UnallocatedFill, b, val, ax) + return UnallocatedFill(Fill(val, ax), alloctype(a)) +end +FillArrays.mult_fill(a, b::UnallocatedFill, val, ax) = mult_fill(b, a, val, ax) +function FillArrays.mult_fill(a::UnallocatedFill, b::UnallocatedFill, val, ax) + @assert(alloctype(a) == alloctype(b)) + return UnallocatedFill(Fill(val, ax), alloctype(a)) +end + +function FillArrays.broadcasted_fill(f, a::UnallocatedFill, val, ax) + return UnallocatedFill(Fill(val, ax), alloctype(a)) +end +function FillArrays.broadcasted_fill(f, a::UnallocatedFill, b::UnallocatedFill, val, ax) + @assert(alloctype(a) == alloctype(b)) + return UnallocatedFill(Fill(val, ax), alloctype(a)) +end + +function FillArrays.broadcasted_fill(f, a::UnallocatedFill, b, val, ax) + return UnallocatedFill(Fill(val, ax), alloctype(a)) +end +function FillArrays.broadcasted_fill(f, a, b::UnallocatedFill, val, ax) + return broadcasted_fill(f, b, a, val, ax) +end + +function FillArrays.kron_fill(a::UnallocatedFill, b::UnallocatedFill, val, ax) + @assert alloctype(a) == alloctype(b) + return UnallocatedFill(Fill(val, ax), alloctype(a)) +end From 4a1cef2a14a876a65490049175ea46fbddf59d2e Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 8 Dec 2023 15:46:50 -0500 Subject: [PATCH 080/156] overload kron_fill not kron_zeros --- .../src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 85bb4767b5..f85411d153 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -50,11 +50,12 @@ function FillArrays.kron_zeros(a::UnallocatedZeros, b::UnallocatedZeros, elt, ax return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end -function FillArrays.kron_zeros(a::UnallocatedZeros, b::UnallocatedFill, elt, ax) +function FillArrays.kron_fill(a::UnallocatedZeros, b::UnallocatedFill, val, ax) @assert alloctype(a) == alloctype(b) + elt = typeof(val) return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end -function FillArrays.kron_zeros(a::UnallocatedFill, b::UnallocatedZeros, elt, ax) - return kron_zeros(b, a, elt, ax) +function FillArrays.kron_fill(a::UnallocatedFill, b::UnallocatedZeros, val, ax) + return kron_fill(b, a, val, ax) end From 40af2cc447a3b9e7b5d47d4e87c4f500dd862ee5 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 8 Dec 2023 15:47:09 -0500 Subject: [PATCH 081/156] Update functions needed --- .../src/lib/UnallocatedArrays/src/UnallocatedArrays.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index 7be0496040..9cb77ee463 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -6,9 +6,12 @@ using FillArrays: Fill, Zeros, broadcasted_zeros, + broadcasted_fill, + fill_add, getindex_value, - kron_zeros, + kron_fill, mult_zeros + using NDTensors.SetParameters: SetParameters, Position, @@ -18,6 +21,8 @@ using NDTensors.SetParameters: set_parameter, set_parameters + #using Adapt: Adapt + include("abstractfill/abstractfill.jl") include("abstractfill/set_types.jl") From 1d107a6cbd099683a1d00c597a331f18e551cbf2 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 8 Dec 2023 15:47:23 -0500 Subject: [PATCH 082/156] Add more tests and sort them --- .../lib/UnallocatedArrays/test/runtests.jl | 220 +++++++++++------- 1 file changed, 135 insertions(+), 85 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 4a2d5db915..cf9dd5396a 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -9,95 +9,145 @@ include(joinpath(pkgdir(NDTensors), "test", "NDTensorsTestUtils", "NDTensorsTest using .NDTensorsTestUtils: devices_list @testset "Testing UnallocatedArrays" for dev in devices_list(ARGS), - elt in (Float64, Float32, ComplexF64, ComplexF32) - - z = Zeros{elt}((2, 3)) - Z = UnallocatedZeros(z, dev(Matrix{eltype(z)})) - - @test Z isa AbstractFill - @test size(Z) == (2, 3) - @test length(Z) == 6 - @test sum(Z) == 0 - @test norm(Z) == 0 - @test Z[2, 3] == 0 - @test allocate(Z) isa dev(Matrix{elt}) - Zp = set_alloctype(z, dev(Matrix{elt})) - @test Zp == Z - Zc = copy(Z) - @test Zc == Z - Zc = complex(Z) - @test eltype(Zc) == complex(eltype(z)) - @test Zc[1, 2] == 0.0 + 0.0im - - ## Things that are still broken - R = Zc * Zc' - @test R isa UnallocatedZeros - @test alloctype(R) == alloctype(Zc) - @test size(R) == (2, 2) - M = rand(elt, (3, 4)) - R = Zc * M - @test R isa UnallocatedZeros - @test alloctype(R) == alloctype(Zc) - @test size(R) == (2, 4) - R = M' * Zc' - @test R isa UnallocatedZeros - @test alloctype(R) == alloctype(Zc) - @test size(R) == (4, 2) - R = transpose(M) * transpose(Zc) - @test R isa UnallocatedZeros - @test alloctype(R) == alloctype(Zc) - @test size(R) == (4, 2) - - R = eltype(Zc)(2.0) .* Zc - @test R isa UnallocatedZeros - @test alloctype(R) == alloctype(Zc) - R = Zc .* eltype(Zc)(2.0) - @test R isa UnallocatedZeros - @test alloctype(R) == alloctype(Zc) - - R = Zc .* Zc - @test R isa UnallocatedZeros - @test alloctype(R) == alloctype(Zc) - - A = UnallocatedZeros(Zeros{elt}(2), Vector{elt}) - B = UnallocatedZeros(Zeros{elt}(2), Vector{elt}) - C = kron(A, B) - @test C isa UnallocatedZeros - @test alloctype(C) == alloctype(B) - ## TODO make other kron tests + elt in (Float64, Float32, ComplexF64, ComplexF32) + + @testset "Basic funcitonality" begin + z = Zeros{elt}((2, 3)) + Z = UnallocatedZeros(z, dev(Matrix{eltype(z)})) + + @test Z isa AbstractFill + @test size(Z) == (2, 3) + @test length(Z) == 6 + @test sum(Z) == 0 + @test norm(Z) == 0 + @test Z[2, 3] == 0 + @test allocate(Z) isa dev(Matrix{elt}) + Zp = set_alloctype(z, dev(Matrix{elt})) + @test Zp == Z + Zc = copy(Z) + @test Zc == Z + Zc = complex(Z) + @test eltype(Zc) == complex(eltype(z)) + @test Zc[1, 2] == 0.0 + 0.0im + + ######################################### + # UnallocatedFill + f = Fill{elt}(3.0, (2, 3, 4)) + F = UnallocatedFill(f, Array{elt,ndims(f)}) + @test F isa AbstractFill + @test size(F) == (2, 3, 4) + @test length(F) == 24 + @test sum(F) ≈ 3 * 24 + @test norm(F) ≈ sqrt(3^2 * 24) + @test F[2, 3, 1] == 3.0 + @test allocate(F) isa Array{elt,3} + Fp = allocate(F) + @test norm(Fp) ≈ norm(F) + + Fp = set_alloctype(f, dev(Array{elt,ndims(f)})) + @test allocate(Fp) isa dev(Array{elt,ndims(f)}) + @test Fp == F + Fc = copy(F) + @test Fc == F + Fc = allocate(complex(F)) + @test eltype(Fc) == complex(eltype(F)) + @test typeof(Fc) == alloctype(complex(F)) + ## This allocates is this correct? + ## TODO this is broken because it doesn't call allocate + Fc[2, 3, 4] = 4.0 + 3.0im + @test Fc[2, 3, 4] == 4.0 + 3.0im + end + + @testset "Multiplication" begin + z = Zeros{elt}((2, 3)) + Z = UnallocatedZeros(z, dev(Matrix{eltype(z)})) + ## Things that are still broken + R = Z * Z' + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Z) + @test size(R) == (2, 2) + M = rand(elt, (3, 4)) + R = Z * M + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Z) + @test size(R) == (2, 4) + R = M' * Z' + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Z) + @test size(R) == (4, 2) + R = transpose(M) * transpose(Z) + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Z) + @test size(R) == (4, 2) + + ################################### + ## UnallocatedFill + f = Fill{elt}(3.0, (2,12)) + F = UnallocatedFill(f, dev(Matrix{elt})) + p = Fill{elt}(4.0, (12,5)) + P = UnallocatedFill(p, dev(Array{elt, ndims(p)})) + R = F * P + @test F isa UnallocatedFill + @test R[1,1] == 144 + @test alloctype(R) == alloctype(F) + @test size(R) == (2,5) + end + + @testset "Broadcast" begin + z = Zeros{elt}((2, 3)) + Z = UnallocatedZeros(z, dev(Matrix{elt})) + R = elt(2.0) .* Z + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Z) + R = Z .* elt(2.0) + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Z) + + R = Z .* Z + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Z) + + ######################## + # UnallocatedFill + f = Fill(elt(3.0), (2,3,4)) + F = UnallocatedFill(f, Array{elt, ndims(f)}) + F2 = F .* 2 + @test F2 isa UnallocatedFill + @test F2[1,1,1] == elt(6.0) + @test alloctype(F2) == alloctype(F) + end + + ## TODO make other kron tests + @testset "Kron" begin + A = UnallocatedZeros(Zeros{elt}(2), dev(Vector{elt})) + B = UnallocatedZeros(Zeros{elt}(2), dev(Vector{elt})) + C = kron(A, B) + @test C isa UnallocatedZeros + @test alloctype(C) == alloctype(B) + + B = UnallocatedFill(Fill(elt(2.0), (2)), dev(Vector{elt})) + C = kron(A, B) + @test C isa UnallocatedZeros + @test alloctype(C) == alloctype(B) + + C = kron(B, A) + @test C isa UnallocatedZeros + @test alloctype(C) == alloctype(B) + + A = UnallocatedFill(Fill(elt(3.0), (2)), dev(Vector{elt})) + C = kron(A, B) + @test C isa UnallocatedFill + @test alloctype(C) == alloctype(B) + @test C[1] == elt(6) + end ## The following two tests don't work properly yet - R = Zc + Zc + Z = UnallocatedZeros(Zeros{elt}((2,3)), dev(Matrix{elt})) + R = Z + Z @test_broken R isa UnallocatedZeros - @test_broken alloctype(R) == alloctype(Zc) - R = Zc .+ eltype(Zc)(2.0) + @test_broken alloctype(R) == alloctype(Z) + R = Z .+ eltype(Z)(2.0) @test_broken R isa UnallocatedFill - ######################################### - # UnallocatedFill - f = Fill{elt}(3.0, (2, 3, 4)) - F = UnallocatedFill{elt,ndims(f),typeof(axes(f)),Array{elt,ndims(f)}}(f) - @test F isa AbstractFill - @test size(F) == (2, 3, 4) - @test length(F) == 24 - @test sum(F) ≈ 3 * 24 - @test norm(F) ≈ sqrt(3^2 * 24) - @test F[2, 3, 1] == 3.0 - @test allocate(F) isa Array{elt,3} - Fp = allocate(F) - @test norm(Fp) ≈ norm(F) - - Fp = set_alloctype(f, dev(Array{elt,ndims(f)})) - @test allocate(Fp) isa dev(Array{elt,ndims(f)}) - @test Fp == F - Fc = copy(F) - @test Fc == F - Fc = allocate(complex(F)) - @test eltype(Fc) == complex(eltype(F)) - @test typeof(Fc) == alloctype(complex(F)) - ## This allocates is this correct? - ## TODO this is broken because it doesn't call allocate - Fc[2, 3, 4] = 4.0 + 3.0im - @test Fc[2, 3, 4] == 4.0 + 3.0im end end From e05d5ff782328077611f35468df6bc10b9c84389 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 8 Dec 2023 15:47:37 -0500 Subject: [PATCH 083/156] format --- .../src/UnallocatedArrays.jl | 2 +- .../src/abstractunallocatedarray.jl | 2 +- .../lib/UnallocatedArrays/test/runtests.jl | 25 +++++++++---------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index 9cb77ee463..2e5e9e095c 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -21,7 +21,7 @@ using NDTensors.SetParameters: set_parameter, set_parameters - #using Adapt: Adapt +#using Adapt: Adapt include("abstractfill/abstractfill.jl") include("abstractfill/set_types.jl") diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 96823e31f6..e2eaaa5539 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -25,4 +25,4 @@ end # return set_alloctype(parent(x), to) # end -# function Adapt.adapt_storage(to::Type{<:Number}, x::) \ No newline at end of file +# function Adapt.adapt_storage(to::Type{<:Number}, x::) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index cf9dd5396a..e9cdf0c107 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -9,7 +9,7 @@ include(joinpath(pkgdir(NDTensors), "test", "NDTensorsTestUtils", "NDTensorsTest using .NDTensorsTestUtils: devices_list @testset "Testing UnallocatedArrays" for dev in devices_list(ARGS), - elt in (Float64, Float32, ComplexF64, ComplexF32) + elt in (Float64, Float32, ComplexF64, ComplexF32) @testset "Basic funcitonality" begin z = Zeros{elt}((2, 3)) @@ -82,15 +82,15 @@ using .NDTensorsTestUtils: devices_list ################################### ## UnallocatedFill - f = Fill{elt}(3.0, (2,12)) + f = Fill{elt}(3.0, (2, 12)) F = UnallocatedFill(f, dev(Matrix{elt})) - p = Fill{elt}(4.0, (12,5)) - P = UnallocatedFill(p, dev(Array{elt, ndims(p)})) + p = Fill{elt}(4.0, (12, 5)) + P = UnallocatedFill(p, dev(Array{elt,ndims(p)})) R = F * P @test F isa UnallocatedFill - @test R[1,1] == 144 + @test R[1, 1] == 144 @test alloctype(R) == alloctype(F) - @test size(R) == (2,5) + @test size(R) == (2, 5) end @testset "Broadcast" begin @@ -109,15 +109,15 @@ using .NDTensorsTestUtils: devices_list ######################## # UnallocatedFill - f = Fill(elt(3.0), (2,3,4)) - F = UnallocatedFill(f, Array{elt, ndims(f)}) + f = Fill(elt(3.0), (2, 3, 4)) + F = UnallocatedFill(f, Array{elt,ndims(f)}) F2 = F .* 2 @test F2 isa UnallocatedFill - @test F2[1,1,1] == elt(6.0) + @test F2[1, 1, 1] == elt(6.0) @test alloctype(F2) == alloctype(F) end - ## TODO make other kron tests + ## TODO make other kron tests @testset "Kron" begin A = UnallocatedZeros(Zeros{elt}(2), dev(Vector{elt})) B = UnallocatedZeros(Zeros{elt}(2), dev(Vector{elt})) @@ -133,7 +133,7 @@ using .NDTensorsTestUtils: devices_list C = kron(B, A) @test C isa UnallocatedZeros @test alloctype(C) == alloctype(B) - + A = UnallocatedFill(Fill(elt(3.0), (2)), dev(Vector{elt})) C = kron(A, B) @test C isa UnallocatedFill @@ -142,12 +142,11 @@ using .NDTensorsTestUtils: devices_list end ## The following two tests don't work properly yet - Z = UnallocatedZeros(Zeros{elt}((2,3)), dev(Matrix{elt})) + Z = UnallocatedZeros(Zeros{elt}((2, 3)), dev(Matrix{elt})) R = Z + Z @test_broken R isa UnallocatedZeros @test_broken alloctype(R) == alloctype(Z) R = Z .+ eltype(Z)(2.0) @test_broken R isa UnallocatedFill - end end From 410a8df64723d3d56a6d0d728e0d0a0e1da18c1f Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 8 Dec 2023 15:58:11 -0500 Subject: [PATCH 084/156] Add more tests --- .../lib/UnallocatedArrays/test/runtests.jl | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index e9cdf0c107..4017775ba9 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -115,6 +115,21 @@ using .NDTensorsTestUtils: devices_list @test F2 isa UnallocatedFill @test F2[1, 1, 1] == elt(6.0) @test alloctype(F2) == alloctype(F) + + #F2 .+= elt(2.0) ## This is broken + F2 = F2 .+ elt(2.0) + @test F2 isa UnallocatedFill + @test F2[1,1,1] == elt(8.0) + @test alloctype(F2) == alloctype(F) + + F = UnallocatedFill(Fill(elt(2.0), (2,3)), dev(Matrix{elt})) + R = Z + F + @test R isa UnallocatedFill + @test alloctype(R) == alloctype(Z) + + R = F + Z + @test R isa UnallocatedFill + @test alloctype(R) == alloctype(Z) end ## TODO make other kron tests @@ -146,7 +161,12 @@ using .NDTensorsTestUtils: devices_list R = Z + Z @test_broken R isa UnallocatedZeros @test_broken alloctype(R) == alloctype(Z) - R = Z .+ eltype(Z)(2.0) + R = Z .+ elt(2.0) @test_broken R isa UnallocatedFill + + F = UnallocatedFill(Fill(elt(2),(2,3)), dev(Matrix{elt})) + R = F + F + @test_broken R isa UnallocatedFill + end end From 5e3601e9fa6636d51a0799c85d075a4fb50d5221 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 8 Dec 2023 15:59:22 -0500 Subject: [PATCH 085/156] format --- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 4017775ba9..77529c8537 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -119,10 +119,10 @@ using .NDTensorsTestUtils: devices_list #F2 .+= elt(2.0) ## This is broken F2 = F2 .+ elt(2.0) @test F2 isa UnallocatedFill - @test F2[1,1,1] == elt(8.0) + @test F2[1, 1, 1] == elt(8.0) @test alloctype(F2) == alloctype(F) - F = UnallocatedFill(Fill(elt(2.0), (2,3)), dev(Matrix{elt})) + F = UnallocatedFill(Fill(elt(2.0), (2, 3)), dev(Matrix{elt})) R = Z + F @test R isa UnallocatedFill @test alloctype(R) == alloctype(Z) @@ -164,9 +164,8 @@ using .NDTensorsTestUtils: devices_list R = Z .+ elt(2.0) @test_broken R isa UnallocatedFill - F = UnallocatedFill(Fill(elt(2),(2,3)), dev(Matrix{elt})) + F = UnallocatedFill(Fill(elt(2), (2, 3)), dev(Matrix{elt})) R = F + F @test_broken R isa UnallocatedFill - end end From 4246757e4075636fbba06d5409b2753931f77e82 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 11 Dec 2023 12:09:39 -0500 Subject: [PATCH 086/156] Add more tests for UnallocatedFill --- .../lib/UnallocatedArrays/test/runtests.jl | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 77529c8537..a710582a58 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -80,6 +80,11 @@ using .NDTensorsTestUtils: devices_list @test alloctype(R) == alloctype(Z) @test size(R) == (4, 2) + R = Z .* Z + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Z) + @test R[1,2] == elt(0) + ################################### ## UnallocatedFill f = Fill{elt}(3.0, (2, 12)) @@ -91,6 +96,29 @@ using .NDTensorsTestUtils: devices_list @test R[1, 1] == 144 @test alloctype(R) == alloctype(F) @test size(R) == (2, 5) + + R = F * F' + @test R isa UnallocatedFill + @test R[1,2] == elt(108) + @test alloctype(R) == alloctype(F) + @test size(R) == (2,2) + + R = transpose(F) * F + @test R isa UnallocatedFill + @test R[12,3] == elt(18) + @test alloctype(R) == alloctype(F) + @test size(R) == (12,12) + + R = F .* F + @test R isa UnallocatedFill + @test R[2,9] == elt(9) + @test alloctype(R) == alloctype(F) + @test size(R) == (2,12) + + R = transpose(Z) * F + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Z) + @test size(R) == (3,12) end @testset "Broadcast" begin From 417e025f27d2a29b062a9991187bc63aeadf4bae Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 11 Dec 2023 12:13:45 -0500 Subject: [PATCH 087/156] Move tests to braodcast --- .../lib/UnallocatedArrays/test/runtests.jl | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index a710582a58..3c6b0ccc78 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -80,11 +80,6 @@ using .NDTensorsTestUtils: devices_list @test alloctype(R) == alloctype(Z) @test size(R) == (4, 2) - R = Z .* Z - @test R isa UnallocatedZeros - @test alloctype(R) == alloctype(Z) - @test R[1,2] == elt(0) - ################################### ## UnallocatedFill f = Fill{elt}(3.0, (2, 12)) @@ -109,12 +104,6 @@ using .NDTensorsTestUtils: devices_list @test alloctype(R) == alloctype(F) @test size(R) == (12,12) - R = F .* F - @test R isa UnallocatedFill - @test R[2,9] == elt(9) - @test alloctype(R) == alloctype(F) - @test size(R) == (2,12) - R = transpose(Z) * F @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Z) @@ -158,6 +147,19 @@ using .NDTensorsTestUtils: devices_list R = F + Z @test R isa UnallocatedFill @test alloctype(R) == alloctype(Z) + + F = UnallocatedFill(Fill(elt(3.0), (2,12)), dev(Matrix{elt})) + R = F .* F + @test R isa UnallocatedFill + @test R[2,9] == elt(9) + @test alloctype(R) == alloctype(F) + @test size(R) == (2,12) + + P = UnallocatedFill(Fill(elt(4.0), (2,3)), dev(Matrix{elt})) + R = Z .* P + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(P) + @test size(R) == (2,3) end ## TODO make other kron tests From 4e70bb378011391a4c45667a37d9f3a5c315c606 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 11 Dec 2023 12:21:44 -0500 Subject: [PATCH 088/156] Add more tests --- .../lib/UnallocatedArrays/test/runtests.jl | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 3c6b0ccc78..3f468b9cad 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -1,6 +1,6 @@ @eval module $(gensym()) using FillArrays: FillArrays, AbstractFill, Fill, Zeros -using NDTensors: NDTensors +using NDTensors: NDTensors, Dense, Tensor, array using NDTensors.UnallocatedArrays using LinearAlgebra: norm using Test: @test, @testset, @test_broken @@ -30,6 +30,9 @@ using .NDTensorsTestUtils: devices_list @test eltype(Zc) == complex(eltype(z)) @test Zc[1, 2] == 0.0 + 0.0im + Zs = similar(Z) + @test_broken Zs isa UnallocatedZeros + ######################################### # UnallocatedFill f = Fill{elt}(3.0, (2, 3, 4)) @@ -52,8 +55,9 @@ using .NDTensorsTestUtils: devices_list Fc = allocate(complex(F)) @test eltype(Fc) == complex(eltype(F)) @test typeof(Fc) == alloctype(complex(F)) - ## This allocates is this correct? - ## TODO this is broken because it doesn't call allocate + + ## TODO without prior call to allocate this is broken because it doesn't + ## consider how to form Fc i.e. allocate Fc[2, 3, 4] = 4.0 + 3.0im @test Fc[2, 3, 4] == 4.0 + 3.0im end @@ -61,7 +65,7 @@ using .NDTensorsTestUtils: devices_list @testset "Multiplication" begin z = Zeros{elt}((2, 3)) Z = UnallocatedZeros(z, dev(Matrix{eltype(z)})) - ## Things that are still broken + R = Z * Z' @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Z) @@ -94,20 +98,20 @@ using .NDTensorsTestUtils: devices_list R = F * F' @test R isa UnallocatedFill - @test R[1,2] == elt(108) + @test R[1, 2] == elt(108) @test alloctype(R) == alloctype(F) - @test size(R) == (2,2) + @test size(R) == (2, 2) R = transpose(F) * F @test R isa UnallocatedFill - @test R[12,3] == elt(18) + @test R[12, 3] == elt(18) @test alloctype(R) == alloctype(F) - @test size(R) == (12,12) + @test size(R) == (12, 12) R = transpose(Z) * F @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Z) - @test size(R) == (3,12) + @test size(R) == (3, 12) end @testset "Broadcast" begin @@ -148,18 +152,18 @@ using .NDTensorsTestUtils: devices_list @test R isa UnallocatedFill @test alloctype(R) == alloctype(Z) - F = UnallocatedFill(Fill(elt(3.0), (2,12)), dev(Matrix{elt})) + F = UnallocatedFill(Fill(elt(3.0), (2, 12)), dev(Matrix{elt})) R = F .* F @test R isa UnallocatedFill - @test R[2,9] == elt(9) + @test R[2, 9] == elt(9) @test alloctype(R) == alloctype(F) - @test size(R) == (2,12) + @test size(R) == (2, 12) - P = UnallocatedFill(Fill(elt(4.0), (2,3)), dev(Matrix{elt})) + P = UnallocatedFill(Fill(elt(4.0), (2, 3)), dev(Matrix{elt})) R = Z .* P @test R isa UnallocatedZeros @test alloctype(R) == alloctype(P) - @test size(R) == (2,3) + @test size(R) == (2, 3) end ## TODO make other kron tests @@ -186,6 +190,20 @@ using .NDTensorsTestUtils: devices_list @test C[1] == elt(6) end + @testset "Tensor" begin + Z = UnallocatedZeros(Zeros{elt}(6), dev(Vector{elt})) + D = Dense(Z) + @test D isa Dense{elt,UnallocatedZeros{elt,1,Tuple{Base.OneTo{Int64}},Vector{elt}}} + @test D[3] == elt(0) + R = D .* D + @test_broken R isa + Dense{elt,UnallocatedZeros{elt,1,Tuple{Base.OneTo{Int64}},Vector{elt}}} + + T = Tensor(D, (2, 3)) + @test T[1, 2] == zero(elt) + #T * transpose(T) + end + ## The following two tests don't work properly yet Z = UnallocatedZeros(Zeros{elt}((2, 3)), dev(Matrix{elt})) R = Z + Z From 52e21b041772e7515212d47530f3df3e517855df Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 11 Dec 2023 12:22:07 -0500 Subject: [PATCH 089/156] Create a trival convert foreach UnallocatedArray --- NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl | 5 +++++ NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 433b06e048..920fb80f43 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -22,6 +22,11 @@ set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) = UnallocatedFill(f, alloc) Base.parent(F::UnallocatedFill) = F.f +Base.convert(::Type{<:UnallocatedFill}, A::UnallocatedFill) = A + +############################################# +# Arithmatic + # mult_fill(a, b, val, ax) = Fill(val, ax) function FillArrays.mult_fill(a::UnallocatedFill, b, val, ax) return UnallocatedFill(Fill(val, ax), alloctype(a)) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index f85411d153..1dea139a98 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -21,6 +21,11 @@ set_alloctype(f::Zeros, alloc::Type{<:AbstractArray}) = UnallocatedZeros(f, allo Base.parent(Z::UnallocatedZeros) = Z.z +Base.convert(::Type{<:UnallocatedZeros}, A::UnallocatedZeros) = A + +############################################# +# Arithmatic + function FillArrays.mult_zeros(a::UnallocatedZeros, b, elt, ax) return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end From d874b1b4ba5f932794d0faaca09209508537aae0 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 11:29:32 -0500 Subject: [PATCH 090/156] Remove promote_rules --- .../UnallocatedArrays/src/promote_rules.jl | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 NDTensors/src/lib/UnallocatedArrays/src/promote_rules.jl diff --git a/NDTensors/src/lib/UnallocatedArrays/src/promote_rules.jl b/NDTensors/src/lib/UnallocatedArrays/src/promote_rules.jl deleted file mode 100644 index 2ac0bf01ff..0000000000 --- a/NDTensors/src/lib/UnallocatedArrays/src/promote_rules.jl +++ /dev/null @@ -1,22 +0,0 @@ -## TODO this section is not finished. I just pasted this from the previous PR. -## Still working here -function Base.promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:UnallocatedZeros}) - ElT = promote_type(eltype(z1), eltype(z2)) - @assert ndims(z1) == ndims(z2) - Axs = axes(z1) - Alloc = promote_type(alloctype(z1), alloctype(z2)) - return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} -end - -function Base.promote_rule(z1::Type{<:UnallocatedZeros}, z2::Type{<:AbstractArray}) - ElT = promote_type(eltype(z1), eltype(z2)) - @assert ndims(z1) == ndims(z2) - Axs = axes(z1) - Alloc = promote_type(alloctype(z1), z2) - set_eltype(Alloc, ElT) - return UnallocatedZeros{ElT,ndims(z1),Axs,Alloc} -end - -function Base.promote_rule(z1::Type{<:AbstractArray}, z2::Type{<:UnallocatedZeros}) - return promote_rule(z2, z1) -end From 611420b279c9905ed8a4153b64fbbed9ead45c7c Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 13:10:50 -0500 Subject: [PATCH 091/156] Move using to each file --- .../src/UnallocatedArrays.jl | 24 ------------------- .../src/abstractfill/abstractfill.jl | 2 ++ .../src/abstractfill/set_types.jl | 5 +++- .../src/abstractunallocatedarray.jl | 2 ++ .../lib/UnallocatedArrays/src/set_types.jl | 7 ++++-- .../UnallocatedArrays/src/unallocatedfill.jl | 5 ++-- .../UnallocatedArrays/src/unallocatedzeros.jl | 2 ++ 7 files changed, 18 insertions(+), 29 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index 2e5e9e095c..ed2aa9d1b4 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,28 +1,4 @@ module UnallocatedArrays -using FillArrays: - FillArrays, - AbstractFill, - AbstractZeros, - Fill, - Zeros, - broadcasted_zeros, - broadcasted_fill, - fill_add, - getindex_value, - kron_fill, - mult_zeros - -using NDTensors.SetParameters: - SetParameters, - Position, - default_parameter, - get_parameter, - nparameters, - set_parameter, - set_parameters - -#using Adapt: Adapt - include("abstractfill/abstractfill.jl") include("abstractfill/set_types.jl") diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl index 0648ec4f46..82edf5f7d9 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl @@ -1,3 +1,5 @@ +using FillArrays: AbstractFill +using NDTensors.SetParameters: Position, get_parameter, set_parameters ## Here are functions specifically defined for UnallocatedArrays ## not implemented by FillArrays ## TODO determine min number of functions needed to be forwarded diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl index 8ff9e944de..985e57885d 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl @@ -1,3 +1,6 @@ +using FillArrays: AbstractFill +using NDTensors.SetParameters: SetParameters, Position +using NDTensors.UnspecifiedTypes: UnspecifiedZero ## TODO make unit tests for all of these functions ## TODO remove P4 # `SetParameters.jl` overloads. @@ -32,7 +35,7 @@ end ## To quickly specify P1, P2, and P3 ## default parameters function SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{1}) - return UnspecifiedTypes.UnallocatedZeros + return UnspecifiedTypes.UnspecifiedZero end SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{2}) = 0 SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{3}) = Tuple{} diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index e2eaaa5539..828a237c49 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -1,3 +1,5 @@ +using FillArrays: FillArrays, getindex_value +using NDTensors.SetParameters: set_parameters @inline Base.axes(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = axes(parent(A)) Base.size(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = size(parent(A)) function FillArrays.getindex_value(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl index b2254bbe82..dbb9e217f1 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl @@ -1,3 +1,5 @@ +using NDTensors.SetParameters: SetParameters, Position +using NDTensors.UnspecifiedTypes: UnspecifiedArray, UnspecifiedNumber, UnspecifiedZero # ## TODO make unit tests for all of these functions ## TODO All I need to do is overload AbstractFill functions with 4 parameters # `SetParameters.jl` overloads. @@ -25,11 +27,12 @@ function SetParameters.set_parameter( end # ## default parameters +## TODO add info for eltype and N function SetParameters.default_parameter(::Type{<:UnallocatedFill}, ::Position{4}) - return UnspecifiedTypes.UnspecifiedArray + return UnspecifiedArray{UnspecifiedNumber{UnspecifiedZero}, 0} end function SetParameters.default_parameter(::Type{<:UnallocatedZeros}, ::Position{4}) - return UnspecifiedTypes.UnspecifiedArray + return UnspecifiedArray{UnspecifiedNumber{UnspecifiedZero}, 0} end SetParameters.nparameters(::Type{<:UnallocatedFill}) = Val(4) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 920fb80f43..b4c5f25412 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -1,5 +1,6 @@ -## TODO All constructors not fully implemented but so far it matches the -## constructors found in `FillArrays`. Need to fix io +using FillArrays: FillArrays, AbstractFill, Fill, broadcasted_fill, kron_fill, mult_fill +using NDTensors.SetParameters: Position, set_parameters + struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: AbstractFill{ElT,N,Axes} f::Fill{ElT,N,Axes} end diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 1dea139a98..a6891d0312 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -1,3 +1,5 @@ +using FillArrays: FillArrays, AbstractZeros, Zeros, broadcasted_zeros, kron_fill, kron_zeros, mult_zeros +using NDTensors.SetParameters: Position, set_parameters ## TODO Should Alloc also be of ElT and N or should there be ## More freedom there? struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: AbstractZeros{ElT,N,Axes} From 6011fbc3fc3358feaae8b96614d226ada14377c4 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 13:40:12 -0500 Subject: [PATCH 092/156] Create an unallocatedArray typedef --- .../src/abstractunallocatedarray.jl | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 828a237c49..e3b15418a8 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -1,25 +1,33 @@ using FillArrays: FillArrays, getindex_value using NDTensors.SetParameters: set_parameters -@inline Base.axes(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = axes(parent(A)) -Base.size(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) = size(parent(A)) -function FillArrays.getindex_value(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) +using Adapt: adapt + +const UnallocatedArray{ElT, N, AxesT, AllocT} = Union{<:UnallocatedFill{ElT, N, AxesT, AllocT},<:UnallocatedZeros{ElT, N, AxesT, AllocT}} + +@inline Base.axes(A::UnallocatedArray) = axes(parent(A)) +Base.size(A::UnallocatedArray) = size(parent(A)) +function FillArrays.getindex_value(A::UnallocatedArray) return getindex_value(parent(A)) end -function Base.complex(A::Union{<:UnallocatedFill,<:UnallocatedZeros}) +function Base.complex(A::UnallocatedArray) return set_alloctype( complex(parent(A)), set_parameters(alloctype(A), Position{1}(), complex(eltype(A))) ) end -function Base.transpose(a::Union{<:UnallocatedFill,<:UnallocatedZeros}) +function Base.transpose(a::UnallocatedArray) return set_alloctype(transpose(parent(a)), alloctype(a)) end -function Base.adjoint(a::Union{<:UnallocatedFill,<:UnallocatedZeros}) +function Base.adjoint(a::UnallocatedArray) return set_alloctype(adjoint(parent(a)), alloctype(a)) end +function Base.similar(a::UnallocatedArray) + return alloctype(a)(undef, size(a)) +end + ## TODO fix this because reshape loses alloctype #FillArrays.reshape(a::Union{<:UnallocatedFill, <:UnallocatedZeros}, dims) = set_alloctype(reshape(parent(a), dims), allocate(a)) From bcf89f1e54174d9863f5065afb486c4e514b1b00 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 13:40:24 -0500 Subject: [PATCH 093/156] Remove comment --- NDTensors/src/lib/UnallocatedArrays/src/set_types.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl index dbb9e217f1..97721a6c14 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl @@ -27,7 +27,6 @@ function SetParameters.set_parameter( end # ## default parameters -## TODO add info for eltype and N function SetParameters.default_parameter(::Type{<:UnallocatedFill}, ::Position{4}) return UnspecifiedArray{UnspecifiedNumber{UnspecifiedZero}, 0} end From a6b9ec7ce1171ad85b38faa4ecb70e54b493dbd6 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 15:17:32 -0500 Subject: [PATCH 094/156] Definitions to fix UnallocatedZeros addition --- .../lib/UnallocatedArrays/src/unallocatedzeros.jl | 13 ++++++++++++- .../src/lib/UnallocatedArrays/test/runtests.jl | 9 +++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index a6891d0312..627e906e06 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -1,4 +1,4 @@ -using FillArrays: FillArrays, AbstractZeros, Zeros, broadcasted_zeros, kron_fill, kron_zeros, mult_zeros +using FillArrays: FillArrays, AbstractZeros, Fill, Zeros, broadcasted_fill, broadcasted_zeros, kron_fill, kron_zeros, mult_zeros using NDTensors.SetParameters: Position, set_parameters ## TODO Should Alloc also be of ElT and N or should there be ## More freedom there? @@ -52,6 +52,17 @@ function FillArrays.broadcasted_zeros(f, a, b::UnallocatedZeros, elt, ax) return broadcasted_zeros(f, b, a, elt, ax) end +function FillArrays.broadcasted_fill(f, a::UnallocatedZeros, val, ax) + return UnallocatedFill(Fill(val, ax), alloctype(a)) +end +function FillArrays.broadcasted_fill(f, a::UnallocatedZeros, b, val, ax) + return UnallocatedFill(Fill(val, ax), alloctype(a)) +end + +function FillArrays.broadcasted_fill(f, a, b::UnallocatedZeros, val, ax) + return broadcasted_fill(f, b, a, val, ax) +end + function FillArrays.kron_zeros(a::UnallocatedZeros, b::UnallocatedZeros, elt, ax) @assert alloctype(a) == alloctype(b) return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 3f468b9cad..04c49b3524 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -128,6 +128,9 @@ using .NDTensorsTestUtils: devices_list @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Z) + R = Z .+ elt(2.0) + @test R isa UnallocatedFill + @test alloctype(R) == alloctype(Z) ######################## # UnallocatedFill f = Fill(elt(3.0), (2, 3, 4)) @@ -205,12 +208,6 @@ using .NDTensorsTestUtils: devices_list end ## The following two tests don't work properly yet - Z = UnallocatedZeros(Zeros{elt}((2, 3)), dev(Matrix{elt})) - R = Z + Z - @test_broken R isa UnallocatedZeros - @test_broken alloctype(R) == alloctype(Z) - R = Z .+ elt(2.0) - @test_broken R isa UnallocatedFill F = UnallocatedFill(Fill(elt(2), (2, 3)), dev(Matrix{elt})) R = F + F From 03655d0f1e30063713a0a659c0a5d9a13adb804f Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 15:18:50 -0500 Subject: [PATCH 095/156] Some additional fixes --- .../src/abstractfill/abstractfill.jl | 2 +- .../UnallocatedArrays/src/abstractunallocatedarray.jl | 11 +++++++++++ .../src/lib/UnallocatedArrays/src/unallocatedfill.jl | 6 +----- .../src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 6 +----- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 5 +++++ 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl index 82edf5f7d9..86f0614936 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl @@ -12,4 +12,4 @@ allocate(A::AbstractFill) = alloctype(A)(parent(A)) set_eltype(T::Type{<:AbstractFill}, elt::Type) = set_parameters(T, Position{1}(), elt) set_ndims(T::Type{<:AbstractFill}, n) = set_parameters(T, Position{2}(), n) -set_axes(T::Type{<:AbstractFill}, ax::Type) = set_parameters(T, Position{3}(), ax) +set_axestype(T::Type{<:AbstractFill}, ax::Type) = set_parameters(T, Position{3}(), ax) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index e3b15418a8..4fde917f9a 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -28,6 +28,17 @@ function Base.similar(a::UnallocatedArray) return alloctype(a)(undef, size(a)) end +function set_alloctype(T::Type{<:UnallocatedArray}, alloc::Type{<:AbstractArray}) + return set_parameters(T, Position{4}(), alloc) +end + +for STYPE in (:AbstractArray, :AbstractFill) + @eval begin + @inline $STYPE{T}(F::UnallocatedArray{T}) where T = F + @inline $STYPE{T,N}(F::UnallocatedArray{T,N}) where {T,N} = F + end +end + ## TODO fix this because reshape loses alloctype #FillArrays.reshape(a::Union{<:UnallocatedFill, <:UnallocatedZeros}, dims) = set_alloctype(reshape(parent(a), dims), allocate(a)) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index b4c5f25412..794837bab4 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -8,17 +8,13 @@ end ## TODO use `set_parameters` as constructor to these types function UnallocatedFill(f::Fill, alloc::Type{<:AbstractArray}) return set_alloctype( - set_axes(set_ndims(set_eltype(UnallocatedFill, eltype(f)), ndims(f)), typeof(axes(f))), + set_axestype(set_ndims(set_eltype(UnallocatedFill, eltype(f)), ndims(f)), typeof(axes(f))), alloc, )( f ) end -function set_alloctype(T::Type{<:UnallocatedFill}, alloc::Type{<:AbstractArray}) - return set_parameters(T, Position{4}(), alloc) -end - set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) = UnallocatedFill(f, alloc) Base.parent(F::UnallocatedFill) = F.f diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 627e906e06..a290d9dd21 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -8,17 +8,13 @@ end function UnallocatedZeros(f::Zeros, alloc::Type{<:AbstractArray}) return set_alloctype( - set_axes(set_ndims(set_eltype(UnallocatedZeros, eltype(f)), ndims(f)), typeof(axes(f))), + set_axestype(set_ndims(set_eltype(UnallocatedZeros, eltype(f)), ndims(f)), typeof(axes(f))), alloc, )( f ) end -function set_alloctype(T::Type{<:UnallocatedZeros}, alloc::Type{<:AbstractArray}) - return set_parameters(T, Position{4}(), alloc) -end - set_alloctype(f::Zeros, alloc::Type{<:AbstractArray}) = UnallocatedZeros(f, alloc) Base.parent(Z::UnallocatedZeros) = Z.z diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 04c49b3524..892ac88ecf 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -128,6 +128,11 @@ using .NDTensorsTestUtils: devices_list @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Z) + Z = UnallocatedZeros(Zeros{elt}((2, 3)), dev(Matrix{elt})) + R = Z + Z + @test R isa UnallocatedZeros + @test alloctype(R) == alloctype(Z) + R = Z .+ elt(2.0) @test R isa UnallocatedFill @test alloctype(R) == alloctype(Z) From e9ed6c55b06470948b56193b4d9ad700f7ce22a4 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 15:19:03 -0500 Subject: [PATCH 096/156] Remove tensor tests --- .../src/lib/UnallocatedArrays/test/runtests.jl | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 892ac88ecf..0e22af5218 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -198,20 +198,6 @@ using .NDTensorsTestUtils: devices_list @test C[1] == elt(6) end - @testset "Tensor" begin - Z = UnallocatedZeros(Zeros{elt}(6), dev(Vector{elt})) - D = Dense(Z) - @test D isa Dense{elt,UnallocatedZeros{elt,1,Tuple{Base.OneTo{Int64}},Vector{elt}}} - @test D[3] == elt(0) - R = D .* D - @test_broken R isa - Dense{elt,UnallocatedZeros{elt,1,Tuple{Base.OneTo{Int64}},Vector{elt}}} - - T = Tensor(D, (2, 3)) - @test T[1, 2] == zero(elt) - #T * transpose(T) - end - ## The following two tests don't work properly yet F = UnallocatedFill(Fill(elt(2), (2, 3)), dev(Matrix{elt})) From 0c03fab471cec45f808b6ef061cd3e04c59b57af Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 15:19:29 -0500 Subject: [PATCH 097/156] Test similar --- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 0e22af5218..0df852bd48 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -31,7 +31,7 @@ using .NDTensorsTestUtils: devices_list @test Zc[1, 2] == 0.0 + 0.0im Zs = similar(Z) - @test_broken Zs isa UnallocatedZeros + @test Zs isa alloctype(Z) ######################################### # UnallocatedFill @@ -46,6 +46,9 @@ using .NDTensorsTestUtils: devices_list @test allocate(F) isa Array{elt,3} Fp = allocate(F) @test norm(Fp) ≈ norm(F) + Fs = similar(F) + @test Fs isa alloctype(F) + @test Fs[1,1,1] != 3.0 Fp = set_alloctype(f, dev(Array{elt,ndims(f)})) @test allocate(Fp) isa dev(Array{elt,ndims(f)}) From 2a04e0a2a0b6c6b8c5fe2357ca385c360391f177 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 15:19:50 -0500 Subject: [PATCH 098/156] Remove NDTensor variables --- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 0df852bd48..73140dd5bd 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -1,6 +1,6 @@ @eval module $(gensym()) using FillArrays: FillArrays, AbstractFill, Fill, Zeros -using NDTensors: NDTensors, Dense, Tensor, array +using NDTensors: NDTensors using NDTensors.UnallocatedArrays using LinearAlgebra: norm using Test: @test, @testset, @test_broken From 24f185cf6ca6e6df1662dd4c59c4478ed97c178c Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 15:20:08 -0500 Subject: [PATCH 099/156] Remove comment --- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 73140dd5bd..17bb874824 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -58,9 +58,6 @@ using .NDTensorsTestUtils: devices_list Fc = allocate(complex(F)) @test eltype(Fc) == complex(eltype(F)) @test typeof(Fc) == alloctype(complex(F)) - - ## TODO without prior call to allocate this is broken because it doesn't - ## consider how to form Fc i.e. allocate Fc[2, 3, 4] = 4.0 + 3.0im @test Fc[2, 3, 4] == 4.0 + 3.0im end From b64372dbe46db7061e26ccf5ac8d83c27e132ed2 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 15:38:02 -0500 Subject: [PATCH 100/156] Add comment --- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 17bb874824..72a272eab8 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -200,6 +200,7 @@ using .NDTensorsTestUtils: devices_list ## The following two tests don't work properly yet + ### TODO this is broken because +(a::AbstractFill, b::AbstractFill) = Fill(...) F = UnallocatedFill(Fill(elt(2), (2, 3)), dev(Matrix{elt})) R = F + F @test_broken R isa UnallocatedFill From afebe2aacc55aa641ab5b34fba3e635ce5ddc4ac Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 15:44:47 -0500 Subject: [PATCH 101/156] format --- .../src/abstractunallocatedarray.jl | 9 ++++++--- .../src/lib/UnallocatedArrays/src/set_types.jl | 4 ++-- .../lib/UnallocatedArrays/src/unallocatedfill.jl | 4 +++- .../lib/UnallocatedArrays/src/unallocatedzeros.jl | 15 +++++++++++++-- .../src/lib/UnallocatedArrays/test/runtests.jl | 4 +++- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 4fde917f9a..8d981eb384 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -2,7 +2,9 @@ using FillArrays: FillArrays, getindex_value using NDTensors.SetParameters: set_parameters using Adapt: adapt -const UnallocatedArray{ElT, N, AxesT, AllocT} = Union{<:UnallocatedFill{ElT, N, AxesT, AllocT},<:UnallocatedZeros{ElT, N, AxesT, AllocT}} +const UnallocatedArray{ElT,N,AxesT,AllocT} = Union{ + <:UnallocatedFill{ElT,N,AxesT,AllocT},<:UnallocatedZeros{ElT,N,AxesT,AllocT} +} @inline Base.axes(A::UnallocatedArray) = axes(parent(A)) Base.size(A::UnallocatedArray) = size(parent(A)) @@ -32,10 +34,11 @@ function set_alloctype(T::Type{<:UnallocatedArray}, alloc::Type{<:AbstractArray} return set_parameters(T, Position{4}(), alloc) end +## This overloads the definition defined in `FillArrays.jl` for STYPE in (:AbstractArray, :AbstractFill) @eval begin - @inline $STYPE{T}(F::UnallocatedArray{T}) where T = F - @inline $STYPE{T,N}(F::UnallocatedArray{T,N}) where {T,N} = F + @inline $STYPE{T}(F::UnallocatedArray{T}) where {T} = F + @inline $STYPE{T,N}(F::UnallocatedArray{T,N}) where {T,N} = F end end diff --git a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl index 97721a6c14..6f4bfcbc34 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl @@ -28,10 +28,10 @@ end # ## default parameters function SetParameters.default_parameter(::Type{<:UnallocatedFill}, ::Position{4}) - return UnspecifiedArray{UnspecifiedNumber{UnspecifiedZero}, 0} + return UnspecifiedArray{UnspecifiedNumber{UnspecifiedZero},0} end function SetParameters.default_parameter(::Type{<:UnallocatedZeros}, ::Position{4}) - return UnspecifiedArray{UnspecifiedNumber{UnspecifiedZero}, 0} + return UnspecifiedArray{UnspecifiedNumber{UnspecifiedZero},0} end SetParameters.nparameters(::Type{<:UnallocatedFill}) = Val(4) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 794837bab4..e08eca324f 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -8,7 +8,9 @@ end ## TODO use `set_parameters` as constructor to these types function UnallocatedFill(f::Fill, alloc::Type{<:AbstractArray}) return set_alloctype( - set_axestype(set_ndims(set_eltype(UnallocatedFill, eltype(f)), ndims(f)), typeof(axes(f))), + set_axestype( + set_ndims(set_eltype(UnallocatedFill, eltype(f)), ndims(f)), typeof(axes(f)) + ), alloc, )( f diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index a290d9dd21..dc0a0a13e3 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -1,4 +1,13 @@ -using FillArrays: FillArrays, AbstractZeros, Fill, Zeros, broadcasted_fill, broadcasted_zeros, kron_fill, kron_zeros, mult_zeros +using FillArrays: + FillArrays, + AbstractZeros, + Fill, + Zeros, + broadcasted_fill, + broadcasted_zeros, + kron_fill, + kron_zeros, + mult_zeros using NDTensors.SetParameters: Position, set_parameters ## TODO Should Alloc also be of ElT and N or should there be ## More freedom there? @@ -8,7 +17,9 @@ end function UnallocatedZeros(f::Zeros, alloc::Type{<:AbstractArray}) return set_alloctype( - set_axestype(set_ndims(set_eltype(UnallocatedZeros, eltype(f)), ndims(f)), typeof(axes(f))), + set_axestype( + set_ndims(set_eltype(UnallocatedZeros, eltype(f)), ndims(f)), typeof(axes(f)) + ), alloc, )( f diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 72a272eab8..49365989f3 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -48,7 +48,7 @@ using .NDTensorsTestUtils: devices_list @test norm(Fp) ≈ norm(F) Fs = similar(F) @test Fs isa alloctype(F) - @test Fs[1,1,1] != 3.0 + @test Fs[1, 1, 1] != 3.0 Fp = set_alloctype(f, dev(Array{elt,ndims(f)})) @test allocate(Fp) isa dev(Array{elt,ndims(f)}) @@ -60,6 +60,8 @@ using .NDTensorsTestUtils: devices_list @test typeof(Fc) == alloctype(complex(F)) Fc[2, 3, 4] = 4.0 + 3.0im @test Fc[2, 3, 4] == 4.0 + 3.0im + ## TODO this isn't working + #Fc[2,3,4] = Base.setindex(F, 4.0+3.0im, 2,3,4) end @testset "Multiplication" begin From 5bd1fd3e0b8d4d8bcf792900de3673b08e36abc6 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 12 Dec 2023 18:08:41 -0500 Subject: [PATCH 102/156] Update constructors and fix + for UnallocatedFill --- .../src/abstractfill/abstractfill.jl | 7 ++- .../UnallocatedArrays/src/unallocatedfill.jl | 54 +++++++++++++++---- .../UnallocatedArrays/src/unallocatedzeros.jl | 30 +++++++---- .../lib/UnallocatedArrays/test/runtests.jl | 11 ++-- 4 files changed, 73 insertions(+), 29 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl index 86f0614936..87b17134b7 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl @@ -3,7 +3,12 @@ using NDTensors.SetParameters: Position, get_parameter, set_parameters ## Here are functions specifically defined for UnallocatedArrays ## not implemented by FillArrays ## TODO determine min number of functions needed to be forwarded -alloctype(A::AbstractFill) = alloctype(typeof(A)) +function alloctype(A::AbstractFill) + al = A.alloc + return (al isa Type ? al : typeof(al)) +end + +## TODO this fails if the parameter is a type function alloctype(Atype::Type{<:AbstractFill}) return get_parameter(Atype, Position{4}()) end diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index e08eca324f..b223dc0935 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -1,20 +1,30 @@ using FillArrays: FillArrays, AbstractFill, Fill, broadcasted_fill, kron_fill, mult_fill using NDTensors.SetParameters: Position, set_parameters +import Base: + -struct UnallocatedFill{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: AbstractFill{ElT,N,Axes} +struct UnallocatedFill{ElT,N,Axes,Alloc} <: AbstractFill{ElT,N,Axes} f::Fill{ElT,N,Axes} + alloc::Alloc end -## TODO use `set_parameters` as constructor to these types -function UnallocatedFill(f::Fill, alloc::Type{<:AbstractArray}) - return set_alloctype( - set_axestype( - set_ndims(set_eltype(UnallocatedFill, eltype(f)), ndims(f)), typeof(axes(f)) - ), - alloc, - )( - f - ) +function UnallocatedFill{ElT,N,Axes}(f::Fill, alloc::Type) where {ElT,N,Axes} + return UnallocatedFill{ElT,N,Axes,Type{alloc}}(f, alloc) +end + +function UnallocatedFill{ElT,N,Axes}(f::Fill, alloc) where {ElT,N,Axes} + return UnallocatedFill{ElT,N,Axes,typeof(alloc)}(f, alloc) +end + +function UnallocatedFill{ElT,N}(f::Fill, alloc) where {ElT,N} + return UnallocatedFill{ElT,N,typeof(axes(f))}(f, alloc) +end + +function UnallocatedFill{ElT}(f::Fill, alloc) where {ElT} + return UnallocatedFill{ElT,ndims(f)}(f, alloc) +end + +function UnallocatedFill(f::Fill, alloc) + return UnallocatedFill{eltype(f)}(f, alloc) end set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) = UnallocatedFill(f, alloc) @@ -55,3 +65,25 @@ function FillArrays.kron_fill(a::UnallocatedFill, b::UnallocatedFill, val, ax) @assert alloctype(a) == alloctype(b) return UnallocatedFill(Fill(val, ax), alloctype(a)) end + +function +(A::UnallocatedFill, B::UnallocatedFill) + FillArrays.promote_shape(A, B) + b = getindex_value(B) + return A .+ b +end + +# #### +# ## TODO use `set_parameters` as constructor to these types +# function UnallocatedFill(f::Fill, alloc::Type{<:AbstractArray}) +# ## set_axes -> set_axes_type +# return set_alloctype( +# set_axes(set_ndims(set_eltype(UnallocatedFill, eltype(f)), ndims(f)), typeof(axes(f))), +# alloc, +# )( +# f +# ) +# end + +## Things to fix +## in different Change syntax of set_xxx_if_unspecified +# adapt diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index dc0a0a13e3..dd39344d01 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -11,19 +11,29 @@ using FillArrays: using NDTensors.SetParameters: Position, set_parameters ## TODO Should Alloc also be of ElT and N or should there be ## More freedom there? -struct UnallocatedZeros{ElT,N,Axes,Alloc<:AbstractArray{ElT,N}} <: AbstractZeros{ElT,N,Axes} +struct UnallocatedZeros{ElT,N,Axes,Alloc} <: AbstractZeros{ElT,N,Axes} z::Zeros{ElT,N,Axes} + alloc::Alloc end -function UnallocatedZeros(f::Zeros, alloc::Type{<:AbstractArray}) - return set_alloctype( - set_axestype( - set_ndims(set_eltype(UnallocatedZeros, eltype(f)), ndims(f)), typeof(axes(f)) - ), - alloc, - )( - f - ) +function UnallocatedZeros{ElT,N,Axes}(z::Zeros, alloc::Type) where {ElT,N,Axes} + return UnallocatedZeros{ElT,N,Axes,Type{alloc}}(z, alloc) +end + +function UnallocatedZeros{ElT,N,Axes}(z::Zeros, alloc) where {ElT,N,Axes} + return UnallocatedZeros{ElT,N,Axes,typeof(alloc)}(z, alloc) +end + +function UnallocatedZeros{ElT,N}(z::Zeros, alloc) where {ElT,N} + return UnallocatedZeros{ElT,N,typeof(axes(z))}(z, alloc) +end + +function UnallocatedZeros{ElT}(z::Zeros, alloc) where {ElT} + return UnallocatedZeros{ElT,ndims(z)}(z, alloc) +end + +function UnallocatedZeros(z::Zeros, alloc) + return UnallocatedZeros{eltype(z)}(z, alloc) end set_alloctype(f::Zeros, alloc::Type{<:AbstractArray}) = UnallocatedZeros(f, alloc) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 49365989f3..f1236673d3 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -174,6 +174,10 @@ using .NDTensorsTestUtils: devices_list @test R isa UnallocatedZeros @test alloctype(R) == alloctype(P) @test size(R) == (2, 3) + + F = UnallocatedFill(Fill(elt(2), (2, 3)), dev(Matrix{elt})) + R = F + F + @test R isa UnallocatedFill end ## TODO make other kron tests @@ -199,12 +203,5 @@ using .NDTensorsTestUtils: devices_list @test alloctype(C) == alloctype(B) @test C[1] == elt(6) end - - ## The following two tests don't work properly yet - - ### TODO this is broken because +(a::AbstractFill, b::AbstractFill) = Fill(...) - F = UnallocatedFill(Fill(elt(2), (2, 3)), dev(Matrix{elt})) - R = F + F - @test_broken R isa UnallocatedFill end end From a41a70c0f80e91e892183ac667fe03aa0857747c Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Wed, 13 Dec 2023 12:27:58 -0500 Subject: [PATCH 103/156] Make only one constructor for now that requires an abstractarray type --- .../src/lib/UnallocatedArrays/src/unallocatedfill.jl | 10 +++------- .../lib/UnallocatedArrays/src/unallocatedzeros.jl | 12 +++++------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index b223dc0935..508192fdb7 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -5,14 +5,10 @@ import Base: + struct UnallocatedFill{ElT,N,Axes,Alloc} <: AbstractFill{ElT,N,Axes} f::Fill{ElT,N,Axes} alloc::Alloc -end - -function UnallocatedFill{ElT,N,Axes}(f::Fill, alloc::Type) where {ElT,N,Axes} - return UnallocatedFill{ElT,N,Axes,Type{alloc}}(f, alloc) -end -function UnallocatedFill{ElT,N,Axes}(f::Fill, alloc) where {ElT,N,Axes} - return UnallocatedFill{ElT,N,Axes,typeof(alloc)}(f, alloc) + function UnallocatedFill{ElT,N,Axes}(f::Fill, alloc::Type{<:AbstractArray{ElT, N}}) where {ElT,N,Axes} + return new{ElT, N, Axes, Type{alloc}}(f, alloc) + end end function UnallocatedFill{ElT,N}(f::Fill, alloc) where {ElT,N} diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index dd39344d01..7ea1e9c15a 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -14,16 +14,14 @@ using NDTensors.SetParameters: Position, set_parameters struct UnallocatedZeros{ElT,N,Axes,Alloc} <: AbstractZeros{ElT,N,Axes} z::Zeros{ElT,N,Axes} alloc::Alloc -end - -function UnallocatedZeros{ElT,N,Axes}(z::Zeros, alloc::Type) where {ElT,N,Axes} - return UnallocatedZeros{ElT,N,Axes,Type{alloc}}(z, alloc) -end -function UnallocatedZeros{ElT,N,Axes}(z::Zeros, alloc) where {ElT,N,Axes} - return UnallocatedZeros{ElT,N,Axes,typeof(alloc)}(z, alloc) + function UnallocatedZeros{ElT,N,Axes}(z::Zeros, alloc::Type{<:AbstractArray{ElT, N}}) where {ElT,N,Axes} + return new{ElT, N, Axes, Type{alloc}}(z, alloc) + #return UnallocatedZeros{ElT,N,Axes,Type{alloc}}(z, alloc) + end end +## TODO make this an error until its fully defined function UnallocatedZeros{ElT,N}(z::Zeros, alloc) where {ElT,N} return UnallocatedZeros{ElT,N,typeof(axes(z))}(z, alloc) end From 4bd30283c96b30ba57ac4be847bb036aa4b87b42 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Wed, 13 Dec 2023 12:29:37 -0500 Subject: [PATCH 104/156] Assue alloc defines constructor for type --- .../lib/UnallocatedArrays/src/abstractfill/abstractfill.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl index 87b17134b7..2de5efe1d7 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl @@ -2,10 +2,9 @@ using FillArrays: AbstractFill using NDTensors.SetParameters: Position, get_parameter, set_parameters ## Here are functions specifically defined for UnallocatedArrays ## not implemented by FillArrays -## TODO determine min number of functions needed to be forwarded +## TODO this might need a more generic name maybe like compute unit function alloctype(A::AbstractFill) - al = A.alloc - return (al isa Type ? al : typeof(al)) + return A.alloc end ## TODO this fails if the parameter is a type @@ -17,4 +16,4 @@ allocate(A::AbstractFill) = alloctype(A)(parent(A)) set_eltype(T::Type{<:AbstractFill}, elt::Type) = set_parameters(T, Position{1}(), elt) set_ndims(T::Type{<:AbstractFill}, n) = set_parameters(T, Position{2}(), n) -set_axestype(T::Type{<:AbstractFill}, ax::Type) = set_parameters(T, Position{3}(), ax) +set_axestype(T::Type{<:AbstractFill}, ax::Type) = set_parameters(T, Position{3}(), ax) \ No newline at end of file From cd4262884da9c8b8191c8dfe9f2c493bb7007518 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Wed, 13 Dec 2023 12:30:12 -0500 Subject: [PATCH 105/156] Remove comment --- NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 7ea1e9c15a..4799e2e107 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -17,7 +17,6 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc} <: AbstractZeros{ElT,N,Axes} function UnallocatedZeros{ElT,N,Axes}(z::Zeros, alloc::Type{<:AbstractArray{ElT, N}}) where {ElT,N,Axes} return new{ElT, N, Axes, Type{alloc}}(z, alloc) - #return UnallocatedZeros{ElT,N,Axes,Type{alloc}}(z, alloc) end end From 16daf494ce822606f094561ae9c1a703788f88dc Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Wed, 13 Dec 2023 12:30:28 -0500 Subject: [PATCH 106/156] remove comment --- NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 4799e2e107..771ae88df9 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -20,7 +20,6 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc} <: AbstractZeros{ElT,N,Axes} end end -## TODO make this an error until its fully defined function UnallocatedZeros{ElT,N}(z::Zeros, alloc) where {ElT,N} return UnallocatedZeros{ElT,N,typeof(axes(z))}(z, alloc) end From 2693dfc2425b435243bb01576e6154c77ea36738 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Wed, 13 Dec 2023 12:31:15 -0500 Subject: [PATCH 107/156] format --- .../lib/UnallocatedArrays/src/abstractfill/abstractfill.jl | 2 +- NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl | 6 ++++-- NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 6 ++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl index 2de5efe1d7..7df2b799f3 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl @@ -16,4 +16,4 @@ allocate(A::AbstractFill) = alloctype(A)(parent(A)) set_eltype(T::Type{<:AbstractFill}, elt::Type) = set_parameters(T, Position{1}(), elt) set_ndims(T::Type{<:AbstractFill}, n) = set_parameters(T, Position{2}(), n) -set_axestype(T::Type{<:AbstractFill}, ax::Type) = set_parameters(T, Position{3}(), ax) \ No newline at end of file +set_axestype(T::Type{<:AbstractFill}, ax::Type) = set_parameters(T, Position{3}(), ax) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 508192fdb7..f98ade35d8 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -6,8 +6,10 @@ struct UnallocatedFill{ElT,N,Axes,Alloc} <: AbstractFill{ElT,N,Axes} f::Fill{ElT,N,Axes} alloc::Alloc - function UnallocatedFill{ElT,N,Axes}(f::Fill, alloc::Type{<:AbstractArray{ElT, N}}) where {ElT,N,Axes} - return new{ElT, N, Axes, Type{alloc}}(f, alloc) + function UnallocatedFill{ElT,N,Axes}( + f::Fill, alloc::Type{<:AbstractArray{ElT,N}} + ) where {ElT,N,Axes} + return new{ElT,N,Axes,Type{alloc}}(f, alloc) end end diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 771ae88df9..e6623ed71f 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -15,8 +15,10 @@ struct UnallocatedZeros{ElT,N,Axes,Alloc} <: AbstractZeros{ElT,N,Axes} z::Zeros{ElT,N,Axes} alloc::Alloc - function UnallocatedZeros{ElT,N,Axes}(z::Zeros, alloc::Type{<:AbstractArray{ElT, N}}) where {ElT,N,Axes} - return new{ElT, N, Axes, Type{alloc}}(z, alloc) + function UnallocatedZeros{ElT,N,Axes}( + z::Zeros, alloc::Type{<:AbstractArray{ElT,N}} + ) where {ElT,N,Axes} + return new{ElT,N,Axes,Type{alloc}}(z, alloc) end end From d149ef73ce19187d86eaf8257831d4618f5cb05e Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 19 Dec 2023 13:18:10 -0500 Subject: [PATCH 108/156] Remove `<:` --- .../src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 8d981eb384..5b1dc9fe8f 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -3,7 +3,7 @@ using NDTensors.SetParameters: set_parameters using Adapt: adapt const UnallocatedArray{ElT,N,AxesT,AllocT} = Union{ - <:UnallocatedFill{ElT,N,AxesT,AllocT},<:UnallocatedZeros{ElT,N,AxesT,AllocT} + UnallocatedFill{ElT,N,AxesT,AllocT},UnallocatedZeros{ElT,N,AxesT,AllocT} } @inline Base.axes(A::UnallocatedArray) = axes(parent(A)) From fab15ff52056a5b09337fcb0656060a18b1a05ec Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 19 Dec 2023 17:46:14 -0500 Subject: [PATCH 109/156] Update complex function to use `set_eltype` --- .../src/abstractfill/set_types.jl | 76 ++++++++++--------- .../src/abstractunallocatedarray.jl | 4 +- .../UnallocatedArrays/src/unallocatedfill.jl | 8 ++ .../UnallocatedArrays/src/unallocatedzeros.jl | 6 ++ 4 files changed, 56 insertions(+), 38 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl index 985e57885d..5a0a63234f 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl @@ -2,7 +2,6 @@ using FillArrays: AbstractFill using NDTensors.SetParameters: SetParameters, Position using NDTensors.UnspecifiedTypes: UnspecifiedZero ## TODO make unit tests for all of these functions -## TODO remove P4 # `SetParameters.jl` overloads. SetParameters.get_parameter(::Type{<:AbstractFill{P1}}, ::Position{1}) where {P1} = P1 SetParameters.get_parameter(::Type{<:AbstractFill{<:Any,P2}}, ::Position{2}) where {P2} = P2 @@ -15,20 +14,52 @@ end ## Setting paramaters # right now I am just defining the necessary ones for my implementation still working on full implementation # Set parameter 1 -SetParameters.set_parameter(T::Type{<:AbstractFill}, ::Position{1}, P1) = T{P1} +function SetParameters.set_parameter(T::Type{<:AbstractFill}, ::Position{1}, P1) + return strip_parameters(T){P1} +end +function SetParameters.set_parameter( + T::Type{<:AbstractFill{<:Any,P2}}, ::Position{1}, P1 +) where {P2} + return strip_parameters(T){P1,P2} +end +function SetParameters.set_parameter( + T::Type{<:AbstractFill{<:Any,P2,P3}}, ::Position{1}, P1 +) where {P2,P3} + return strip_parameters(T){P1,P2,P3} +end # Set parameter 2 function SetParameters.set_parameter( T::Type{<:AbstractFill{P1}}, ::Position{2}, P2 ) where {P1} - return T{P2} + return strip_parameters(T){P1,P2} +end +function SetParameters.set_parameter( + T::Type{<:AbstractFill{P1,<:Any}}, ::Position{2}, P2 +) where {P1} + return strip_parameters(T){P1,P2} +end +function SetParameters.set_parameter( + T::Type{<:AbstractFill{P1,<:Any,P3}}, ::Position{2}, P2 +) where {P1,P3} + return T{P1,P2,P3} end # Set parameter 3 +function SetParameters.set_parameter( + T::Type{<:AbstractFill{P1}}, ::Position{3}, P3 +) where {P1} + return strip_parameters(T){P1,<:Any,P3} +end function SetParameters.set_parameter( T::Type{<:AbstractFill{P1,P2}}, ::Position{3}, P3 ) where {P1,P2} - return T{P3} + return strip_parameters(T){P1,P2,P3} +end +function SetParameters.set_parameter( + T::Type{<:AbstractFill{P1,P2,<:Any}}, ::Position{3}, P3 +) where {P1,P2} + return strip_parameters(T){P1,P2,P3} end ## TODO define a specify_parameters function @@ -42,34 +73,9 @@ SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{3}) = Tuple{} SetParameters.nparameters(::Type{<:AbstractFill}) = Val(3) -# Set parameter 1 -## Right now using AbstractArray -## TODO These are more difficult because T is technically defined so need some way to strip T of it {} types -# function set_parameter( -# T::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{1}, P1 -# ) where {P3} -# return T{P1,<:Any,P3} -# end -# function set_parameter( -# T::Type{<:AbstractFill{<:Any,P2,P3}}, ::Position{1}, P1 -# ) where {P2,P3} -# return T{P1,P2,P3} -# end -# function set_parameter( -# T::Type{<:AbstractFill{<:Any,<:Any,P3}}, ::Position{2}, P2 -# ) where {P3} -# return T{<:Any,P2,P3} -# end -# function set_parameter( -# T::Type{<:AbstractFill{P1,<:Any,P3}}, ::Position{2}, P2 -# ) where {P1,P3} -# return T -# end -# set_parameter(T::Type{<:AbstractFill}, ::Position{3}, P3) = T{<:Any,<:Any,P3} -# set_parameter(T::Type{<:AbstractFill{P1}}, ::Position{3}, P3) where {P1} = T{<:Any,P3} -# function set_parameter(T::Type{<:AbstractFill{<:Any,P2}}, ::Position{3}, P3) where {P2} -# return T{<:Any,P2,P3} -# end - -# Set parameter 2 -## using AbstractArray +## This is a helper function which can be renamed. +## Essentially, because I use AbstractFill I don't know what the name +## of the object is in `set_parameter`, so I use this to find the +## name with out any of the parameters attached. +strip_parameters(::Type{<:Fill}) = Fill +strip_parameters(::Type{<:Zeros}) = Zeros diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 5b1dc9fe8f..609971b1dd 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -13,9 +13,7 @@ function FillArrays.getindex_value(A::UnallocatedArray) end function Base.complex(A::UnallocatedArray) - return set_alloctype( - complex(parent(A)), set_parameters(alloctype(A), Position{1}(), complex(eltype(A))) - ) + return set_eltype(A, complex(eltype(A))) end function Base.transpose(a::UnallocatedArray) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index f98ade35d8..38ae883c77 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -31,6 +31,14 @@ Base.parent(F::UnallocatedFill) = F.f Base.convert(::Type{<:UnallocatedFill}, A::UnallocatedFill) = A +function set_eltype(T::UnallocatedFill, elt::Type) + f = parent(T) + FT = set_eltype(typeof(f), elt) + return set_alloctype( + FT(getindex_value(f), axes(f)), set_parameters(alloctype(T), Position{1}(), elt) + ) +end + ############################################# # Arithmatic diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index e6623ed71f..98bf7aa92c 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -40,6 +40,12 @@ Base.parent(Z::UnallocatedZeros) = Z.z Base.convert(::Type{<:UnallocatedZeros}, A::UnallocatedZeros) = A +function set_eltype(T::UnallocatedZeros, elt::Type) + z = parent(T) + ZT = set_eltype(typeof(z), elt) + return set_alloctype(ZT(axes(z)), set_parameters(alloctype(T), Position{1}(), elt)) +end + ############################################# # Arithmatic From baaf361390aeec0a95c1ae6b85ac1ac499581e60 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 19 Dec 2023 17:52:12 -0500 Subject: [PATCH 110/156] Some fixes --- .../UnallocatedArrays/src/abstractfill/set_types.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl index 5a0a63234f..d9773337e4 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl @@ -1,4 +1,4 @@ -using FillArrays: AbstractFill +using FillArrays: AbstractFill, Fill, Zeros using NDTensors.SetParameters: SetParameters, Position using NDTensors.UnspecifiedTypes: UnspecifiedZero ## TODO make unit tests for all of these functions @@ -35,8 +35,8 @@ function SetParameters.set_parameter( return strip_parameters(T){P1,P2} end function SetParameters.set_parameter( - T::Type{<:AbstractFill{P1,<:Any}}, ::Position{2}, P2 -) where {P1} + T::Type{<:AbstractFill{P1,P}}, ::Position{2}, P2 +) where {P1, P} return strip_parameters(T){P1,P2} end function SetParameters.set_parameter( @@ -57,8 +57,8 @@ function SetParameters.set_parameter( return strip_parameters(T){P1,P2,P3} end function SetParameters.set_parameter( - T::Type{<:AbstractFill{P1,P2,<:Any}}, ::Position{3}, P3 -) where {P1,P2} + T::Type{<:AbstractFill{P1,P2,P}}, ::Position{3}, P3 +) where {P1,P2,P} return strip_parameters(T){P1,P2,P3} end From ea8735fcf783a8b25445a15ac98730d998409b89 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 19 Dec 2023 17:52:47 -0500 Subject: [PATCH 111/156] Update Base.:+ --- NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 38ae883c77..879a4fac92 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -1,6 +1,5 @@ using FillArrays: FillArrays, AbstractFill, Fill, broadcasted_fill, kron_fill, mult_fill using NDTensors.SetParameters: Position, set_parameters -import Base: + struct UnallocatedFill{ElT,N,Axes,Alloc} <: AbstractFill{ElT,N,Axes} f::Fill{ElT,N,Axes} @@ -72,11 +71,7 @@ function FillArrays.kron_fill(a::UnallocatedFill, b::UnallocatedFill, val, ax) return UnallocatedFill(Fill(val, ax), alloctype(a)) end -function +(A::UnallocatedFill, B::UnallocatedFill) - FillArrays.promote_shape(A, B) - b = getindex_value(B) - return A .+ b -end +Base.:+(A::UnallocatedFill, B::UnallocatedFill) = A .+ B # #### # ## TODO use `set_parameters` as constructor to these types From edf78cb88b08f9fb3214ceec27ba23d619a3b870 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 19 Dec 2023 17:52:51 -0500 Subject: [PATCH 112/156] format --- .../src/lib/UnallocatedArrays/src/abstractfill/set_types.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl index d9773337e4..86ad93326b 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl @@ -36,7 +36,7 @@ function SetParameters.set_parameter( end function SetParameters.set_parameter( T::Type{<:AbstractFill{P1,P}}, ::Position{2}, P2 -) where {P1, P} +) where {P1,P} return strip_parameters(T){P1,P2} end function SetParameters.set_parameter( From b33f82e84946b3736560c10ca0ab06d54bdeae25 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 19 Dec 2023 17:55:35 -0500 Subject: [PATCH 113/156] fix assert parentheses [no-ci] Co-authored-by: Matt Fishman --- NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index e6623ed71f..c463c0bc1e 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -56,7 +56,7 @@ function FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, elt, ax) return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end function FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, b::UnallocatedZeros, elt, ax) - @assert(alloctype(a) == alloctype(b)) + @assert alloctype(a) == alloctype(b) return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end From e2c53d17c0a51e761183cc2f3796f1ae8837fb2a Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 19 Dec 2023 18:12:04 -0500 Subject: [PATCH 114/156] Use smallest number size and `iszero` function --- .../lib/UnallocatedArrays/test/runtests.jl | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index f1236673d3..9e032f66bb 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -18,9 +18,9 @@ using .NDTensorsTestUtils: devices_list @test Z isa AbstractFill @test size(Z) == (2, 3) @test length(Z) == 6 - @test sum(Z) == 0 - @test norm(Z) == 0 - @test Z[2, 3] == 0 + @test iszero(sum(Z)) + @test iszero(norm(Z)) + @test iszero(Z[2, 3]) @test allocate(Z) isa dev(Matrix{elt}) Zp = set_alloctype(z, dev(Matrix{elt})) @test Zp == Z @@ -28,27 +28,27 @@ using .NDTensorsTestUtils: devices_list @test Zc == Z Zc = complex(Z) @test eltype(Zc) == complex(eltype(z)) - @test Zc[1, 2] == 0.0 + 0.0im + @test iszero(Zc[1, 2]) Zs = similar(Z) @test Zs isa alloctype(Z) ######################################### # UnallocatedFill - f = Fill{elt}(3.0, (2, 3, 4)) + f = Fill{elt}(3, (2, 3, 4)) F = UnallocatedFill(f, Array{elt,ndims(f)}) @test F isa AbstractFill @test size(F) == (2, 3, 4) @test length(F) == 24 - @test sum(F) ≈ 3 * 24 - @test norm(F) ≈ sqrt(3^2 * 24) - @test F[2, 3, 1] == 3.0 + @test sum(F) ≈ elt(3) * 24 + @test norm(F) ≈ sqrt(elt(3)^2 * 24) + @test F[2, 3, 1] == elt(3) @test allocate(F) isa Array{elt,3} Fp = allocate(F) @test norm(Fp) ≈ norm(F) Fs = similar(F) @test Fs isa alloctype(F) - @test Fs[1, 1, 1] != 3.0 + @test Fs[1, 1, 1] != elt(3) Fp = set_alloctype(f, dev(Array{elt,ndims(f)})) @test allocate(Fp) isa dev(Array{elt,ndims(f)}) @@ -58,10 +58,8 @@ using .NDTensorsTestUtils: devices_list Fc = allocate(complex(F)) @test eltype(Fc) == complex(eltype(F)) @test typeof(Fc) == alloctype(complex(F)) - Fc[2, 3, 4] = 4.0 + 3.0im - @test Fc[2, 3, 4] == 4.0 + 3.0im - ## TODO this isn't working - #Fc[2,3,4] = Base.setindex(F, 4.0+3.0im, 2,3,4) + Fc[2, 3, 4] = elt(0) + @test iszero(Fc[2, 3, 4]) end @testset "Multiplication" begin @@ -88,13 +86,13 @@ using .NDTensorsTestUtils: devices_list ################################### ## UnallocatedFill - f = Fill{elt}(3.0, (2, 12)) + f = Fill{elt}(3, (2, 12)) F = UnallocatedFill(f, dev(Matrix{elt})) - p = Fill{elt}(4.0, (12, 5)) + p = Fill{elt}(4, (12, 5)) P = UnallocatedFill(p, dev(Array{elt,ndims(p)})) R = F * P @test F isa UnallocatedFill - @test R[1, 1] == 144 + @test R[1, 1] == elt(144) @test alloctype(R) == alloctype(F) @test size(R) == (2, 5) @@ -119,10 +117,10 @@ using .NDTensorsTestUtils: devices_list @testset "Broadcast" begin z = Zeros{elt}((2, 3)) Z = UnallocatedZeros(z, dev(Matrix{elt})) - R = elt(2.0) .* Z + R = elt(2) .* Z @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Z) - R = Z .* elt(2.0) + R = Z .* elt(2) @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Z) @@ -135,25 +133,24 @@ using .NDTensorsTestUtils: devices_list @test R isa UnallocatedZeros @test alloctype(R) == alloctype(Z) - R = Z .+ elt(2.0) + R = Z .+ elt(2) @test R isa UnallocatedFill @test alloctype(R) == alloctype(Z) ######################## # UnallocatedFill - f = Fill(elt(3.0), (2, 3, 4)) + f = Fill(elt(3), (2, 3, 4)) F = UnallocatedFill(f, Array{elt,ndims(f)}) F2 = F .* 2 @test F2 isa UnallocatedFill - @test F2[1, 1, 1] == elt(6.0) + @test F2[1, 1, 1] == elt(6) @test alloctype(F2) == alloctype(F) - #F2 .+= elt(2.0) ## This is broken - F2 = F2 .+ elt(2.0) + F2 = F2 .+ elt(2) @test F2 isa UnallocatedFill - @test F2[1, 1, 1] == elt(8.0) + @test F2[1, 1, 1] == elt(8) @test alloctype(F2) == alloctype(F) - F = UnallocatedFill(Fill(elt(2.0), (2, 3)), dev(Matrix{elt})) + F = UnallocatedFill(Fill(elt(2), (2, 3)), dev(Matrix{elt})) R = Z + F @test R isa UnallocatedFill @test alloctype(R) == alloctype(Z) @@ -162,14 +159,14 @@ using .NDTensorsTestUtils: devices_list @test R isa UnallocatedFill @test alloctype(R) == alloctype(Z) - F = UnallocatedFill(Fill(elt(3.0), (2, 12)), dev(Matrix{elt})) + F = UnallocatedFill(Fill(elt(3), (2, 12)), dev(Matrix{elt})) R = F .* F @test R isa UnallocatedFill @test R[2, 9] == elt(9) @test alloctype(R) == alloctype(F) @test size(R) == (2, 12) - P = UnallocatedFill(Fill(elt(4.0), (2, 3)), dev(Matrix{elt})) + P = UnallocatedFill(Fill(elt(4), (2, 3)), dev(Matrix{elt})) R = Z .* P @test R isa UnallocatedZeros @test alloctype(R) == alloctype(P) @@ -188,7 +185,7 @@ using .NDTensorsTestUtils: devices_list @test C isa UnallocatedZeros @test alloctype(C) == alloctype(B) - B = UnallocatedFill(Fill(elt(2.0), (2)), dev(Vector{elt})) + B = UnallocatedFill(Fill(elt(2), (2)), dev(Vector{elt})) C = kron(A, B) @test C isa UnallocatedZeros @test alloctype(C) == alloctype(B) @@ -197,7 +194,7 @@ using .NDTensorsTestUtils: devices_list @test C isa UnallocatedZeros @test alloctype(C) == alloctype(B) - A = UnallocatedFill(Fill(elt(3.0), (2)), dev(Vector{elt})) + A = UnallocatedFill(Fill(elt(3), (2)), dev(Vector{elt})) C = kron(A, B) @test C isa UnallocatedFill @test alloctype(C) == alloctype(B) From 5408031706e81df7aa059aa76e1fc767aee04394 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 11:19:02 -0500 Subject: [PATCH 115/156] Remove set_eltype function --- .../src/lib/UnallocatedArrays/src/unallocatedfill.jl | 8 -------- .../src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 6 ------ 2 files changed, 14 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 879a4fac92..85a86b90ca 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -30,14 +30,6 @@ Base.parent(F::UnallocatedFill) = F.f Base.convert(::Type{<:UnallocatedFill}, A::UnallocatedFill) = A -function set_eltype(T::UnallocatedFill, elt::Type) - f = parent(T) - FT = set_eltype(typeof(f), elt) - return set_alloctype( - FT(getindex_value(f), axes(f)), set_parameters(alloctype(T), Position{1}(), elt) - ) -end - ############################################# # Arithmatic diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 02647959ab..c463c0bc1e 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -40,12 +40,6 @@ Base.parent(Z::UnallocatedZeros) = Z.z Base.convert(::Type{<:UnallocatedZeros}, A::UnallocatedZeros) = A -function set_eltype(T::UnallocatedZeros, elt::Type) - z = parent(T) - ZT = set_eltype(typeof(z), elt) - return set_alloctype(ZT(axes(z)), set_parameters(alloctype(T), Position{1}(), elt)) -end - ############################################# # Arithmatic From 199158bf9e8b381f8627f9d7fa19a97885688684 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 11:22:27 -0500 Subject: [PATCH 116/156] Rename to unspecified_parameters and use in set_parameter functions. --- .../src/abstractfill/set_types.jl | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl index 86ad93326b..b0ac1697b8 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl @@ -15,55 +15,53 @@ end # right now I am just defining the necessary ones for my implementation still working on full implementation # Set parameter 1 function SetParameters.set_parameter(T::Type{<:AbstractFill}, ::Position{1}, P1) - return strip_parameters(T){P1} + return unspecify_parameters(T){P1} end function SetParameters.set_parameter( T::Type{<:AbstractFill{<:Any,P2}}, ::Position{1}, P1 ) where {P2} - return strip_parameters(T){P1,P2} + return unspecify_parameters(T){P1,P2} end function SetParameters.set_parameter( T::Type{<:AbstractFill{<:Any,P2,P3}}, ::Position{1}, P1 ) where {P2,P3} - return strip_parameters(T){P1,P2,P3} + return unspecify_parameters(T){P1,P2,P3} end # Set parameter 2 function SetParameters.set_parameter( T::Type{<:AbstractFill{P1}}, ::Position{2}, P2 ) where {P1} - return strip_parameters(T){P1,P2} + return unspecify_parameters(T){P1,P2} end function SetParameters.set_parameter( T::Type{<:AbstractFill{P1,P}}, ::Position{2}, P2 ) where {P1,P} - return strip_parameters(T){P1,P2} + return unspecify_parameters(T){P1,P2} end function SetParameters.set_parameter( - T::Type{<:AbstractFill{P1,<:Any,P3}}, ::Position{2}, P2 -) where {P1,P3} - return T{P1,P2,P3} + T::Type{<:AbstractFill{P1,P,P3}}, ::Position{2}, P2 +) where {P1,P,P3} + return unspecify_parameters(T){P1,P2,P3} end # Set parameter 3 function SetParameters.set_parameter( T::Type{<:AbstractFill{P1}}, ::Position{3}, P3 ) where {P1} - return strip_parameters(T){P1,<:Any,P3} + return unspecify_parameters(T){P1,<:Any,P3} end function SetParameters.set_parameter( T::Type{<:AbstractFill{P1,P2}}, ::Position{3}, P3 ) where {P1,P2} - return strip_parameters(T){P1,P2,P3} + return unspecify_parameters(T){P1,P2,P3} end function SetParameters.set_parameter( T::Type{<:AbstractFill{P1,P2,P}}, ::Position{3}, P3 ) where {P1,P2,P} - return strip_parameters(T){P1,P2,P3} + return unspecify_parameters(T){P1,P2,P3} end -## TODO define a specify_parameters function -## To quickly specify P1, P2, and P3 ## default parameters function SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{1}) return UnspecifiedTypes.UnspecifiedZero @@ -73,9 +71,8 @@ SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{3}) = Tuple{} SetParameters.nparameters(::Type{<:AbstractFill}) = Val(3) -## This is a helper function which can be renamed. -## Essentially, because I use AbstractFill I don't know what the name -## of the object is in `set_parameter`, so I use this to find the -## name with out any of the parameters attached. -strip_parameters(::Type{<:Fill}) = Fill -strip_parameters(::Type{<:Zeros}) = Zeros +## These helper functions take a UnallocatedArray type and +## remove all the parameters, this way all parameters can be set +## at once in the `set_parameter` functions above. +unspecify_parameters(::Type{<:Fill}) = Fill +unspecify_parameters(::Type{<:Zeros}) = Zeros From d34f637a917f870b8cd7a446d165478d6de48634 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 11:34:15 -0500 Subject: [PATCH 117/156] Add test for SetParameters of FillArray --- .../lib/UnallocatedArrays/test/runtests.jl | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 9e032f66bb..78810c1d68 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -2,6 +2,7 @@ using FillArrays: FillArrays, AbstractFill, Fill, Zeros using NDTensors: NDTensors using NDTensors.UnallocatedArrays +using NDTensors.SetParameters: Position, set_parameters using LinearAlgebra: norm using Test: @test, @testset, @test_broken @@ -201,4 +202,37 @@ using .NDTensorsTestUtils: devices_list @test C[1] == elt(6) end end +@testset "SetParameters" begin + for typ in (:Zeros, :Fill) + @eval begin + ft1 = $(typ){1} + ft2 = $typ{1,2} + ft3 = $typ{1,2,3} + + ## check 1 parameter specified + ftn1 = set_parameters(ft1, Position{1}(), 4) + ftn2 = set_parameters(ft1, Position{2}(), 4) + ftn3 = set_parameters(ft1, Position{3}(), 4) + @test ftn1 == $typ{4} + @test ftn2 == $typ{1,4} + @test ftn3 == $typ{1,<:Any,4} + + ## check 2 parameters specified + ftn1 = set_parameters(ft2, Position{1}(), 4) + ftn2 = set_parameters(ft2, Position{2}(), 4) + ftn3 = set_parameters(ft2, Position{3}(), 4) + @test ftn1 == $typ{4,2} + @test ftn2 == $typ{1,4} + @test ftn3 == $typ{1,2,4} + + ## check 3 parameters specified + ftn1 = set_parameters(ft3, Position{1}(), 4) + ftn2 = set_parameters(ft3, Position{2}(), 4) + ftn3 = set_parameters(ft3, Position{3}(), 4) + @test ftn1 == $typ{4,2,3} + @test ftn2 == $typ{1,4,3} + @test ftn3 == $typ{1,2,4} + end + end +end end From fee954b68666919a64b081b432e73c9e040e69d4 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 12:17:41 -0500 Subject: [PATCH 118/156] format --- NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index c463c0bc1e..e6623ed71f 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -56,7 +56,7 @@ function FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, elt, ax) return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end function FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, b::UnallocatedZeros, elt, ax) - @assert alloctype(a) == alloctype(b) + @assert(alloctype(a) == alloctype(b)) return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end From b74140cac66f39d43727722750c7da9cd0c9645a Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 12:18:05 -0500 Subject: [PATCH 119/156] Remove comments --- .../src/lib/UnallocatedArrays/src/abstractfill/set_types.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl index b0ac1697b8..8e53665e05 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl @@ -1,7 +1,7 @@ using FillArrays: AbstractFill, Fill, Zeros using NDTensors.SetParameters: SetParameters, Position using NDTensors.UnspecifiedTypes: UnspecifiedZero -## TODO make unit tests for all of these functions + # `SetParameters.jl` overloads. SetParameters.get_parameter(::Type{<:AbstractFill{P1}}, ::Position{1}) where {P1} = P1 SetParameters.get_parameter(::Type{<:AbstractFill{<:Any,P2}}, ::Position{2}) where {P2} = P2 @@ -12,7 +12,6 @@ function SetParameters.get_parameter( end ## Setting paramaters -# right now I am just defining the necessary ones for my implementation still working on full implementation # Set parameter 1 function SetParameters.set_parameter(T::Type{<:AbstractFill}, ::Position{1}, P1) return unspecify_parameters(T){P1} @@ -64,7 +63,7 @@ end ## default parameters function SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{1}) - return UnspecifiedTypes.UnspecifiedZero + return UnspecifiedZero end SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{2}) = 0 SetParameters.default_parameter(::Type{<:AbstractFill}, ::Position{3}) = Tuple{} From 5b5c05793becc326f034e79f942a45f2bdbcaaef Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 12:18:38 -0500 Subject: [PATCH 120/156] format --- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 78810c1d68..c1bd033592 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -2,14 +2,13 @@ using FillArrays: FillArrays, AbstractFill, Fill, Zeros using NDTensors: NDTensors using NDTensors.UnallocatedArrays -using NDTensors.SetParameters: Position, set_parameters using LinearAlgebra: norm -using Test: @test, @testset, @test_broken +using Test: @test, @testset include(joinpath(pkgdir(NDTensors), "test", "NDTensorsTestUtils", "NDTensorsTestUtils.jl")) using .NDTensorsTestUtils: devices_list -@testset "Testing UnallocatedArrays" for dev in devices_list(ARGS), +@testset "Testing UnallocatedArrays on $dev with eltype $elt" for dev in devices_list(ARGS), elt in (Float64, Float32, ComplexF64, ComplexF32) @testset "Basic funcitonality" begin From 8f0e3612bd56375778d182d3d966a8a6667ee29c Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 12:19:05 -0500 Subject: [PATCH 121/156] Add SetParameter tests --- .../lib/UnallocatedArrays/test/runtests.jl | 79 +++++++++++-------- 1 file changed, 47 insertions(+), 32 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index c1bd033592..ee82fe067f 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -201,37 +201,52 @@ using .NDTensorsTestUtils: devices_list @test C[1] == elt(6) end end -@testset "SetParameters" begin - for typ in (:Zeros, :Fill) - @eval begin - ft1 = $(typ){1} - ft2 = $typ{1,2} - ft3 = $typ{1,2,3} - - ## check 1 parameter specified - ftn1 = set_parameters(ft1, Position{1}(), 4) - ftn2 = set_parameters(ft1, Position{2}(), 4) - ftn3 = set_parameters(ft1, Position{3}(), 4) - @test ftn1 == $typ{4} - @test ftn2 == $typ{1,4} - @test ftn3 == $typ{1,<:Any,4} - - ## check 2 parameters specified - ftn1 = set_parameters(ft2, Position{1}(), 4) - ftn2 = set_parameters(ft2, Position{2}(), 4) - ftn3 = set_parameters(ft2, Position{3}(), 4) - @test ftn1 == $typ{4,2} - @test ftn2 == $typ{1,4} - @test ftn3 == $typ{1,2,4} - - ## check 3 parameters specified - ftn1 = set_parameters(ft3, Position{1}(), 4) - ftn2 = set_parameters(ft3, Position{2}(), 4) - ftn3 = set_parameters(ft3, Position{3}(), 4) - @test ftn1 == $typ{4,2,3} - @test ftn2 == $typ{1,4,3} - @test ftn3 == $typ{1,2,4} - end - end end + +using FillArrays: Fill, Zeros +using NDTensors.UnallocatedArrays +using NDTensors.SetParameters: Position, default_parameter, nparameters, get_parameter, set_parameters +using Test: @test, @testset + +@testset "SetParameters" for (typ) in (:Fill, :Zeros) + @eval begin + t1 = default_parameter($typ, Position{1}()) + t2 = default_parameter($typ, Position{2}()) + t3 = default_parameter($typ, Position{3}()) + t4 = Any + ft1 = $typ{t1} + ft2 = $typ{t1,t2} + ft3 = $typ{t1,t2,t3} + + ## check 1 parameter specified + ftn1 = set_parameters(ft1, Position{1}(), t4) + ftn2 = set_parameters(ft1, Position{2}(), t4) + ftn3 = set_parameters(ft1, Position{3}(), t4) + @test ftn1 == $typ{t4} + @test ftn2 == $typ{t1,t4} + @test ftn3 == $typ{t1,<:Any,t4} + + ## check 2 parameters specified + ftn1 = set_parameters(ft2, Position{1}(), t4) + ftn2 = set_parameters(ft2, Position{2}(), t4) + ftn3 = set_parameters(ft2, Position{3}(), t4) + @test ftn1 == $typ{t4,t2} + @test ftn2 == $typ{t1,t4} + @test ftn3 == $typ{t1,t2,t4} + + ## check 3 parameters specified + ftn1 = set_parameters(ft3, Position{1}(), t4) + ftn2 = set_parameters(ft3, Position{2}(), t4) + ftn3 = set_parameters(ft3, Position{3}(), t4) + @test ftn1 == $typ{t4,t2,t3} + @test ftn2 == $typ{t1,t4,t3} + @test ftn3 == $typ{t1,t2,t4} + + @test get_parameter(ft3, Position{1}()) == t1 + @test get_parameter(ft3, Position{2}()) == t2 + @test get_parameter(ft3, Position{3}()) == t3 + + @test nparameters(ft3) == Val(3) + end end +end \ No newline at end of file From 85ece9471431d50acb4d644edc576200a302e236 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 12:20:19 -0500 Subject: [PATCH 122/156] Overwrite Base.Braodcast.broadcasted for UnallocatedArrays to preserve alloc --- .../src/abstractunallocatedarray.jl | 2 +- .../UnallocatedArrays/src/unallocatedfill.jl | 19 ++++--------------- .../UnallocatedArrays/src/unallocatedzeros.jl | 6 ++++++ 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 609971b1dd..a41923501a 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -13,7 +13,7 @@ function FillArrays.getindex_value(A::UnallocatedArray) end function Base.complex(A::UnallocatedArray) - return set_eltype(A, complex(eltype(A))) + return complex(eltype(A)).(A) end function Base.transpose(a::UnallocatedArray) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 85a86b90ca..58ce3c4549 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -65,18 +65,7 @@ end Base.:+(A::UnallocatedFill, B::UnallocatedFill) = A .+ B -# #### -# ## TODO use `set_parameters` as constructor to these types -# function UnallocatedFill(f::Fill, alloc::Type{<:AbstractArray}) -# ## set_axes -> set_axes_type -# return set_alloctype( -# set_axes(set_ndims(set_eltype(UnallocatedFill, eltype(f)), ndims(f)), typeof(axes(f))), -# alloc, -# )( -# f -# ) -# end - -## Things to fix -## in different Change syntax of set_xxx_if_unspecified -# adapt +function Base.Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle, op, r::UnallocatedFill) + f = op.(parent(r)) + return set_alloctype(f, set_parameters(alloctype(r), Position{1}(), eltype(f))) +end diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index e6623ed71f..98787e197c 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -92,3 +92,9 @@ end function FillArrays.kron_fill(a::UnallocatedFill, b::UnallocatedZeros, val, ax) return kron_fill(b, a, val, ax) end + +function Base.Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle, op, r::UnallocatedZeros) + elt = typeof(op(getindex_value(r))) + z = Zeros(elt, axes(r)) + return set_alloctype(z, set_parameters(alloctype(r), Position{1}(), eltype(z))) +end \ No newline at end of file From aabd4e93dece930c8d85bbe479a472214a6a2976 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 12:37:45 -0500 Subject: [PATCH 123/156] Clean up set types with Unspecify_parameters and UnallocatedArray --- .../lib/UnallocatedArrays/src/set_types.jl | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl index 6f4bfcbc34..eacc82f662 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl @@ -4,35 +4,42 @@ using NDTensors.UnspecifiedTypes: UnspecifiedArray, UnspecifiedNumber, Unspecifi ## TODO All I need to do is overload AbstractFill functions with 4 parameters # `SetParameters.jl` overloads. function SetParameters.get_parameter( - ::Type{<:UnallocatedFill{<:Any,<:Any,<:Any,P4}}, ::Position{4} -) where {P4} - return P4 -end -function SetParameters.get_parameter( - ::Type{<:UnallocatedZeros{<:Any,<:Any,<:Any,P4}}, ::Position{4} + ::Type{<:UnallocatedArray{<:Any,<:Any,<:Any,P4}}, ::Position{4} ) where {P4} return P4 end # ## Setting paramaters function SetParameters.set_parameter( - T::Type{<:UnallocatedFill{P1,P2,P3,<:Any}}, ::Position{4}, P4::Type{<:AbstractArray} -) where {P1,P2,P3} - return T{P4} + T::Type{<:UnallocatedArray{P,P2,P3,P4}}, ::Position{1}, P1 +) where {P,P2,P3,P4} + return unspecify_parameters(T){P1,P2,P3,P4} end + function SetParameters.set_parameter( - T::Type{<:UnallocatedZeros{P1,P2,P3,<:Any}}, ::Position{4}, P4::Type{<:AbstractArray} + T::Type{<:UnallocatedArray{P1,P,P3,P4}}, ::Position{2}, P2 +) where {P1,P,P3,P4} + return unspecify_parameters(T){P1,P2,P3,P4} +end + +function SetParameters.set_parameter( + T::Type{<:UnallocatedArray{P1,P2,P,P4}}, ::Position{3}, P3 +) where {P1,P2,P,P4} + return unspecify_parameters(T){P1,P2,P3,P4} +end + +function SetParameters.set_parameter( + T::Type{<:UnallocatedArray{P1,P2,P3}}, ::Position{4}, P4 ) where {P1,P2,P3} - return T{P4} + return unspecify_parameters(T){P1,P2,P3,P4} end # ## default parameters -function SetParameters.default_parameter(::Type{<:UnallocatedFill}, ::Position{4}) - return UnspecifiedArray{UnspecifiedNumber{UnspecifiedZero},0} -end -function SetParameters.default_parameter(::Type{<:UnallocatedZeros}, ::Position{4}) +function SetParameters.default_parameter(::Type{<:UnallocatedArray}, ::Position{4}) return UnspecifiedArray{UnspecifiedNumber{UnspecifiedZero},0} end -SetParameters.nparameters(::Type{<:UnallocatedFill}) = Val(4) -SetParameters.nparameters(::Type{<:UnallocatedZeros}) = Val(4) +SetParameters.nparameters(::Type{<:UnallocatedArray}) = Val(4) + +unspecify_parameters(::Type{<:UnallocatedFill}) = UnallocatedFill +unspecify_parameters(::Type{<:UnallocatedZeros}) = UnallocatedZeros \ No newline at end of file From 16afdb8110cdc4e281be85a7b05b9877a4c61d4d Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 13:09:53 -0500 Subject: [PATCH 124/156] Make more tests for UnallocatedArrays --- .../lib/UnallocatedArrays/test/runtests.jl | 109 +++++++++++------- 1 file changed, 69 insertions(+), 40 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index ee82fe067f..6892dfd89c 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -208,45 +208,74 @@ using NDTensors.UnallocatedArrays using NDTensors.SetParameters: Position, default_parameter, nparameters, get_parameter, set_parameters using Test: @test, @testset -@testset "SetParameters" for (typ) in (:Fill, :Zeros) - @eval begin - t1 = default_parameter($typ, Position{1}()) - t2 = default_parameter($typ, Position{2}()) - t3 = default_parameter($typ, Position{3}()) - t4 = Any - ft1 = $typ{t1} - ft2 = $typ{t1,t2} - ft3 = $typ{t1,t2,t3} - - ## check 1 parameter specified - ftn1 = set_parameters(ft1, Position{1}(), t4) - ftn2 = set_parameters(ft1, Position{2}(), t4) - ftn3 = set_parameters(ft1, Position{3}(), t4) - @test ftn1 == $typ{t4} - @test ftn2 == $typ{t1,t4} - @test ftn3 == $typ{t1,<:Any,t4} - - ## check 2 parameters specified - ftn1 = set_parameters(ft2, Position{1}(), t4) - ftn2 = set_parameters(ft2, Position{2}(), t4) - ftn3 = set_parameters(ft2, Position{3}(), t4) - @test ftn1 == $typ{t4,t2} - @test ftn2 == $typ{t1,t4} - @test ftn3 == $typ{t1,t2,t4} - - ## check 3 parameters specified - ftn1 = set_parameters(ft3, Position{1}(), t4) - ftn2 = set_parameters(ft3, Position{2}(), t4) - ftn3 = set_parameters(ft3, Position{3}(), t4) - @test ftn1 == $typ{t4,t2,t3} - @test ftn2 == $typ{t1,t4,t3} - @test ftn3 == $typ{t1,t2,t4} - - @test get_parameter(ft3, Position{1}()) == t1 - @test get_parameter(ft3, Position{2}()) == t2 - @test get_parameter(ft3, Position{3}()) == t3 - - @test nparameters(ft3) == Val(3) +@testset "SetParameters" begin + @testset "Tetsing $typ" for (typ) in (:Fill, :Zeros) + @eval begin + t1 = default_parameter($typ, Position{1}()) + t2 = default_parameter($typ, Position{2}()) + t3 = default_parameter($typ, Position{3}()) + t4 = Any + ft1 = $typ{t1} + ft2 = $typ{t1,t2} + ft3 = $typ{t1,t2,t3} + + ## check 1 parameter specified + ftn1 = set_parameters(ft1, Position{1}(), t4) + ftn2 = set_parameters(ft1, Position{2}(), t4) + ftn3 = set_parameters(ft1, Position{3}(), t4) + @test ftn1 == $typ{t4} + @test ftn2 == $typ{t1,t4} + @test ftn3 == $typ{t1,<:Any,t4} + + ## check 2 parameters specified + ftn1 = set_parameters(ft2, Position{1}(), t4) + ftn2 = set_parameters(ft2, Position{2}(), t4) + ftn3 = set_parameters(ft2, Position{3}(), t4) + @test ftn1 == $typ{t4,t2} + @test ftn2 == $typ{t1,t4} + @test ftn3 == $typ{t1,t2,t4} + + ## check 3 parameters specified + ftn1 = set_parameters(ft3, Position{1}(), t4) + ftn2 = set_parameters(ft3, Position{2}(), t4) + ftn3 = set_parameters(ft3, Position{3}(), t4) + @test ftn1 == $typ{t4,t2,t3} + @test ftn2 == $typ{t1,t4,t3} + @test ftn3 == $typ{t1,t2,t4} + + @test get_parameter(ft3, Position{1}()) == t1 + @test get_parameter(ft3, Position{2}()) == t2 + @test get_parameter(ft3, Position{3}()) == t3 + + @test nparameters(ft3) == Val(3) + end + end + + @testset "Tetsing $typ" for (typ) in (:UnallocatedFill, :UnallocatedZeros) + @eval begin + t1 = default_parameter($typ, Position{1}()) + t2 = default_parameter($typ, Position{2}()) + t3 = default_parameter($typ, Position{3}()) + t4 = default_parameter($typ, Position{4}()) + t5 = Any + ft = $typ{t1,t2,t3,t4} + + ## check 4 parameters specified + ftn1 = set_parameters(ft, Position{1}(), t5) + ftn2 = set_parameters(ft, Position{2}(), t5) + ftn3 = set_parameters(ft, Position{3}(), t5) + ftn4 = set_parameters(ft, Position{4}(), t5) + @test ftn1 == $typ{t5,t2,t3,t4} + @test ftn2 == $typ{t1,t5,t3,t4} + @test ftn3 == $typ{t1,t2,t5,t4} + @test ftn4 == $typ{t1,t2,t3,t5} + + @test get_parameter(ft, Position{1}()) == t1 + @test get_parameter(ft, Position{2}()) == t2 + @test get_parameter(ft, Position{3}()) == t3 + @test get_parameter(ft, Position{4}()) == t4 + + @test nparameters(ft) == Val(4) + end end end -end \ No newline at end of file From 8f36ed4a2b8b1cb600e2885f31bbd37066abc212 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 13:18:15 -0500 Subject: [PATCH 125/156] Redefine allocate using similar --- .../src/abstractfill/abstractfill.jl | 2 -- .../src/abstractunallocatedarray.jl | 14 ++++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl index 7df2b799f3..5b1dd7ae72 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/abstractfill.jl @@ -12,8 +12,6 @@ function alloctype(Atype::Type{<:AbstractFill}) return get_parameter(Atype, Position{4}()) end -allocate(A::AbstractFill) = alloctype(A)(parent(A)) - set_eltype(T::Type{<:AbstractFill}, elt::Type) = set_parameters(T, Position{1}(), elt) set_ndims(T::Type{<:AbstractFill}, n) = set_parameters(T, Position{2}(), n) set_axestype(T::Type{<:AbstractFill}, ax::Type) = set_parameters(T, Position{3}(), ax) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index a41923501a..9305247b6c 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -24,10 +24,6 @@ function Base.adjoint(a::UnallocatedArray) return set_alloctype(adjoint(parent(a)), alloctype(a)) end -function Base.similar(a::UnallocatedArray) - return alloctype(a)(undef, size(a)) -end - function set_alloctype(T::Type{<:UnallocatedArray}, alloc::Type{<:AbstractArray}) return set_parameters(T, Position{4}(), alloc) end @@ -40,6 +36,16 @@ for STYPE in (:AbstractArray, :AbstractFill) end end +function allocate(f::UnallocatedArray) + a = similar(f) + fill!(a, getindex_value(f)) + return a +end + +function Base.similar(f::UnallocatedArray) + return similar(alloctype(f), axes(f)) +end + ## TODO fix this because reshape loses alloctype #FillArrays.reshape(a::Union{<:UnallocatedFill, <:UnallocatedZeros}, dims) = set_alloctype(reshape(parent(a), dims), allocate(a)) From 026a4877856d2d6c530a80041d88029c3926342b Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 13:24:14 -0500 Subject: [PATCH 126/156] format --- NDTensors/src/lib/UnallocatedArrays/src/set_types.jl | 2 +- .../src/lib/UnallocatedArrays/src/unallocatedfill.jl | 4 +++- .../src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 6 ++++-- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 9 +++++---- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl index eacc82f662..d2ec636cb3 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/set_types.jl @@ -42,4 +42,4 @@ end SetParameters.nparameters(::Type{<:UnallocatedArray}) = Val(4) unspecify_parameters(::Type{<:UnallocatedFill}) = UnallocatedFill -unspecify_parameters(::Type{<:UnallocatedZeros}) = UnallocatedZeros \ No newline at end of file +unspecify_parameters(::Type{<:UnallocatedZeros}) = UnallocatedZeros diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 58ce3c4549..142a2ff10a 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -65,7 +65,9 @@ end Base.:+(A::UnallocatedFill, B::UnallocatedFill) = A .+ B -function Base.Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle, op, r::UnallocatedFill) +function Base.Broadcast.broadcasted( + ::Base.Broadcast.DefaultArrayStyle, op, r::UnallocatedFill +) f = op.(parent(r)) return set_alloctype(f, set_parameters(alloctype(r), Position{1}(), eltype(f))) end diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 98787e197c..21fc92bb50 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -93,8 +93,10 @@ function FillArrays.kron_fill(a::UnallocatedFill, b::UnallocatedZeros, val, ax) return kron_fill(b, a, val, ax) end -function Base.Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle, op, r::UnallocatedZeros) +function Base.Broadcast.broadcasted( + ::Base.Broadcast.DefaultArrayStyle, op, r::UnallocatedZeros +) elt = typeof(op(getindex_value(r))) z = Zeros(elt, axes(r)) return set_alloctype(z, set_parameters(alloctype(r), Position{1}(), eltype(z))) -end \ No newline at end of file +end diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 6892dfd89c..341b80180d 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -205,10 +205,11 @@ end using FillArrays: Fill, Zeros using NDTensors.UnallocatedArrays -using NDTensors.SetParameters: Position, default_parameter, nparameters, get_parameter, set_parameters +using NDTensors.SetParameters: + Position, default_parameter, nparameters, get_parameter, set_parameters using Test: @test, @testset -@testset "SetParameters" begin +@testset "SetParameters" begin @testset "Tetsing $typ" for (typ) in (:Fill, :Zeros) @eval begin t1 = default_parameter($typ, Position{1}()) @@ -245,7 +246,7 @@ using Test: @test, @testset @test get_parameter(ft3, Position{1}()) == t1 @test get_parameter(ft3, Position{2}()) == t2 - @test get_parameter(ft3, Position{3}()) == t3 + @test get_parameter(ft3, Position{3}()) == t3 @test nparameters(ft3) == Val(3) end @@ -272,7 +273,7 @@ using Test: @test, @testset @test get_parameter(ft, Position{1}()) == t1 @test get_parameter(ft, Position{2}()) == t2 - @test get_parameter(ft, Position{3}()) == t3 + @test get_parameter(ft, Position{3}()) == t3 @test get_parameter(ft, Position{4}()) == t4 @test nparameters(ft) == Val(4) From 68736f55cf37c06afafd3f5aaa45e18b61b43a34 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 15:19:01 -0500 Subject: [PATCH 127/156] Update similar to have an allocate function based on AbstractArrays --- .../lib/UnallocatedArrays/src/abstractunallocatedarray.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 9305247b6c..54d981653d 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -42,8 +42,13 @@ function allocate(f::UnallocatedArray) return a end +function allocate(arraytype::Type{<:AbstractArray}, elt::Type, axes::Tuple) + ArrayT = set_parameters(arraytype, Position{1}(), elt) + return similar(ArrayT, axes) +end + function Base.similar(f::UnallocatedArray) - return similar(alloctype(f), axes(f)) + return allocate(alloctype(f), eltype(f), axes(f)) end ## TODO fix this because reshape loses alloctype From a16027ea8a5c64b219b09f63017724de3ef91030 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 15:25:18 -0500 Subject: [PATCH 128/156] Set N --- .../src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 54d981653d..4bacd893e1 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -44,6 +44,7 @@ end function allocate(arraytype::Type{<:AbstractArray}, elt::Type, axes::Tuple) ArrayT = set_parameters(arraytype, Position{1}(), elt) + ArrayT = set_parameters(ArrayT, Position{2}(), length(axes)) return similar(ArrayT, axes) end From 623af883ad55b38f720d964eb628bbb9171a09cb Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 16:37:31 -0500 Subject: [PATCH 129/156] Move UnspecifiedTypes higher because its used in SetParameters --- NDTensors/src/NDTensors.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/NDTensors.jl b/NDTensors/src/NDTensors.jl index ad9059d887..cd91a265bd 100644 --- a/NDTensors/src/NDTensors.jl +++ b/NDTensors/src/NDTensors.jl @@ -23,6 +23,7 @@ for lib in [ :AlgorithmSelection, :AllocateData, :BaseExtensions, + :UnspecifiedTypes, :SetParameters, :BroadcastMapConversion, :Unwrap, @@ -37,7 +38,6 @@ for lib in [ :SmallVectors, :SortedSets, :TagSets, - :UnspecifiedTypes, :UnallocatedArrays, ] include("lib/$(lib)/src/$(lib).jl") From c18f1fb511b89e892cd4344c24249fcf99f08ce7 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 16:37:52 -0500 Subject: [PATCH 130/156] To prepare for "SetParameters" to be a registered package make FillArrays extension to SetParameters and create Project.toml. Currently its part of NDTensors which has FillArrays in [deps]. Extension aren't being added properly without it. --- NDTensors/src/lib/SetParameters/Project.toml | 8 ++++++++ .../SetParametersFillArraysExt.jl | 4 ++++ .../ext/SetParametersFillArraysExt}/set_types.jl | 0 NDTensors/src/lib/SetParameters/src/SetParameters.jl | 9 +++++++++ .../src/lib/UnallocatedArrays/src/UnallocatedArrays.jl | 1 - 5 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 NDTensors/src/lib/SetParameters/Project.toml create mode 100644 NDTensors/src/lib/SetParameters/ext/SetParametersFillArraysExt/SetParametersFillArraysExt.jl rename NDTensors/src/lib/{UnallocatedArrays/src/abstractfill => SetParameters/ext/SetParametersFillArraysExt}/set_types.jl (100%) diff --git a/NDTensors/src/lib/SetParameters/Project.toml b/NDTensors/src/lib/SetParameters/Project.toml new file mode 100644 index 0000000000..d4b3d82f02 --- /dev/null +++ b/NDTensors/src/lib/SetParameters/Project.toml @@ -0,0 +1,8 @@ +[deps] +PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930" + +[extensions] +SetParametersFillArraysExt = "FillArrays" + +[compat] +PackageExtensionCompat = "1" \ No newline at end of file diff --git a/NDTensors/src/lib/SetParameters/ext/SetParametersFillArraysExt/SetParametersFillArraysExt.jl b/NDTensors/src/lib/SetParameters/ext/SetParametersFillArraysExt/SetParametersFillArraysExt.jl new file mode 100644 index 0000000000..db022866a0 --- /dev/null +++ b/NDTensors/src/lib/SetParameters/ext/SetParametersFillArraysExt/SetParametersFillArraysExt.jl @@ -0,0 +1,4 @@ +module SetParametersFillArraysExt +include("set_types.jl") + +end \ No newline at end of file diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl b/NDTensors/src/lib/SetParameters/ext/SetParametersFillArraysExt/set_types.jl similarity index 100% rename from NDTensors/src/lib/UnallocatedArrays/src/abstractfill/set_types.jl rename to NDTensors/src/lib/SetParameters/ext/SetParametersFillArraysExt/set_types.jl diff --git a/NDTensors/src/lib/SetParameters/src/SetParameters.jl b/NDTensors/src/lib/SetParameters/src/SetParameters.jl index 66f295d899..723e178bb1 100644 --- a/NDTensors/src/lib/SetParameters/src/SetParameters.jl +++ b/NDTensors/src/lib/SetParameters/src/SetParameters.jl @@ -12,6 +12,15 @@ include("Base/val.jl") include("Base/array.jl") include("Base/subarray.jl") +## TODO when this is a full package utilize this function to +# # enable extensions +# using PackageExtensionCompat +# function __init__() +# @require_extensions +# end + +include("../ext/SetParametersFillArraysExt/SetParametersFillArraysExt.jl") + export DefaultParameter, DefaultParameters, Position, diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index ed2aa9d1b4..c9b747575d 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -1,6 +1,5 @@ module UnallocatedArrays include("abstractfill/abstractfill.jl") -include("abstractfill/set_types.jl") include("unallocatedfill.jl") include("unallocatedzeros.jl") From e13694688f23a09828f2011dfd86d4edd9f52eed Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Fri, 22 Dec 2023 16:40:16 -0500 Subject: [PATCH 131/156] format --- .../SetParametersFillArraysExt/SetParametersFillArraysExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/lib/SetParameters/ext/SetParametersFillArraysExt/SetParametersFillArraysExt.jl b/NDTensors/src/lib/SetParameters/ext/SetParametersFillArraysExt/SetParametersFillArraysExt.jl index db022866a0..35f122dc05 100644 --- a/NDTensors/src/lib/SetParameters/ext/SetParametersFillArraysExt/SetParametersFillArraysExt.jl +++ b/NDTensors/src/lib/SetParameters/ext/SetParametersFillArraysExt/SetParametersFillArraysExt.jl @@ -1,4 +1,4 @@ module SetParametersFillArraysExt include("set_types.jl") -end \ No newline at end of file +end From b383f06a5d66081e4fd962704f5cde91abc1e25d Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 2 Jan 2024 11:37:42 -0500 Subject: [PATCH 132/156] Add comments about set_eltype and set_ndim function --- NDTensors/src/lib/SetParameters/TODO.md | 9 +++++++++ .../UnallocatedArrays/src/abstractunallocatedarray.jl | 3 +++ 2 files changed, 12 insertions(+) diff --git a/NDTensors/src/lib/SetParameters/TODO.md b/NDTensors/src/lib/SetParameters/TODO.md index 264165241c..f20ba6b57d 100644 --- a/NDTensors/src/lib/SetParameters/TODO.md +++ b/NDTensors/src/lib/SetParameters/TODO.md @@ -27,3 +27,12 @@ default_parameter(::Type{<:AbstractArray}, ::Position{2}) = 1 nparameters(::Type{<:AbstractArray}) = Val(2) ``` + +https://github.com/ITensor/ITensors.jl/pull/1213/files#r1431708585 + +# Create generic set_eltype and set_ndims functions which can be defined on +# Array structures and use the `set_parameter` +```julia +set_eltype(T::Type, elt::Type) = Error("set_eltype is not defined for datatype $T") +set_eltype(T::Type{<:Array}, elt::Type) = set_parameter(T, Position{1}(), elt) +``` \ No newline at end of file diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 4bacd893e1..35e6386d5b 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -43,6 +43,9 @@ function allocate(f::UnallocatedArray) end function allocate(arraytype::Type{<:AbstractArray}, elt::Type, axes::Tuple) + ## TODO rewrite this using set_eltype and set_ndims functions + ## currently these functions are defined in `NDTensors` + ## In the future they should be defined in `SetParameters` ArrayT = set_parameters(arraytype, Position{1}(), elt) ArrayT = set_parameters(ArrayT, Position{2}(), length(axes)) return similar(ArrayT, axes) From 7de3788bd434e2cd1dea3f87a7a6dcc3fafea5fd Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Fri, 5 Jan 2024 15:28:21 -0500 Subject: [PATCH 133/156] Bump to jenkins to julia 1.10 --- jenkins/Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index fb14fca47f..c252e00998 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -34,7 +34,7 @@ pipeline { ''' } } - stage('julia-1.9') { + stage('julia-1.10') { options { timeout(time: 45, unit: 'MINUTES') } From 2b48d3eb9da8f89fb6bc9cd79665f6f3f6c23dd3 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 8 Jan 2024 15:15:36 -0500 Subject: [PATCH 134/156] Remove type constraint in `set_alloctype` --- NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl | 2 +- NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 142a2ff10a..0d8f28b23c 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -24,7 +24,7 @@ function UnallocatedFill(f::Fill, alloc) return UnallocatedFill{eltype(f)}(f, alloc) end -set_alloctype(f::Fill, alloc::Type{<:AbstractArray}) = UnallocatedFill(f, alloc) +set_alloctype(f::Fill, alloc::Type) = UnallocatedFill(f, alloc) Base.parent(F::UnallocatedFill) = F.f diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 21fc92bb50..164611814a 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -34,7 +34,7 @@ function UnallocatedZeros(z::Zeros, alloc) return UnallocatedZeros{eltype(z)}(z, alloc) end -set_alloctype(f::Zeros, alloc::Type{<:AbstractArray}) = UnallocatedZeros(f, alloc) +set_alloctype(f::Zeros, alloc::Type) = UnallocatedZeros(f, alloc) Base.parent(Z::UnallocatedZeros) = Z.z From 108d8d9c87753d44678b1ccc990c37656a37d5ba Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 8 Jan 2024 15:16:51 -0500 Subject: [PATCH 135/156] No parenthasis around assert --- NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl | 4 ++-- NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 0d8f28b23c..8357cce427 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -39,7 +39,7 @@ function FillArrays.mult_fill(a::UnallocatedFill, b, val, ax) end FillArrays.mult_fill(a, b::UnallocatedFill, val, ax) = mult_fill(b, a, val, ax) function FillArrays.mult_fill(a::UnallocatedFill, b::UnallocatedFill, val, ax) - @assert(alloctype(a) == alloctype(b)) + @assert alloctype(a) == alloctype(b) return UnallocatedFill(Fill(val, ax), alloctype(a)) end @@ -47,7 +47,7 @@ function FillArrays.broadcasted_fill(f, a::UnallocatedFill, val, ax) return UnallocatedFill(Fill(val, ax), alloctype(a)) end function FillArrays.broadcasted_fill(f, a::UnallocatedFill, b::UnallocatedFill, val, ax) - @assert(alloctype(a) == alloctype(b)) + @assert alloctype(a) == alloctype(b) return UnallocatedFill(Fill(val, ax), alloctype(a)) end diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 164611814a..c9f00837d7 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -48,7 +48,7 @@ function FillArrays.mult_zeros(a::UnallocatedZeros, b, elt, ax) end FillArrays.mult_zeros(a, b::UnallocatedZeros, elt, ax) = mult_zeros(b, a, elt, ax) function FillArrays.mult_zeros(a::UnallocatedZeros, b::UnallocatedZeros, elt, ax) - @assert(alloctype(a) == alloctype(b)) + @assert alloctype(a) == alloctype(b) return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end @@ -56,7 +56,7 @@ function FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, elt, ax) return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end function FillArrays.broadcasted_zeros(f, a::UnallocatedZeros, b::UnallocatedZeros, elt, ax) - @assert(alloctype(a) == alloctype(b)) + @assert alloctype(a) == alloctype(b) return UnallocatedZeros(Zeros{elt}(ax), alloctype(a)) end From ca5bb48fc8b2d0994ea50f72d24c7320894737b7 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 8 Jan 2024 16:44:48 -0500 Subject: [PATCH 136/156] Define `similar(unallocated, eltype, size)` as is called by `similar(array)` --- .../lib/UnallocatedArrays/src/abstractunallocatedarray.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 35e6386d5b..3326826d85 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -47,12 +47,11 @@ function allocate(arraytype::Type{<:AbstractArray}, elt::Type, axes::Tuple) ## currently these functions are defined in `NDTensors` ## In the future they should be defined in `SetParameters` ArrayT = set_parameters(arraytype, Position{1}(), elt) - ArrayT = set_parameters(ArrayT, Position{2}(), length(axes)) return similar(ArrayT, axes) end -function Base.similar(f::UnallocatedArray) - return allocate(alloctype(f), eltype(f), axes(f)) +function Base.similar(f::UnallocatedArray, elt::Type, axes::Tuple{Int64, Vararg{Int64}}) + return allocate(alloctype(f), elt, axes) end ## TODO fix this because reshape loses alloctype From b63f603ff2e5d46002d8478ac5410d9c5d19d7f4 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 8 Jan 2024 16:44:59 -0500 Subject: [PATCH 137/156] Remove unecessary broadcast call --- .../src/lib/UnallocatedArrays/src/unallocatedzeros.jl | 8 -------- 1 file changed, 8 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index c9f00837d7..62c1a2dcc1 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -92,11 +92,3 @@ end function FillArrays.kron_fill(a::UnallocatedFill, b::UnallocatedZeros, val, ax) return kron_fill(b, a, val, ax) end - -function Base.Broadcast.broadcasted( - ::Base.Broadcast.DefaultArrayStyle, op, r::UnallocatedZeros -) - elt = typeof(op(getindex_value(r))) - z = Zeros(elt, axes(r)) - return set_alloctype(z, set_parameters(alloctype(r), Position{1}(), eltype(z))) -end From 4df754cb88a31a511fc17ec5ad2aadefc45bf792 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 8 Jan 2024 16:51:06 -0500 Subject: [PATCH 138/156] Remove `::tuple` from `allocate` --- .../src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 3326826d85..415598768b 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -42,7 +42,7 @@ function allocate(f::UnallocatedArray) return a end -function allocate(arraytype::Type{<:AbstractArray}, elt::Type, axes::Tuple) +function allocate(arraytype::Type{<:AbstractArray}, elt::Type, axes) ## TODO rewrite this using set_eltype and set_ndims functions ## currently these functions are defined in `NDTensors` ## In the future they should be defined in `SetParameters` From 9646e5deb2fc63beac3cabfb3c7e62fe7c6fff27 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 8 Jan 2024 17:14:46 -0500 Subject: [PATCH 139/156] loosen tollerance on a contract result --- NDTensors/src/lib/TensorAlgebra/test/runtests.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/lib/TensorAlgebra/test/runtests.jl b/NDTensors/src/lib/TensorAlgebra/test/runtests.jl index 1f643a58ad..71bab4936c 100644 --- a/NDTensors/src/lib/TensorAlgebra/test/runtests.jl +++ b/NDTensors/src/lib/TensorAlgebra/test/runtests.jl @@ -163,7 +163,9 @@ end a_dest_tensoroperations = TensorOperations.tensorcontract( labels_dest, a1, labels1, a2, labels2 ) - @test a_dest ≈ α * a_dest_tensoroperations + β * a_dest_init rtol = default_rtol( + ## Here we loosened the tolerance because of some floating point roundoff issue. + ## with Float32 numbers + @test a_dest ≈ α * a_dest_tensoroperations + β * a_dest_init rtol = 10 * default_rtol( elt_dest ) end From 26d7699343428cf253a0931136db9619de103e5c Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 8 Jan 2024 17:15:24 -0500 Subject: [PATCH 140/156] Don't test if an element isn't 3 when constructing with similar. Test other things --- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 341b80180d..53e0e6386a 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -48,7 +48,10 @@ using .NDTensorsTestUtils: devices_list @test norm(Fp) ≈ norm(F) Fs = similar(F) @test Fs isa alloctype(F) - @test Fs[1, 1, 1] != elt(3) + @test length(Fs) == 2 * 3 * 4 + Fs[1,1,1] = elt(10) + @test Fs[1,1,1] == elt(10) + Fp = set_alloctype(f, dev(Array{elt,ndims(f)})) @test allocate(Fp) isa dev(Array{elt,ndims(f)}) From 8ae8539c2649f633507f6d89d733729f4c630d8d Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Mon, 8 Jan 2024 17:22:39 -0500 Subject: [PATCH 141/156] format --- NDTensors/src/lib/TensorAlgebra/test/runtests.jl | 5 ++--- .../lib/UnallocatedArrays/src/abstractunallocatedarray.jl | 2 +- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 5 ++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/NDTensors/src/lib/TensorAlgebra/test/runtests.jl b/NDTensors/src/lib/TensorAlgebra/test/runtests.jl index 71bab4936c..5e2cf60a66 100644 --- a/NDTensors/src/lib/TensorAlgebra/test/runtests.jl +++ b/NDTensors/src/lib/TensorAlgebra/test/runtests.jl @@ -165,9 +165,8 @@ end ) ## Here we loosened the tolerance because of some floating point roundoff issue. ## with Float32 numbers - @test a_dest ≈ α * a_dest_tensoroperations + β * a_dest_init rtol = 10 * default_rtol( - elt_dest - ) + @test a_dest ≈ α * a_dest_tensoroperations + β * a_dest_init rtol = + 10 * default_rtol(elt_dest) end end @testset "qr (eltype=$elt)" for elt in elts diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 415598768b..0b675d2846 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -50,7 +50,7 @@ function allocate(arraytype::Type{<:AbstractArray}, elt::Type, axes) return similar(ArrayT, axes) end -function Base.similar(f::UnallocatedArray, elt::Type, axes::Tuple{Int64, Vararg{Int64}}) +function Base.similar(f::UnallocatedArray, elt::Type, axes::Tuple{Int64,Vararg{Int64}}) return allocate(alloctype(f), elt, axes) end diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 53e0e6386a..520a2a732c 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -49,9 +49,8 @@ using .NDTensorsTestUtils: devices_list Fs = similar(F) @test Fs isa alloctype(F) @test length(Fs) == 2 * 3 * 4 - Fs[1,1,1] = elt(10) - @test Fs[1,1,1] == elt(10) - + Fs[1, 1, 1] = elt(10) + @test Fs[1, 1, 1] == elt(10) Fp = set_alloctype(f, dev(Array{elt,ndims(f)})) @test allocate(Fp) isa dev(Array{elt,ndims(f)}) From 2f852381bbae6f181d8458ff7101712148aca33e Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 9 Jan 2024 10:51:51 -0500 Subject: [PATCH 142/156] Move Type based constructor out of UnallocatedX --- .../src/lib/UnallocatedArrays/src/unallocatedfill.jl | 12 +++--------- .../lib/UnallocatedArrays/src/unallocatedzeros.jl | 12 +++--------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 8357cce427..8070b71242 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -4,12 +4,10 @@ using NDTensors.SetParameters: Position, set_parameters struct UnallocatedFill{ElT,N,Axes,Alloc} <: AbstractFill{ElT,N,Axes} f::Fill{ElT,N,Axes} alloc::Alloc +end - function UnallocatedFill{ElT,N,Axes}( - f::Fill, alloc::Type{<:AbstractArray{ElT,N}} - ) where {ElT,N,Axes} - return new{ElT,N,Axes,Type{alloc}}(f, alloc) - end +function UnallocatedFill{ElT,N,Axes}(f::Fill, alloc::Type) where {ElT,N,Axes} + return new{ElT,N,Axes,Type{alloc}}(f, alloc) end function UnallocatedFill{ElT,N}(f::Fill, alloc) where {ElT,N} @@ -20,10 +18,6 @@ function UnallocatedFill{ElT}(f::Fill, alloc) where {ElT} return UnallocatedFill{ElT,ndims(f)}(f, alloc) end -function UnallocatedFill(f::Fill, alloc) - return UnallocatedFill{eltype(f)}(f, alloc) -end - set_alloctype(f::Fill, alloc::Type) = UnallocatedFill(f, alloc) Base.parent(F::UnallocatedFill) = F.f diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl index 62c1a2dcc1..66f9876e19 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedzeros.jl @@ -14,12 +14,10 @@ using NDTensors.SetParameters: Position, set_parameters struct UnallocatedZeros{ElT,N,Axes,Alloc} <: AbstractZeros{ElT,N,Axes} z::Zeros{ElT,N,Axes} alloc::Alloc +end - function UnallocatedZeros{ElT,N,Axes}( - z::Zeros, alloc::Type{<:AbstractArray{ElT,N}} - ) where {ElT,N,Axes} - return new{ElT,N,Axes,Type{alloc}}(z, alloc) - end +function UnallocatedZeros{ElT,N,Axes}(z::Zeros, alloc::Type) where {ElT,N,Axes} + return new{ElT,N,Axes,Type{alloc}}(z, alloc) end function UnallocatedZeros{ElT,N}(z::Zeros, alloc) where {ElT,N} @@ -30,10 +28,6 @@ function UnallocatedZeros{ElT}(z::Zeros, alloc) where {ElT} return UnallocatedZeros{ElT,ndims(z)}(z, alloc) end -function UnallocatedZeros(z::Zeros, alloc) - return UnallocatedZeros{eltype(z)}(z, alloc) -end - set_alloctype(f::Zeros, alloc::Type) = UnallocatedZeros(f, alloc) Base.parent(Z::UnallocatedZeros) = Z.z From 209ce52b893b3fddef7a8b3766335da6d9a2067b Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 9 Jan 2024 10:58:38 -0500 Subject: [PATCH 143/156] Add tests where parameters are unset --- .../src/lib/UnallocatedArrays/test/runtests.jl | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 520a2a732c..446fe0027e 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -3,7 +3,7 @@ using FillArrays: FillArrays, AbstractFill, Fill, Zeros using NDTensors: NDTensors using NDTensors.UnallocatedArrays using LinearAlgebra: norm -using Test: @test, @testset +using Test: @test, @testset, @test_broken include(joinpath(pkgdir(NDTensors), "test", "NDTensorsTestUtils", "NDTensorsTestUtils.jl")) using .NDTensorsTestUtils: devices_list @@ -33,6 +33,11 @@ using .NDTensorsTestUtils: devices_list Zs = similar(Z) @test Zs isa alloctype(Z) + Z = UnallocatedZeros(z, dev(Array)) + Za = allocate(Z) + @test Za isa dev(Array{elt,2}) + @test Za[1, 3] == zero(elt) + ######################################### # UnallocatedFill f = Fill{elt}(3, (2, 3, 4)) @@ -62,6 +67,15 @@ using .NDTensorsTestUtils: devices_list @test typeof(Fc) == alloctype(complex(F)) Fc[2, 3, 4] = elt(0) @test iszero(Fc[2, 3, 4]) + + F = UnallocatedFill(f, dev(Array)) + Fa = allocate(F) + @test Fa isa dev(Array{elt,3}) + @test Fa[2, 1, 4] == elt(3) + + F = UnallocatedFill(f, dev(Vector)) + ## This is expected to fail because similar(Vector, (2,3,4)) is not defined + @test_broken Fa = allocate(F) end @testset "Multiplication" begin From c538803e39e81593b8659e8d20ba9bf85cb492f1 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 9 Jan 2024 11:49:27 -0500 Subject: [PATCH 144/156] Fix a typo --- jenkins/Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index c252e00998..cc9f211f88 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -43,7 +43,7 @@ pipeline { label 'gpu&&v100' filename 'Dockerfile' dir 'jenkins' - additionalBuildArgs '--build-arg JULIA=1.9' + additionalBuildArgs '--build-arg JULIA=1.10' args '--gpus "device=1"' } } From 35d01a5ba5be346bb3f2c8912bbd69f2b81a1b0b Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 9 Jan 2024 14:45:16 -0500 Subject: [PATCH 145/156] Remove test and add todo --- NDTensors/src/lib/UnspecifiedTypes/TODO.md | 6 ++++++ NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 NDTensors/src/lib/UnspecifiedTypes/TODO.md diff --git a/NDTensors/src/lib/UnspecifiedTypes/TODO.md b/NDTensors/src/lib/UnspecifiedTypes/TODO.md new file mode 100644 index 0000000000..f836b420e3 --- /dev/null +++ b/NDTensors/src/lib/UnspecifiedTypes/TODO.md @@ -0,0 +1,6 @@ +## TODO +Currently this code is relatively unimplemented things we need to do are +1. Fully implement all of the types +2. Make sure these types work properly with infrastructure like `UnallocatedArrays`... +3. Make sure type promotion works as expected for each `UnallocatedType` +4. Make a test suite for the module \ No newline at end of file diff --git a/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl b/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl index 700ad9fac2..d32c60a449 100644 --- a/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl +++ b/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl @@ -1,9 +1,8 @@ -## TODO This needs work here @eval module $(gensym()) using NDTensors.UnspecifiedTypes using Test: @test, @testset @testset "Testing UnspecifiedTypes" begin - UA = UnspecifiedArray{} + end end From d0d27ba74e653a1782afb1989690ae7f8eb30927 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 9 Jan 2024 14:45:30 -0500 Subject: [PATCH 146/156] format --- NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl b/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl index d32c60a449..f84abb1257 100644 --- a/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl +++ b/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl @@ -3,6 +3,5 @@ using NDTensors.UnspecifiedTypes using Test: @test, @testset @testset "Testing UnspecifiedTypes" begin - end end From abe185ed6b783bb6efeb7dc0aea766b195189894 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Tue, 9 Jan 2024 14:49:57 -0500 Subject: [PATCH 147/156] format --- NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl b/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl index f84abb1257..e9649c3c50 100644 --- a/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl +++ b/NDTensors/src/lib/UnspecifiedTypes/test/runtests.jl @@ -2,6 +2,5 @@ using NDTensors.UnspecifiedTypes using Test: @test, @testset -@testset "Testing UnspecifiedTypes" begin -end +@testset "Testing UnspecifiedTypes" begin end end From c1f1410fa37959cf2acd6c54fa911bca38d16804 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 10 Jan 2024 14:01:45 -0500 Subject: [PATCH 148/156] Add broadcast functions for `UnallocatedZeros` to preserve type --- .../src/UnallocatedArrays.jl | 1 + .../lib/UnallocatedArrays/src/broadcast.jl | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl diff --git a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl index c9b747575d..fb95f4ec21 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/UnallocatedArrays.jl @@ -3,6 +3,7 @@ include("abstractfill/abstractfill.jl") include("unallocatedfill.jl") include("unallocatedzeros.jl") +include("broadcast.jl") include("abstractunallocatedarray.jl") include("set_types.jl") diff --git a/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl b/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl new file mode 100644 index 0000000000..5ac51ef9fd --- /dev/null +++ b/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl @@ -0,0 +1,23 @@ +abstract type ZeroPreserving end +struct IsZeroPreserving <: ZeroPreserving end +struct NotZeroPreserving <: ZeroPreserving end + +# Assume operations don't preserve zeros for safety +ZeroPreserving(x) = NotZeroPreserving() +@eval for type in (complex, Complex, Float16, Float32, Float64, Int) + ZeroPreserving(::typeof($type)) = IsZeroPreserving() +end + +function Broadcast.broadcasted(style::Broadcast.DefaultArrayStyle, f, a::UnallocatedZeros) + return _broadcasted(style, f, ZeroPreserving(f), a) +end + +function _broadcasted(style::Broadcast.DefaultArrayStyle, f, ::IsZeroPreserving, a::UnallocatedZeros) + z = f.(parent(a)) + return FillArrays.broadcasted_zeros(f, a, eltype(z), axes(z)) +end + +function _broadcasted(style::Broadcast.DefaultArrayStyle, f, ::NotZeroPreserving, a::UnallocatedZeros) + f = f.(parent(a)) + return FillArrays.broadcasted_fill(f, a, FillArrays.getindex_value(f), axes(f)) +end \ No newline at end of file From bc627237a3759d9c39850b61e790af1b8f1f4bee Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 10 Jan 2024 16:03:10 -0500 Subject: [PATCH 149/156] To ensure that FillArrays can be allocated, set ndims as well as eltype --- .../src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl index 0b675d2846..ec887f55ca 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/abstractunallocatedarray.jl @@ -47,6 +47,7 @@ function allocate(arraytype::Type{<:AbstractArray}, elt::Type, axes) ## currently these functions are defined in `NDTensors` ## In the future they should be defined in `SetParameters` ArrayT = set_parameters(arraytype, Position{1}(), elt) + ArrayT = set_parameters(ArrayT, Position{2}(), length(axes)) return similar(ArrayT, axes) end From a34337d291a80e16796b2196e5b0246103bf40bf Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 10 Jan 2024 16:04:56 -0500 Subject: [PATCH 150/156] Update broadcast functions --- .../lib/UnallocatedArrays/src/broadcast.jl | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl b/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl index 5ac51ef9fd..0a9665bb68 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl @@ -4,20 +4,23 @@ struct NotZeroPreserving <: ZeroPreserving end # Assume operations don't preserve zeros for safety ZeroPreserving(x) = NotZeroPreserving() -@eval for type in (complex, Complex, Float16, Float32, Float64, Int) - ZeroPreserving(::typeof($type)) = IsZeroPreserving() -end +ZeroPreserving(::typeof(Complex)) = IsZeroPreserving() +ZeroPreserving(::typeof(Real)) = IsZeroPreserving() function Broadcast.broadcasted(style::Broadcast.DefaultArrayStyle, f, a::UnallocatedZeros) return _broadcasted(style, f, ZeroPreserving(f), a) end -function _broadcasted(style::Broadcast.DefaultArrayStyle, f, ::IsZeroPreserving, a::UnallocatedZeros) - z = f.(parent(a)) +function _broadcasted( + style::Broadcast.DefaultArrayStyle, f, ::IsZeroPreserving, a::UnallocatedZeros +) + z = f.(parent(a)) return FillArrays.broadcasted_zeros(f, a, eltype(z), axes(z)) end -function _broadcasted(style::Broadcast.DefaultArrayStyle, f, ::NotZeroPreserving, a::UnallocatedZeros) - f = f.(parent(a)) +function _broadcasted( + style::Broadcast.DefaultArrayStyle, f, ::NotZeroPreserving, a::UnallocatedZeros +) + f = f.(parent(a)) return FillArrays.broadcasted_fill(f, a, FillArrays.getindex_value(f), axes(f)) -end \ No newline at end of file +end From 777fd847494c0b93a5ae9e231451b704f065f371 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 10 Jan 2024 16:05:43 -0500 Subject: [PATCH 151/156] Add more tests to UnallocatedArrays --- .../lib/UnallocatedArrays/test/runtests.jl | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 446fe0027e..3a88c73755 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -1,4 +1,6 @@ @eval module $(gensym()) +using Pkg +Pkg.activate() using FillArrays: FillArrays, AbstractFill, Fill, Zeros using NDTensors: NDTensors using NDTensors.UnallocatedArrays @@ -13,7 +15,7 @@ using .NDTensorsTestUtils: devices_list @testset "Basic funcitonality" begin z = Zeros{elt}((2, 3)) - Z = UnallocatedZeros(z, dev(Matrix{eltype(z)})) + Z = UnallocatedZeros(z, dev(Matrix{elt})) @test Z isa AbstractFill @test size(Z) == (2, 3) @@ -29,6 +31,8 @@ using .NDTensorsTestUtils: devices_list Zc = complex(Z) @test eltype(Zc) == complex(eltype(z)) @test iszero(Zc[1, 2]) + @test Zc isa UnallocatedZeros + @test alloctype(Zc) == alloctype(Z) Zs = similar(Z) @test Zs isa alloctype(Z) @@ -74,13 +78,14 @@ using .NDTensorsTestUtils: devices_list @test Fa[2, 1, 4] == elt(3) F = UnallocatedFill(f, dev(Vector)) - ## This is expected to fail because similar(Vector, (2,3,4)) is not defined - @test_broken Fa = allocate(F) + Fa = allocate(F) + @test ndims(Fa) == 3 + @test Fa isa dev(Array) end @testset "Multiplication" begin z = Zeros{elt}((2, 3)) - Z = UnallocatedZeros(z, dev(Matrix{eltype(z)})) + Z = UnallocatedZeros(z, dev(Matrix{elt})) R = Z * Z' @test R isa UnallocatedZeros @@ -152,6 +157,16 @@ using .NDTensorsTestUtils: devices_list R = Z .+ elt(2) @test R isa UnallocatedFill @test alloctype(R) == alloctype(Z) + + R = (x -> x .+ 1).(Z) + @test R isa UnallocatedFill + @test alloctype(R) == alloctype(Z) + @test R[1, 1] == elt(1) + + Z .*= 1.0 + @test Z isa UnallocatedZeros + @test alloctype(R) == dev(Matrix{elt}) + @test Z[2, 1] == zero(elt) ######################## # UnallocatedFill f = Fill(elt(3), (2, 3, 4)) @@ -191,6 +206,12 @@ using .NDTensorsTestUtils: devices_list F = UnallocatedFill(Fill(elt(2), (2, 3)), dev(Matrix{elt})) R = F + F @test R isa UnallocatedFill + @test R[1, 3] == elt(4) + + R = (x -> x .+ 1).(F) + @test R isa UnallocatedFill + @test R[2, 1] == elt(3) + @test alloctype(R) == alloctype(F) end ## TODO make other kron tests From 529c45027bc4a10c0d28405f89e1ddd6669f9b4d Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 10 Jan 2024 17:16:28 -0500 Subject: [PATCH 152/156] Remove inline `FillArrays.` --- NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl b/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl index 0a9665bb68..7e77955c7a 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl @@ -1,3 +1,5 @@ +using FillArrays: broadcasted_fill, broadcasted_zeros, getindex_value + abstract type ZeroPreserving end struct IsZeroPreserving <: ZeroPreserving end struct NotZeroPreserving <: ZeroPreserving end @@ -15,12 +17,12 @@ function _broadcasted( style::Broadcast.DefaultArrayStyle, f, ::IsZeroPreserving, a::UnallocatedZeros ) z = f.(parent(a)) - return FillArrays.broadcasted_zeros(f, a, eltype(z), axes(z)) + return broadcasted_zeros(f, a, eltype(z), axes(z)) end function _broadcasted( style::Broadcast.DefaultArrayStyle, f, ::NotZeroPreserving, a::UnallocatedZeros ) f = f.(parent(a)) - return FillArrays.broadcasted_fill(f, a, FillArrays.getindex_value(f), axes(f)) + return broadcasted_fill(f, a, getindex_value(f), axes(f)) end From a8f21fc942d6f7ebdb9f5cf948c791b3f5ab71e5 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Wed, 10 Jan 2024 22:35:00 -0500 Subject: [PATCH 153/156] Use broadcast fill in `UnallocatedFill` --- NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl | 4 ++-- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 8070b71242..3c99c0b62d 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -1,4 +1,4 @@ -using FillArrays: FillArrays, AbstractFill, Fill, broadcasted_fill, kron_fill, mult_fill +using FillArrays: FillArrays, AbstractFill, Fill, broadcasted_fill, getindex_value, kron_fill, mult_fill using NDTensors.SetParameters: Position, set_parameters struct UnallocatedFill{ElT,N,Axes,Alloc} <: AbstractFill{ElT,N,Axes} @@ -63,5 +63,5 @@ function Base.Broadcast.broadcasted( ::Base.Broadcast.DefaultArrayStyle, op, r::UnallocatedFill ) f = op.(parent(r)) - return set_alloctype(f, set_parameters(alloctype(r), Position{1}(), eltype(f))) + return broadcasted_fill(op, r, getindex_value(f), axes(f)) end diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 3a88c73755..16a28a1ec6 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -1,6 +1,4 @@ @eval module $(gensym()) -using Pkg -Pkg.activate() using FillArrays: FillArrays, AbstractFill, Fill, Zeros using NDTensors: NDTensors using NDTensors.UnallocatedArrays @@ -68,7 +66,10 @@ using .NDTensorsTestUtils: devices_list @test Fc == F Fc = allocate(complex(F)) @test eltype(Fc) == complex(eltype(F)) - @test typeof(Fc) == alloctype(complex(F)) + ## Here we no longer require the eltype of the alloctype to + ## Be the same as the eltype of the `UnallocatedArray`. It will be + ## replaced when the array is allocated + @test_broken typeof(Fc) == alloctype(complex(F)) Fc[2, 3, 4] = elt(0) @test iszero(Fc[2, 3, 4]) From 7bc4d0e1698eabd8586a7fa74373d36dbd577407 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Wed, 10 Jan 2024 22:38:12 -0500 Subject: [PATCH 154/156] Replace f with z --- NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl b/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl index 7e77955c7a..c5e98e71d4 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/broadcast.jl @@ -23,6 +23,6 @@ end function _broadcasted( style::Broadcast.DefaultArrayStyle, f, ::NotZeroPreserving, a::UnallocatedZeros ) - f = f.(parent(a)) - return broadcasted_fill(f, a, getindex_value(f), axes(f)) + z = f.(parent(a)) + return broadcasted_fill(f, a, getindex_value(z), axes(z)) end From f19e090a6aa49053889dd4a607e53571fca13348 Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Wed, 10 Jan 2024 22:38:30 -0500 Subject: [PATCH 155/156] Comment out test which is only occasionally broken --- NDTensors/src/lib/UnallocatedArrays/test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl index 16a28a1ec6..131d509ef6 100644 --- a/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl +++ b/NDTensors/src/lib/UnallocatedArrays/test/runtests.jl @@ -69,7 +69,7 @@ using .NDTensorsTestUtils: devices_list ## Here we no longer require the eltype of the alloctype to ## Be the same as the eltype of the `UnallocatedArray`. It will be ## replaced when the array is allocated - @test_broken typeof(Fc) == alloctype(complex(F)) + # @test_broken typeof(Fc) == alloctype(complex(F)) Fc[2, 3, 4] = elt(0) @test iszero(Fc[2, 3, 4]) From f8ed0135f47b1d993fa294b44e1c34b6961b3ffb Mon Sep 17 00:00:00 2001 From: Karl Pierce Date: Wed, 10 Jan 2024 22:38:46 -0500 Subject: [PATCH 156/156] format --- NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl index 3c99c0b62d..08aa7a4bc3 100644 --- a/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl +++ b/NDTensors/src/lib/UnallocatedArrays/src/unallocatedfill.jl @@ -1,4 +1,5 @@ -using FillArrays: FillArrays, AbstractFill, Fill, broadcasted_fill, getindex_value, kron_fill, mult_fill +using FillArrays: + FillArrays, AbstractFill, Fill, broadcasted_fill, getindex_value, kron_fill, mult_fill using NDTensors.SetParameters: Position, set_parameters struct UnallocatedFill{ElT,N,Axes,Alloc} <: AbstractFill{ElT,N,Axes}