内置类型
MPI_CHAR: 字符型
- MPI_UNSIGNED_CHAR: 无符号字符型
- MPI_BYTE: 字节型
- MPI_SHORT: 短整型
- MPI_UNSIGNED_SHORT: 无符号短整型
- MPI_INT: 整型
- MPI_UNSIGNED: 无符号整型
- MPI_LONG: 长整型
- MPI_UNSIGNED_LONG: 无符号长整型
- MPI_FLOAT: 单精度浮点型
- MPI_DOUBLE: 双精度浮点型
- MPI_LONG_DOUBLE: 长双精度浮点型
自定义类型
MPI_Type_contiguous: 创建一个由相同大小的元素组成的类型
函数原型
int MPI_Type_contiguous(int count, MPI_Datatype oldtype,
MPI_Datatype *newtype)
参数详解
- count:新类型中元素的数量。
- oldtype:待复制元素的类型。
- newtype:返回一个新类型。
MPI_Type_vector: 创建一个由相同大小、位于相隔固定间距的元素组成的类型
函数原型
int MPI_Type_vector(int count,
int blocklength, int stride, MPI_Datatype oldtype,
MPI_Datatype *newtype)
参数详解
- count:向量中连续元素的数量。
- blocklength:向量中相邻元素之间的间距。
- stride:元素之间的间距(读取到该元素后,要跳过多少个元素才能读取下一个元素)。
- oldtype:待复制元素的类型。
- newtype:返回一个新类型。
MPI_Type_create_struct: 创建一个由不同类型的元素组成的类型
函数原型
int MPI_Type_create_struct(int count, const int* array_of_blocklengths,
const MPI_Aint* array_of_displacements,
const MPI_Datatype* array_of_types, MPI_Datatype* newtype)
参数详解
- count:新类型中元素的数量。
- array_of_blocklengths:指定每个元素的长度。
- array_of_displacements:指定每个元素的偏移量。需要注意的是,对于数组类型,偏移量必须是 MPI_Aint 类型。
- array_of_types:指定每个元素的类型。
- newtype:返回一个新类型。
代码实例
#include <stdio.h>
#include <mpi.h>typedef struct {int x, y;
} Vector2D;int main(int argc, char** argv) {int size, rank;MPI_Init(&argc, &argv);MPI_Comm_size(MPI_COMM_WORLD, &size);MPI_Comm_rank(MPI_COMM_WORLD, &rank);MPI_Datatype Vector2D_type;MPI_Type_vector(1, 2, 3, MPI_INT, &Vector2D_type);const MPI_Aint displacements[] = {0, offsetof(Vector2D, y)};const int blocklengths[] = {1, 1};MPI_Datatype types[] = {MPI_INT, MPI_INT};MPI_Type_create_struct(2, blocklengths, displacements, types, &Vector2D_type);MPI_Type_commit(&Vector2D_type);if (rank == 0) {Vector2D v = {1, 2};MPI_Send(&v, 1, Vector2D_type, 1, 0, MPI_COMM_WORLD);printf("Process 0 sent vector [%d, %d] to process 1\n", v.x, v.y);} else if (rank == 1) {Vector2D v_recv;MPI_Recv(&v_recv, 1, Vector2D_type, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);printf("Process 1 received vector [%d, %d] from process 0\n", v_recv.x, v_recv.y);}MPI_Type_free(&Vector2D_type);MPI_Finalize();return 0;
}
MPI_Type_indexed函数创建一个由相同大小的元素组成的类型,但这些元素并不连续,而是位于一个数组的不同位置
函数原型
int MPI_Type_indexed(int count, const int* array_of_blocklengths,
const int* array_of_displacements,MPI_Datatype oldtype, MPI_Datatype* newtype)
参数详解
- count:新类型中元素的数量。
- array_of_blocklengths:一个整数数组,指定每个块中连续元素的数量。
- array_of_displacements:一个整数数组,指定每个块的起始位置。
- oldtype:待复制元素的类型。
- newtype:返回一个新类型。
代码实例
#include <stdio.h>
#include <mpi.h>int main(int argc, char** argv) {int size, rank;MPI_Init(&argc, &argv);MPI_Comm_size(MPI_COMM_WORLD, &size);MPI_Comm_rank(MPI_COMM_WORLD, &rank);int block_lengths[3] = {2, 3, 2};int displacements[3] = {0, 4, 12};int data[7] = {1, 2, 3, 4, 5, 6, 7};MPI_Datatype Complex_type;MPI_Type_indexed(3, block_lengths, displacements, MPI_INT, &Complex_type);MPI_Type_commit(&Complex_type);if (rank == 0) {printf("Sending complex data...\n");MPI_Send(data, 1, Complex_type, 1, 0, MPI_COMM_WORLD);} else if (rank == 1) {int recv_data[7];MPI_Recv(recv_data, 7, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);printf("Received complex data: [");for (int i = 0; i < 7; i++) {printf("%d ", recv_data[i]);}printf("]\n");}MPI_Type_free(&Complex_type);MPI_Finalize();return 0;
}
MPI_Type_hvector 创建一个向量数据类型,但与 MPI_Type_vector 不同的是,所有的数据元素不需要具有相同的大小和类型。具体来说,MPI_Type_hvector 允许用户按照任意的字节长距离来描述向量的结构
函数原型
int MPI_Type_hvector(int count, int blocklength,
MPI_Aint stride, MPI_Datatype oldtype, MPI_Datatype *newtype)
参数详解
- count:向量中元素的数量。
- blocklength:向量中每个元素的个数。
- stride:相邻元素之间的偏移(以字节为单位)。
- oldtype:要重复的原始数据类型。
- newtype:输出的新数据类型。
代码实例
假设有一个数组 a,它的每个元素的大小是 4 字节,我们想要创建一个新的 MPI 类型,每 2 个元素组合在一起,组成一个长度为 8 字节的结构体。在这种情况下,我们可以使用 MPI_Type_hvector 来创建新的数据类型:
MPI_Datatype struct_type, temp_type;
MPI_Type_contiguous(2, MPI_INT, &temp_type);
MPI_Type_create_resized(temp_type, 0, 8, &struct_type);
MPI_Type_commit(&struct_type);
MPI_Type_free(&temp_type);MPI_Datatype vector_type;
MPI_Type_hvector(4, 1, 8, struct_type, &vector_type);
MPI_Type_commit(&vector_type);