staticallocator.rst 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. Static Allocator
  14. ================
  15. Assigning locations directly within the model might be tedious or even impossible in some situations.
  16. In some other cases, the number of nodes might be variable, thus requiring more code in the model itself to determine the correct location.
  17. To solve these problems, an allocator can be defined.
  18. This section will handle about the static allocators.
  19. The static signifies that they use no run-time information, in contrast to the :doc:`Dynamic Allocators <dynamicallocator>` discussed later on.
  20. Allocation will happen as soon as the model is direct connected.
  21. As a result of allocation, a file *locationsave.txt* is created, which contains the allocation that was found.
  22. In future runs, it is then possible to load from this file instead of doing the allocation process all over again.
  23. Writing a custom allocator
  24. --------------------------
  25. Writing an allocator is rather simple. The class has a simple method called *allocate*,
  26. which will return a dictionary with a model_id as its key and the node to place it on as the value.
  27. For the allocators, it is required that all model_ids are assigned a location, as otherwise they will not appear in the saved allocation file.
  28. The *allocate* has the following parameters:
  29. #. *models*: an iterable containing all models to allocate
  30. #. *edges*: **must** be ignored, as it is constantly *None*
  31. #. *nrnodes*: the number of nodes to allocate over
  32. #. *totalActivities*: **must** be ignored, as it is constantly *None*
  33. Furthermore, a method *getTerminationTime* is also required, but it should always return 0 for a static allocator.
  34. This gives the following template::
  35. class MyAllocator():
  36. def allocate(self, models, edges, nrnodes, totalActivities):
  37. # Do NOT use the edges and totalActivities arguments
  38. # To allocate model_ids 0, 1 and 2 to node 0 and model_id 3 to node 1
  39. return {0: 0, 1: 0, 2: 0, 3: 1}
  40. def getTerminationTime(self):
  41. return 0
  42. Using the allocator
  43. -------------------
  44. Using the static allocator that is provided in the PyPDEVS distribution is as simple as calling::
  45. sim = Simulator(Model())
  46. sim.setAutoAllocation()
  47. sim.simulate()
  48. Running a custom allocator uses the same methodology as a custom scheduler.
  49. For the allocator with classname *MyAllocator*, in the file *myAllocator*, the configuration is as follows::
  50. sim = Simulator(Model())
  51. sim.setInitialAllocator("myAllocator", "MyAllocator")
  52. sim.simulate()