【问题标题】:MPI Subarray Sending ErrorMPI 子数组发送错误
【发布时间】:2015-06-06 20:13:30
【问题描述】:

我首先初始化一个 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;

}

【问题讨论】:

    标签: c mpi


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,这是一个内存分配错误,与 MPI 无关。
    猜你喜欢
    • 2012-03-19
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2017-12-17
    • 2012-10-24
    • 2019-08-12
    相关资源
    最近更新 更多