gpt4 book ai didi

c++ - MPI_Send + struct + 动态内存分配

转载 作者:行者123 更新时间:2023-11-28 06:24:16 26 4
gpt4 key购买 nike

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

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

该类(class)现在使用数组,但我也使用 vector 获得相同的结果。通过注释/取消注释几行,您也可以尝试这样做。

#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;
}

最佳答案

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

(void*) 不会为我编译。

关于c++ - MPI_Send + struct + 动态内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28796229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com