为什么pmap比map慢?

时间:2016-12-23 15:47:51

标签: parallel-processing julia

我试图通过以下代码使用pmap并行求解各种方程组:

#A function that returns an invertible matrix.
function invertible_matrix(n)
  A=zeros(n,n)
  while true
    A=rand(-10:10,n,n)#ones(Float64,n,n)
    try
      Ai=inv(A)
      break
    end
  end
  return A
end
################################################################################
parallel=true#; parallel=false
N=100 # The number of systems of equations that need to be solved.
n=1000# The number of variables and equations.
A=invertible_matrix(n)

A=[A for i in 1:N]#We transform A into a list of copies of the matrix A
# We generate the solutions of the systems before hand.
x0=[[float(i) for j in 1:n] for i in 1:N]

# We generate the right hand sides of the systems.
b=[ A[i]*x0[i] for i in 1:N]

#We solve all systems either in parallel or in series.
if parallel
  x=pmap(\,A,b)
else
  x=map(\,A,b)
end

s=sum(x)[1]
#This should print 5050.
println(s)

这应解决100个并联的1000个变量系统。如果我使用htop监视我的核心的使用,我可以验证运行$ julia -p 8 test.jl确实使用了我的所有处理器,但是它比仅使用$julia test.jl的核心需要更多的时间!这是我的试验

$ time julia test.jl 
5049.999977945663

real    0m27.934s
user    0m28.268s
sys 0m1.776s
$ time julia -p 8 test.jl 
5050.000001198174

real    0m39.169s
user    4m22.088s
sys 0m3.308s

如果我通过设置parallel=false来使用map而不是pmap,我会得到同样更快的结果:

$ time julia test.jl 5049.999999852108

real    0m26.691s
user    0m27.140s
sys 0m1.612s
$ time julia -p     8 test.jl 
5050.000000001826

real    0m32.142s
user    0m49.284s
sys 0m2.064s

为什么会这样?

0 个答案:

没有答案