Why does array + = (without @.) Create so much memory?

I don’t understand why the + = operation for arrays produces so much memory allocation, but it gets fixed when @ is applied.

function loop()
    a = randn(10)
    total = similar(a)

    for i=1:1000
        total += a
    end
end

function loopdot()
    a = randn(10)
    total = similar(a)

    for i=1:1000
        @. total += a
    end
end


loop()
loopdot()

Profile.clear_malloc_data()

loop()
loopdot()

produces

160000         total += a

and

0         @. total += a
+4
source share
1 answer

total += amatches with total = a + total, which is a vectorized type operation:

out = similar(a)
for i in eachindex(a)
  out[i] = total[i] + a[i]
end
total = out

as inside

total = +(total,a)

, MATLAB, Python R , , , , = total . , - NumPy , Python, C (- !).

@. total += a total .= total .+ a. , Julia , , :

# Build an anonymous function for the fused operation
f! = (a,b,c) -> (a[i] = b[i] + c[i])
# Now loop it, since it `.=` don't make a temporary
for i in eachindex(a)
  f!(total,total,a)
end

total .

Fusion Julia : broadcast! ( , ) , , . . broadcast! f!, GPUArrays.jl , . MATLAB, Python R, , , , , .

+8

Source: https://habr.com/ru/post/1691548/


All Articles