parallel-processing - BLAS v. Julia SharedArray 对象的并行更新

标签 parallel-processing julia blas

我有兴趣使用 Julia SharedArray s 用于科学计算项目。我当前的实现对所有矩阵向量运算都使用 BLAS,但我认为可能是 SharedArray会在多核机器上提供一些加速。我的想法是简单地逐个索引更新输出向量,将索引更新分配给工作进程。

以前的讨论 here关于 SharedArray s 和 here关于共享内存对象并没有就这个问题提供明确的指导。直观上看起来很简单,但经过测试,我对为什么这种方法效果如此差感到有些困惑(请参阅下面的代码)。对于初学者来说,它似乎是 @parallel for分配大量内存。如果我在循环前面加上 @sync ,如果稍后需要整个输出向量,这似乎是一件明智的事情,那么并行循环会显着变慢(尽管没有 @sync ,循环非常快)。

我是否错误地解释了 SharedArray 的正确使用?目的?或者我是否没有有效地分配计算?

### test for speed gain w/ SharedArray vs. Array ###

# problem dimensions
n = 10000; p = 25000

# set BLAS threads; 64 seems reasonable in testing
blas_set_num_threads(64)

# make normal Arrays
x = randn(n,p)
y = ones(p)
z = zeros(n)

# make SharedArrays
X = convert(SharedArray{Float64,2}, x)  
Y = convert(SharedArray{Float64,1}, y)  
Z = convert(SharedArray{Float64,1}, z)  

# run BLAS.gemv! on Arrays twice, time second case
BLAS.gemv!('N', 1.0, x, y, 0.0, z)
@time BLAS.gemv!('N', 1.0, x, y, 0.0, z)

# does BLAS work equally well for SharedArrays? 
# check timing result and ensure same answer
BLAS.gemv!('N', 1.0, X, Y, 0.0, Z)
@time BLAS.gemv!('N', 1.0, X, Y, 0.0, Z)
println("$(isequal(z,Z))")  # should be true

# SharedArrays can be updated in parallel
# code a loop to farm updates to worker nodes
# use transposed X to place rows of X in columnar format
# should (hopefully) help with performance issues from stride
Xt = X'  
@parallel for i = 1:n 
    Z[i] = dot(Y, Xt[:,i])
end

# now time the synchronized copy of this
@time @sync @parallel for i = 1:n 
    Z[i] = dot(Y, Xt[:,i])
end

# still get same result?
println("$(isequal(z,Z))")  # should be true

4 个 worker + 1 个主节点的测试输出:
elapsed time: 0.109010169 seconds (80 bytes allocated)
elapsed time: 0.110858551 seconds (80 bytes allocated)
true
elapsed time: 1.726231048 seconds (119936 bytes allocated)
true

最佳答案

您遇到了几个问题,其中最重要的是 Xt[:,i]创建一个新数组(分配内存)。这是一个演示,可以让您更接近您想要的:

n = 10000; p = 25000

# make normal Arrays
x = randn(n,p)
y = ones(p)
z = zeros(n)

# make SharedArrays
X = convert(SharedArray, x)  
Y = convert(SharedArray, y)  
Z = convert(SharedArray, z)

Xt = X'

@everywhere function dotcol(a, B, j)
    length(a) == size(B,1) || throw(DimensionMismatch("a and B must have the same number of rows"))
    s = 0.0
    @inbounds @simd for i = 1:length(a)
        s += a[i]*B[i,j]
    end
    s
end

function run1!(Z, Y, Xt)
    for j = 1:size(Xt, 2)
        Z[j] = dotcol(Y, Xt, j)
    end
    Z
end

function runp!(Z, Y, Xt)
    @sync @parallel for j = 1:size(Xt, 2)
        Z[j] = dotcol(Y, Xt, j)
    end
    Z
end

run1!(Z, Y, Xt)
runp!(Z, Y, Xt)
@time run1!(Z, Y, Xt)
zc = copy(sdata(Z))
fill!(Z, -1)
@time runp!(Z, Y, Xt)

@show sdata(Z) == zc

结果(开始时 julia -p 8 ):
julia> include("/tmp/paralleldot.jl")
elapsed time: 0.465755791 seconds (80 bytes allocated)
elapsed time: 0.076751406 seconds (282 kB allocated)
sdata(Z) == zc = true

为了比较,在同一台机器上运行时:
julia> blas_set_num_threads(8)

julia> @time A_mul_B!(Z, X, Y);
elapsed time: 0.067611858 seconds (80 bytes allocated)

所以原始的 Julia 实现至少可以与 BLAS 竞争。

关于parallel-processing - BLAS v. Julia SharedArray 对象的并行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28360850/

相关文章:

algorithm - 巨大图形的直径

c++11 - 通过 MPI 发送函数指针

visual-studio-code - 如何使用 VSCode Jupyter 扩展渲染 Julia 数据帧?

julia - 在 Julia 中编写一个返回多个顶级表达式的宏

c - GSL BLAS 函数所需的 "const gsl_vector_float"是什么?

c++ - Armadillo+OpenBLAS 比 MATLAB 慢?

go - 在 Go 中创建一个并行字计数器

c++ - 当构建显示 "loop parallelized"时,为什么 CPU 不会超过 25%

julia - 保存数组的数组、HDF5、Julia

python - 在 scipy 中使用大 F 阶矩阵进行 dgemm 段错误