【问题标题】:MPI_Send + struct + dynamic memory allocationMPI_Send + struct + 动态内存分配
【发布时间】:2015-05-02 00:06:16
【问题描述】:

我正在尝试使用 MPI 在 C++ 中处理一些动态分配的多维数组。为了避免担心不连续的内存,我编写了一个类包装器,它允许我访问 1d 数组,就好像它是 2d 一样。我正在尝试创建一个 MPI 数据类型以通过 MPI_Send 发送类的实例。

我的代码如下。当我在自己的 MPI_Send 缓冲区中发送类的每个元素时,它会给出预期的结果。当我尝试使用我的自定义 MPI 数据类型时,它会出现分段错误。通过注释/取消注释几行,您可以尝试两种方式。

该类现在使用数组,但我也得到与向量相同的结果。通过注释/取消注释几行,您也可以尝试。

#include "mpi.h"
#include <stdio.h>
#include <vector>
using namespace std;

//. arrays will be this big
const int N(2);

//. this class is a lightweight wrapper around a 1d vector so that it can be accessed as 2d. this
//. ensures that the memory use is contiguous, so it can be sent through mpi. by commenting/
//. uncommenting, it can be set to use either a vector or an array. 
template <class type> class arr2d{
  public:
    int s[2]; //. size (length and width)
/*    vector<type> v; */ //. vector data container 
    type* v; //. array data container
/*    void init(const int& s0, const int& s1){s[0] = s0; s[1] = s1; v.resize(s[0]*s[1]);} */
    void init(const int& s0, const int& s1){s[0] = s0; s[1] = s1; v = new type[s[0]*s[1]];}
    type& operator()(const int& i, const int& k){return v[s[1]*i + k];}
};

int main(){
  //. standard mpi stuff
  int mpi_rank, mpi_size;
  MPI_Status stat;

  //. declare an arr2d object
  arr2d<double> x;
  x.init(N,N);

  //. displacements, types, and elements (for mpi_type_create_struct)
  MPI_Aint     disp[2];
  MPI_Datatype type[2];
  int          elts[2];

  //. this will hold the arr2d mpi data type
  MPI_Datatype mpi_arr2d;

  //. fire up mpi
  MPI_Init(NULL,NULL);
  MPI_Comm_rank(MPI_COMM_WORLD,&mpi_rank);
  MPI_Comm_size(MPI_COMM_WORLD,&mpi_size);

  //. put some values in the rank 0 version of x
  if(mpi_rank == 0){
    for(int i=0;i<N;i++){
      for(int k=0;k<N;k++){
        x(i,k) = i+k+0.5;
      }
    }
  } else { //. rank 1 starts with x full of zeros
    for(int i=0;i<N;i++){
      for(int k=0;k<N;k++){
        x(i,k) = 0;
      }
    }
  }

  //. displaceemnt of elements of x (vector implementation)
/*  disp[0] = (int*)&x.s - (int*)&x;
  disp[1] = (int*)&x.v.front() - (int*)&x; */

  //. displaceemnt of elements of x (array implementation)
  disp[0] = (int*)&x.s - (int*)&x;
  disp[1] = (int*)&x.v[0] - (int*)&x; 

  //. types of elements of x
  type[0] = MPI_INT;
  type[1] = MPI_DOUBLE;

  //. quantities of elements of x
  elts[0] = 2;
  elts[1] = N*N;

  //. assemble and commit mpi_arr2d
  MPI_Type_create_struct(2,elts,disp,type,&mpi_arr2d);
  MPI_Type_commit(&mpi_arr2d);

  //. check what each rank sees before communication
  printf("rank %d sees %f %f %f %f \n",mpi_rank,x(0,0),x(0,1),x(1,0),x(1,1));

  if(mpi_rank == 0){
    MPI_Send(&x,1,mpi_arr2d,1,123,MPI_COMM_WORLD); 
/*    MPI_Send(&x.s,2,MPI_INT,1,124,MPI_COMM_WORLD); */ //. send just the size
/*    MPI_Send(&x.v.front(),N*N,MPI_DOUBLE,1,125,MPI_COMM_WORLD); */ //. send just the vector
/*    MPI_Send(&x.v[0],N*N,MPI_DOUBLE,1,125,MPI_COMM_WORLD); */ //. send just the array
    printf("just send to rank 1\n");
  }
  if(mpi_rank == 1){
    MPI_Recv(&x,1,mpi_arr2d,0,123,MPI_COMM_WORLD,&stat); 
/*    MPI_Recv(&x.s,2,MPI_INT,0,124,MPI_COMM_WORLD,&stat); */ //. recv the size
/*    MPI_Recv(&x.v.front(),N*N,MPI_DOUBLE,0,125,MPI_COMM_WORLD,&stat); */ //. recv the vector
/*    MPI_Recv(&x.v[0],N*N,MPI_DOUBLE,0,125,MPI_COMM_WORLD,&stat); */ //. recv the array
    printf("just recved from rank 0\n");
  }

  //. check what each rank sees after communication
  printf("rank %d sees %f %f %f %f \n",mpi_rank,x(0,0),x(0,1),x(1,0),x(1,1));

  MPI_Finalize();
  return 0;
}

【问题讨论】:

  • By commenting/uncommenting a few lines, you can try both ways 那么,您发布的版本,没有任何进一步的修改,是有效的还是无效的?
  • 上面的版本不行。将 int* 更改为 char* 使一切正常(根据 Tower 博士)。

标签: c++ mpi


【解决方案1】:

我认为问题在于您在计算位移时有(int*) 指针转换,而位移值需要以字节为单位。在计算 disp[0]disp[1] 值时,我能够使用 (char*) 指针转换使其工作。

(void*) 不会为我编译。

【讨论】:

  • 这对数组和向量实现都有效。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-17
  • 1970-01-01
  • 2016-01-04
  • 1970-01-01
  • 2021-02-15
  • 2013-07-01
相关资源
最近更新 更多