Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constructors to fix 321 #348

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion ext/SparseArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Finch
using Finch: AbstractCompiler, DefaultStyle, Extent
using Finch: Unfurled, Furlable, Stepper, Jumper, Run, Fill, Lookup, Simplify, Sequence, Phase, Thunk, Spike
using Finch: virtual_size, virtual_default, getstart, getstop, freshen
using Finch: virtual_size, virtual_default, getstart, getstop, freshen, SwizzleArray
using Finch.FinchNotation

using Base: @kwdef
Expand Down Expand Up @@ -34,6 +34,36 @@
return Fiber(SparseList{Ti}(Element{zero(Tv)}(arr.nzval), n, [1, length(arr.nzind) + 1], arr.nzind))
end

"""
SparseMatrixCSC(arr::Union{Fiber, SwizzleArray})

Construct a sparse matrix from a fiber or swizzle. May reuse the underlying storage if possible.
"""
function SparseArrays.SparseMatrixCSC(arr::Union{Fiber, SwizzleArray})
default(arr) === zero(eltype(arr)) || throw(ArgumentError("SparseArrays, a Julia stdlib, only supports zero default values, was given $(default(arr)) as default"))
return SparseMatrixCSC(Fiber!(Dense(SparseList(Element(0.0))), arr))
end

function SparseArrays.SparseMatrixCSC(arr::Fiber{<:Dense{Ti, <:SparseList{Ti, Ptr, Idx, <:Element{D, Tv}}}}) where {D, Ti, Ptr, Idx, Tv}
D === zero(Tv) || throw(ArgumentError("SparseArrays, a Julia stdlib, only supports zero default values, was given $D as default"))
return SparseMatrixCSC{Tv, Ti}(size(arr)..., arr.lvl.lvl.ptr, arr.lvl.lvl.idx, arr.lvl.lvl.lvl.val)
end

"""
sparse(arr::Union{Fiber, SwizzleArray})

Construct a SparseArray from a Fiber or Swizzle. May reuse the underlying storage if possible.
"""
function SparseArrays.sparse(fbr::Union{Fiber, SwizzleArray})
if ndims(fbr) == 1
return SparseVector(fbr)
elseif ndims(fbr) == 2
return SparseMatrixCSC(fbr)
else
throw(ArgumentError("SparseArrays, a Julia stdlib, only supports 1-D and 2-D arrays, was given a $(ndims(fbr))-D array"))

Check warning on line 63 in ext/SparseArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseArraysExt.jl#L63

Added line #L63 was not covered by tests
end
end

@kwdef mutable struct VirtualSparseMatrixCSC
ex
Tv
Expand Down Expand Up @@ -131,6 +161,20 @@
Finch.virtual_default(arr::VirtualSparseMatrixCSC, ctx) = zero(arr.Tv)
Finch.virtual_eltype(tns::VirtualSparseMatrixCSC, ctx) = tns.Tv

"""
SparseVector(arr::Union{Fiber, SwizzleArray})

Construct a sparse matrix from a fiber or swizzle. May reuse the underlying storage if possible.
"""
function SparseArrays.SparseVector(arr::Union{Fiber, SwizzleArray})
default(arr) === zero(eltype(arr)) || throw(ArgumentError("SparseArrays, a Julia stdlib, only supports zero default values, was given $(default(arr)) as default"))
return SparseVector(Fiber!(SparseList(Element(0.0)), arr))
end

function SparseArrays.SparseVector(arr::Fiber{<:SparseList{Ti, Ptr, Idx, <:Element{D, Tv}}}) where {Ti, Ptr, Idx, Tv, D}
D === zero(Tv) || throw(ArgumentError("SparseArrays, a Julia stdlib, only supports zero default values, was given $D as default"))
return SparseVector{Tv, Ti}(size(arr)..., arr.lvl.idx, arr.lvl.lvl.val)
end
@kwdef mutable struct VirtualSparseVector
ex
Tv
Expand Down
10 changes: 10 additions & 0 deletions src/interface/abstractarrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ end
default(a::AbstractArray) = default(typeof(a))
default(T::Type{<:AbstractArray}) = zero(eltype(T))

"""
Array(arr::Union{Fiber, SwizzleArray})

Construct an array from a fiber or swizzle. May reuse memory, will usually densify the fiber.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like the implementation here can't ever re-use memory; is the "may reuse memory" here so that this could be optimized later?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finch stores multidimensional dense arrays as vectors, so without adding a reshaped array operator, only vectors can share memory. However, in the future I'm not sure if it will be possible to reshape arrays without creating a reshapedarray, so I thought I'd leave the possibility open. At the very least, I could add a case for dense vectors.

"""
function Base.Array(fbr::Union{Fiber, SwizzleArray})
arr = Array{eltype(fbr)}(undef, size(fbr)...)
return copyto!(arr, fbr)
end

struct AsArray{T, N, Fbr} <: AbstractArray{T, N}
fbr::Fbr
function AsArray{T, N, Fbr}(fbr::Fbr) where {T, N, Fbr}
Expand Down
22 changes: 22 additions & 0 deletions test/test_issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,26 @@ using CIndices

@finch (output_tensor .=0; for j=_,i=_,k=_; output_tensor[i,k] += a_fiber[i,j] * b_fiber[k,j]; end)
end

#https://github.com/willow-ahrens/Finch.jl/issues/321
let
A = fsprand((10, 10), 0.1)
B = sparse(A)
@test B isa SparseMatrixCSC
@test B == A
B = SparseMatrixCSC(A)
@test B isa SparseMatrixCSC
@test B == A
A = fsprand((10,), 0.1)
B = sparse(A)
@test B isa SparseVector
@test B == A
B = SparseVector(A)
@test B isa SparseVector
@test B == A
A = fsprand((10, 10), 0.1)
B = Array(A)
@test B isa Array
@test B == A
end
end
Loading