ring_test.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Copyright (C) 2006 Douglas Gregor <doug.gregor -at- gmail.com>.
  2. # Use, modification and distribution is subject to the Boost Software
  3. # License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. # http://www.boost.org/LICENSE_1_0.txt)
  5. # Test basic communication.
  6. import boost.parallel.mpi as mpi
  7. def ring_test(comm, value, kind, root):
  8. next_peer = (comm.rank + 1) % comm.size;
  9. prior_peer = (comm.rank + comm.size - 1) % comm.size;
  10. if comm.rank == root:
  11. print ("Passing %s around a ring from root %d..." % (kind, root)),
  12. comm.send(next_peer, 0, value)
  13. (other_value, stat) = comm.recv(return_status = True)
  14. assert value == other_value
  15. assert stat.source == prior_peer
  16. assert stat.tag == 0
  17. else:
  18. msg = comm.probe()
  19. other_value = comm.recv(msg.source, msg.tag)
  20. assert value == other_value
  21. comm.send(next_peer, 0, other_value)
  22. comm.barrier()
  23. if comm.rank == root:
  24. print "OK"
  25. pass
  26. if mpi.world.size < 2:
  27. print "ERROR: ring_test.py must be executed with more than one process"
  28. mpi.world.abort(-1);
  29. ring_test(mpi.world, 17, 'integers', 0)
  30. ring_test(mpi.world, 17, 'integers', 1)
  31. ring_test(mpi.world, 'Hello, World!', 'string', 0)
  32. ring_test(mpi.world, 'Hello, World!', 'string', 1)
  33. ring_test(mpi.world, ['Hello', 'MPI', 'Python', 'World'], 'list of strings', 0)
  34. ring_test(mpi.world, ['Hello', 'MPI', 'Python', 'World'], 'list of strings', 1)