How can I "fetch" without caching the struct?


#1

Greetings,
I ran into trouble, or maybe my approach is just wrong .
What I want is to create an object on remote workers, which perform some kind of work. Then I need to grab some data from this object. The problem is that I do not want to fetch this object only once after creating it, but I want to address it multiple times, as it is a work unit that is being updated (e.g. contains a JuMP model that is solved for different values). I have some mini-code that clarifies the issue. Does anyone know how to achieve this?

@everywhere mutable struct teststruct
    a::Number
    b::Number
end

@everywhere function createstruct()
    return teststruct(randn(), randn())
end

@everywhere function changestruct(in::teststruct)
    in.a = randn()
    in.b = randn()
end

@everywhere function get_a(in::teststruct)
    return in.a
end

function test(workers)
    N = 100
    Out = Array{Float64}(N)

    # Create struct on every worker
    mystructs = Array{Any}(length(workers))
    for (i, p) in enumerate(workers)
        mystructs[i] = remotecall(createstruct, p)
    end

    j = 0
    @sync begin
        for (i, p) in enumerate(workers)
            @async begin
                while true
                    j+=1
                    if j>N
                        break
                    end
                    j_local = j
                    remotecall_fetch(changestruct, p, fetch(mystructs[i]))
                    Out[j_local] = remotecall_fetch(get_a, p, fetch(mystructs[i]))
                end
            end
        end
    end
    return Out
end

temp = test(workers());
unique(temp) # I would like to have 100 values here instead of nworkers()