用于2D阵列的MPI数据类型 [英] MPI data type for 2D array

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

问题描述

对于小时,试图发送二维数组通过MPI到不同的线程。代码看起来像这样(我会留下评论来显示我已经尝试过的技巧):



类型定义:

  //定义数据类型
// MPI_Type_contiguous(N,MPI_INT和& mpi_vector);
// MPI_Type_vector(N,N,N,mpi_vector,& mpi_matrix);
// MPI_Type_vector(N,N,N,MPI_INT,& mpi_matrix);
// MPI_Type_contiguous(N,MPI_INTEGER,& mpi_vector);
// MPI_Type_contiguous(N,mpi_vector,& mpi_matrix);
MPI_Type_vector(N,N,0,MPI_INTEGER,& mpi_matrix);
// MPI_Type_commit(& mpi_vector);
MPI_Type_commit(& mpi_matrix);

发送和接收:

  int ** tmp = new int * [N]; 
switch(r){
case T1:
inputMatrix(tmp,2);
MPI_Send(tmp,1,mpi_matrix,T2,0,MPI_COMM_WORLD);
// task_T1();
break;
case T2:
// task_T2();
inputMatrix(tmp,-1);
MPI_Recv(tmp,1,mpi_matrix,T1,0,MPI_COMM_WORLD,& s);
for(int i = 0; i {
for(int j = 0; j cout ; < tmp [i] [j];
}
cout<< endl;
}
break;
}

我需要这样做,直到早上smb能够帮助我。

解决方案

MPI DOES NOT SUPPORT 单独的,一个指向行的指针数组。 MPI仅在平面数组中工作, ,其中行连续存储在内存中。使用平面数组:

  int ** tmp = ... 
pre>

应为

  int * tmp = new int [rows * cols]; 

然后 tmp [row] [col] 应该访问 tmp [row * cols + col]



code> MPI_INT ,然后从整个矩阵的上一个连续类型构造一个连续类型。您还可以构造单个连续类型的 rows * cols MPI_INT 元素。第一种方法更灵活,因为您可能需要稍后发送单独的行。


For hours im trying to send 2D array over MPI to diffrent thread. Code looks like this (i'll leave comments to show tricks that i have already tryed):

Type definition:

// Define datatype
//MPI_Type_contiguous(N, MPI_INT, &mpi_vector);
//MPI_Type_vector(N, N, N, mpi_vector, &mpi_matrix);
//MPI_Type_vector(N, N, N, MPI_INT, &mpi_matrix);
//MPI_Type_contiguous(N, MPI_INTEGER, &mpi_vector);
//MPI_Type_contiguous(N, mpi_vector, &mpi_matrix);
MPI_Type_vector(N, N, 0, MPI_INTEGER, &mpi_matrix);
//MPI_Type_commit(&mpi_vector);
MPI_Type_commit(&mpi_matrix);

Sending and recieving:

int** tmp = new int*[N];
switch(r) {
case T1:
    inputMatrix(tmp, 2);
    MPI_Send(tmp, 1, mpi_matrix, T2, 0, MPI_COMM_WORLD);
    //task_T1();
    break;
case T2:
    //task_T2();
    inputMatrix(tmp, -1);
    MPI_Recv(tmp, 1, mpi_matrix, T1, 0, MPI_COMM_WORLD, &s);
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++) {
            cout << "  " << tmp[i][j];
        }
        cout << endl;
    }
    break;
}

I need to have this done till morning (in 7 hours), i hope smb is able to help me.

解决方案

MPI DOES NOT SUPPORT matrices in which each row is allocated separately, a.k.a. array of pointers to rows. MPI works only with flat arrays where rows are stored consecutively in memory. Use a flat array:

int** tmp = ...

should become

int* tmp = new int[rows*cols];

and then tmp[row][col] should be accessed as tmp[row*cols + col].

You can construct a contiguous type from MPI_INT for a single row and then construct a contiguous type from the previous contiguous type for the whole matrix. You can also construct a single contiguous type of rows*cols MPI_INT elements. The first approach is more flexible as you might need to send individual rows later.

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

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