Julia:如何将数据复制到 Julia 中的另一个处理器 [英] Julia: How to copy data to another processor in Julia

查看:14
本文介绍了Julia:如何将数据复制到 Julia 中的另一个处理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 julia 中将数据从一个处理器移动到另一个处理器?

How do you move data from one processor to another in julia?

假设我有一个数组

a = [1:10]

或其他一些数据结构.将它放在所有其他可用处理器上的正确方法是什么,以便在这些处理器上以相同的变量名使用它?

Or some other data structure. What is the proper way to put it on all other available processors so that it will be available on those processors as the same variable name?

推荐答案

一开始我不知道怎么做,所以我花了一些时间弄清楚.

I didn't know how to do this at first, so I spent some time figuring it out.

以下是我编写的一些传递对象的函数:

Here are some functions I wrote to pass objects:

向指定进程发送任意数量的变量.

Send an arbitrary number of variables to specified processes.

在指定进程的主模块中创建新变量.这名称将是关键字参数的键,值将是关联值.

New variables are created in the Main module on specified processes. The name will be the key of the keyword argument and the value will be the associated value.

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


function sendto(ps::Vector{Int}; args...)
    for p in ps
        sendto(p; args...)
    end
end

示例

# creates an integer x and Matrix y on processes 1 and 2
sendto([1, 2], x=100, y=rand(2, 3))

# create a variable here, then send it everywhere else
z = randn(10, 10); sendto(workers(), z=z)

在任意模块上检索定义在任意模块中的对象过程.默认为主模块.

getfrom

Retrieve an object defined in an arbitrary module on an arbitrary process. Defaults to the Main module.

要检索的对象的名称应该是一个符号.

The name of the object to be retrieved should be a symbol.

getfrom(p::Int, nm::Symbol; mod=Main) = fetch(@spawnat(p, getfield(mod, nm)))

示例

# get an object from named x from Main module on process 2. Name it x
x = getfrom(2, :x)

passobj

将任意数量的对象从一个进程传递到任意过程.该变量必须在 from_mod 模块中定义src 进程,并将同名复制到 to_mod每个目标进程的模块.

passobj

Pass an arbitrary number of objects from one process to arbitrary processes. The variable must be defined in the from_mod module of the src process and will be copied under the same name to the to_mod module on each target process.

function passobj(src::Int, target::Vector{Int}, nm::Symbol;
                 from_mod=Main, to_mod=Main)
    r = RemoteRef(src)
    @spawnat(src, put!(r, getfield(from_mod, nm)))
    for to in target
        @spawnat(to, eval(to_mod, Expr(:(=), nm, fetch(r))))
    end
    nothing
end


function passobj(src::Int, target::Int, nm::Symbol; from_mod=Main, to_mod=Main)
    passobj(src, [target], nm; from_mod=from_mod, to_mod=to_mod)
end


function passobj(src::Int, target, nms::Vector{Symbol};
                 from_mod=Main, to_mod=Main)
    for nm in nms
        passobj(src, target, nm; from_mod=from_mod, to_mod=to_mod)
    end
end

示例

# pass variable named x from process 2 to all other processes
passobj(2, filter(x->x!=2, procs()), :x)

# pass variables t, u, v from process 3 to process 1
passobj(3, 1, [:t, :u, :v])

# Pass a variable from the `Foo` module on process 1 to Main on workers
passobj(1, workers(), [:foo]; from_mod=Foo)

这篇关于Julia:如何将数据复制到 Julia 中的另一个处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆