debugging - 在 @parallel 中调用函数会导致大量内存分配

标签 debugging memory memory-management julia

我已经为我之前的问题( Julia allocates huge amount of memory for unknown reason )创建了一个最小的工作示例,以隔离问题。这个可以直接在REPL中测试。考虑代码:

function test1(n)
    s = zero(Float64)
    for i = 1:10^n
        s += sqrt(rand()^2 + rand()^2 + rand()^2)
    end
    return s
end

--

function test2(n)
    @parallel (+) for i = 1:10^n
        sqrt(rand()^2 + rand()^2 +rand()^2)
    end
end

--

function test3(n)
    function add(one, two, three)
        one + two + three
    end

    @parallel (+) for i = 1:10^n
        sqrt(add(rand()^2, rand()^2, rand()^2))
    end
end

然后,我测试代码:

@time test1(8);
@time test1(8);

@time test2(8);
@time test2(8);

@time test3(8);
@time test3(8);

这是输出:

elapsed time: 1.017241708 seconds (183868 bytes allocated)
elapsed time: 1.033503964 seconds (96 bytes allocated)

elapsed time: 1.214897591 seconds (3682220 bytes allocated)
elapsed time: 1.020521156 seconds (2104 bytes allocated)

elapsed time: 15.23876415 seconds (9600679268 bytes allocated, 26.69% gc time)
elapsed time: 15.418865707 seconds (9600002736 bytes allocated, 26.19% gc time)

谁能解释一下:

  • 为什么每个函数的第一次运行会分配这么多内存?
  • 为什么test2(8)中分配的内存高于test1(8)?他们做同样的事情。
  • 最重要的是,test3(8) 到底发生了什么?它分配了大量的内存。

编辑:

Julia Version 0.3.1
Commit c03f413* (2014-09-21 21:30 UTC)
Platform Info:
  System: Darwin (x86_64-apple-darwin13.3.0)
  CPU: Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas
  LIBM: libopenlibm
  LLVM: libLLVM-3.3

最佳答案

在每个函数的第一次运行中,分配是由于编译造成的:请记住,julia 的 JIT 编译器的大部分都是用 julia 编写的,因此编译过程中消耗的任何内存(主要是类型分析)都会被包含在内。一旦函数被编译,这种分配就会消失。

对我来说,test2 和 test3 在第二次运行时分配了大约 50K 字节(使用 julia -p 2)。

最后,并行版本分配一些额外内存的原因与@parallel 的工作方式有关。它基本上必须从您的函数中创建一个“thunk”并将其传递给其他进程。此 thunk 未预编译,因为它可能依赖于您作为参数传入的变量。

关于debugging - 在 @parallel 中调用函数会导致大量内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27310927/

相关文章:

c# - 变量更改时通过调试进行错误跟踪

iphone - didReceiveMemoryWarning - 手动卸载 View ?

c - 如果两个指针指向同一个内存地址,是只需要使用free(ptr)一次还是两次?

python - pcolorfast 上的 matplotlib MemoryError

c++ - 内存中的视觉模式?

debugging - ARM Data Abort错误异常调试

wpf - 将 WPF 项目作为 WinForm 解决方案中的进程进行调试

memory - "unknown error"在 CUDA 中的 __device__ 函数内部使用动态分配

java - 如果 equals 方法返回 true,如何在内存中分配相同的空间?

java - 如何最大限度地减少应用程序使用的内存?