Contiguous Derived Data

来源:互联网 发布:sql server offset 编辑:程序博客网 时间:2024/05/01 23:04

1. 连续的构造数据类型

(1)把同一内置数据类型/连续的多个元素组合成一个构造数据类型

2. 举例

#include<stdio.h>#include"mpi.h"#define SIZE 4int main(int argc, char *argv[]){        int totalNumTasks, rankID;        float sendBuf[SIZE][SIZE] = {                {1.0,   2.0,    3.0,    4.0},                {5.0,   6.0,    7.0,    8.0},                {9.0,   10.0,   11.0,   12.0},                {13.0,  14.0,   15.0,   16.0}        };        MPI_Init(&argc, &argv);        MPI_Comm_rank(MPI_COMM_WORLD, &rankID);        MPI_Comm_size(MPI_COMM_WORLD, &totalNumTasks);        if(totalNumTasks == SIZE){                MPI_Datatype rowType;                MPI_Type_contiguous(SIZE, MPI_FLOAT, &rowType);//each rowType has NUM = 4 data                MPI_Type_commit(&rowType);                int source = 0;                int sendCount = 1;                int recvCount = 1;                float recvBuf[SIZE];                MPI_Scatter(sendBuf, sendCount, rowType,                            recvBuf, recvCount, rowType, source, MPI_COMM_WORLD);                printf("my rankID = %d, receive Results: %f %f %f %f, total = %f\n",                        rankID, recvBuf[0], recvBuf[1], recvBuf[2], recvBuf[3],                        recvBuf[0] + recvBuf[1] + recvBuf[2] + recvBuf[3]);        }else if(totalNumTasks == 8){                MPI_Datatype rowType;                int NUM = 2;                MPI_Type_contiguous(NUM, MPI_FLOAT, &rowType);//each rowType has NUM = 2 data                MPI_Type_commit(&rowType);                int source = 0;                int sendCount = 1;                int recvCount = 1;                float recvBuf[2];                MPI_Scatter(sendBuf, sendCount, rowType,                            recvBuf, recvCount, rowType, source, MPI_COMM_WORLD);                printf("my rankID = %d, receive result: %f %f, total = %f\n",                        rankID, recvBuf[0], recvBuf[1], recvBuf[0] + recvBuf[1]);        }else                printf("error, please specify -n %d or -n %d\n", SIZE, 2*SIZE);        MPI_Finalize();        return 0;}


3. 编译执行

[amao@amao991 mpi-study]$ mpicc contiguousDerivedData.c [amao@amao991 mpi-study]$ mpiexec -n 4 -f machinefile ./a.out my rankID = 0, receive Results: 1.000000 2.000000 3.000000 4.000000, total = 10.000000my rankID = 1, receive Results: 5.000000 6.000000 7.000000 8.000000, total = 26.000000my rankID = 3, receive Results: 13.000000 14.000000 15.000000 16.000000, total = 58.000000my rankID = 2, receive Results: 9.000000 10.000000 11.000000 12.000000, total = 42.000000[amao@amao991 mpi-study]$ mpiexec -n 8 -f machinefile ./a.out my rankID = 6, receive result: 13.000000 14.000000, total = 27.000000my rankID = 7, receive result: 15.000000 16.000000, total = 31.000000my rankID = 4, receive result: 9.000000 10.000000, total = 19.000000my rankID = 0, receive result: 1.000000 2.000000, total = 3.000000my rankID = 1, receive result: 3.000000 4.000000, total = 7.000000my rankID = 5, receive result: 11.000000 12.000000, total = 23.000000my rankID = 3, receive result: 7.000000 8.000000, total = 15.000000my rankID = 2, receive result: 5.000000 6.000000, total = 11.000000

4. 总结

(1) 组合数据类型主要是通过以下语句实现的

MPI_Datatype rowType;

MPI_Type_contiguous(SIZE, MPI_FLOAT, &rowType);//each rowType has NUM = 4 data

MPI_Type_commit(&rowType);