Sei sulla pagina 1di 3

http://www.cdac.in/html/events/beta-test/archives/gripsi...

/*..Program : A Sequential C program for two n*n matrix matrix multiplication with ijk loop....................... #include<stdio.h> #include<stdlib.h> #include<sys/time.h> int main(int argc,char **argv) {

/*.......Variable Initialization.....................................................*/ int MatARowSize,MatAColSize,MatBRowSize,MatBColSize; double **Matrix_A,**Matrix_B,**ResultMatrix; int i,j,k,index=1; struct timeval start_time,end_time; int start_usec,end_usec,Timetaken,operations; double mflops ; /*......Read the Size of the Matrix A................................................*/ printf("Enter the Row Size for the Matrix A \n"); scanf("%d",&MatARowSize); printf("Enter the Column Size for the Matrix A \n"); scanf("%d",&MatAColSize);

if ( MatARowSize < 1 || MatAColSize < 1 ) { printf("\n The no. of rows or columns should be positive (Error : Matrix A) \n"); exit(-1); }

/*......Read the Size of the Matrix B................................................*/ printf("Enter the Row Size for the Matrix B \n"); scanf("%d",&MatBRowSize); printf("Enter the Column Size for the Matrix B \n"); scanf("%d",&MatBColSize); if ( MatBRowSize < 1 || MatBColSize < 1 ) { printf("\n The no. of rows or columns should be positive (Error : Matrix B) \n"); exit(-1); }

/*......Check if multiplication can be performed, else give error message and quit...*/ if ( MatAColSize != MatBRowSize ) { printf("\n No. of Column of Matrix A should be equals to the no. of Row of Matrix B to perform Mul exit(-1); } /*.....Allocate the memory for Matrix A & Assign the values in Matrix A..............*/ Matrix_A=(double **)malloc(MatARowSize * sizeof(double *)); for(i=1 ; i <= MatARowSize ; i++) Matrix_A[i]=(double *) malloc(MatAColSize * sizeof(double)); for( i=1 ; i <= MatARowSize ; i++) { for( j=1 ; j <= MatAColSize ; j++) { Matrix_A[i][j]=(double)index; } }

1 of 3

Friday 21 June 2013 01:25 AM

http://www.cdac.in/html/events/beta-test/archives/gripsi...

/*.....Allocate the memory for Matrix B & Assign the values in Matrix B..............*/ Matrix_B=(double **)malloc(MatBRowSize * sizeof(double *)); for(i=1 ; i <= MatBRowSize ; i++) Matrix_B[i]=(double *) malloc(MatBColSize * sizeof(double)); index=1; for( i=1 ; i <= MatBRowSize ; i++) { for( j=1 ; j <= MatBColSize ; j++) { Matrix_B[i][j]=(double)index; } } /*....Allocate the memory for resultant matrix & do the multiplication...............*/ ResultMatrix=(double **)malloc(MatARowSize * sizeof(double *)); for(i=1 ; i <= MatARowSize ; i++) ResultMatrix[i]=(double *) malloc(MatBColSize * sizeof(double)); gettimeofday(&start_time,NULL); for ( i = 1 ; i <= MatARowSize ; i++ ) { for( k = 1 ; k <= MatBColSize ; k++ ) { ResultMatrix[i][j]=0.0; for( j = 1 ; j <= MatBRowSize ; j++) { ResultMatrix[i][j]=ResultMatrix[i][j]+(Matrix_A[i][k]*Matrix_B[k][j]); } } } gettimeofday(&end_time,NULL); /*.....Print the Resultant matrix.............................................*/ for ( i =1 ; i <= MatARowSize ; i++) { for ( j =1 ;j <= MatBColSize ; j++) { printf("%f\t",ResultMatrix[i][j]); } printf("\n"); } /*....Calculate the time taken in Multiplication...............................*/ start_usec end_usec Timetaken = abs((start_time.tv_sec)*1000000 + start_time.tv_usec); = abs((end_time.tv_sec)*1000000 + end_time.tv_usec); = abs(end_usec - start_usec);

/*....Find the no. of operation & Performance in Mflops........................*/ operations = 2*MatARowSize*MatBColSize*MatAColSize; mflops = (double)(operations)/(double)(Timetaken/100000);

printf("\n\n Time at which the multiplication is started in usec is printf("\n Time at which multiplication is completed in usec is printf("\n Time taken for Multiplicationin in usec is printf("\n Mflop/s

: : : :

%ld %ld %ld %lf

micro sec",start_usec); micro sec ",end_usec); micro sec ",Timetaken); \n\n",mflops);

/*....Free the memory.........................................................*/ free(Matrix_A);

2 of 3

Friday 21 June 2013 01:25 AM

http://www.cdac.in/html/events/beta-test/archives/gripsi...
free(Matrix_B); free(ResultMatrix); }

3 of 3

Friday 21 June 2013 01:25 AM

Potrebbero piacerti anche