gpt4 book ai didi

performance - Julia ,为什么函数内的循环会发生内存分配?

转载 作者:行者123 更新时间:2023-12-05 01:29:00 25 4
gpt4 key购买 nike

julia --track-allocation=user的内存分配报告中,分配的最大值在这个函数中:

        - function fuzzy_dot_square( v::Array{Int64, 1} )
- dot_prod = zero(Int64)
7063056168 for i::Int64 in 2:28
0 dot_prod += v[i]*(v[i] + v[i-1] + v[i+1] + v[i+28])# / 4 # no "top" pixel
- end
0 for i in 29:(28*27) # compiler should literate 28*27
0 dot_prod += v[i]*(v[i] + v[i-1] + v[i+1] + v[i-28] + v[i+28])# / 5 # all pixels
- end
0 for i in (28*27):(28*28 - 1)
0 dot_prod += v[i]*(v[i] + v[i-1] + v[i+1] + v[i-28])# / 4 # no "bottom" pixel
- end
-
0 return dot_prod
- end

-- 它是向量的“模糊点积”平方,表示像素图像 28 x 28(已知的 MNIST 数字图像数据集)。

为什么分配发生在那里?据我了解, dot_prod 是唯一要分配的东西。但报告指出了第一个..

我也尝试在 repl 中复制它:

v = Array{Int64,1}(1:100)
dot_prod = zero(Int64)
@allocated for i in 2:28
dot_prod += v[i]
end

-- 我在 @allocated for ... 处收到以下错误:

ERROR: UndefVarError: dot_prod not defined
in macro expansion at ./REPL[3]:2 [inlined]
in (::##1#f#1)() at ./util.jl:256

@time 宏工作正常,所以 @allocated 中可能有一些错误?我有 julia 0.5.0

最佳答案

这是 --track-allocation=user 的限制。没有类型不稳定,也没有分配。

julia> function fuzzy_dot_square(v)
dot_prod = zero(eltype(v))
for i in 2:28
dot_prod += v[i]*(v[i] + v[i-1] + v[i+1] + v[i+28])# / 4 # no "top" pixel
end
for i in 29:(28*27) # compiler should literate 28*27
dot_prod += v[i]*(v[i] + v[i-1] + v[i+1] + v[i-28] + v[i+28])# / 5 # all pixels
end
for i in (28*27):(28*28 - 1)
dot_prod += v[i]*(v[i] + v[i-1] + v[i+1] + v[i-28])# / 4 # no "bottom" pixel
end
return dot_prod
end
fuzzy_dot_square (generic function with 1 method)

julia> const xs = [1:28^2;];

julia> @allocated fuzzy_dot_square(xs)
0

另见 Julia documentation 的这段话:

In interpreting the results, there are a few important details. Under the user setting, the first line of any function directly called from the REPL will exhibit allocation due to events that happen in the REPL code itself. More significantly, JIT-compilation also adds to allocation counts, because much of Julia’s compiler is written in Julia (and compilation usually requires memory allocation). The recommended procedure is to force compilation by executing all the commands you want to analyze, then call Profile.clear_malloc_data() to reset all allocation counters. Finally, execute the desired commands and quit Julia to trigger the generation of the .mem files.

如需更多信息,请参阅 this Julia issue .

关于performance - Julia ,为什么函数内的循环会发生内存分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39680490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com