1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2011 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
14 #define RING_NUM_NEIGHBORS 2
16 static int validate_dgraph(MPI_Comm dgraph_comm)
21 int srcs[RING_NUM_NEIGHBORS], dests[RING_NUM_NEIGHBORS];
22 int *src_wgts, *dest_wgts;
24 int world_rank, world_size;
27 comm_topo = MPI_UNDEFINED;
28 MPI_Topo_test(dgraph_comm, &comm_topo);
33 fprintf(stderr, "dgraph_comm is NOT of type MPI_DIST_GRAPH\n");
37 ierr = MPI_Dist_graph_neighbors_count(dgraph_comm,
38 &src_sz, &dest_sz, &wgt_flag);
39 if (ierr != MPI_SUCCESS) {
40 fprintf(stderr, "MPI_Dist_graph_neighbors_count() fails!\n");
45 fprintf(stderr, "MPI_Dist_graph_neighbors_count() succeeds!\n");
49 fprintf(stderr, "dgraph_comm is NOT created with MPI_UNWEIGHTED\n");
54 fprintf(stderr, "dgraph_comm is created with MPI_UNWEIGHTED\n");
56 if (src_sz != RING_NUM_NEIGHBORS || dest_sz != RING_NUM_NEIGHBORS) {
57 fprintf(stderr, "source or destination edge array is not of size %d.\n",
59 fprintf(stderr, "src_sz = %d, dest_sz = %d\n", src_sz, dest_sz);
64 src_wgts and dest_wgts could be anything, e.g. NULL, since
65 MPI_Dist_graph_neighbors_count() returns MPI_UNWEIGHTED.
66 Since this program has a Fortran77 version, and standard Fortran77
67 has no pointer and NULL, so use MPI_UNWEIGHTED for the weighted arrays.
69 src_wgts = MPI_UNWEIGHTED;
70 dest_wgts = MPI_UNWEIGHTED;
71 ierr = MPI_Dist_graph_neighbors(dgraph_comm,
72 src_sz, srcs, src_wgts,
73 dest_sz, dests, dest_wgts);
74 if (ierr != MPI_SUCCESS) {
75 fprintf(stderr, "MPI_Dist_graph_neighbors() fails!\n");
80 fprintf(stderr, "MPI_Dist_graph_neighbors() succeeds!\n");
84 Check if the neighbors returned from MPI are really
85 the nearest neighbors within a ring.
87 MPI_Comm_size(MPI_COMM_WORLD, &world_size);
88 MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
90 for (idx=0; idx < src_sz; idx++) {
91 nbr_sep = abs(srcs[idx] - world_rank);
92 if ( nbr_sep != 1 && nbr_sep != (world_size-1) ) {
93 fprintf(stderr, "srcs[%d]=%d is NOT a neighbor of my rank %d.\n",
94 idx, srcs[idx], world_rank);
98 for (idx=0; idx < dest_sz; idx++) {
99 nbr_sep = abs(dests[idx] - world_rank);
100 if ( nbr_sep != 1 && nbr_sep != (world_size-1) ) {
101 fprintf(stderr, "dests[%d]=%d is NOT a neighbor of my rank %d.\n",
102 idx, dests[idx], world_rank);
108 fprintf(stderr, "dgraph_comm is of type MPI_DIST_GRAPH "
109 "of a bidirectional ring.\n");
115 Specify a distributed graph of a bidirectional ring of the MPI_COMM_WORLD,
116 i.e. everyone only talks to left and right neighbors.
118 int main(int argc, char *argv[])
120 MPI_Comm dgraph_comm;
121 int world_size, world_rank, ierr;
126 int srcs[RING_NUM_NEIGHBORS], dests[RING_NUM_NEIGHBORS];
128 MTest_Init(&argc, &argv);
129 MPI_Comm_size(MPI_COMM_WORLD, &world_size);
130 MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
133 srcs[0] = world_rank;
134 dests[0] = world_rank-1 < 0 ? world_size-1 : world_rank-1 ;
135 dests[1] = world_rank+1 >= world_size ? 0 : world_rank+1 ;
136 ierr = MPI_Dist_graph_create(MPI_COMM_WORLD, 1, srcs, degs, dests,
137 MPI_UNWEIGHTED, MPI_INFO_NULL, 1,
139 if ( ierr != MPI_SUCCESS ) {
140 fprintf(stderr, "MPI_Dist_graph_create() fails!\n");
141 MPI_Abort(MPI_COMM_WORLD, 1);
144 if (!validate_dgraph(dgraph_comm)) {
145 fprintf(stderr, "MPI_Dist_graph_create() does NOT create "
146 "a bidirectional ring graph!\n");
147 MPI_Abort(MPI_COMM_WORLD, 1);
150 MPI_Comm_free(&dgraph_comm);
153 srcs[0] = world_rank-1 < 0 ? world_size-1 : world_rank-1 ;
154 srcs[1] = world_rank+1 >= world_size ? 0 : world_rank+1 ;
156 dests[0] = world_rank-1 < 0 ? world_size-1 : world_rank-1 ;
157 dests[1] = world_rank+1 >= world_size ? 0 : world_rank+1 ;
158 ierr = MPI_Dist_graph_create_adjacent(MPI_COMM_WORLD,
159 src_sz, srcs, MPI_UNWEIGHTED,
160 dest_sz, dests, MPI_UNWEIGHTED,
161 MPI_INFO_NULL, 1, &dgraph_comm);
162 if ( ierr != MPI_SUCCESS ) {
163 fprintf(stderr, "MPI_Dist_graph_create_adjacent() fails!\n");
164 MPI_Abort(MPI_COMM_WORLD, 1);
167 if (!validate_dgraph(dgraph_comm)) {
168 fprintf(stderr, "MPI_Dist_graph_create_adjacent() does NOT create "
169 "a bidirectional ring graph!\n");
170 MPI_Abort(MPI_COMM_WORLD, 1);
173 MPI_Comm_free(&dgraph_comm);
175 MTest_Finalize(errs);