I have a use-case of large sparse arrays that can be built in parallel. A minimum working example similar to my code is:
using Distributed
@everywhere using SparseArrays
@everywhere function build_array()
return sparsevec(Int32.(1:10), Int32(1), Int32(10))
end
function main()
full_array += @distributed (+) for i in 1:10
build_array()
end
end
main()
I’m trying to reduce RAM usage but noticing that summing two Int32 sparse arrays seems to lead to automatic conversion to Int64 counts. Just looking at REPL:
julia> one = sparsevec(Int32.(1:10), Int32(1), Int32(10))
10-element SparseVector{Int32,Int32} with 10 stored entries
compared to a sum:
julia> two = sparsevec(Int32.(1:10), Int32(1), Int32(10)) + sparsevec(Int32.(1:10), Int32(1), Int32(10))
10-element SparseVector{Int32,Int64} with 10 stored entries
Where you can see that the counting part of the SparseArray is now Int64 and varinfo()
shows a correspondingly higher memory use than an identical sparse(Int32, Int32) object.
julia> equiv = sparsevec(Int32.(1:10), Int32(2), Int32(10))
10-element SparseVector{Int32,Int32} with 10 stored entries
julia> varinfo(r"one")
name size summary
–––– ––––––––– ––––––––––––––––––––––––––––––––––––
one 184 bytes 10-element SparseVector{Int32,Int32}
julia> varinfo(r"two")
name size summary
–––– ––––––––– ––––––––––––––––––––––––––––––––––––
two 224 bytes 10-element SparseVector{Int32,Int64}
julia> varinfo(r"equiv")
name size summary
––––– ––––––––– ––––––––––––––––––––––––––––––––––––
equiv 184 bytes 10-element SparseVector{Int32,Int32}
I understand that in principle summing many Int32’s can lead to an Int64. However in my use case I can guarantee that the sum will stay <2 billion. I’m a bit new to using distributed workflows and sparse arrays and not quite sure what part of code needs to be improved (can the distributed reduction type be pre-specified? can SparseArray types be maintained? it doesn’t seem clear from documentation).
But would appreciate a way to maintain my construction in parallel while staying with Int32 types if that is possible. Thanks!