我正在尝试使用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;
}
答案 0 :(得分:1)
我认为问题是在计算位移时你有(int*)
指针强制转换,当位移值需要以字节为单位时。在计算(char*)
和disp[0]
值时,我能够使用disp[1]
指针强制转换它。
(void*)
不会为我编译。