资源简介
本程序利用mpi实现矩阵并行相乘。你需要安装mpich并配置好环境。编译:mpicc Matrixmulti.c -o Matrixmulti 运行:mpirun -np 5 ./Matrixmulti ;5为进程数,可以更换
代码片段和文件信息
#include
#include
#include
#include
#include
#define blockstart(Mijrows_per_blkcols_per_blkstride)\(M+(i)*(rows_per_blk))*(stride)+(j)*(cols_per_blk)
int main(int argcchar *argv[])
{int SNSPSM;
SN=10000;SP=10100;SM=10200;
double **SA**SB**SC;
int dimNdimPdimM;
int addNaddPaddM;
int dimNbdimPbdimMb;
int ijk;
double *sumC;
double **AA**BB**CC;
double *A*B*C;
double *Abuffer*Bbuffer;
FILE *fp;
fp=fopen(“4A10000-10100.txt““rb“);
if(fp==NULL)
{printf(“file can not open!\n“); exit(0);}
SA=(double**)malloc(SN*sizeof(double*));
for(i=0;i {SA[i]=(double*)malloc(SP*sizeof(double));}
for(i=0;i for(j=0;j fscanf(fp“%lf“&SA[i][j]);}}
// for(i=0;i // for(j=0;j // printf(“%lf “SA[i][j]);}
// printf(“\n“);}
fclose(fp);
SB=(double**)malloc(SP*sizeof(double*));
for(i=0;i {SB[i]=(double*)malloc(SM*sizeof(double));}
fp=fopen(“4B10100-10200.txt““rb“);
if(fp==NULL)
{printf(“file can not open!\n“); exit(0);}
for(i=0;i for(j=0;j fscanf(fp“%lf“&SB[i][j]);}}
// for(i=0;i // for(j=0;j // printf(“%lf “SB[i][j]);}
// printf(“\n“);}
fclose(fp);
int numProcsmyIDmyID_imyID_jNB;
double startend;
MPI_Status status;
MPI_Init(&argc&argv);
MPI_Comm_size(MPI_COMM_WORLD&numProcs);
MPI_Comm_rank(MPI_COMM_WORLD&myID);
NB=(int)sqrt((double)numProcs);
start=MPI_Wtime();
if(SP%NB==0)
{dimPb=SP/NB;addP=0;}
else {dimPb=SP/NB+1;addP=NB*dimPb-SP;}
if(SN%NB==0)
{dimNb=SN/NB;addN=0;}
else {dimNb=SN/NB+1;addN=NB*dimNb-SN;}
if(SM%NB==0)
{dimMb=SM/NB;addM=0;}
else {dimMb=SM/NB+1;addM=NB*dimMb-SM;}
dimN=SN+addN;dimM=SM+addM;dimP=SP+addP;
// printf(“%d %d %d\n“dimNdimPdimM);
AA=(double**)malloc(dimN*sizeof(double*));
for(i=0;i {AA[i]=(double*)malloc(dimP*sizeof(double));}
for(i=0;i for(j=0;j for(k=SP;k for(i=SN;i for(j=0;j AA[i][j]=0;}
free(SA);
/* if(myID==0){
for(i=0;i for(j=0;j
BB=(double**)malloc(dimP*sizeof(double*));
for(i=0;i {BB[i]=(double*)malloc(dimM*sizeof(double));}
for(i=0;i for(j=0;j for(k=SM;k }
free(SB);
for(i=SP;i for(j=0;j /* if(myID==0){
for(i=0;i printf(“\n“);
for(j=0;j printf(“%f “BB[i][j]);}}*/
if(myID==0){
CC=(
- 上一篇:卡匠工具包(务必断网运行
- 下一篇:stc89c52单片机倒计时报警
评论
共有 条评论