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

. mflops = (double)(operations)/(double)(Timetaken/100000)...cdac.... 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".....0... i++) Matrix_B[i]=(double *) malloc(MatBColSize * sizeof(double)).Timetaken)..Print the Resultant matrix..... gettimeofday(&start_time....... /*.... micro sec ".http://www............ } } /*...Calculate the time taken in Multiplication... k <= MatBColSize .. i <= MatARowSize ....... i <= MatBRowSize .......NULL)..Find the no.. } } } gettimeofday(&end_time... i++) ResultMatrix[i]=(double *) malloc(MatBColSize * sizeof(double)). j++) { printf("%f\t".... i <= MatBRowSize .... i <= MatARowSize .tv_usec)..........*/ start_usec end_usec Timetaken = abs((start_time.... } /*.......... micro sec ". \n\n". for(i=1 .Allocate the memory for resultant matrix & do the multiplication. = abs(end_usec ... index=1.. 2 of 3 Friday 21 June 2013 01:25 AM .........*/ ResultMatrix=(double **)malloc(MatARowSize * sizeof(double *))...j <= MatBColSize ...ResultMatrix[i][j])..*/ free(Matrix_A)............ i++) { for( j=1 .start_usec)......Allocate the memory for Matrix B & Assign the values in Matrix B... /*. for( j = 1 ........*/ for ( i =1 .. j++) { ResultMatrix[i][j]=ResultMatrix[i][j]+(Matrix_A[i][k]*Matrix_B[k][j])..in/html/events/beta-test/archives/gripsi. j <= MatBColSize ...... /*.. i++) { for ( j =1 .end_usec).............*/ operations = 2*MatARowSize*MatBColSize*MatAColSize...Free the memory..tv_sec)*1000000 + end_time.. j <= MatBRowSize .. = abs((end_time.start_usec)...... i++ ) { for( k = 1 .tv_sec)*1000000 + start_time.........mflops)... for( i=1 .... /*.. k++ ) { ResultMatrix[i][j]=0............ for(i=1 ..NULL). of operation & Performance in Mflops.. j++) { Matrix_B[i][j]=(double)index.. for ( i = 1 .......*/ Matrix_B=(double **)malloc(MatBRowSize * sizeof(double *))..... i <= MatARowSize ... } printf("\n").tv_usec).......

http://www. free(ResultMatrix).. } 3 of 3 Friday 21 June 2013 01:25 AM .in/html/events/beta-test/archives/gripsi..cdac. free(Matrix_B).