MPI技术测试-结构体类型的创建(结构体内部嵌套)

结构体类型的创建(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);

 // stu数据类型创建
    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_Type_get_extent 该函数以字节为单位返回指定数据类型的跨度extent
    //MPI_Type_size 该函数以字节为单位返回指定数据类型的有用部分的大小,即跨度减去类型中的空隙。 
    MPI_Aint stulb,stuextent;
    MPI_Type_get_extent(stumystruct,&stulb, &stuextent);
    int stusize;
    MPI_Type_size(stumystruct, &stusize);
       
 // peo数据类型创建    
    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;
}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值