allocator.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. class MyAllocator(object):
  2. """
  3. Allocate all models at the start of the simulation. After this, model relocation is handed over to a relocator.
  4. """
  5. def allocate(self, models, edges, nrnodes, totalActivities):
  6. """
  7. Calculate allocations for the nodes, using the information provided.
  8. :param models: the models to allocte
  9. :param edges: the edges between the models
  10. :param nrnodes: the number of nodes to allocate over. Simply an upper bound!
  11. :param totalActivities: activity tracking information from each model
  12. :returns: allocation that was found
  13. """
  14. # Return something of the form: {0: 0, 1: 0, 2: 0, 3: 1}
  15. # To allocate model_ids 0, 1 and 2 to node 0 and model_id 3 to node 1
  16. avgload = sum(totalActivities.values()) / nrnodes
  17. alloc = {}
  18. runningload = 0.0
  19. currentnode = 0
  20. for node, activity in totalActivities.items():
  21. if runningload + (activity / 2) > avgload:
  22. currentnode = (currentnode + 1) % nrnodes
  23. runningload = 0.0
  24. runningload += activity
  25. alloc[node] = currentnode
  26. return alloc
  27. def getTerminationTime(self):
  28. """
  29. Returns the time it takes for the allocator to make an 'educated guess' of the advised allocation.
  30. This time will not be used exactly, but as soon as the GVT passes over it. While this is not exactly
  31. necessary, it avoids the overhead of putting such a test in frequently used code.
  32. :returns: float -- the time at which to perform the allocations (and save them)
  33. """
  34. # No need for any run time information means 0.0
  35. return 2.0