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