MPI_Cart_shift没有返回邻居

时间:2013-02-22 13:47:14

标签: c algorithm parallel-processing mpi

我刚才问了一个关于这个项目的问题,我遇到了一个新问题。当我尝试运行MPI_cart_shift以找到我的进程当前排名的最近邻居时,我没有得到正确的目标排名。

我正在尝试遵循此示例代码。 http://siber.cankaya.edu.tr/ozdogan/GraduateParallelComputing.old/ceng505/node133.html

这是我创建坐标系的代码

MPI_Comm_rank (MPI_COMM_WORLD, &rank);  /* get current process id */
MPI_Comm_size (MPI_COMM_WORLD, &size);  /* get number of processes */
.
.
.

if(is_perfect_square(size))  dim_size[0] = dim_size[1] = (int) sqrt(size);
else
{ //if size = 2 then dims = 2, 1; size = 4 then 2,2; 8 = 4, 2...
    dim_size[0] = (int) sqrt(size + size);
    dim_size[1] = dim_size[0] / 2;
}
fprintf(stderr, "%d | %d \n", dim_size[0], dim_size[1]);
MPI_Cart_create(MPI_COMM_WORLD, 2, dim_size, periods, 1, &CannonsCart);
MPI_Barrier(MPI_COMM_WORLD);
.
.
.
runCannon(CannonsCart, &a, &b, &c);

这里是runCannon。

int runCannon( MPI_Comm comm, Matrix *a, Matrix *b, Matrix *c)
{

    int size, rank, coordinates[2];
    int uprank, downrank, leftrank, rightrank;
    int shiftsource, shiftdest;
    int ix, swap;
    MPI_Status status;

    MPI_Comm_size(comm, &size); 
    MPI_Comm_rank(comm, &rank); 
    MPI_Cart_coords(comm, rank, 2, coordinates); 

    MPI_Cart_shift(comm, 0, -1, &rightrank, &leftrank); //Cart_shift is doing weird stuff that I don't understand.
    MPI_Cart_shift(comm, 1, -1, &downrank, &uprank); 



    printf("rank = %d, rightrank = %d, downrank = %d, leftrank = %d, uprank = %d\n", rank, rightrank, downrank, leftrank, uprank);

如果我分别用4,8,16个进程运行程序,我会得到这些输出。

rank = 0, rightrank = 2, downrank = 1, leftrank = 2, uprank = 1
rank = 1, rightrank = 3, downrank = 0, leftrank = 3, uprank = 0
rank = 3, rightrank = 1, downrank = 2, leftrank = 1, uprank = 2
rank = 2, rightrank = 0, downrank = 3, leftrank = 0, uprank = 3

0应该有rightrank = 1,downrank = 2,left rank = 1,uprank = 2等等。我不能只是交换MPI_shift调用,因为如果我有8个进程,那么这就是我的输出。

rank = 0, rightrank = 2, downrank = 1, leftrank = 6, uprank = 1
rank = 1, rightrank = 3, downrank = 0, leftrank = 7, uprank = 0
rank = 2, rightrank = 4, downrank = 3, leftrank = 0, uprank = 3
rank = 3, rightrank = 5, downrank = 2, leftrank = 1, uprank = 2
rank = 4, rightrank = 6, downrank = 5, leftrank = 2, uprank = 5
rank = 5, rightrank = 7, downrank = 4, leftrank = 3, uprank = 4
rank = 6, rightrank = 0, downrank = 7, leftrank = 4, uprank = 7
rank = 7, rightrank = 1, downrank = 6, leftrank = 5, uprank = 6

0应该有rightrank = 1,downrank = 4,leftrank = 3,uprank = 4

这里只是为了数据,这是16。

rank = 9, rightrank = 13, downrank = 10, leftrank = 5, uprank = 8
rank = 3, rightrank = 7, downrank = 0, leftrank = 15, uprank = 2
rank = 0, rightrank = 4, downrank = 1, leftrank = 12, uprank = 3
rank = 2, rightrank = 6, downrank = 3, leftrank = 14, uprank = 1
rank = 1, rightrank = 5, downrank = 2, leftrank = 13, uprank = 0
rank = 8, rightrank = 12, downrank = 9, leftrank = 4, uprank = 11
rank = 6, rightrank = 10, downrank = 7, leftrank = 2, uprank = 5
rank = 7, rightrank = 11, downrank = 4, leftrank = 3, uprank = 6
rank = 4, rightrank = 8, downrank = 5, leftrank = 0, uprank = 7
rank = 5, rightrank = 9, downrank = 6, leftrank = 1, uprank = 4
rank = 12, rightrank = 0, downrank = 13, leftrank = 8, uprank = 15
rank = 14, rightrank = 2, downrank = 15, leftrank = 10, uprank = 13
rank = 13, rightrank = 1, downrank = 14, leftrank = 9, uprank = 12
rank = 10, rightrank = 14, downrank = 11, leftrank = 6, uprank = 9
rank = 11, rightrank = 15, downrank = 8, leftrank = 7, uprank = 10
rank = 15, rightrank = 3, downrank = 12, leftrank = 11, uprank = 14

1 个答案:

答案 0 :(得分:0)

需要更改两件事才能获得正确的输出。首先,在我的主要代码中,我需要交换dim_size中的行和列,所以:

else
{ //if size = 2 then dims = 2, 1; size = 4 then 2,2; 8 = 4, 2...
    dim_size[1] = (int) sqrt(size + size);
    dim_size[0] = dim_size[1] / 2;
}

接下来,我需要交换我的MPI_Cart_shift调用,所以:

MPI_Cart_shift(comm, 1, -1, &rightrank, &leftrank); 
MPI_Cart_shift(comm, 0, -1, &downrank, &uprank);