Help with shared arrays

I am trying to run a “Simulate” function living in a separate module and extract the states from it with a getStates function. The idea is that I launch the sim to
a parallel process and get the states after some time through a SharedArray.

When I create this script:

using Distributed
using SharedArrays

include(“Simulations.jl”)
using .Simulations

Add workers (processes)

if nprocs() == 1 # Ensure at least one worker is added
addprocs(2) # Add 2 worker processes
end

SharedArray to hold the states

@everywhere using SharedArrays
states = SharedArray{Float64}(1)
states[1] = 1.0

function startSim()
worker_id = 2 # Choose the worker where the task will run
@spawnat worker_id Simulations.Simulate(states)
end

function getStates()
return states[1]
end

Start the HTTP server

println(“Starting sim”)

startSim()

sleep(1)

s = getStates();

println(“States after aprox 1 second:”, s)

With Simulations.jl being:

module Simulations
using SharedArrays

Simulate function

function Simulate(states::SharedArray)
for i in 1:1000
states[1] = states[1] - 0.002 * states[1]
sleep(0.01)
end
end
end

It doesn’t update. However if instead of including the module I declare Simulate function inside the main script it works:

using Distributed
using SharedArrays

Add workers (processes)

if nprocs() == 1 # Ensure at least one worker is added
addprocs(2) # Add 2 worker processes
end

SharedArray to hold the states

@everywhere using SharedArrays
states = SharedArray{Float64}(1)
states[1] = 1.0

Simulate function

@everywhere function Simulate(states::SharedArray)
for i in 1:1000
states[1] = states[1] - 0.002 * states[1]
sleep(0.01)
end
end

function startSim()
worker_id = 2 # Choose the worker where the task will run
@spawnat worker_id Simulate(states)
end

function getStates()
return states[1]
end

Start the HTTP server

println(“Starting sim”)

startSim()

sleep(1)

s = getStates();

println(“States after aprox 1 second:”, s)

I don’t understand where my problem is. Many thanks

I suggest you put your code in “code block”. People will have a much easier read and could quickly test your code.

# Like that
using Distributed
# etc.

Thank you, I have created a new one with it as suggested :slight_smile: : Help with shared arrays - code update

Hi all, this is the same post as I had done before but with the code correctly added.

I am trying to run a “Simulate” function living in a separate module and extract the states from it with a getStates function. The idea is that I launch the sim to
a parallel process and get the states after some time through a SharedArray.

When I create this script:

using Distributed
using SharedArrays

include("Simulations.jl")
using .Simulations
# Add workers (processes)
if nprocs() == 1  # Ensure at least one worker is added
    addprocs(2)  # Add 2 worker processes
end

# SharedArray to hold the states
@everywhere using SharedArrays 
states = SharedArray{Float64}(1)
states[1] = 1.0 

function startSim()
    worker_id = 2  # Choose the worker where the task will run
    @spawnat worker_id Simulations.Simulate(states)
end

function getStates()
    return states[1]
end

# Start the HTTP server
println("Starting sim")

startSim()

sleep(1)

s = getStates();

println("States after aprox 1 second:", s)

where:

module Simulations
using SharedArrays 
# Simulate function
function Simulate(states::SharedArray)
    for i in 1:1000
        states[1] = states[1] - 0.002 * states[1]
        sleep(0.01)
    end
end
end

It doesn’t update. However if instead of including the module I declare Simulate function inside the main script it works:

using Distributed
using SharedArrays

# Add workers (processes)
if nprocs() == 1  # Ensure at least one worker is added
    addprocs(2)  # Add 2 worker processes
end

# SharedArray to hold the states
@everywhere using SharedArrays 
states = SharedArray{Float64}(1)
states[1] = 1.0 

# Simulate function
@everywhere function Simulate(states::SharedArray)
    for i in 1:1000
        states[1] = states[1] - 0.002 * states[1]
        sleep(0.01)
    end
end

function startSim()
    worker_id = 2  # Choose the worker where the task will run
    @spawnat worker_id Simulate(states)
end

function getStates()
    return states[1]
end

# Start the HTTP server
println("Starting sim")

startSim()

sleep(1)

s = getStates();

println("States after aprox 1 second:", s)

I don’t know what I am doing wrong. I’d be very grateful if someone could give me a help.

Thank you!