Hello everyone!
I am trying to multiply arrays using MPI to speed up the process.My problem is that when i execute the program doesnt work and it freezes.
Screenshot:
http://s29.postimg.org/ocaxtka2f/image.png[
^]
Please help me.
Thanks in advance.
Thats my code:
#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)
{
FILE *fp;
colnumA=rownumA=0;
fp=fopen("g.txt","r");
if (fp==NULL) exit(1);
while ((c = getc(fp)) != EOF)
{
if (c==' ')
colnumA++;
if (c == '\n')
rownumA++;
}
colnumA=(colnumA/rownumA)+1;
fclose(fp);
A =(float **) malloc(rownumA*sizeof(float*));
for(i=0; i<rownumA; i++){
A[i]= (float *)malloc(colnumA*sizeof(float));
}
fp=fopen("g.txt","r");
for (i=0;i<rownumA; i++){
for(j=0; j<colnumA; j++){
fscanf(fp,"%f",&A[i][j]);
}
}
fclose(fp);
FILE *fp1;
colnumB=rownumB=0;
fp1=fopen("h.txt","r");
if (fp1==NULL) exit(1);
while ((c1 = getc(fp1)) != EOF)
{
if (c1==' ')
colnumB++;
if (c1 == '\n')
rownumB++;
}
colnumB=(colnumB/rownumB)+1;
fclose(fp1);
B =(float **) malloc(rownumB*sizeof(float *));
for(i=0; i<rownumB; i++){
B[i]= (float *)malloc(colnumB*sizeof(float));
}
fp1=fopen("h.txt","r");
for (i=0;i<rownumB; i++){
for(j=0; j<colnumB; j++){
fscanf(fp1,"%f",&B[i][j]);
}
}
fclose(fp1);
if (colnumA != rownumB ){
printf("\n Error cant mult!\n");
exit(1);
}
C = (float **)malloc(rownumA*sizeof(float *));
for(i=0; i<colnumB; i++){
C[i]= (float *)malloc(colnumB*sizeof(float));
}
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);
smul=smul+temp;
}
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);
}
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);
}
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);
for(k=0; k<colnumB ; k++)
for(i=0; i<temp; i++)
for(j=0; j<colnumA; j++)
{
C[i][k]+=A[i][j]*B[j][k];
}
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();
}