SharedArray of Measurement type

I am trying to put the the output (just one data point) of the solution of an ODE that is solved with uncertainty (measrument.jl) multiple times with different initial conditions within a distributed for loop in a SharedArray. I get this error stating

TaskFailedException:
On worker 2:
MethodError: no method matching Float64(::Measurement{Float64})
Closest candidates are:
  Float64(::Real, !Matched::RoundingMode) where 

I believe it does’t like the way that I have initialized my SharedArray which is

Alpha2 = SharedArray(zeros(100))

The main code is pretty long and might be confusing, so here is something very simple with the exact same problem

using SharedArrays, Distributed, Measurements

A=SharedArray(zeros(10))
@sync @time for i= 1:10
A[i]=i±1
end

which after running returns the same error as

 MethodError: no method matching Float64(::Measurement{Float64})
Closest candidates are:
  Float64(::Real, !Matched::RoundingMode) where T<:AbstractFloat at rounding.jl:200
  Float64(::T) where T<:Number at boot.jl:718
  Float64(!Matched::Int8) at float.jl:60

Can someone please point me in the right direction? How should I initialize the SharedArrays so I can put Measurement{Float64} inside it within a @distributed for loop?

You need to specify the element type in zeros beforehand as zeros(Measurement{Float64}, 10).

I changed it to

using SharedArrays, Distributed, Measurements
A=SharedArray(zeros(Measurement{Float64}, 10))
@sync @time for i= 1:10
A[i]=i±1
end

and I get this

LoadError: ArgumentError: type of SharedArray elements must be bits types, got Measurement{Float64}
in expression starting at untitled-3a064888317d95bbc10641413428b617:2

Measurements types are not isbits so they won’t work with SharedArrays. However, the solution is to just not use SharedArrays: no distributed computing needs it (it’s always faster to just control the transfers yourself), and if you’re not actually doing distributed, then you should probably use multithreading instead.

Thanks, So I have to use Threads.@threads for loops

If you’re not using multiple computers together, I would highly recommend it. It’ll have less overhead and will be easier to work with.

2 Likes