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 // @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 version of subcommTestTeuchosComm.cpp
11 
12 #include <iostream>
13 #include <mpi.h>
14 
15 
16 int main(int narg, char **arg)
17 {
18  MPI_Init(&narg, &arg);
19  MPI_Comm comm = MPI_COMM_WORLD;
20 
21  int me, np;
22  MPI_Comm_rank(comm, &me);
23  MPI_Comm_size(comm, &np);
24 
25  int niter = 4;
26  int *ids = new int[np/2+1];
27  for (int i = 0; i < np/2+1; i++) ids[i] = i;
28 
29  for (int i = 0; i < niter; i++) {
30  MPI_Comm a;
31  MPI_Group cgrp, agrp;
32  MPI_Comm_group(comm, &cgrp);
33  MPI_Group_incl(cgrp, np/2+1, ids, &agrp);
34 
35  MPI_Comm_create(comm, agrp, &a);
36 
37  MPI_Group_free(&agrp);
38  MPI_Group_free(&cgrp);
39 
40  if (a != MPI_COMM_NULL) {
41  int anp;
42  MPI_Comm_size(a, &anp);
43  std::cout << me << " Iteration " << i << " New comm has " << anp << " ranks"
44  << std::endl;
45  MPI_Comm_free(&a);
46  }
47  else {
48  std::cout << me << " not in new communicator" << std::endl;
49  }
50  }
51  delete [] ids;
52  if (me == 0)
53  std::cout << "PASS" << std::endl;
54 
55  MPI_Finalize();
56  return 0;
57 }
int main(int argc, char *argv[])