How to run parallel function in julia?

Viewed 126

I would like to run the function f() on all my 4 processors (intel i7) and fetch the rands sum, as follows:

using Distributed;

@everywhere function f()
return sum(rand(10000))
end

@sync for w in workers()
    @async begin
    res = @spawnat w f()
    values[w-1] = fetch(res)
    end
end

But, getting the following error:

ERROR: TaskFailedException

    nested task error: MethodError: no method matching setindex!(::typeof(values), ::Float64, ::Int64)
    Stacktrace:
     [1] macro expansion
       @ ./REPL[58]:4 [inlined]
     [2] (::var"#68#70"{Channel{Any}, Int64})()
       @ Main ./task.jl:411
Stacktrace:
 [1] sync_end(c::Channel{Any})
   @ Base ./task.jl:369
 [2] top-level scope
   @ task.jl:388

Please guide me in resolving the issue!

1 Answers

For your code the easiest way would be (assuming Julia has been run with -p 4 command line parameter or you have run addprocs(4):

julia> values = @distributed (append!) for i in 1:4
       [f()]
       end
4-element Vector{Float64}:
 5001.232864826896
 4999.244031827526
 4966.883114472259
 5014.022690758762

If you want to do @spawns yourself this code works:

julia> values = Vector{Float64}(undef, 4);

julia> @sync for w in workers()
           @async values[w-1] = fetch(@spawnat w f())
       end

julia> values
4-element Vector{Float64}:
 5029.967318172736
 4993.1064528029
 5016.491407076979
 5062.0706219606345

However your code mostly didn't work because the type of your values was not Vector{Float64}. Here is how to replicate your error:


julia> vv()=0
vv (generic function with 1 method)

julia> vv[1]=11
ERROR: MethodError: no method matching setindex!(::typeof(vv), ::Int64, ::Int64)
Related