MPI_CHAR: 字符型
函数原型
int MPI_Type_contiguous(int count, MPI_Datatype oldtype,
MPI_Datatype *newtype)
参数详解
函数原型
int MPI_Type_vector(int count,
int blocklength, int stride, MPI_Datatype oldtype,
MPI_Datatype *newtype)
参数详解
函数原型
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)
参数详解
代码实例
#include
#include
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;
}
函数原型
int MPI_Type_indexed(int count, const int* array_of_blocklengths,
const int* array_of_displacements,
MPI_Datatype oldtype, MPI_Datatype* newtype)
参数详解
代码实例
#include
#include
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;
}
函数原型
int MPI_Type_hvector(int count, int blocklength,
MPI_Aint stride, MPI_Datatype oldtype, MPI_Datatype *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);