Write a C console program of the matrix multiplication algorithm ([A) Bl C]) for
ID: 3740820 • Letter: W
Question
Write a C console program of the matrix multiplication algorithm ([A) Bl C]) for double precision data types. Instrument and monitor and measure execution time for the computation. a) Your C program should be single threaded, and sequential. All matrices [A], [B], and [C] are to be square, i.e. same number of rows and columns Execution should be scalable and be able to handle matrix dimension N x N, from 4 x 4, 16x16, 32x32, 64x64, 128x128, 256x256, 512x512, 1024x1024 and 2048x2048. Set the matrix dimension, N, number of accuracy improvement loops, and system clock speed using DEFINE statements. You'll need to loop many more times for small array sizes, then reduce the loop iterations as you increase array size . Hint: you should only need only 1 accuracy loops for matrices 1024x1024 and larger. Use a random number generator to fill the random data into the matrices Your console program should print out ( using formatted printf commands) 1) Vector length ( number of vector elements) 2) The number of accuracy improvement loops you run the axpy computation to improve accuracy 3) Total computation time 4) Computation time for the complete NxN matrix multiplication. 5) Computation time per arithmetic operation 6) The number of machine cycles per arithmetic operationExplanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
void main1 ()
{
int r1, c1,i,j,k,p=0;
double **A,**B,**C;
typedef struct timeval time;
time stop,start,stop1,start1;
gettimeofday(&start1,NULL);
printf("Enter number of rows and columns of matrix A : ");
scanf("%d%d",&r1,&c1);
printf("Length of vector is :%d ",r1*c1);
A=(double**) malloc(sizeof(double*)*r1);
B=(double**) malloc(sizeof(double*)*r1);
C=(double**) malloc(sizeof(double*)*r1);
for(i=0;i<r1;i++)
{
A[i]=(double*)malloc(sizeof(double)*c1);
B[i]=(double*)malloc(sizeof(double)*c1);;
C[i]=(double*)malloc(sizeof(double)*c1);;
}
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
{
A[i][j]=(rand()%1000)/100.5;
B[i][j]=(rand()%100)/10.5;
}
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
C[i][j] = 0;
gettimeofday(&start,NULL);
for (k = 0; k < r1; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
gettimeofday(&stop,NULL);
if(p==0)
{
printf(" Time takes in sigle element in Matrix Multiplication ");
if(stop.tv_sec>start.tv_sec)
printf("Seconds:%d ",stop.tv_sec-start.tv_sec);
else
printf("MicroSeconds:%d ",stop.tv_usec-start.tv_usec);
}
p=1;
}
}
gettimeofday(&stop1,NULL);
printf(" Time takes in in Matrix Multiplication ");
if(stop1.tv_sec>start1.tv_sec)
printf("Seconds:%d ",stop1.tv_sec-start1.tv_sec);
else
printf("MicroSeconds:%d ",stop1.tv_usec-start1.tv_usec);
printf("Product of matrices ");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
printf("%f ",C[i][j]);
printf( " ");
}
}
int main()
{
int i,*a,n;
typedef struct timeval time;
time stop,start;
gettimeofday(&start,NULL);
main1();
gettimeofday(&stop,NULL);
printf("Time takes in Program Complrtion ");
if(stop.tv_sec>start.tv_sec)
printf("Seconds:%d ",stop.tv_sec-start.tv_sec);
else
printf("MicroSeconds:%d ",stop.tv_usec-start.tv_usec);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.