1

不久前我问了一个关于这个项目的问题,我遇到了一个新问题。当我尝试运行 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
4

1 回答 1

1

需要改变两件事以获得正确的输出。首先,在我的主要代码中,我需要交换 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); 
于 2013-02-22T14:07:57.260 回答