Scientific Computing, TU Berlin, WS 2019/2020, Lecture 27
Jürgen Fuhrmann, WIAS Berlin
addprocs
remotecall
. fetch(task)
wait for the completion of the taks and retrieve result remotecall_fetch
does remotecall
and fetch
using Distributed
using LinearAlgebra
using BenchmarkTools
addprocs(4)
We can also do addprocs([(hostname,n)])
to work on different hosts
List of workers
workers()
Run a function on all threads
@everywhere println(myid())
Run a function on another worker and return its id multiplied by 10
@everywhere function run_on()
return myid()*10
end
remotecall_fetch(run_on,3)
Distributed Arrays allow to distribute data to all workers
@everywhere using DistributedArrays
Now let us try to calculate a scalar product
Scalar product for two arrays
@everywhere function mydot(A::Array,B::Array)
result=0.0
@inbounds @fastmath for i=1:length(A)
result+=A[i]*B[i]
end
return result
end
Scalar product for two distributed arrays
This uses an asynchronous map, where results are collected as they come in
function mydot(DA::DArray,DB::DArray)
results=asyncmap(p->remotecall_fetch((DA, DB) -> mydot(localpart(DA), localpart(DB)),p,DA,DB), workers() )
reduce(+,results)
end
A=rand(1_000_000)
B=rand(1_000_000)
DA=distribute(A)
DB=distribute(B);
res_s=@btime mydot($A,$B)
res_p=@btime mydot($DA,$DB)
res_s≈ res_p
This notebook was generated using Literate.jl.