MPI分散动态分配的二维数组(PGM文件映像)映像、数组、分散、文件

2023-09-07 14:25:53 作者:乍见之欢不如久处不厌

我实现了一个二维数组Mpi的散射效果很好。我的意思是主处理器可以散射初始大数组的2D部分。问题是,当我输入2D图像文件,动态地分配它不工作使用。我想这一定有什么错误的记忆。有没有办法动态地获得一个大的二维数组的2D部分。

I have implemented a 2d array Mpi scatter which works well. I mean that the master processor can scatter 2d parts of the initial big array. The problem is when I use as input the 2d image file dynamically allocated it doesn't work. I suppose that there must be something wrong with the memory. Is there any way of obtaining 2d parts of a big 2d array dynamically.

推荐答案

我也有类似的问题,但它是一维向量与动态分配的。 解决我的问题如下:

I had a similar problem, but it was one-dimensional vector with dynamically allocated. Solved my problem as follows:

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

main(int argc, char** argv) {

    /* .......Variables Initialisation ......*/
    int Numprocs, MyRank, Root = 0;
    int index;
    int *InputBuffer, *RecvBuffer;
    int Scatter_DataSize;
    int DataSize;
    MPI_Status status;

    /* ........MPI Initialisation .......*/
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &MyRank);
    MPI_Comm_size(MPI_COMM_WORLD, &Numprocs);

    if (MyRank == Root) {
        DataSize = 80000;

        /* ...Allocate memory.....*/

        InputBuffer = (int*) malloc(DataSize * sizeof(int));

        for (index = 0; index < DataSize; index++)
            InputBuffer[index] = index;
    }

    MPI_Bcast(&DataSize, 1, MPI_INT, Root, MPI_COMM_WORLD);
    if (DataSize % Numprocs != 0) {
        if (MyRank == Root)
            printf("Input is not evenly divisible by Number of Processes\n");
        MPI_Finalize();
        exit(-1);
    }

    Scatter_DataSize = DataSize / Numprocs;
    RecvBuffer = (int *) malloc(Scatter_DataSize * sizeof(int));

    MPI_Scatter(InputBuffer, Scatter_DataSize, MPI_INT, RecvBuffer,
            Scatter_DataSize, MPI_INT, Root, MPI_COMM_WORLD);

    for (index = 0; index < Scatter_DataSize; ++index)
        printf("MyRank = %d, RecvBuffer[%d] = %d \n", MyRank, index,
                RecvBuffer[index]);

    MPI_Finalize();

}

该链接有那些帮助过我的例子: http://www.cse.iitd.ernet.in/ 〜dheerajb / MPI /文件/ hos_cont.html

This link has examples that have helped me: http://www.cse.iitd.ernet.in/~dheerajb/MPI/Document/hos_cont.html

希望这有助于。