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

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

问题描述

如何在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

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

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

将一个进程中的任意数量的对象传递给任意对象 流程.该变量必须在 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)

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

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