Write a C console program of the matrix multiplication algorithm ( [A}*[B] = [C]
ID: 3741087 • Letter: W
Question
Write a C console program of the matrix multiplication algorithm ( [A}*[B] = [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. Recompile & execute for each vector length (and # of accuracy loops) . Compiler optimizations should be on for full optimization ( -O3 in gcc or /O2 in MS VS). The console window should execute and remain open until manually closed.
Comment your code to explain what it is doing. Make your code portable. Using malloc is the preferred way of doing this.
b) Your console program should print out ( using formatted printf commands )
1) Your name
2) Vector length ( number of vector elements)
3) The number of accuracy improvement loops you run the axpy computation to improve accuracy.
4) Total computation time
5) Computation time for the complete NxN matrix multiplication.
6) Computation time per arithmetic operation
7) The number of machine cycles per arithmetic operation
Explanation / Answer
i compiled the program and checked it
try using this
#include <stdio.h>
int main()
{
double m, n, p, q, c, d, k, sum = 0;
double first[10][10], second[10][10], multiply[10][10];
printf("Enter number of rows and columns of first matrix ");
scanf("%lf%lf", &m, &n);
printf("Enter elements of first matrix ");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%lf", &first[c][d]);
printf("Enter number of rows and columns of second matrix ");
scanf("%lf%lf", &p, &q);
if (n != p)
printf("The matrices can't be multiplied with each other. ");
else
{
printf("Enter elements of second matrix ");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%lf", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices: ");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%lf ", multiply[c][d]);
printf(" ");
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.