Struct 如何向Julia的新员工发送结构?
我已经定义了一个struct,它应该被用作不同函数的输入,而这些函数又被不同的工作者调用。字段值将在函数中更改,因此我无法在函数调用之间构造新的字段值。如何将结构发送给新员工?我已经试过了Struct 如何向Julia的新员工发送结构?,struct,parallel-processing,julia,distributed-computing,Struct,Parallel Processing,Julia,Distributed Computing,我已经定义了一个struct,它应该被用作不同函数的输入,而这些函数又被不同的工作者调用。字段值将在函数中更改,因此我无法在函数调用之间构造新的字段值。如何将结构发送给新员工?我已经试过了@eval@everywhere,也试过了包中的sendto using Distributed using Parameters @with_kw struct Test path1::String = "" path2::String = "" end test = Test() ad
@eval@everywhere
,也试过了包中的sendto
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所有定义新类型和方法的代码都应该为所有流程所知(因此,应该以某种方式使用
@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 path1(x)
Future(2, 1, 8, nothing)
# And get the answer back
julia> fetch(f)
"foo"
定义新类型和方法的所有代码都应该为所有进程所知(因此,应该使用
@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 path1(x)
Future(2, 1, 8, nothing)
# And get the answer back
julia> fetch(f)
"foo"
我知道您的问题是关于使用远程工作者对本地
struct
s进行变异。
您不能这样做,但可以发送新结构的副本:
using Distributed
addprocs(2)
using Parameters
@everywhere using Parameters
@everywhere begin
@with_kw mutable struct Test
a::String = ""
b::String = ""
end
end
t = Test(a="some")
@everywhere function mutate(t::Test)
t.b = "newval"
t
end
fut = @spawnat 2 mutate(t)
newt = fetch(fut)
现在您可以看到,newt包含t的一个突变副本:
julia> dump(newt)
Test
a: String "some"
b: String "newval"
但是,t
的值保持不变:
julia> dump(t)
Test
a: String "some"
b: String ""
但是,如果辅助对象位于同一主机上,则可以变异基于位类型的数组。(此示例假定已使用前面的代码添加了辅助对象):
现在让我们看一下阵列:
julia> display(sa)
4-element SharedArray{Int64,1}:
1
200
3
4
我知道您的问题是关于使用远程工作者对本地
struct
s进行变异。
您不能这样做,但可以发送新结构的副本:
using Distributed
addprocs(2)
using Parameters
@everywhere using Parameters
@everywhere begin
@with_kw mutable struct Test
a::String = ""
b::String = ""
end
end
t = Test(a="some")
@everywhere function mutate(t::Test)
t.b = "newval"
t
end
fut = @spawnat 2 mutate(t)
newt = fetch(fut)
现在您可以看到,newt包含t的一个突变副本:
julia> dump(newt)
Test
a: String "some"
b: String "newval"
但是,t
的值保持不变:
julia> dump(t)
Test
a: String "some"
b: String ""
但是,如果辅助对象位于同一主机上,则可以变异基于位类型的数组。(此示例假定已使用前面的代码添加了辅助对象):
现在让我们看一下阵列:
julia> display(sa)
4-element SharedArray{Int64,1}:
1
200
3
4