本文介绍了等待任务在Julia中的远程处理器上完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个模仿分布式推理的并行应用程序中,我想有一个初始化步骤",其中所有从属"都从主"那里接收一些初始信息,然后开始他们的任务.

In a parallel application mimicking distributed inference, I would like to have an "initialization step" where all the "slaves" receive some initial information from the "master" then start their task.

目前,我有一个基于sendTo函数的有效实现(代码已找到(在堆栈溢出时).但是我不认为这可以保证工作人员在收到初始对象之前不会开始其任务.

At the moment I have a working implementation based on the sendTo function (the code was found here on stack overflow) but I don't think it guarantees that the worker won't start its task before it has received the initial objects.

这是一个粗略的MWE

Here's a rough MWE

function sendTo(p::Int; args...)
    for (nm, val) in args
        @spawnat(p, eval(Main, Expr(:(=), nm, val)))
    end
end

a = 5

addprocs(4)

[sendTo(worker,a=a+randn()) for worker in workers()]

@everywhere begin
    println(a)
end

上面的方法"有效,但是如何确定工作人员收到a的定义之前@everywhere块中的命令没有被执行?

The above "works" but how can I be sure that the commands in the @everywhere block does not get executed before the worker has received the definition of a?

Rmk :对于我正在使用的上下文,我想保留两个不同的块,一个用于分散数据,另一个用于填充数据.

Rmk: for the context I'm working in, I would like to keep two distinct blocks, one that spreads the data and one that does stuff on it.

其他rmk :很抱歉,这很简单,我对处理并行性还很陌生(对Julia来说也很陌生)

Other rmk: apologies if this is trivial, I'm quite new to dealing with parallelism (and quite new to Julia too)

推荐答案

,您可以fetch每个过程的结果.请参阅文档中的示例

you can just fetch the results for every process. See the example in the docs

function sendTo(p::Int; args...)
    r = []
    for (nm, val) in args
        s = @spawnat(p, eval(Main, Expr(:(=), nm, val)))
        vcat([s],r)
    end
end
#...
[fetch(r) for r in [sendTo(worker,a=a+randn()) for worker in workers()]]

这篇关于等待任务在Julia中的远程处理器上完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:39
查看更多