• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2006 Douglas Gregor <doug.gregor -at- gmail.com>.
2
3# Use, modification and distribution is subject to the Boost Software
4# License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5# http://www.boost.org/LICENSE_1_0.txt)
6
7# Test reduce() collective.
8
9from __future__ import print_function
10import mpi
11from generators import *
12
13def reduce_test(comm, generator, kind, op, op_kind, root):
14    if comm.rank == root:
15        print ("Reducing to %s of %s at root %d..." % (op_kind, kind, root)),
16    my_value = generator(comm.rank)
17    result = mpi.reduce(comm, my_value, op, root)
18    if comm.rank == root:
19        expected_result = generator(0);
20        for p in range(1, comm.size):
21            expected_result = op(expected_result, generator(p))
22        assert result == expected_result
23        print ("OK.")
24    else:
25        assert result == None
26    return
27
28reduce_test(mpi.world, int_generator, "integers", lambda x,y:x + y, "sum", 0)
29reduce_test(mpi.world, int_generator, "integers", lambda x,y:x * y, "product", 1)
30reduce_test(mpi.world, int_generator, "integers", min, "minimum", 0)
31reduce_test(mpi.world, string_generator, "strings", lambda x,y:x + y, "concatenation", 0)
32reduce_test(mpi.world, string_list_generator, "list of strings", lambda x,y:x + y, "concatenation", 0)
33