MPI分散动态分配的二维数组(PGM文件映像) [英] Mpi Scatter dynamical allocated 2d array(pgm file image)

查看:406
本文介绍了MPI分散动态分配的二维数组(PGM文件映像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个二维数组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

希望这有助于。

这篇关于MPI分散动态分配的二维数组(PGM文件映像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆