MPI子阵列发送错误

时间:2015-04-02 00:59:44

标签: c mpi

我首先初始化一个4x4矩阵,然后尝试通过在C中使用MPI将第一个2x2块发送到从属进程。但是从属进程只接收块的第一行,第二行用随机数填充来自电脑公羊。我无法找到遗漏的东西。该计划的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

#define     SIZE        4

int main(int argc, char** argv)
{
int rank, nproc;
const int root = 0;
const int tag = 3;

int** table;
int* datas;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);

datas = malloc(SIZE * SIZE * sizeof(int));
table = malloc(SIZE * sizeof(int*));

for (int i = 0; i < SIZE; i++)
    table[i] = &(datas[i * SIZE]);

for (int i = 0; i < SIZE; i++)
    for (int k = 0; k < SIZE; k++)
        table[i][k] = 0;

table[0][1] = 1;
table[0][2] = 2;
table[1][0] = 3;
table[2][3] = 2;
table[3][1] = 3;
table[3][2] = 4;


if (rank == root){
    MPI_Datatype newtype;
    int sizes[2] = { 4, 4 };  // size of table 
    int subsizes[2] = { 2, 2 };  // size of sub-region 
    int starts[2] = { 0, 0 };

    MPI_Type_create_subarray(2, sizes, subsizes, starts, MPI_ORDER_C, MPI_INT, &newtype);
    MPI_Type_commit(&newtype);

    MPI_Send(&(table[0][0]), 1, newtype, 1, tag, MPI_COMM_WORLD);
}
else{
    int* local_datas = malloc(SIZE * SIZE * sizeof(int));
    int** local = malloc(SIZE * sizeof(int*));

    for (int i = 0; i < SIZE; i++)
        local[i] = &(local_datas[i * SIZE]);

    MPI_Recv(&(local[0][0]), 4, MPI_INT, root, tag, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);

    for (int i = 0; i < 2; i++){
        for (int k = 0; k < 2; k++)
            printf("%3d ", local[i][k]);
        printf("\n");
    }
}


MPI_Finalize();

return 0;

}

1 个答案:

答案 0 :(得分:2)

您已指示接收操作将四个整数值连续放入内存中,因此在接收时将2x2块转换为1x4行(因为local为4x4)。第local行包含随机值,因为内存从未初始化。

您应该在发送方和接收方都使用MPI_Type_create_subarray,以便将接收到的数据放在2x2块中,或者将local重新定义为2x2矩阵而不是4x4。