Parallel processing 如何在MPI_send()中发送struct类型的变量?

Parallel processing 如何在MPI_send()中发送struct类型的变量?,parallel-processing,cluster-computing,mpi,Parallel Processing,Cluster Computing,Mpi,我已经使用MPI用C编写了一个程序,其中struct变量将以环形方式发送到进程,并根据从该变量接收到的值,分配该特定进程的工作 问题是我需要知道如何在 MPIysEnter()/函数中发送结构变量,因为它在运行时给出无效的数据类型,请考虑下面的例子 struct info{ int ne, n, u, v, process, min, strip, mincost, b; } stat; MPI_Send(&stat,sizeof(stat),sizeof(struct info)

我已经使用MPI用C编写了一个程序,其中struct变量将以环形方式发送到进程,并根据从该变量接收到的值,分配该特定进程的工作

问题是我需要知道如何在<代码> MPIysEnter()/<代码>函数中发送结构变量,因为它在运行时给出无效的数据类型,请考虑下面的例子

struct info{
  int ne, n, u, v, process, min, strip, mincost, b;
} stat;

MPI_Send(&stat,sizeof(stat),sizeof(struct info),1,2,MPI_COMM_WORLD);

在发送结构之前,必须执行一些操作。 我为您的示例编写了代码,但为了更好地理解,您应该阅读一些文档。 无论如何,这里有一些提示:

  • 如果您有一个只由一种元素组成的结构,比如在您的示例中,所有变量都是int,那么最好发送一个向量,注意每个变量的位置
  • 如果有其他类型,则必须将计数设置为2或更多,并更改所有其他数组(例如:数组类型的数组、数组块长度的数组等等)
  • 在这种情况下,您可以自己计算\u displaysments的数组\u的值。 例如,如果您有下面的结构,x将从0开始,而y将从8开始,因为将添加4个字节的填充以对齐元素<代码>结构点{intx;双y;}
  • 如果不想计算\u显示元素的数组,请始终使用MPI\u Get\u地址,而不要依赖于&运算符
代码如下:

struct info{
    int ne, n, u, v, process,min,strip,mincost,b;
}stat;


int main(...){

/*MPI INIT*/

struct info _info,  

int count; //Says how many kinds of data your structure has
count = 1; //1, 'cause you just have int

// Says the type of every block
MPI_Datatype array_of_types[count];
// You just have int
array_of_types[0] = MPI_INT;

// Says how many elements for block
int array_of_blocklengths[count];
// You have 8 int
array_of_blocklengths[0] = {8};

/* Says where every block starts in memory, counting from the beginning of the struct. */
MPI_Aint array_of_displaysments[coun];
MPI_Aint address1, address2;
MPI_Get_address(&_info,&address1);
MPI_Get_address(&_info.ne,&address2);
array_of_displaysments[0] = address2 - address1;

/*Create MPI Datatype and commit*/
MPI_Datatype stat_type;
MPI_Type_create_struct(count, array_of_blocklengths, array_of_displaysments, array_of_types, &stat_type);
MPI_Type_commit(&stat_type);

// Now we are ready to send
MPI_Send(&_info, 1, stat_type, dest, tag, comm),

/* . . . */


// Free datatype
MPI_Type_free(&stat_type);

// MPI finalization
MPI_Finalize();
}

在发送结构之前,必须执行一些操作。 我为您的示例编写了代码,但为了更好地理解,您应该阅读一些文档。 无论如何,这里有一些提示:

  • 如果您有一个只由一种元素组成的结构,比如在您的示例中,所有变量都是int,那么最好发送一个向量,注意每个变量的位置
  • 如果有其他类型,则必须将计数设置为2或更多,并更改所有其他数组(例如:数组类型的数组、数组块长度的数组等等)
  • 在这种情况下,您可以自己计算\u displaysments的数组\u的值。 例如,如果您有下面的结构,x将从0开始,而y将从8开始,因为将添加4个字节的填充以对齐元素<代码>结构点{intx;双y;}
  • 如果不想计算\u显示元素的数组,请始终使用MPI\u Get\u地址,而不要依赖于&运算符
代码如下:

struct info{
    int ne, n, u, v, process,min,strip,mincost,b;
}stat;


int main(...){

/*MPI INIT*/

struct info _info,  

int count; //Says how many kinds of data your structure has
count = 1; //1, 'cause you just have int

// Says the type of every block
MPI_Datatype array_of_types[count];
// You just have int
array_of_types[0] = MPI_INT;

// Says how many elements for block
int array_of_blocklengths[count];
// You have 8 int
array_of_blocklengths[0] = {8};

/* Says where every block starts in memory, counting from the beginning of the struct. */
MPI_Aint array_of_displaysments[coun];
MPI_Aint address1, address2;
MPI_Get_address(&_info,&address1);
MPI_Get_address(&_info.ne,&address2);
array_of_displaysments[0] = address2 - address1;

/*Create MPI Datatype and commit*/
MPI_Datatype stat_type;
MPI_Type_create_struct(count, array_of_blocklengths, array_of_displaysments, array_of_types, &stat_type);
MPI_Type_commit(&stat_type);

// Now we are ready to send
MPI_Send(&_info, 1, stat_type, dest, tag, comm),

/* . . . */


// Free datatype
MPI_Type_free(&stat_type);

// MPI finalization
MPI_Finalize();
}
试试这个

MPI_Send(&stat,sizeof(struct info),MPI_CHAR,1,2,MPI_COMM_WORLD);
MPI_Recv(&data,sizeof(struct info), MPI_CHAR, 0, DEFAULT_TAG, MPI_COMM_WORLD, &status);
stat = (struct info *) data;
试试这个

MPI_Send(&stat,sizeof(struct info),MPI_CHAR,1,2,MPI_COMM_WORLD);
MPI_Recv(&data,sizeof(struct info), MPI_CHAR, 0, DEFAULT_TAG, MPI_COMM_WORLD, &status);
stat = (struct info *) data;

你对这些行的原因有何评论?@VladimirF将结构序列化为一个字符数组并以此方式发送。你对这些行的原因有何评论?@VladimirF将结构序列化为一个字符数组并以此方式发送。