Mpi Scatter动态分配2d数组(pgm文件图像)

时间:2012-11-15 00:46:24

标签: image image-processing 2d mpi scatter

我已经实现了2d数组Mpi散射,效果很好。我的意思是主处理器可以分散初始大数组的2d部分。问题是当我使用动态分配的2d图像文件作为输入它不起作用。我想记忆肯定有问题。有没有办法动态获取大型2d阵列的2d部分。

1 个答案:

答案 0 :(得分:0)

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

#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/Document/hos_cont.html

希望这有帮助。