From 7453e040651173b98b2493de31b6923e71024365 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Wed, 18 Oct 2023 14:18:13 -0400 Subject: [PATCH 01/14] 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 02/14] 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 9daa5c9bb4a8f7c35c492e963ba12247c94cd673 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 23 Oct 2023 09:10:33 -0400 Subject: [PATCH 03/14] updates to prevent elementwise operations on gpu --- NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl | 7 +++++++ NDTensors/src/linearalgebra/svd.jl | 13 ++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl b/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl index c144a4a802..442a61df41 100644 --- a/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl +++ b/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl @@ -11,3 +11,10 @@ function NDTensors.svd_catch_error(A::CuMatrix; alg="JacobiAlgorithm") end return USV end + +function NDTensors.truncate!(P::CuArray; kwargs...) + cpuP = NDTensors.cpu(P) + value = NDTensors.truncate!(cpuP; kwargs...) + P = adapt(typeof(P), cpuP) + return value; +end \ No newline at end of file diff --git a/NDTensors/src/linearalgebra/svd.jl b/NDTensors/src/linearalgebra/svd.jl index 6b9a09ae83..2e4749e49e 100644 --- a/NDTensors/src/linearalgebra/svd.jl +++ b/NDTensors/src/linearalgebra/svd.jl @@ -2,10 +2,11 @@ function checkSVDDone(S::AbstractArray, thresh::Float64) N = length(S) (N <= 1 || thresh < 0.0) && return (true, 1) - S1t = S[1] * thresh + Scpu = NDTensors.cpu(S) + S1t = Scpu[1] * thresh start = 2 while start <= N - (S[start] < S1t) && break + (Scpu[start] < S1t) && break start += 1 end if start >= N @@ -32,9 +33,11 @@ function svd_recursive(M::AbstractMatrix; thresh::Float64=1E-3, north_pass::Int= V = M' * U V, R = qr_positive(V) - for n in 1:Nd - D[n] = R[n, n] - end + n = size(R)[1] + D = diag(R, (n - Nd)) + # for n in 1:Nd + # D[n] = R[n, n] + # end (done, start) = checkSVDDone(D, thresh) From 41a17a983adf35a98c8ada8327af07b9ce901a70 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 23 Oct 2023 09:11:54 -0400 Subject: [PATCH 04/14] Changes to copy(dense) --- NDTensors/src/dense/dense.jl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/dense/dense.jl b/NDTensors/src/dense/dense.jl index 96afd695b5..de1ecf87e4 100644 --- a/NDTensors/src/dense/dense.jl +++ b/NDTensors/src/dense/dense.jl @@ -101,7 +101,16 @@ Dense(::Type{ElT}) where {ElT} = Dense{ElT}() setdata(D::Dense, ndata) = Dense(ndata) setdata(storagetype::Type{<:Dense}, data) = Dense(data) -copy(D::Dense) = Dense(copy(data(D))) +## There is an GPU arrays which are ReshapedArray can +## fail when trying to copy (fail meaning they call get_index which is slow) so this forces a fix. +## TODO make a better implementation +function copy(D::Dense) + d = data(D) + if d isa Base.ReshapedArray + return Dense(copy(parent(d))) + end + Dense(copy(data(D))) +end function Base.real(T::Type{<:Dense}) return set_datatype(T, similartype(datatype(T), real(eltype(T)))) From 281cef0f8988473ca780d0cf054e7a06203663bd Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 23 Oct 2023 09:13:07 -0400 Subject: [PATCH 05/14] Add comment --- NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl b/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl index 442a61df41..479a696298 100644 --- a/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl +++ b/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl @@ -12,6 +12,9 @@ function NDTensors.svd_catch_error(A::CuMatrix; alg="JacobiAlgorithm") return USV end +## TODO Here truncate does logical operations of the values in P +## So its more efficient to just make it a CPU vector and +## convert back to GPU function NDTensors.truncate!(P::CuArray; kwargs...) cpuP = NDTensors.cpu(P) value = NDTensors.truncate!(cpuP; kwargs...) From cd48a2f333498526e97f7c371c2e9af36577fcfd Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 23 Oct 2023 09:14:37 -0400 Subject: [PATCH 06/14] Add comment about change --- NDTensors/src/linearalgebra/svd.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NDTensors/src/linearalgebra/svd.jl b/NDTensors/src/linearalgebra/svd.jl index 2e4749e49e..8674f1e574 100644 --- a/NDTensors/src/linearalgebra/svd.jl +++ b/NDTensors/src/linearalgebra/svd.jl @@ -1,4 +1,7 @@ +## TODO here it looks at the elements of S so convert to CPU when on GPU +## Could write this as a GPU impl which just converts S to array. S +## is not used again so we don't need to convert back to GPU. function checkSVDDone(S::AbstractArray, thresh::Float64) N = length(S) (N <= 1 || thresh < 0.0) && return (true, 1) From ed80c9e65c9a62d16c8540daf8a3b5fb9be465ef Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 23 Oct 2023 09:18:27 -0400 Subject: [PATCH 07/14] remove fieldtypes --- NDTensors/src/FieldTypes/README.md | 3 - NDTensors/src/FieldTypes/src/FieldTypes.jl | 19 --- .../src/unallocatedarray/unallocatedfill.jl | 1 - .../src/unallocatedarray/unallocatedzeros.jl | 143 ------------------ .../src/unspecifiedarray/unspecifiedarray.jl | 1 - .../unspecifiednumber/unspecifiednumber.jl | 5 - .../src/unspecifiednumber/unspecifiedzero.jl | 39 ----- 7 files changed, 211 deletions(-) delete mode 100644 NDTensors/src/FieldTypes/README.md delete mode 100644 NDTensors/src/FieldTypes/src/FieldTypes.jl delete mode 100644 NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl delete mode 100644 NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl delete mode 100644 NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl delete mode 100644 NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiednumber.jl delete mode 100644 NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl 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/FieldTypes/src/unallocatedarray/unallocatedfill.jl b/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl deleted file mode 100644 index 70247a1cd6..0000000000 --- a/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedfill.jl +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index b23b396bf7..0000000000 --- a/NDTensors/src/FieldTypes/src/unallocatedarray/unallocatedzeros.jl +++ /dev/null @@ -1,143 +0,0 @@ -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 diff --git a/NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl b/NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl deleted file mode 100644 index e47e9832e5..0000000000 --- a/NDTensors/src/FieldTypes/src/unspecifiedarray/unspecifiedarray.jl +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index c44587b376..0000000000 --- a/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiednumber.jl +++ /dev/null @@ -1,5 +0,0 @@ -abstract type AbstractUnspecifiedNumber <: Number end - -struct UnspecifiedNumber{T} <: AbstractUnspecifiedNumber - value::T -end diff --git a/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl b/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl deleted file mode 100644 index 4ca1858218..0000000000 --- a/NDTensors/src/FieldTypes/src/unspecifiednumber/unspecifiedzero.jl +++ /dev/null @@ -1,39 +0,0 @@ -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)} From a0c8671b00257bfb57fe9ba540c97d6587384e92 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 23 Oct 2023 09:19:00 -0400 Subject: [PATCH 08/14] remove fillarrays --- NDTensors/Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/NDTensors/Project.toml b/NDTensors/Project.toml index 10b239334b..a5843f2ec6 100644 --- a/NDTensors/Project.toml +++ b/NDTensors/Project.toml @@ -8,7 +8,6 @@ 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" From a015beed100581305afd452c2bcb4486c5cd42e1 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 23 Oct 2023 09:19:37 -0400 Subject: [PATCH 09/14] remove fieldtypes --- NDTensors/src/NDTensors.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/NDTensors/src/NDTensors.jl b/NDTensors/src/NDTensors.jl index 8322fc8c4f..641253dec0 100644 --- a/NDTensors/src/NDTensors.jl +++ b/NDTensors/src/NDTensors.jl @@ -20,8 +20,6 @@ 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 8d00ea4b418f8041f10f9d0142c86f45d0d0f069 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 23 Oct 2023 09:20:36 -0400 Subject: [PATCH 10/14] remove commented code --- NDTensors/src/linearalgebra/svd.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/NDTensors/src/linearalgebra/svd.jl b/NDTensors/src/linearalgebra/svd.jl index 8674f1e574..d931076bd6 100644 --- a/NDTensors/src/linearalgebra/svd.jl +++ b/NDTensors/src/linearalgebra/svd.jl @@ -38,9 +38,6 @@ function svd_recursive(M::AbstractMatrix; thresh::Float64=1E-3, north_pass::Int= V, R = qr_positive(V) n = size(R)[1] D = diag(R, (n - Nd)) - # for n in 1:Nd - # D[n] = R[n, n] - # end (done, start) = checkSVDDone(D, thresh) From 1d579fe5bc457f7983543d7f62e948e12d369f4d Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 23 Oct 2023 09:22:05 -0400 Subject: [PATCH 11/14] format --- NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl | 4 ++-- NDTensors/src/dense/dense.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl b/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl index 479a696298..3bdc4ca1bf 100644 --- a/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl +++ b/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl @@ -19,5 +19,5 @@ function NDTensors.truncate!(P::CuArray; kwargs...) cpuP = NDTensors.cpu(P) value = NDTensors.truncate!(cpuP; kwargs...) P = adapt(typeof(P), cpuP) - return value; -end \ No newline at end of file + return value +end diff --git a/NDTensors/src/dense/dense.jl b/NDTensors/src/dense/dense.jl index de1ecf87e4..8ca6fad263 100644 --- a/NDTensors/src/dense/dense.jl +++ b/NDTensors/src/dense/dense.jl @@ -104,12 +104,12 @@ setdata(storagetype::Type{<:Dense}, data) = Dense(data) ## There is an GPU arrays which are ReshapedArray can ## fail when trying to copy (fail meaning they call get_index which is slow) so this forces a fix. ## TODO make a better implementation -function copy(D::Dense) +function copy(D::Dense) d = data(D) if d isa Base.ReshapedArray return Dense(copy(parent(d))) end - Dense(copy(data(D))) + return Dense(copy(data(D))) end function Base.real(T::Type{<:Dense}) From 0a901e8c8e3b511b5c6613eb279a0f8d2f9c90dc Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 23 Oct 2023 10:52:54 -0400 Subject: [PATCH 12/14] Make truncate!! to have a type based call of truncate! --- .../ext/NDTensorsCUDAExt/linearalgebra.jl | 10 --------- NDTensors/src/truncate.jl | 21 ++++++++++++++++++- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl b/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl index 3bdc4ca1bf..c144a4a802 100644 --- a/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl +++ b/NDTensors/ext/NDTensorsCUDAExt/linearalgebra.jl @@ -11,13 +11,3 @@ function NDTensors.svd_catch_error(A::CuMatrix; alg="JacobiAlgorithm") end return USV end - -## TODO Here truncate does logical operations of the values in P -## So its more efficient to just make it a CPU vector and -## convert back to GPU -function NDTensors.truncate!(P::CuArray; kwargs...) - cpuP = NDTensors.cpu(P) - value = NDTensors.truncate!(cpuP; kwargs...) - P = adapt(typeof(P), cpuP) - return value -end diff --git a/NDTensors/src/truncate.jl b/NDTensors/src/truncate.jl index 3acc2f2099..1933629a0c 100644 --- a/NDTensors/src/truncate.jl +++ b/NDTensors/src/truncate.jl @@ -1,4 +1,23 @@ -export truncate! +export truncate!!, truncate! + +## TODO Here truncate does logical operations of the values in P +## So its more efficient to just make it a CPU vector and +## convert back to GPU + +function truncate!!(P::AbstractVector; kwargs...) + truncate!(leaf_parenttype(P), P; kwargs...) +end + +function truncate!(::Type{<:Array}, P; kwargs...) + return truncate!(P; kwargs...) +end + +function truncate!(::Type{<:AbstractArray}, P; kwargs...) + P_cpu = cpu(P) + values = truncate!(P_cpu; kwargs...) + P = adapt(leaf_parenttype(P), P_cpu) + return values; +end function truncate!(P::AbstractVector{ElT}; kwargs...)::Tuple{ElT,ElT} where {ElT} cutoff::Union{Nothing,ElT} = get(kwargs, :cutoff, zero(ElT)) From da8b1935f72ab6af88cccffe71b197b0afb71aae Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 23 Oct 2023 11:01:42 -0400 Subject: [PATCH 13/14] use truncate!! --- NDTensors/src/blocksparse/linearalgebra.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NDTensors/src/blocksparse/linearalgebra.jl b/NDTensors/src/blocksparse/linearalgebra.jl index 2835d262c7..c1a34598a8 100644 --- a/NDTensors/src/blocksparse/linearalgebra.jl +++ b/NDTensors/src/blocksparse/linearalgebra.jl @@ -75,7 +75,7 @@ function LinearAlgebra.svd(T::BlockSparseMatrix{ElT}; kwargs...) where {ElT} dropblocks = Int[] if truncate - truncerr, docut = truncate!(d; kwargs...) + truncerr, docut = truncate!!(d; kwargs...) for n in 1:nnzblocks(T) blockdim = _truncated_blockdim( Ss[n], docut; min_blockdim, singular_values=true, truncate @@ -237,7 +237,7 @@ function LinearAlgebra.eigen( sort!(d; rev=true, by=abs) if truncate - truncerr, docut = truncate!(d; kwargs...) + truncerr, docut = truncate!!(d; kwargs...) for n in 1:nnzblocks(T) blockdim = _truncated_blockdim(Ds[n], docut) if blockdim == 0 From 17fb6bf19a6f0df0b6d57c729510e34529e886c1 Mon Sep 17 00:00:00 2001 From: kmp5VT Date: Mon, 23 Oct 2023 11:02:06 -0400 Subject: [PATCH 14/14] format --- NDTensors/src/truncate.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NDTensors/src/truncate.jl b/NDTensors/src/truncate.jl index 1933629a0c..b1c8947b21 100644 --- a/NDTensors/src/truncate.jl +++ b/NDTensors/src/truncate.jl @@ -5,7 +5,7 @@ export truncate!!, truncate! ## convert back to GPU function truncate!!(P::AbstractVector; kwargs...) - truncate!(leaf_parenttype(P), P; kwargs...) + return truncate!(leaf_parenttype(P), P; kwargs...) end function truncate!(::Type{<:Array}, P; kwargs...) @@ -16,7 +16,7 @@ function truncate!(::Type{<:AbstractArray}, P; kwargs...) P_cpu = cpu(P) values = truncate!(P_cpu; kwargs...) P = adapt(leaf_parenttype(P), P_cpu) - return values; + return values end function truncate!(P::AbstractVector{ElT}; kwargs...)::Tuple{ElT,ElT} where {ElT}