结构体类型的创建(merge)
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#define DOUBLE_NUMS 2
struct People{
struct Student{
int a;
double b[DOUBLE_NUMS];
}stu;
int age;
}people;
void initArray(struct People *array, int size,int bSize)
{
for (int i = 0; i < size; i++)
{
array[i].stu.a = (int)(rand() % 10 + 1);
for (int j = 0; j < bSize; j++) array[i].stu.b[j] = (double)(rand() % 10 + 1);
array[i].age= (int)(rand() % 10 + 1);
}
}
void printArray(const char *name,struct People *array, int ranknode, int size,int bSize)
{
printf("%s rank%d\n", name, ranknode);
for (int i = 0; i < size; i++)
{
printf("i=%d age=%d a=%d", i,array[i].age,array[i].stu.a);
for (int j = 0; j < bSize; j++) printf(" b[%d]=%lf",j, array[i].stu.b[j]);
printf("\n");
}
}
int main(int argc, char *argv[])
{
int structSize=2;
int rank;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Datatype stumystruct;
int stublocklens[2];
MPI_Aint stuindices[2];
MPI_Datatype stuold_types[2];
stublocklens[0] = 1;
stublocklens[1] = 2;
stuold_types[0] = MPI_INT;
stuold_types[1] = MPI_DOUBLE;
MPI_Get_address(&people.stu.a, &stuindices[0]);
MPI_Get_address(&people.stu.b, &stuindices[1]);
stuindices[1] = stuindices[1]-stuindices[0];
stuindices[0] = 0;
MPI_Type_create_struct(2, stublocklens, stuindices, stuold_types, &stumystruct);
MPI_Type_commit(&stumystruct);
MPI_Aint stulb,stuextent;
MPI_Type_get_extent(stumystruct,&stulb, &stuextent);
int stusize;
MPI_Type_size(stumystruct, &stusize);
MPI_Datatype peomystruct;
int peoblocklens[2];
MPI_Aint peoindices[2];
MPI_Datatype peoold_types[2];
struct People peo;
peoblocklens[0] = 1;
peoblocklens[1] = 1;
peoold_types[0] = stumystruct;
peoold_types[1] = MPI_INT;
MPI_Get_address(&people.stu, &peoindices[0]);
MPI_Get_address(&people.age, &peoindices[1]);
peoindices[1] =peoindices[1]-peoindices[0];
peoindices[0] = 0;
MPI_Type_create_struct(2, peoblocklens, peoindices, peoold_types, &peomystruct);
MPI_Type_commit(&peomystruct);
MPI_Aint peolb,peoextent;
MPI_Type_get_extent(peomystruct,&peolb, &peoextent);
int peosize;
MPI_Type_size(peomystruct, &peosize);
struct People *pt = (struct People *)malloc(structSize * sizeof(people));
if (0==rank) {
printf("double stride: %d\n",(int)stuindices[1]);
printf("MPI_Type_get_extent length:%d\n",(int)stuextent);
printf("MPI_Type_get_extent min: %d\n",(int)stulb);
printf("size of stustruct: %d\n",(int)sizeof(people.stu));
printf("MPI_Type_size: %d\n\n", stusize);
printf("double stride: %d\n",(int)peoindices[1]);
printf("MPI_Type_get_extent length:%d\n",(int)peoextent);
printf("MPI_Type_get_extent min: %d\n",(int)peolb);
printf("size of stustruct: %d\n",(int)sizeof(people));
printf("MPI_Type_size: %d\n", peosize);
initArray(pt, structSize,DOUBLE_NUMS);
printf("\nrank0 打印 ");
printArray("struct", pt,rank, structSize,DOUBLE_NUMS);
}
MPI_Bcast(pt, structSize, peomystruct, 0, MPI_COMM_WORLD);
printArray("struct", pt,rank, structSize,DOUBLE_NUMS);
MPI_Type_free(&stumystruct);
MPI_Type_free(&peomystruct);
free(pt);
MPI_Finalize();
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c1e0ab7ad7e6aca33b167122faa30812.png)