我正在尝试使用MPI进行矩阵乘法。我的问题是,当我运行我的代码时,它会打印消息ARRAY C (C=AxB)
并停止工作。任何人都可以帮助我吗?
这是我的代码。
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
float **A,**B,**C;
int rownumA,colnumA,c,i,j,k,rank,size,nrank,srowA,srowB,smul,node,node1,temp,rrowA,sender;
int rownumB,colnumB,c1,rc;
MPI_Status stat;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if (size < 2 ) {
printf("Error\n");
MPI_Abort(MPI_COMM_WORLD, rc);
exit(1);
}
nrank=rank-1;
if (rank == 0)
{
//READ AND FILL FIRST ARRAY
FILE *fp;
colnumA=rownumA=0;
fp=fopen("g.txt","r");
if (fp==NULL) exit(1);
//count rows and lines of 2d array
while ((c = getc(fp)) != EOF)
{
if (c==' ')
colnumA++;
if (c == '\n')
rownumA++;
}
colnumA=(colnumA/rownumA)+1;
fclose(fp);
// end count
//create dynamic array
A =(float **) malloc(rownumA*sizeof(float*));
for(i=0; i<rownumA; i++){
A[i]= (float *)malloc(colnumA*sizeof(float));
}
//Fill 2d array from file
fp=fopen("g.txt","r");
for (i=0;i<rownumA; i++){
for(j=0; j<colnumA; j++){
fscanf(fp,"%f",&A[i][j]);
}
}
//end add
fclose(fp);
//READ AND FILL ARRAY B
FILE *fp1;
colnumB=rownumB=0;
fp1=fopen("h.txt","r");
if (fp1==NULL) exit(1);
//count rows and lines of 2d array
while ((c1 = getc(fp1)) != EOF)
{
if (c1==' ')
colnumB++;
if (c1 == '\n')
rownumB++;
}
colnumB=(colnumB/rownumB)+1;
fclose(fp1);
// end count
//create dynamic array
B =(float **) malloc(rownumB*sizeof(float *));
for(i=0; i<rownumB; i++){
B[i]= (float *)malloc(colnumB*sizeof(float));
}
//Fill 2d array from file
fp1=fopen("h.txt","r");
for (i=0;i<rownumB; i++){
for(j=0; j<colnumB; j++){
fscanf(fp1,"%f",&B[i][j]);
}
}
//end add
fclose(fp1);
if (colnumA != rownumB ){
printf("\n Error cant mult!\n");
exit(1);
}
//create dynamic array
C = (float **)malloc(rownumA*sizeof(float *));
for(i=0; i<colnumB; i++){
C[i]= (float *)malloc(colnumB*sizeof(float));
}
//Init C with 0
for (i=0;i<rownumA; i++){
for(j=0; j<colnumB; j++){
C[i][j]=0;
}
}
srowA=rownumA/nrank;
rrowA=rownumA%nrank;
smul=0;
for (node=1; node<=nrank; node++)
{
if (node<=rrowA)
temp=srowA+1;
else
temp=srowA;
MPI_Send(&smul,1,MPI_INT,node,1,MPI_COMM_WORLD);
MPI_Send(&temp,1,MPI_INT,node,1,MPI_COMM_WORLD);
MPI_Send(&colnumA,1,MPI_INT,node,1,MPI_COMM_WORLD);
MPI_Send(&colnumB,1,MPI_INT,node,1,MPI_COMM_WORLD);
MPI_Send(&A[smul][0],temp*colnumA,MPI_DOUBLE,node,1,MPI_COMM_WORLD);
MPI_Send(&B,colnumA*colnumB,MPI_DOUBLE,node,1,MPI_COMM_WORLD);
MPI_Send(&rownumA,1,MPI_INT,node,1,MPI_COMM_WORLD);
smul=smul+temp;
}
//receive results
for (node1=1; node1<nrank; node1++)
{
sender=node1;
MPI_Recv(&smul,1,MPI_INT,sender,2,MPI_COMM_WORLD,&stat);
MPI_Recv(&temp,1,MPI_INT,sender,2,MPI_COMM_WORLD,&stat);
MPI_Recv(&C[smul][0],temp*colnumB,MPI_DOUBLE,sender,2,MPI_COMM_WORLD,&stat);
}
//print results
//Print at file C array
FILE *fpr;
fpr=fopen("results.txt","w");
if (fpr == NULL) exit(1);
printf("\n\n /// ARRAY C (C=AxB) /// \n \n \n");
for (i=0;i<rownumA; i++){
for(j=0; j<colnumB; j++){
fprintf(fpr,"%0.3f ",C[i][j]);
}
fprintf(fpr,"\n");
}
fclose(fpr);
} //end master
if (rank>0)
{
MPI_Recv(&smul,1,MPI_INT,0,1,MPI_COMM_WORLD,&stat);
MPI_Recv(&temp,1,MPI_INT,0,1,MPI_COMM_WORLD,&stat);
MPI_Recv(&colnumA,1,MPI_INT,0,1,MPI_COMM_WORLD,&stat);
MPI_Recv(&colnumB,1,MPI_INT,0,1,MPI_COMM_WORLD,&stat);
MPI_Recv(&A,temp*colnumA,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&stat);
MPI_Recv(&B,colnumA*colnumB,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&stat);
MPI_Recv(&rownumA,1,MPI_INT,0,1,MPI_COMM_WORLD,&stat);
for(k=0; j<colnumB ; k++)
for(i=0; i<temp; i++)
for(j=0; j<colnumA; j++)
{
C[i][k]+=A[i][j]*B[j][k];
}
//send results
MPI_Send(&smul, 1,MPI_INT,0,2,MPI_COMM_WORLD);
MPI_Send(&temp,1,MPI_INT,0,2,MPI_COMM_WORLD);
MPI_Send(&C,temp*colnumB,MPI_DOUBLE,0,2,MPI_COMM_WORLD);
}
MPI_Finalize();
}
答案 0 :(得分:0)
下面
//create dynamic array
C = (float **)malloc(rownumA*sizeof(float *));
for(i=0; i<colnumB; i++){
C[i]= (float *)malloc(colnumB*sizeof(float));
}
for
循环的上限应为rownumA
。