-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM-M_naive.c
61 lines (51 loc) · 1.49 KB
/
M-M_naive.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @author Rohin Arora
* @email rohinarora07@gmail.com
* @create date 2020-02-25
* @modify date 2020-02-25
*
* Naive matrix multiplication
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define M 512
double CLOCK()
{
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (t.tv_sec * 1000) + (t.tv_nsec * 1e-6);
}
int main(int argc, char **argv)
{
int i, j, k, jj, kk, en;
double start, finish, total, sum;
float a[M][M], b[M][M], c[M][M];
/* Set the seed for the random number generators for the data */
srand(145);
/* Initialize a, b and c */
for (i = 0; i < M; i++)
for (j = 0; j < M; j++)
a[i][j] = (float)rand() / (float)RAND_MAX;
for (i = 0; i < M; i++)
for (j = 0; j < M; j++)
b[i][j] = (float)rand() / (float)RAND_MAX;
for (i = 0; i < M; i++)
for (j = 0; j < M; j++)
c[i][j] = 0.;
/* Start timing */
start = CLOCK();
/* This is the only portion of the code you should modify to improve performance. */
for (i = 0; i < M; i++)
for (j = 0; j < M; j++)
for (k = 0; k < M; k++)
c[i][j] += a[i][k] * b[k][j];
finish = CLOCK();
/* End timing */
total = finish - start;
printf("Time for the loop = %4.2f milliseconds\n", total);
printf("Element %d,%d = %f\n", 0, 0, c[0][0]);
printf("Element %d,%d = %f\n", 63, 63, c[63][63]);
printf("Element %d,%d = %f\n", 511, 511, c[511][511]);
return 0;
}