Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
subcommTestMpi.cpp
Go to the documentation of this file.
1 // MPI-only version of subcommTestTeuchosComm.cpp
2 
3 #include <iostream>
4 #include <mpi.h>
5 
6 
7 int main(int narg, char **arg)
8 {
9  MPI_Init(&narg, &arg);
10  MPI_Comm comm = MPI_COMM_WORLD;
11 
12  int me, np;
13  MPI_Comm_rank(comm, &me);
14  MPI_Comm_size(comm, &np);
15 
16  int niter = 4;
17  int *ids = new int[np/2+1];
18  for (int i = 0; i < np/2+1; i++) ids[i] = i;
19 
20  for (int i = 0; i < niter; i++) {
21  MPI_Comm a;
22  MPI_Group cgrp, agrp;
23  MPI_Comm_group(comm, &cgrp);
24  MPI_Group_incl(cgrp, np/2+1, ids, &agrp);
25 
26  MPI_Comm_create(comm, agrp, &a);
27 
28  MPI_Group_free(&agrp);
29  MPI_Group_free(&cgrp);
30 
31  if (a != MPI_COMM_NULL) {
32  int anp;
33  MPI_Comm_size(a, &anp);
34  std::cout << me << " Iteration " << i << " New comm has " << anp << " ranks"
35  << std::endl;
36  MPI_Comm_free(&a);
37  }
38  else {
39  std::cout << me << " not in new communicator" << std::endl;
40  }
41  }
42  delete [] ids;
43  if (me == 0)
44  std::cout << "PASS" << std::endl;
45 
46  MPI_Finalize();
47  return 0;
48 }
int main(int argc, char *argv[])