我有一个与 pmap 一起使用的函数来并行化它。我想每次使用 10 个工作人员异步运行 4 次此函数,但我不能同时运行两个或更多 pmap。
我在 linux 上使用 Julia v1.1 和 40-CPUs 机器。
using Distributed
addprocs(4)
@everywhere function TestParallel(x)
a = 0
while a < 4
println("Value = ",x, " in worker = ", myid())
sleep(1)
a += 1
end
end
a = WorkerPool([2,3])
b = WorkerPool([4,5])
c = [i for i = 1:10]
@sync @async for i in c
pmap(x-> TestParallel(x), a, c)
pmap(x-> TestParallel(x), b, c)
end
我希望有:
From worker 2: Value = 1 in worker = 2
From worker 3: Value = 2 in worker = 3
From worker 4: Value = 3 in worker = 4
From worker 5: Value = 4 in worker = 5
因此,c 的前两个元素进入第一个 pmap,接下来的两个元素进入第二个 pmap,然后谁先完成,就会得到接下来的两个元素。
现在我得到:
From worker 2: Value = 1 in worker = 2
From worker 3: Value = 2 in worker = 3
From worker 2: Value = 1 in worker = 2
From worker 3: Value = 2 in worker = 3
在第一个 pmap 完成 c 的所有元素后,第二个 pmap 重新开始求解所有元素。
From worker 2: Value = 9 in worker = 2
From worker 3: Value = 10 in worker = 3
From worker 5: Value = 2 in worker = 5
From worker 4: Value = 1 in worker = 4