如何在 MPI_Send() 中发送结构类型的变量? [英] How to send a variable of type struct in MPI_Send()?
问题描述
我使用 MPI 用 C 编写了一个程序,其中结构变量将以环形方式发送到进程,并根据从该变量接收到的值,分配该特定进程的工作.
I have coded a program in C using MPI wherein the struct variable is to be sent in a ring fashion to the processes and, based on the value received from that variable, the work for that particular process is assigned.
问题是我需要知道如何在 MPI_Send()
函数中发送一个结构变量,因为它在运行时给出了 INVALID DATATYPE,考虑下面的例子
The problem is I need to know how to to send a struct variable in the MPI_Send()
function as it is giving INVALID DATATYPE at the runtime , Consider the following example
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);
推荐答案
在发送结构体之前,您必须进行一些操作.我为您的示例编写了代码,但为了更好地理解您应该阅读一些文档.无论如何,这里有一些提示:
You have to do some operation before send a struct. I wrote the code for your example but to understand better you should read some documentation. Anyway, here some tips:
- 如果您有一个仅由一种元素组成的结构,例如在您的示例中所有变量都是 int,那么最好发送一个向量来处理每个变量的位置.
- 如果您有其他类型,则必须将 count 设置为 2 或更多,并更改所有其他数组(例如:array_of_types、array_of_blocklengths 等).
- 在这种情况下,您可以自己计算 array_of_displaysments 的值,请注意数据结构对齐.例如,如果您有后面的结构,x 将从 0 开始,而 y 从 8 开始,因为将添加 4 个字节的填充来对齐元素.
struct point{ int x;双y;};
- 如果您不想计算 array_of_displaysments,请始终使用 MPI_Get_Address 并且不要依赖 & 运算符.
- If you have a struct made of just one kind of elements, like in your example that all the vars are int, it's better to send a vector taking care the position of each variable.
- If you had other kinds, you have to set count to 2 or more and change all the other arrays (e.g: array_of_types, array_of_blocklengths et cetera).
- You can calculate the values of array_of_displaysments on your own in that case take care of the Data structure alignment.
If for example you have the struct that follows, x will start from 0 but y from 8, because a padding of 4 bytes will be add to align the elements.
struct point{ int x; double y; };
- If you don't want to calculate the array_of_displaysments always use MPI_Get_Address and do not rely on the & operator.
代码如下:
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() 中发送结构类型的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!