reduce_test.py 1.3 KB

12345678910111213141516171819202122232425262728293031
  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 reduce() collective.
  6. import boost.parallel.mpi as mpi
  7. from generators import *
  8. def reduce_test(comm, generator, kind, op, op_kind, root):
  9. if comm.rank == root:
  10. print ("Reducing to %s of %s at root %d..." % (op_kind, kind, root)),
  11. my_value = generator(comm.rank)
  12. result = mpi.reduce(comm, my_value, op, root)
  13. if comm.rank == root:
  14. expected_result = generator(0);
  15. for p in range(1, comm.size):
  16. expected_result = op(expected_result, generator(p))
  17. assert result == expected_result
  18. print "OK."
  19. else:
  20. assert result == None
  21. return
  22. reduce_test(mpi.world, int_generator, "integers", lambda x,y:x + y, "sum", 0)
  23. reduce_test(mpi.world, int_generator, "integers", lambda x,y:x * y, "product", 1)
  24. reduce_test(mpi.world, int_generator, "integers", min, "minimum", 0)
  25. reduce_test(mpi.world, string_generator, "strings", lambda x,y:x + y, "concatenation", 0)
  26. reduce_test(mpi.world, string_list_generator, "list of strings", lambda x,y:x + y, "concatenation", 0)