Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
ReduceAllAndScatter_test.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include "Teuchos_ConfigDefs.hpp"
43 #include "Teuchos_DefaultComm.hpp"
44 #include "Teuchos_CommHelpers.hpp"
46 
47 int main(int argc, char* argv[]) {
48 
49  using Teuchos::RCP;
50 
51  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
52 
53  int ierr = 0;
54 
55  RCP<const Teuchos::Comm<int> >
57 
58  const int size = Teuchos::size(*comm);
59  const int rank = Teuchos::rank(*comm);
60 
61  if (rank == 0) {
62  std::cout
63  << "Rank: " << rank
64  << "\t\tSize: " << size << std::endl;
65  }
66 
67  const int one = Teuchos::OrdinalTraits<int>::one();
68 
69  std::vector<int> sendbuf(size,one);
70  std::vector<int> recvcounts(size,one);
71 
72  // Try straight MPI version
73 #ifdef HAVE_MPI
74  {
75  std::vector<int> sbuf(sendbuf);
76  std::vector<int> rcnt(recvcounts);
77  int rbuf;
78 
79  MPI_Comm mpi_comm = MPI_COMM_WORLD;
80  MPI_Reduce_scatter(&sbuf[0],&rbuf,&rcnt[0],MPI_INT,MPI_SUM,mpi_comm);
81 
82  // should be size
83  if (rank == 0) {
84  std::cout << "Received from direct MPI call: " << rbuf << std::endl;
85  }
86  ierr += (rbuf == size ? 0 : 1);
87  }
88 #endif
89 
90  // Try straight MPI version with user-defined type
91 #ifdef HAVE_MPI
92  {
93  std::vector<int> sbuf(sendbuf);
94  std::vector<int> rcnt(recvcounts);
95  int rbuf;
96 
97  MPI_Comm mpi_comm = MPI_COMM_WORLD;
98  MPI_Datatype _chars_type;
99  MPI_Type_contiguous(sizeof(int), MPI_CHAR, &_chars_type);
100  MPI_Reduce_scatter(&sbuf[0], &rbuf, &rcnt[0], _chars_type, MPI_SUM, mpi_comm);
101 
102  // should be size
103  if (rank == 0) {
104  std::cout << "Received from direct MPI call with user type: " << rbuf << std::endl;
105  }
106  ierr += (rbuf == size ? 0 : 1);
107  }
108 #endif
109 
110  // Try Teuchos version
111  {
112  std::vector<int> sbuf(sendbuf);
113  std::vector<int> rcnt(recvcounts);
114  int rbuf;
115 
116  Teuchos::reduceAllAndScatter<int>(*comm,Teuchos::REDUCE_SUM,(int)sbuf.size(),&sbuf[0],&rcnt[0],&rbuf);
117 
118  // should be size
119  if (rank == 0) {
120  std::cout << "Received from MPI-via-Teuchos call: " << rbuf << std::endl;
121  }
122  ierr += (rbuf == size ? 0 : 1);
123  }
124 
125  if (rank == 0) {
126  if (ierr) {
127  std::cerr << "Test FAILED." << std::endl;
128  }
129  else {
130  std::cerr << "Test passed." << std::endl;
131  }
132  }
133  return ierr;
134 }
static T one()
Returns representation of one for this ordinal type.
int main(int argc, char *argv[])
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
int rank(const Comm< Ordinal > &comm)
Get the process rank.
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
Return the default global communicator.
Initialize, finalize, and query the global MPI session.
int size(const Comm< Ordinal > &comm)
Get the number of processes in the communicator.
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Smart reference counting pointer class for automatic garbage collection.