distribution.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. Distribution
  14. ============
  15. For the modeller, distribution of models is as simple as providing a model (either atomic or coupled) with the location where it should run and afterwards running it as a distributed simulation (as mentioned in :doc:`howto`).
  16. .. note:: Since version 2.1, model distribution was simplified and a lot of restrictions on the model were dropped. Every model that was valid in local simulation, should now also be valid in distributed simulation.
  17. Distributing the queue
  18. ----------------------
  19. Let's extend the *CQueue* example from previous sections and add some distribution to it. Of course, this model only has two atomic models, which is clearly not an ideal model to distribute. Therefore, we will add some *Queue* atomic models in parallel, all taking the output from the single generator. Our model should look something like:
  20. .. image:: distribution_local_model.png
  21. :alt: The model to distribute
  22. We will start of with implementing this model locally. This implementation is simple and shouldn't be more difficult than the model in :doc:`examples`. It should look something like::
  23. class DQueue(CoupledDEVS):
  24. def __init__(self):
  25. CoupledDEVS.__init__(self, "DQueue")
  26. self.generator = self.addSubModel(Generator())
  27. self.queue1 = self.addSubModel(Queue())
  28. self.queue2 = self.addSubModel(Queue())
  29. self.connectPorts(self.generator.outport, self.queue1.inport)
  30. self.connectPorts(self.generator.outport, self.queue2.inport)
  31. .. note:: Our original *Queue* atomic model was not written with multiple instances in mind. Therefore, the model name will **not** be unique in this simulation. In later versions of PyPDEVS, this doesn't pose a problem apart from possibly confusing trace output.
  32. Now all that is left is performing the actual distribution. Suppose we run 3 different nodes, with every atomic model on a seperate node. Thus the *Generator* runs on node 0, the first *Queue* runs on node 1 and the final *Queue* runs on node 2. This is as simple as altering the *addSubModel* method calls and add the desired node of the model being added. This results in::
  33. class DQueue(CoupledDEVS):
  34. def __init__(self):
  35. CoupledDEVS.__init__(self, "DQueue")
  36. self.generator = self.addSubModel(Generator(), 0)
  37. self.queue1 = self.addSubModel(Queue(), 1)
  38. self.queue2 = self.addSubModel(Queue(), 2)
  39. self.connectPorts(self.generator.outport, self.queue1.inport)
  40. self.connectPorts(self.generator.outport, self.queue2.inport)
  41. Setting the location of a model, will automatically set the location of all its submodels with an unspecified location to the same location.
  42. .. note:: Models for location 0 do not necessarily need to be specified, as the default is node 0. Leaving this option open (or specifying the location as *None*) means that it should take the location of the parent.
  43. Additional options
  44. ------------------
  45. A *Simulator* object that is initialized with a distributed model has several extra options that can be configured.
  46. More advanced options are elaborated further on in their own :doc:`advanced` subsection.
  47. +------------------------------------+-------------------------------------------------------+
  48. |*setTerminationModel(model)* | Marks *model* as being used in termination condition |
  49. +------------------------------------+-------------------------------------------------------+
  50. |*registerState(variable, model)* | Register a state to be fetched after simulation |
  51. +------------------------------------+-------------------------------------------------------+
  52. |*setFetchAllAfterSimulation(fetch)* | Completely update the model after simulation |
  53. +------------------------------------+-------------------------------------------------------+
  54. |*setGVTInterval(gvt_int)* | Calculate the GVT after every *gvt_int* seconds |
  55. +------------------------------------+-------------------------------------------------------+
  56. |*setCheckpointing(name, chk_int)* | Create a checkpoint after every *chk_int* GVT creation|
  57. +------------------------------------+-------------------------------------------------------+
  58. |*setStateSaving(state_saving)* | Change the method for state saving to *state_saving* |
  59. +------------------------------------+-------------------------------------------------------+
  60. .. note:: If any of these options are set during a local simulation, they will either throw a Exception or will simply have no effect.
  61. Automatic allocation
  62. --------------------
  63. Some models are very simple, but tedious, to distribute. As long as the actual allocation has no real importance, there is an additional option *setAutoAllocation(autoAllocate)* which will have the simulator distribute the models automatically. This distribution is rather efficient, though it is often suboptimal. It will simply try to balance the amount of atomic models at every node, not taking into account the actual activity of these models. Furthermore, it only works at the root level.
  64. Even though it doesn't perform intelligent distribution, it will work in situations where root models can work in parallel without influencing each other. More information can be found in :doc:`Static Allocators <staticallocator>`
  65. General tips
  66. ------------
  67. The distributed simulation algorithm that is being used is Time Warp. This means that every node will simulate *optimistically* in the assumption that other models have progressed as far as itself. As soon as a message from the past arrives, simulation at that node will be rolled back. This implies that nodes should have an equal load and that as few as messages should be exchanged between different nodes.
  68. Therefore, the following rules should be taken into account to maximize the performance in distributed simulation:
  69. * Models that exchange lots of messages should be on the same node
  70. * Balance the load between nodes, so that the deviation is minimal
  71. * Use homogeneous nodes
  72. * Use quantums where possible, thus reducing the amount of messages
  73. .. note:: Due to time warp's property of saving (nearly) everything, it is possible to quickly run out of memory. It is therefore adviced to set the GVT calculation time to a reasonable number. Running the GVT algorithm frequently yields slightly worse performance, though it will clean up a lot of memory.