如何向Julia的新工作人员发送结构?

问题描述 投票:0回答:1

我已经定义了一个结构,该结构应用作不同功能的输入,而这些功能又是从不同的工作人员调用的。字段值将在函数中更改,因此我无法在函数调用之间构造新的字段值。如何将结构发送给新员工?我已经尝试过@eval @everywheresendto包中的ParallelDataTransfer


using Distributed
using Parameters

@with_kw struct Test
    path1::String = ""
    path2::String = ""
end

test = Test()
addprocs(2)
@eval @everywhere test = $test

Output:
ERROR: On worker 2:
UndefVarError: Test not defined

using ParallelDataTransfer
sendto(workers(), test=test)

@everywhere print(test)

Output:
ERROR: On worker 2:
UndefVarError: test not defined

我使用Julia 1.3.1

struct parallel-processing julia distributed-computing
1个回答
0
投票

所有定义新类型和方法的代码都应为所有进程所了解(因此,使用@everywhere子句以某种方式将其包括在内)。否则,其他进程不知道Test指的是什么。

总体而言,如果我正确理解您要达到的目标,您的示例可能如下所示:

julia> using Distributed

# Add new processes at first, so that they will be affected by subsequent
# @everywhere invocations
julia> addprocs(2)
2-element Array{Int64,1}:
 2
 3

# All code defining new types / methods should be evaluated in all processes
julia> @everywhere begin
           # This common code should probably live somewhere else, like in
           # another source file:
           #  include("some_file.jl")

           struct Test
               path1::String
           end

           path1(x::Test) = x.path1
       end

# Create the object in the master process
julia> x = Test("foo")
Test("foo")

# Send it to another one for processing
julia> f = @spawnat 2 x.path1
Future(2, 1, 8, nothing)

# And get the answer back
julia> fetch(f)
"foo"

© www.soinside.com 2019 - 2024. All rights reserved.