如何将MPI派生的数据类型用于3D阵列? [英] How to use MPI derived data type for 3D array?

查看:95
本文介绍了如何将MPI派生的数据类型用于3D阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个在3D矩阵上工作的并行代码,其中每个进程都有其自己的子矩阵,但是为了完成其工作,他们需要一些有关其相邻进程的子矩阵(只是边界平面)的信息.我通过点对点通信发送这些信息,但是我知道对于大型矩阵来说,这不是一个好主意,因此我决定使用派生数据类型进行通信.我对mpi_type_vector有问题:例如,我有一个NX*NY*NZ矩阵,我想将常数为NY的平面发送到另一个进程,我为此编写了这些行:

I want to write a parallel code that works on a 3D matrix where each processes has it's own sub matrix but for doing their jobs they need some information about their neighbouring processes' sub matrix (just boundary planes). I send these informations with point to point communication but I know that for large matrix it is not a good idea so I decide to use derived data type for communication. I have problem with mpi_type_vector: for example I have a NX*NY*NZ matrix and I want to send plane with constant NY to another process I write these lines for doing this:

MPI_Datatype sub;

MPI_Type_vector(NX, NZ, NY*NZ, MPI_DOUBLE, &sub);

MPI_Type_commit(&sub);

但是它不起作用(无法发送我想要的飞机).怎么了? 我的测试代码在这里:

but it doesn't work (can not send my desired plane). What is wrong? my test code is here:

#include <mpi.h>
#include <iostream>

using namespace std;

int main(int argc,char ** argv)
{

    int const IE=100,JE=25,KE=100;
    int size,rank;
    MPI_Status status;

    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&size);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Datatype sub;
    MPI_Type_vector(KE,IE,IE+(JE-1)*IE,MPI_DOUBLE,&sub);
    MPI_Type_commit(&sub);

    if (rank==0){

        double*** a=new double**[IE];

        for(int i=0;i<IE;i++){
            a[i]=new double *[JE];
            for(int j=0;j<JE;j++){
                a[i][j]=new double [KE];
            }
        }

        for(int i=0;i<IE;i++){
            for(int j=0;j<JE;j++){
                for(int k=0;k<KE;k++){
                    a[i][j][k]=2;
                }}}

        for(int i=0;i<IE;i++){
            for(int j=0;j<JE;j++){
                a[i][j][0]=2;
            }}

        MPI_Send(&a[0][0][0],1,sub,1,52,MPI_COMM_WORLD);

    }

    if (rank==1){

        double*** b=new double**[IE];

        for(int i=0;i<IE;i++){
            b[i]=new double *[JE];
            for(int j=0;j<JE;j++){
                b[i][j]=new double [KE];
            }
        }

        for(int i=0;i<IE;i++){
            for(int j=0;j<JE;j++){
                for(int k=0;k<KE;k++){
                    b[i][j][k]=0;
                }}}

        MPI_Recv(&b[0][0][0][0],1,sub,0,52,MPI_COMM_WORLD,&status);

        for(int i=0;i<IE;i++){
            for(int j=0;j<JE;j++){
                for(int k=0;k<KE;k++){
                    if(b[i][j][k]>0){
                        cout<<"b["<<i<<"]["<<j<<"]["<<k<<"]="<<b[i][j][k]<<endl;
                    }}}}

    }

    MPI_Finalize();

}

推荐答案

对于3d矩阵,通常必须使用向量vector(因为涉及两个步幅)-可能,但是简单得多是使用 MPI_Type_create_subarray()您可以切出所需的多维数组的平板.

With a 3d matrix, in general you'd have to use a vector of vectors (because there are two strides involved) - which is possible, but much simpler is to use MPI_Type_create_subarray() which just lets you carve out the slab of a multidimensional array you want.

更新:上述代码中的一个问题是您分配的3d数组不连续;它是IE * JE分配的1d数组的集合,该数组可能彼此相距不远.因此,没有可靠的方法可以从中提取数据平面.

Update: One problem in the above code is that the 3d array you allocate isn't contiguous; it's a collection of IE*JE allocated 1d arrays which may or may not be anywhere near each other. So there's no reliable way of extracting a plane of data out of it.

您需要执行以下操作:

double ***alloc3d(int l, int m, int n) {
    double *data = new double [l*m*n];
    double ***array = new double **[l];
    for (int i=0; i<l; i++) {
        array[i] = new double *[m];
        for (int j=0; j<m; j++) {
            array[i][j] = &(data[(i*m+j)*n]);
        }
    }
    return array;
}

然后,将数据放在一个大立方体中,就像您期望的那样,其中指向数组的指针指向其中.这-C没有真正的多维数组的事实-C + MPI一直存在.

Then the data is in one big cube, like you'd expect, with an array of pointers pointing into it. This - the fact that C doesn't have real multidimensional arrays - comes up all the time with C + MPI.

这篇关于如何将MPI派生的数据类型用于3D阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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