我正在尝试使用 MPI 实现 Cannons 算法,我正在使用以下示例代码:
http://siber.cankaya.edu.tr/ozdogan/GraduateParallelComputing.old/ceng505/node133.html
有一段看不懂。这是示例代码。
37 /* Perform the initial matrix alignment. First for A and then for B */
38 MPI_Cart_shift(comm_2d, 0, -mycoords[0], &shiftsource, &shiftdest);
39 MPI_Sendrecv_replace(a, nlocal*nlocal, MPI_DOUBLE, shiftdest,
40 1, shiftsource, 1, comm_2d, &status);
41
42 MPI_Cart_shift(comm_2d, 1, -mycoords[1], &shiftsource, &shiftdest);
43 MPI_Sendrecv_replace(b, nlocal*nlocal, MPI_DOUBLE,
44 shiftdest, 1, shiftsource, 1, comm_2d, &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);
MPI_Cart_shift(comm, 1, -1, &downrank, &uprank);
MPI_Cart_shift(comm, 0, -coordinates[0], &shiftsource, &shiftdest);
printf("coordinates[0] = %d for a shiftsource = %d, shiftdest = %d\n", coordinates[0], shiftsource, shiftdest);
//MPI_Sendrecv_replace(a, a->rowNum * a->colNum, MPI_INT, shiftdest,
//1, shiftsource, 1, comm, &status);
MPI_Cart_shift(comm, 1, -coordinates[1], &shiftsource, &shiftdest);
printf("coordinates[1] = %d for b shiftsource = %d, shiftdest = %d\n", coordinates[1], shiftsource, shiftdest);
//MPI_Sendrecv_replace(b, b->rowNum * b->colNum, MPI_INT,
// shiftdest, 1, shiftsource, 1, comm, &status);
我在不同的函数中调用 MPI_Cart_create,但它与示例代码中的基本调用相同。
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;
}
MPI_Cart_create(MPI_COMM_WORLD, 2, dim_size, periods, 1, &CannonsCart);
现在我只是想了解 shiftsource 和 shiftdest 的意义。我假设它是用于初始转变,但是当我运行此代码时,我的 printf 语句会这样说。
coordinates[0] = 0 for a shiftsource = 0, shiftdest = 0
coordinates[1] = 0 for b shiftsource = 0, shiftdest = 0
coordinates[0] = 1 for a shiftsource = 1, shiftdest = 1
coordinates[1] = 1 for b shiftsource = 2, shiftdest = 2
coordinates[0] = 1 for a shiftsource = 0, shiftdest = 0
coordinates[1] = 0 for b shiftsource = 2, shiftdest = 2
coordinates[0] = 0 for a shiftsource = 1, shiftdest = 1
coordinates[1] = 1 for b shiftsource = 0, shiftdest = 0
我不明白为什么 shiftsource 和 shiftdest 是一样的。对于矩阵 a 和 b,它应该是左边一个,一个向上。
对于这个测试用例,这个调用的进程数(IE 大小)是 4。如果需要,我将托管我的所有代码。