MPI contains routines to translate between Cartesian coordinates and ranks in a communicator, and to access the properties of a Cartesian topology.
The routine MPI_Graph_create allows the creation of a general graph topology.