Julia: How to copy data to another processor in Julia

后端 未结 4 1689
我在风中等你
我在风中等你 2020-11-27 16:41

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

Say I have an array

a = [1:10]

Or some other data structure. What is

4条回答
  •  没有蜡笔的小新
    2020-11-27 17:11

    To supplement @spencerlyon2 's answer here are some macros:

    function sendtosimple(p::Int, nm, val)
        ref = @spawnat(p, eval(Main, Expr(:(=), nm, val)))
    end 
    
    macro sendto(p, nm, val)
        return :( sendtosimple($p, $nm, $val) )
    end
    
    macro broadcast(nm, val)
        quote
        @sync for p in workers()
            @async sendtosimple(p, $nm, $val)
        end
        end
    end
    

    The @spawnat macro binds a value to a symbol on a particular process

    julia> @sendto 2 :bip pi/3
    RemoteRef{Channel{Any}}(9,1,5340)
    
    julia> @fetchfrom 2 bip
    1.0471975511965976
    

    The @broadcast macro binds a value to a symbol in all processes except 1 (as I found doing so made future expressions using the name copy the version from process 1)

    julia> @broadcast :bozo 5
    
    julia> @fetchfrom 2 bozo
    5
    
    julia> bozo
    ERROR: UndefVarError: bozo not defined
    
    julia> bozo = 3             #these three lines are why I exclude pid 1
    3
    
    julia> @fetchfrom 7 bozo
    3
    
    julia> @fetchfrom 7 Main.bozo
    5
    

提交回复
热议问题