oxinabox / AutoPreallocation.jl

What if your code allocated less? Remember what memory we needed last time and use it again every time after
MIT License
103 stars 4 forks source link

+ is not correctly recorded #17

Open Roger-luo opened 4 years ago

Roger-luo commented 4 years ago

I find this while writing documentation, it's strange since it seems not because of the BLACK_LIST

julia> A, B = (rand(100, 100) for _ in 1:2)
Base.Generator{UnitRange{Int64},var"#3#4"}(var"#3#4"(), 1:2)

julia> record_allocations(+, A, B)
(value = [1.0495016055493258 1.3273418213359485 … 0.5963185747522923 1.1503808755746068; 0.3158719236919174 1.5157179087539658 … 1.5485087164398046 0.4956053992013334; … ; 0.8055821535049874 1.0735544173517684 … 0.8744412783135622 0.9502501256860647; 1.1511881175529912 0.3931796416314606 … 1.2724735635978146 0.4405171223405957], allocation_record = AutoPreallocation.AllocationRecord(
    [],
    []
))

in principle this should record one Array, but it didn't, however, it works fine on broadcast, and the broadcast_preserving_zero_d(+, A, Bs...) that +(A, B) uses

julia> record_allocations(broadcast, +, A, B)
(value = [1.0495016055493258 1.3273418213359485 … 0.5963185747522923 1.1503808755746068; 0.3158719236919174 1.5157179087539658 … 1.5485087164398046 0.4956053992013334; … ; 0.8055821535049874 1.0735544173517684 … 0.8744412783135622 0.9502501256860647; 1.1511881175529912 0.3931796416314606 … 1.2724735635978146 0.4405171223405957], allocation_record = AutoPreallocation.AllocationRecord(
    [Array{Float64,2}(undef, (100, 100))],
    [(100, 100)]
))
julia> record_allocations(Base.broadcast_preserving_zero_d, +, A, B)
(value = [1.0495016055493258 1.3273418213359485 … 0.5963185747522923 1.1503808755746068; 0.3158719236919174 1.5157179087539658 … 1.5485087164398046 0.4956053992013334; … ; 0.8055821535049874 1.0735544173517684 … 0.8744412783135622 0.9502501256860647; 1.1511881175529912 0.3931796416314606 … 1.2724735635978146 0.4405171223405957], allocation_record = AutoPreallocation.AllocationRecord(
    [Array{Float64,2}(undef, (100, 100))],
    [(100, 100)]
))

I guess this might be caused by splitting?

oxinabox commented 4 years ago

oh weird, weird weird. Maybe there is a nother way to construct an Array that we are missing?

Roger-luo commented 4 years ago

I mean in principal one could workaround this by using .+ instead of +, but this is very strange indeed. I suspect this is due to the call of splatting that make Cassette escape that call? Not sure how to debug this however.