autoAllocator.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright 2014 Modelling, Simulation and Design Lab (MSDL) at
  2. # McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
  3. #
  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. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. class AutoAllocator(object):
  16. """
  17. Allocate all models in a static manner, simply trying to divide the number of models equally.
  18. Our 'heuristic' is to allocate in chunks as defined in the root coupled model.
  19. """
  20. def allocate(self, models, edges, nr_nodes, total_activities):
  21. """
  22. Calculate allocations for the nodes, using the information provided.
  23. :param models: the models to allocte
  24. :param edges: the edges between the models
  25. :param nr_nodes: the number of nodes to allocate over. Simply an upper bound!
  26. :param total_activities: activity tracking information from each model
  27. :returns: allocation that was found
  28. """
  29. allocation = {}
  30. allocated_topmost = {}
  31. current_node = 0
  32. total_models = len(models)
  33. for model in models:
  34. # Not yet allocated, so allocate it somewhere
  35. child = model
  36. searchmodel = model
  37. while searchmodel.parent is not None:
  38. child = searchmodel
  39. searchmodel = searchmodel.parent
  40. # searchmodel is now the root model
  41. # child is its 1st decendant, on which we will allocate
  42. try:
  43. node = allocated_topmost[child]
  44. except KeyError:
  45. current_node = (current_node + 1) % nr_nodes
  46. allocated_topmost[child] = current_node
  47. node = current_node
  48. allocation[model.model_id] = node
  49. return allocation
  50. def getTerminationTime(self):
  51. """
  52. Returns the time it takes for the allocator to make an 'educated guess' of the advised allocation.
  53. This time will not be used exactly, but as soon as the GVT passes over it. While this is not exactly
  54. necessary, it avoids the overhead of putting such a test in frequently used code.
  55. :returns: float -- the time at which to perform the allocations (and save them)
  56. """
  57. # No need for any run time information
  58. return 0.0