-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat_mul_unopt.c
86 lines (70 loc) · 1.49 KB
/
mat_mul_unopt.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <unistd.h>
#include <sys/time.h>
#define M 4096
#define N 4096
#define K 4096
#define alpha 1
#define beta 1
double A[M][K];
double B[K][N];
double C[M][N];
#ifdef TIME
#define IF_TIME(foo) foo;
#else
#define IF_TIME(foo)
#endif
void init_array()
{
int i, j;
for (i=0; i<N; i++) {
for (j=0; j<N; j++) {
A[i][j] = (i + j);
B[i][j] = (double)(i*j);
C[i][j] = 0.0;
}
}
}
void print_array()
{
int i, j;
for (i=0; i<N; i++) {
for (j=0; j<N; j++) {
fprintf(stdout, "%lf ", C[i][j]);
if (j%80 == 79) fprintf(stdout, "\n");
}
fprintf(stdout, "\n");
}
}
double rtclock()
{
struct timezone Tzp;
struct timeval Tp;
int stat;
stat = gettimeofday (&Tp, &Tzp);
if (stat != 0) printf("Error return from gettimeofday: %d",stat);
return(Tp.tv_sec + Tp.tv_usec*1.0e-6);
}
double t_start, t_end;
int main()
{
int i, j, k;
register double s;
init_array();
IF_TIME(t_start = rtclock());
/* Code to be optimized - start */
for(i=0; i<M; i++)
for(j=0; j<N; j++)
for(k=0; k<K; k++)
C[i][j] = beta*C[i][j] + alpha*A[i][k] * B[k][j];
/* Code to be optimized - end */
IF_TIME(t_end = rtclock());
IF_TIME(fprintf(stderr, "%0.6lfs\n", t_end - t_start));
if (fopen(".test", "r")) {
print_array();
}
return 0;
}