Podstawowe funkcje |
MPI_CART_COORDS (comm,rank,maxdims,coords(),ierr)
MPI_CART_GET (comm,maxdims,dims,periods,coords(),ierr)
MPI_CART_MAP (comm_old,ndims,dims(),periods(),newrank,ierr)
MPI_CART_RANK (comm,coords(),rank,ierr)
MPI_CART_SHIFT (comm,direction,displ,source,dest,ierr)
MPI_CART_SUB (comm,remain_dims(),comm_new,ierr)
MPI_CARTDIM_GET (comm,ndims,ierr)
MPI_GRAPH_GET (comm,maxindex,maxedges,index(),edges(),ierr)
MPI_GRAPH_MAP (comm_old,nnodes,index(),edges(),newrank,ierr)
MPI_GRAPH_NEIGHBORS (comm,rank,maxneighbors,neighbors(),ierr)
MPI_GRAPHDIMS_GET (comm,nnodes,nedges,ierr)
MPI_TOPO_TEST (comm,top_type,ierr)
Przykład: Wirtualna topologia
Tworzy topologię kartezjańską 4 x 4 z 16 procesorów w które mają połączenia ze swoimi sąsiadami.
Język C |
#include "mpi.h" #include <stdio.h> #define SIZE 16 #define UP 0 #define DOWN 1 #define LEFT 2 #define RIGHT 3 main(int argc, char *argv[]) { int numtasks, rank, source, dest, outbuf, i, tag=1, inbuf[4]={MPI_PROC_NULL,MPI_PROC_NULL,MPI_PROC_NULL,MPI_PROC_NULL,}, nbrs[4], dims[2]={4,4}, periods[2]={0,0}, reorder=0, coords[2]; MPI_Request reqs[8]; MPI_Status stats[8]; MPI_Comm cartcomm; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD, &numtasks); if (numtasks == SIZE) { MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, reorder, &cartcomm); MPI_Comm_rank(cartcomm, &rank); MPI_Cart_coords(cartcomm, rank, 2, coords); MPI_Cart_shift(cartcomm, 0, 1, &nbrs[UP], &nbrs[DOWN]); MPI_Cart_shift(cartcomm, 1, 1, &nbrs[LEFT], &nbrs[RIGHT]); outbuf = rank; for (i=0; i<4; i++) { dest = nbrs[i]; source = nbrs[i]; MPI_Isend(&outbuf, 1, MPI_INT, dest, tag, MPI_COMM_WORLD, &reqs[i]); MPI_Irecv(&inbuf[i], 1, MPI_INT, source, tag, MPI_COMM_WORLD, &reqs[i+4]); } MPI_Waitall(8, reqs, stats); printf("rank= %d coords= %d %d neighbors(u,d,l,r)= %d %d %d %d\n", rank,coords[0],coords[1],nbrs[UP],nbrs[DOWN],nbrs[LEFT], nbrs[RIGHT]); printf("rank= %d inbuf(u,d,l,r)= %d %d %d %d\n", rank,inbuf[UP],inbuf[DOWN],inbuf[LEFT],inbuf[RIGHT]); } else printf("Must specify %d processors. Terminating.\n",SIZE); MPI_Finalize(); } |
Przykładowy wynik działania programu
rank= 0 coords= 0 0 neighbors(u,d,l,r)= -3 4 -3 1 rank= 0 inbuf(u,d,l,r)= -3 4 -3 1 rank= 1 coords= 0 1 neighbors(u,d,l,r)= -3 5 0 2 rank= 1 inbuf(u,d,l,r)= -3 5 0 2 rank= 2 coords= 0 2 neighbors(u,d,l,r)= -3 6 1 3 rank= 2 inbuf(u,d,l,r)= -3 6 1 3 . . . . . rank= 14 coords= 3 2 neighbors(u,d,l,r)= 10 -3 13 15 rank= 14 inbuf(u,d,l,r)= 10 -3 13 15 rank= 15 coords= 3 3 neighbors(u,d,l,r)= 11 -3 14 -3 rank= 15 inbuf(u,d,l,r)= 11 -3 14 -3