Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
waterman_mpi.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 // MPI-only (no Teuchos::Comm) version of waterman_teuchoscomm.cpp
11 // This version runs correctly on waterman.
12 #include <iostream>
13 #include <fstream>
14 #include <mpi.h>
15 
16 void runTest(int ndoubles, MPI_Comm &comm)
17 {
18  int me, np;
19  MPI_Comm_rank(comm, &me);
20  MPI_Comm_size(comm, &np);
21 
22  double *sendBuf = new double[ndoubles];
23  for (int i = 0; i < ndoubles; i++) sendBuf[i] = 0.;
24  int nMy = ndoubles / np + (me < (ndoubles % np));
25  int myBegin = (ndoubles / np) * me;
26  myBegin += ((ndoubles % np) < me ? (ndoubles % np) : me);
27  int myEnd = myBegin + nMy;
28  for (int i = myBegin; i < myEnd; ++i) sendBuf[i] = me;
29 
30  double *recvBuf = new double[ndoubles];
31 
32  if (me == 0)
33  std::cout << "Trying reduceAll with ndoubles = " << ndoubles << std::endl;
34 
35  MPI_Allreduce(sendBuf, recvBuf, ndoubles, MPI_DOUBLE, MPI_SUM, comm);
36 
37  delete [] recvBuf;
38  delete [] sendBuf;
39 }
40 
41 
42 int main(int narg, char **arg)
43 {
44  MPI_Init(&narg, &arg);
45  MPI_Comm comm = MPI_COMM_WORLD;
46 
47  int me;
48  MPI_Comm_rank(comm, &me);
49 
50  if (narg != 2) {
51  if (me == 0)
52  std::cout << "Usage: a.out [Y|N] \n"
53  << " a.out Y ==> duplicate communicator \n"
54  << " a.out N ==> do not duplicate communicator \n"
55  << std::endl;
56  return -1;
57  }
58 
59  bool dupComm = (arg[1][0] == 'Y' ? true : false);
60  MPI_Comm commdup;
61 
62  if (dupComm)
63  MPI_Comm_dup(comm, &commdup);
64 
65  runTest(512, comm);
66 
67  if (me == 0)
68  std::cout << "PASSED with "
69  << (dupComm ? "comm duplication " : "no comm duplication ")
70  << std::endl;
71 
72  if (dupComm) MPI_Comm_free(&commdup);
73  MPI_Finalize();
74  return 0;
75 }
void runTest(int ndoubles, MPI_Comm &comm)
int main(int argc, char *argv[])