我有一个3 x 2的矩形域,如图(a)所示。使用MPI笛卡尔拓扑,我可以识别每个单元的4个邻居(-1表示NULL),如下所示
Figure: Rectangular and arbitrary domain
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int rank, size;
int top = 0, right = 1, bottom = 2, left = 3;
int neighbors[4], dimSize[2] = {3, 2};
int usePeriods[2] = {0, 0}, newCoords[2];
MPI_Comm cartComm = MPI_COMM_WORLD;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (size != 6) {
if (rank == 0)
printf("Please Launch with ONLY 6 processes \n");
MPI_Finalize();
exit(0);
}
// Create a carthesian communicator
MPI_Cart_create(MPI_COMM_WORLD, 2, dimSize, usePeriods, 1, &cartComm);
// Obtain the 2D coordinates in the new communicator
MPI_Cart_coords(cartComm, rank, 2, newCoords);
// Obtain the direct neighbor ranks
MPI_Cart_shift(cartComm, 0, 1, neighbors + left, neighbors + right);
MPI_Cart_shift(cartComm, 1, 1, neighbors + top, neighbors + bottom);
printf("Rank: %d \t Neighbors(top, right, bottom, left): %2d, %2d, %2d, %2d \n", rank, neighbors[0], neighbors[1], neighbors[2], neighbors[3]);
MPI_Finalize();
return 0;
}
结果:
Rank: 0 Neighbors(top, right, bottom, left): -1, 2, 1, -1
Rank: 1 Neighbors(top, right, bottom, left): 0, 3, -1, -1
Rank: 2 Neighbors(top, right, bottom, left): -1, 4, 3, 0
Rank: 3 Neighbors(top, right, bottom, left): 2, 5, -1, 1
Rank: 4 Neighbors(top, right, bottom, left): -1, -1, 5, 2
Rank: 5 Neighbors(top, right, bottom, left): 4, -1, -1, 3
我的问题:是否存在使用MPI识别非矩形域的这些邻居的方法,如图(b)所示。在这里,我们假设没有周期性邻居。现在,我只能想到为3x3实现类似的笛卡尔拓扑方法(图c):
mpirun -np 9 ./run_cartesian
接下来,删除NULL单元格(灰色)并从0-5重新排序。将结果保存到文件,然后加载文件(使用root)并将相应的邻居分散到每个处理器。
mpirun -np 6 ./run_load_scatter
有关更好方法的建议吗?感谢。