如何在构造函数中使用SharedArray
?
我有这段代码:
@everywhere mutable struct DDFC{T <: Real}
n::Int64
m::Int64
f_valueN::SharedArray{Float64,2}
f_policy::SharedArray{Float64,2}
end
@everywhere function DDFC(;α = 0.3,β = 0.5)
f_valueN = SharedArray{Float64,2}(n, m)
f_policy = SharedArray{Float64,2}(n, m)
n= 2
m=3
DDFC(α, β,f_valueN, f_policy, n, m)
end
但是通过运行ddfc = DDFC()
我收到了这个错误:
MethodError: no method matching DDFC(::Float64, ::Float64, ::Type{SharedArray{Float64,2}}, ::Type{SharedArray{Float64,2}}, ::Int64, ::Int64)
我怎么能在这里使用sharedarray?
答案 0 :(得分:1)
为了帮助你继续我在这里给你一个有效的代码更正(请注意差异):
@everywhere mutable struct DDFC{T <: Real}
n::Int64
m::Int64
f_valueN::SharedArray{Float64,2}
f_policy::SharedArray{Float64,2}
end
@everywhere function DDFC(;α = 0.3,β = 0.5)
n= 2
m=3
f_valueN = SharedArray{Float64,2}(n, m)
f_policy = SharedArray{Float64,2}(n, m)
DDFC{Float64}(n, m, f_valueN, f_policy)
end
现在正在运行ddfc = DDFC()
,但正如Alexander Morley评论的那样,此代码无用,因为您不清楚要实现的目标,T
类型未在struct
中使用无论如何。特别注意我在外部构造函数中调用{Float64}
时添加了DDFC
(这可能是你的问题)。
答案 1 :(得分:1)
@everywhere mutable struct DDFC{T <: Real}
n::Int64
m::Int64
f_valueN::SharedArray{T,2}
f_policy::SharedArray{T,2}
end
@everywhere function DDFC(;α = 0.3,β = 0.5, n=2, m=2)
f_valueN = SharedArray{Float64,2}(n, m)
f_policy = SharedArray{Float64,2}(n, m)
DDFC(n, m, f_valueN, f_policy)
end
OR
@everywhere mutable struct DDFC{T <: Real}
α::Float64
β::Float64
n::Int64
m::Int64
f_valueN::SharedArray{T,2}
f_policy::SharedArray{T,2}
end
@everywhere function DDFC(;α = 0.3,β = 0.5, n=2, m=2)
f_valueN = SharedArray{Float64,2}(n, m)
f_policy = SharedArray{Float64,2}(n, m)
DDFC(α, β, n, m, f_valueN, f_policy)
end
两者都应该有效,取决于你想要做什么。仅供参考,此问题与SharedArray
无关,而只与您如何定义/调用构造函数有关。