-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat_mul_tiled.c
93 lines (77 loc) · 1.78 KB
/
mat_mul_tiled.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
87
88
89
90
91
92
93
#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
#define TILE 512
#define MIN(a,b) (((a)<(b))?(a):(b))
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;
#pragma omp parallel for private(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, ii, jj, kk;
register double s;
init_array();
IF_TIME(t_start = rtclock());
/* Code to be optimized - start */
#pragma omp parallel for private(i,j,k,ii,jj,kk)
for(i=0; i<M; i+=TILE)
for(k=0; k<K; k+=TILE)
for(j=0; j<N; j+=TILE)
for(ii=i; ii<MIN(M,i+TILE); ii++)
for(kk=k; kk<MIN(K,k+TILE); kk++)
for(jj=j; jj<MIN(N,j+TILE); jj++)
C[ii][jj] = beta*C[ii][jj] + alpha*A[ii][kk] * B[kk][jj];
/* 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;
}