middleware.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. """
  16. Middleware detection and setup code
  17. """
  18. import sys
  19. def startupMiddleware():
  20. """
  21. Do the actual detection and startup, also defines all necessary globals
  22. :returns: tuple -- current server rank and total world size
  23. """
  24. if "MPI" in globals():
  25. # Force local simulation
  26. return 0, 1
  27. # Try loading MPI
  28. global COMM_WORLD
  29. global MPI
  30. try:
  31. from mpi4py import MPI
  32. COMM_WORLD = MPI.COMM_WORLD
  33. except ImportError:
  34. # No MPI4Py found, so force local MPI simulation
  35. from pypdevs.MPIRedirect import MPIFaker
  36. COMM_WORLD = MPIFaker()
  37. # Now we should take care of the starting of the server
  38. rank = COMM_WORLD.Get_rank()
  39. if rank != 0:
  40. # We should stop immediately, to prevent multiple constructions of the model
  41. # This is a 'good' stop, so return with a zero
  42. from pypdevs.server import Server
  43. server = Server(int(rank), COMM_WORLD.Get_size())
  44. sys.exit(0)
  45. else:
  46. # We should still shutdown every simulation kernel at exit by having the controller send these messages
  47. # Use the atexit code at the end
  48. if COMM_WORLD.Get_size() > 1:
  49. import atexit
  50. atexit.register(cleanupMPI)
  51. return 0, COMM_WORLD.Get_size()
  52. def cleanupMPI():
  53. """
  54. Shut down the MPI backend by sending a termination message to all listening nodes
  55. """
  56. for i in range(COMM_WORLD.Get_size()):
  57. if i == COMM_WORLD.Get_rank():
  58. req = COMM_WORLD.isend(0, dest=i, tag=0)
  59. else:
  60. COMM_WORLD.send(0, dest=i, tag=0)
  61. if COMM_WORLD.Get_size() > 1:
  62. MPI.Request.wait(req)