This example shows how to use Future
objects and their methods fetchNext
, fetchOutput
, cancel
, and wait
to defer output retrieval for environment simulations running on workers, monitor the status of ongoing simulations, fetch outputs of completed simulations, cancel ongoing simulations, or wait for ongoing simulations to complete.
Load a predefined environment and a suitable agent. For this example use both the environment and agent described in Train AC Agent to Balance Discrete Cart-Pole System.
Start a parallel pool and set up the environment so that it simulates on workers.
Starting parallel pool (parpool) using the 'Processes' profile ...
Connected to parallel pool with 2 workers.
To display the simulation completion times, start a timer.
Schedule six simulation to run on the available workers. At the beginning of the simulation, the reset function of the cart-pole environment sets the initial angle of the pole to a random position in the neighborhood of zero (the upward position). This randomization ensures that each simulation is different.
Each element of the Future
array ftr
represents a scheduled simulation.
ftr=1×6 object
1×6 Future array with properties:
Read
State
Diary
ID
Display the state of each simulation.
Two simulations are ongoing while the others are queued.
Use fetchNext
with a timeout of 0.1
seconds to retrieve results for simulations that complete within that time (if any).
Both the outputs are empty, which means that none of the four simulation has completed yet.
Display how many output results have been already retrieved.
Use fetchNext
without any timeout to wait until an unretrieved simulation output becomes available and then return the results.
out = struct with fields:
SimulationInfo: [1×1 struct]
AgentData: [1×1 struct]
Display the state of the simulations.
As expected, the first two simulations, which were running in parallel on the two workers, are finished, while the next two, which were previously queued, are now running, and the final two are still queued.
Display the time taken for the first two simulations to complete.
Elapsed time is 10.451231 seconds.
Note that once the results from a simulation has been already retrieved, any attempt to use fetchNext
to retrieve it again, such as in fetchNext(ftr(2))
, will result in an error. To retrieve the results from a Future
object that has already been read, you can use fetchOuptuts
, such as in fetchOutputs(ftr(2))
.
Retrieve the next available result, and display the time elapsed since the simulations started.
out = struct with fields:
SimulationInfo: [1×1 struct]
AgentData: [1×1 struct]
Elapsed time is 11.945070 seconds.
As expected, fetchNext
promptly returns the results from the second simulation, since it was already available.
Display how many output results have been already retrieved.
Cancel the last simulation.
Wait for the fourth simulation to complete. The wait
function blocks the command prompt until the fourth simulation is completed.
Display the elapsed time since the simulations started.
Elapsed time is 12.414076 seconds.
Display the state of the simulations.
The status of the last element of the array, for which the simulation has been canceled, is classified as 'finished'
.
Since any attempt to retrieve results from a simulation that has been canceled will result in an error, remove the canceled object from the array.
ftr=1×5 object
1×5 Future array with properties:
Read
State
Diary
ID
Use fetchOutputs
to wait until all remaining simulations are completed and then retrieve all outputs.
outs=5×1 struct array with fields:
SimulationInfo
AgentData
Display the elapsed time.
Elapsed time is 16.265069 seconds.
Plot the action and observations from the fifth simulation.
Clear the array of Future
objects, the environment, and delete the parallel pool (this is the reverse order in which they were created).