假设我有一个结构类型如下:
typedef struct {
float x, y, z;
float velocity;
int n, type;
} Particle;
Run Code Online (Sandbox Code Playgroud)
我想发送它.我必须创建一个MPI_Type.我知道有4种方法可以做到.我在下面列出了它们.我想知道它们的区别,限制和好处是什么.
运用 MPI_Type_extent
使用offsetof()in stddef.h,在这个答案中解释:MPI派生类型发送答案
使用MPI_Get_address,也是同一答案中的一个例子.
使用reinterpret_cast<const unsigned char*>,我没有尝试,但这里有一个例子:MPI创建自定义数据
我尝试将派生类型发送到处理器。该类型包含来自其他派生类型的对象。我从示例:结构派生数据类型开始该示例。我添加我的代码。代码有点长,但是两种类型基本相同。我有 Part一个对象,它也有一个Particle对象,我想发送Part。我得到的结果是在代码之后。
#include "mpi.h"
#include <stdio.h>
#define NELEM 25
main(int argc, char *argv[]) {
int numtasks, rank, source=0, dest, tag=1, i;
typedef struct {
float x, y, z;
float velocity;
int n, type;
} Particle;
// Another struct to send
typedef struct {
char character;
Particle part ;
} Part ;
MPI_Request send_req;
MPI_Status stat;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
// Particle type
Particle particles;
MPI_Datatype particletype, oldtypes[2];
int blockcounts[2];
MPI_Aint offsets[2], extent; …Run Code Online (Sandbox Code Playgroud)