distributedtermination.rst 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ..
  2. Copyright 2014 Modelling, Simulation and Design Lab (MSDL) at
  3. McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. Distributed Termination
  14. =======================
  15. Termination was simple in local, sequential simulation. All that had to be done was placing a simple check before each step in the simulation and check whether or not this simulation step should still be executed.
  16. Distributed simulation on the other hand, should only terminate as soon as all nodes are able to quit. But due to the use of time warp, even if a node has stopped running, it might have to start again some time later. Clearly a global termination time is still relatively simple to use, as all nodes just compare their clocks to it, but a termination condition is a lot more difficult.
  17. In this section, the two different ways of termination in a distributed setting will be discussed.
  18. Termination time
  19. ----------------
  20. A global termination time is clearly the most efficient solution. All nodes will receive this time at startup and will simply compare to it. It requires no inter-node communication whatsoever because every node can determine for itself whether or not simulation is finished. If at all possible, this approach is highly recommended over the other option.
  21. Due to its simplicity, the exact same methods and semantics can be used as in sequential simulation::
  22. sim = Simulator(Model())
  23. sim.setTerminationTime(50.0)
  24. sim.simulate()
  25. Termination condition: frequent state checks
  26. --------------------------------------------
  27. .. warning:: Due to the use of time warp, the *time* parameter of the function now returns a **tuple** instead of a **float**. Most often, only the first value of the tuple is used, which contains the actual simulation time. The second value is the so called *age* field, which indicates how often this exact same simulation time has already happened.
  28. A termination condition is still possible in distributed simulation, albeit in a reduced form. First of all, the granularity of a termination condition is not guaranteed in distributed simulation. Since DEVS takes non-fixed timesteps, they are depending on the models that are present on the current node. This means that the termination condition will also be checked only at these steps. Generally, this should not be a big problem, though it is something to keep in mind.
  29. Only a single node can be responsible for the termination condition, due to the model being completely distributed. This node is always the controller. The controller should thus have all models involved in the termination condition running, as otherwise invalid states will be read.
  30. .. note:: PyPDEVS will **not** complain when reading an invalid state, as such readings are done behind the back of PyPDEVS. If you want certainty that the state you are accessing is local, check whether or not the *location* attribute is equal to zero.
  31. To be able to cope with allocation and relocation, the simulator should have its *setTerminationModel(model)* method be called. This will mark the model as being used in the termination condition and will guarantee that this model stays on the controller, whatever allocation or relocation is given. Note though, that this could potentially move a single atomic model from a remote coupled model, causing many revertions.
  32. As soon as the termination condition triggers the end of the simulation, the controller will send a termination message to all other nodes, which will then keep running until they have reached the same time as passed in the termination condition. Should the controller be reverted, its termination messages will also be cancelled.
  33. If we want the generator atomic model of the queue to be used in the termination condition, we could write::
  34. def generatedEnough(time, model):
  35. return model.generator.state.generated > 5
  36. myQueue = CQueue()
  37. sim = Simulator(myQueue)
  38. sim.setTerminationCondition(generatedEnough)
  39. sim.setTerminationModel(myQueue.generator)
  40. sim.simulate()
  41. Termination condition: sporadic state checks
  42. --------------------------------------------
  43. .. warning:: The complete *time* tuple should be passed to the *getState(time)* method!
  44. If the model state is only required sporadically, it would be wasteful to run the model at the controller, simply for this one access. To cope with this, a *pull based* method is possible. Every atomic DEVS model will have a *getState(time)* method. If the model is non-local, this method can be called to retrieve the state of the model at that specific time. If this approach is used, the model need not be marked as a termination model.
  45. .. note:: This time attribute is necessary due to time warp being used: it is very unlikely that the remote model is at exactly the same time in simulated time, so the time at which this call was made should be passed.
  46. Such *getState(time)* calls are blocking, meaning that they will not return a result if the remote model is not yet at the simulated time that was requested. Furthermore, such requests cause artificial dependencies between different models. This pull based approach is thus only recommended if it is done (very) sporadically. It goes without saying that these remote calls also incur a latency due to the network delay.
  47. To write the same as the previous example, we can write::
  48. def generatedEnough(time, model):
  49. return model.generator.getState(time).generated > 5
  50. myQueue = CQueue()
  51. sim = Simulator(myQueue)
  52. sim.setTerminationCondition(generatedEnough)
  53. sim.simulate()
  54. Termination condition: mixed state checks
  55. -----------------------------------------
  56. Both approaches could be mixed if it is required, for example if the generator is checked at every iteration (and is running local). If the generator passed a certain check, then other remote models need to be checked, which will only be done very sporadically. This could give::
  57. def generatedEnough(time, model):
  58. # First a local check
  59. if model.generator.state.generated <= 5:
  60. return False
  61. else:
  62. # Now a remote check, but we know that this will only be called rarely
  63. return model.processor2.getState(time).processed > 5
  64. myQueue = CQueue()
  65. sim = Simulator(myQueue)
  66. sim.setTerminationCondition(generatedEnough)
  67. # Only mark the generator as a termination model
  68. sim.setTerminationModel(myQueue.generator)
  69. sim.simulate()