想问一下,我在利用@distributed 对for循环进行多进程并行计算时,并行后的时间比未并行的时间还长,应该是没有并行成功。目前认为并行后时间变长了,是我想让程序并行,但没有并行成功,反而造成了多余的损耗。但现在我找不出没有并行成功的原因,所以想问一下。下面是一个示例代码,我的实际代码比较长,但主要就是用到了下面几个部分:
include("example.jl")
using .example
using Distributed
using SharedArrays
using LinearAlgebra
df=[1 2 3 4 5 6 7 8 9 10 11 22 23 24 25 26 27 28 29 30 31 32 1 12;
2 3 4 5 6 7 8 9 10 11 22 33 24 25 26 27 28 29 30 31 32 33 12 23;
1 3 5 7 9 11 13 15 17 19 20 40 22 24 26 28 30 32 34 36 38 40 1 21;
12 13 14 15 16 17 18 19 20 21 21 32 13 14 15 16 17 18 19 20 21 22 2 13]
a,b,c,d=foo(df)
using Distributed
addprocs()
@everywhere module example
using Distributed
using SharedArrays
using LinearAlgebra
export foo
function foo(df)
a=SharedArray{Int64,1}(size(df,2))
b=SharedArray{Int64,1}(size(df,2))
c=SharedArray{Int64,1}(size(df,2))
d=SharedArray{Int64,1}(size(df,2))
@inbounds @sync @distributed for i=1:size(df,2)
a[i]=df[1,i]
b[i]=df[2,i]
c[i]=df[3,i]
d[i]=df[4,i]
end
return a,b,c,d
end
end