2014-12-05 27 views
4

我已經創建了我的上一個問題(Julia allocates huge amount of memory for unknown reason)的最低工作示例,將問題隔離開來。這可以直接在REPL中進行測試。考慮代碼:在@parallel中調用函數會導致巨大的內存分配

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 
+0

這不會發生在我身上。 'versioninfo()'說什麼? – rickhg12hs 2014-12-06 04:45:01

+0

@ rickhg12hs編輯添加Julia版本 – Nick 2014-12-06 07:53:37

回答

2

在各功能的第一次運行,分配是由於編譯:記得這麼多朱莉婭的JIT編譯器是寫在朱莉婭,並讓得到任何內存在編譯過程中消耗(主要是類型分析)包括在內。一旦函數編譯完成,這個分配就會消失。

對我來說,test2和test3在第二次運行時分配大約50K字節(使用julia -p 2)。

最後,並行版本分配一些額外內存的原因與@parallel的工作方式有關。它基本上必須從你的函數中創建一個「thunk」並將它傳遞給其他進程。這個thunk沒有預編譯,因爲它可能依賴於你作爲參數傳入的變量。

+0

test3爲您運行多久,以及您使用的是哪個版本的Julia?我試着在Linux機器上運行它3.2版本,而我仍然分配了9600711508字節,24.90秒(比test1和test2長得多) – Nick 2014-12-07 21:25:47

+0

在我用Julia 0.4.0-dev + 2043版本的老舊筆記本電腦上, 'addprocs(2)',我得到以下內容:(函數調用,近似時間(秒),分配的字節) - > [('test1(8)',9.8,72),('test2(8)',4.9 ,〜32k),('test3(8)',39,〜34k)] – rickhg12hs 2014-12-10 06:12:28

+0

@Nick在[JuliaBox](https://juliabox.org/)上試試你的代碼。也沒有看到鉅額撥款。它比我的筆記本電腦快得多。 – rickhg12hs 2014-12-10 06:19:16

相關問題