Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
waterman_teuchoscomm.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 // Program demonstrating the Teuchos::Comm issues causing #3331.
11 // On waterman, this test passes if it skips the call to
12 // Teuchos::Comm::duplicate, but segfaults if Teuchos::Comm::duplicate
13 // is called.
14 #include <iostream>
15 #include <fstream>
16 #include <Teuchos_RCP.hpp>
17 #include <Teuchos_DefaultComm.hpp>
18 #include <Teuchos_Comm.hpp>
19 #include <Teuchos_CommHelpers.hpp>
20 
21 void runTest(int ndoubles, Teuchos::RCP<const Teuchos::Comm<int> > &comm)
22 {
23  int me = comm->getRank();
24  int np = comm->getSize();
25 
26  double *sendBuf = new double[ndoubles];
27  for (int i = 0; i < ndoubles; i++) sendBuf[i] = 0.;
28  int nMy = ndoubles / np + (me < (ndoubles % np));
29  int myBegin = (ndoubles / np) * me;
30  myBegin += ((ndoubles % np) < me ? (ndoubles % np) : me);
31  int myEnd = myBegin + nMy;
32  for (int i = myBegin; i < myEnd; ++i) sendBuf[i] = me;
33 
34  double *recvBuf = new double[ndoubles];
35 
36  if (me == 0)
37  std::cout << "Trying reduceAll with ndoubles = " << ndoubles << std::endl;
38 
39  std::cout << *comm << std::endl;
40 
42  ndoubles, sendBuf, recvBuf);
43 
44  delete [] recvBuf;
45  delete [] sendBuf;
46 }
47 
48 
49 int main(int narg, char **arg)
50 {
51  Teuchos::GlobalMPISession mpiSession(&narg,&arg);
54 
55  int me = comm->getRank();
56 
57  if (narg != 2) {
58  if (me == 0)
59  std::cout << "Usage: a.out [Y|N] \n"
60  << " a.out Y ==> duplicate communicator \n"
61  << " a.out N ==> do not duplicate communicator \n"
62  << std::endl;
63  return -1;
64  }
65 
66  bool dupComm = (arg[1][0] == 'Y' ? true : false);
67 
68  if (dupComm)
69  Teuchos::RCP<const Teuchos::Comm<int> > commdup = comm->duplicate();
70 
71  runTest(512, comm);
72 
73  if (me == 0)
74  std::cout << "PASSED with "
75  << (dupComm ? "comm duplication " : "no comm duplication ")
76  << std::endl;
77  return 0;
78 }
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
Return the default global communicator.
Initialize, finalize, and query the global MPI session.
void runTest(int ndoubles, MPI_Comm &comm)
void reduceAll< int, double >(const Comm< int > &comm, const EReductionType reductType, const int count, const double sendBuffer[], double globalReducts[])
int main(int argc, char *argv[])
Smart reference counting pointer class for automatic garbage collection.
Reference-counted pointer class and non-member templated function implementations.