Problem 3
/*
int MPI_Graph_create(MPI_Comm oldcomm, int nnodes, int *index, int *edges, int reorder, MPI_Comm *graphcomm),
where:
oldcomm - the initial communicator,
nnodes - the number of the graph vertices,
index - An index into the list of edges, giving the arcs proceeding from each vertex,
edges - the sequential list of the graph arcs,
reorder - the flag for pointing out if the process ranks can be reodered,
cartcomm - the created communicator with the graph type topology.
*/
#include<mpi.h>
#include<stdio.h>
int main(int argc, char *argv[]) {
MPI_Status status;
int rank,i;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
/***************************************************/
int reorder=1; /* Can the processes be reordered, may result in improved performance.*/
int index[] = { 4,7,10,13,16};
int edges[] = { 1,2,3,4,0,2,3,0,1,4,0,1,4,0,2,3};
/***************************************************/
int nneighbors;
int * intptr=&nneighbors;
int the_neighbors[10];
MPI_Comm MPI_Tree_World;
MPI_Graph_create(MPI_COMM_WORLD,5, index, edges, reorder,&MPI_Tree_World);
/*int MPI_Graph_neighbors_count(MPI_Comm comm,int rank, int *nneighbors); */
MPI_Graph_neighbors_count(MPI_Tree_World,rank, intptr);
/*int MPI_Graph_neighbors (MPI_Comm comm,int rank,int mneighbors, int *neighbors); */
MPI_Graph_neighbors (MPI_Tree_World,rank,nneighbors, the_neighbors);
printf(" %i has %i neighbors.\n",rank,nneighbors);
for (i=0;i<nneighbors;i++) {
if (the_neighbors[i]!=rank) printf(" A neighbor of %i is %i .\n",rank,the_neighbors[i]);
}
MPI_Finalize();
}
A Run
$ mpirun -n 5 mytopology
0 has 4 neighbors.
A neighbor of 0 is 1 .
A neighbor of 0 is 2 .
A neighbor of 0 is 3 .
A neighbor of 0 is 4 .
1 has 3 neighbors.
A neighbor of 1 is 0 .
A neighbor of 1 is 2 .
A neighbor of 1 is 3 .
2 has 3 neighbors.
A neighbor of 2 is 0 .
A neighbor of 2 is 1 .
A neighbor of 2 is 4 .
3 has 3 neighbors.
A neighbor of 3 is 0 .
A neighbor of 3 is 1 .
A neighbor of 3 is 4 .
4 has 3 neighbors.
A neighbor of 4 is 0 .
A neighbor of 4 is 2 .
A neighbor of 4 is 3 .