下面程序中MPI_Type_hvector改为MPI_Type_vector 就错了,我以为是这样的,因为这时是用的新类型stype构造ntype。stype为矩阵的一列数据的类型。ntype为整个转置后的矩阵类型。
MPI_Type_vector第三个参数stride代表的是number of elements between start of each block (integer) 。
而这时的数据类型是stype,不是MPI_INT,两者之间隔的元素个数自然不是1了。而MPI_Type_hvector
stride 为number of bytes between start of each block (integer) ,自然没问题了。
//转置
//默认运行进程数为2
#include "mpi.h"
#include<stdio.h>
int main(int argc,char **argv){
int rows=6,cols=5;
int tag=0;
MPI_Init(&argc,&argv);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
int A[rows][cols];
int B[cols][rows];
MPI_Datatype stype,ntype;
MPI_Type_vector(rows,1,cols,MPI_INT,&stype);
//MPI_Type_vector(cols,1,1,stype,&ntype);
MPI_Type_hvector(cols,1,sizeof(int),stype,&ntype);
MPI_Type_commit(&ntype);
MPI_Status sta;
if(rank==0){
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
A[i][j]=i*cols+j;
printf("%d ",A[i][j]);
}
printf("\n");
}
printf("\n");
MPI_Send(A,1,ntype,1,tag,MPI_COMM_WORLD);
}
else{
MPI_Recv(B,rows*cols,MPI_INT,0,tag,MPI_COMM_WORLD,&sta);
for(int i=0;i<cols;i++){
for(int j=0;j<rows;j++)
printf("%d ",B[i][j]);
printf("\n");
}
}
MPI_Type_free(&ntype);
MPI_Finalize();
}

本文详细解析使用MPI进行矩阵转置的过程,特别是针对MPI_Type_hvector与MPI_Type_vector的区别进行了说明,并通过具体代码示例展示了如何正确配置数据类型。
1106

被折叠的 条评论
为什么被折叠?



