ReduceAllAndScatter_test.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //          Tpetra: Templated Linear Algebra Services Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Teuchos_ConfigDefs.hpp"
00030 #include "Teuchos_DefaultComm.hpp"
00031 #include "Teuchos_CommHelpers.hpp"
00032 #include "Teuchos_GlobalMPISession.hpp"
00033 
00034 int main(int argc, char* argv[]) {
00035 
00036   using Teuchos::RCP;
00037 
00038   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00039 
00040   int ierr = 0;
00041 
00042   RCP<const Teuchos::Comm<int> >
00043     comm = Teuchos::DefaultComm<int>::getComm();
00044 
00045   const int size = Teuchos::size(*comm);
00046   const int rank = Teuchos::rank(*comm);
00047 
00048   if (rank == 0) {
00049     std::cout
00050       << "Rank: " << rank 
00051       << "\t\tSize: " << size << std::endl;
00052   }
00053 
00054   const int one  = Teuchos::OrdinalTraits<int>::one();
00055 
00056   std::vector<int> sendbuf(size,one);
00057   std::vector<int> recvcounts(size,one);
00058 
00059   // Try straight MPI version
00060 #ifdef HAVE_MPI
00061   {
00062     std::vector<int> sbuf(sendbuf);
00063     std::vector<int> rcnt(recvcounts);
00064     int rbuf;
00065 
00066     MPI_Comm mpi_comm = MPI_COMM_WORLD;
00067     MPI_Reduce_scatter(&sbuf[0],&rbuf,&rcnt[0],MPI_INT,MPI_SUM,mpi_comm);
00068 
00069     // should be size
00070     if (rank == 0) {
00071       std::cout << "Received from direct MPI call: " << rbuf << std::endl;
00072     }
00073     ierr += (rbuf == size ? 0 : 1);
00074   }
00075 #endif
00076 
00077   // Try straight MPI version with user-defined type
00078 #ifdef HAVE_MPI
00079   {
00080     std::vector<int> sbuf(sendbuf);
00081     std::vector<int> rcnt(recvcounts);
00082     int rbuf;
00083 
00084     MPI_Comm mpi_comm = MPI_COMM_WORLD;
00085     MPI_Datatype _chars_type;
00086     MPI_Type_contiguous(sizeof(int), MPI_CHAR, &_chars_type);
00087     MPI_Reduce_scatter(&sbuf[0], &rbuf, &rcnt[0], _chars_type, MPI_SUM, mpi_comm);
00088 
00089     // should be size
00090     if (rank == 0) {
00091       std::cout << "Received from direct MPI call with user type: " << rbuf << std::endl;
00092     }
00093     ierr += (rbuf == size ? 0 : 1);
00094   }
00095 #endif
00096 
00097   // Try Teuchos version
00098   { 
00099     std::vector<int> sbuf(sendbuf);
00100     std::vector<int> rcnt(recvcounts);
00101     int rbuf;
00102 
00103     Teuchos::reduceAllAndScatter<int>(*comm,Teuchos::REDUCE_SUM,(int)sbuf.size(),&sbuf[0],&rcnt[0],&rbuf);
00104 
00105     // should be size
00106     if (rank == 0) {
00107       std::cout << "Received from MPI-via-Teuchos call: " << rbuf << std::endl;
00108     }
00109     ierr += (rbuf == size ? 0 : 1);
00110   }
00111 
00112   if (rank == 0) {
00113     if (ierr) {
00114       std::cerr << "Test FAILED." << std::endl;
00115     }
00116     else {
00117       std::cerr << "Test passed." << std::endl;
00118     }
00119   }
00120   return ierr;
00121 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 09:57:28 2011 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.3