-
Notifications
You must be signed in to change notification settings - Fork 0
/
trans.c
306 lines (293 loc) · 8.99 KB
/
trans.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* trans.c - Matrix transpose B = A^T
*
* Each transpose function must have a prototype of the form:
* void trans(int M, int N, int A[N][M], int B[M][N]);
*
* A transpose function is evaluated by counting the number of misses
* on a 1KB direct mapped cache with a block size of 32 bytes.
*/
#include <stdio.h>
#include "cachelab.h"
int is_transpose(int M, int N, int A[N][M], int B[M][N]);
/*
* transpose_submit - This is the solution transpose function that you
* will be graded on for Part B of the assignment. Do not change
* the description string "Transpose submission", as the driver
* searches for that string to identify the transpose function to
* be graded.
*/
char transpose_submit_desc[] = "Transpose submission";
void transpose_submit(int M, int N, int A[N][M], int B[M][N])
{
int i,j,ii,jj,tmp0,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7;
if(M==32)
{// equivolent to 8*8 blocking
for(ii=0;ii<32;ii+=8)
{
for(jj=0;jj<32;jj+=8)
{
for(i=ii;i<ii+8;i++)
{
tmp0=A[i][jj];
tmp1=A[i][jj+1];
tmp2=A[i][jj+2];
tmp3=A[i][jj+3];
tmp4=A[i][jj+4];
tmp5=A[i][jj+5];
tmp6=A[i][jj+6];
tmp7=A[i][jj+7];
B[jj][i]=tmp0;
B[jj+1][i]=tmp1;
B[jj+2][i]=tmp2;
B[jj+3][i]=tmp3;
B[jj+4][i]=tmp4;
B[jj+5][i]=tmp5;
B[jj+6][i]=tmp6;
B[jj+7][i]=tmp7;
}
}
}
}
else if(M==64)
{ // equivolent to 8*8 blocking and 4*4 sub-blocking
for(ii=0;ii<64;ii+=8)
{
for(jj=0;jj<64;jj+=8)
{
for(i=ii;i<ii+4;i++)
{
tmp0=A[i][jj];
tmp1=A[i][jj+1];
tmp2=A[i][jj+2];
tmp3=A[i][jj+3];
tmp4=A[i][jj+4];
tmp5=A[i][jj+5];
tmp6=A[i][jj+6];
tmp7=A[i][jj+7];
B[jj][i]=tmp0;
B[jj+1][i]=tmp1;
B[jj+2][i]=tmp2;
B[jj+3][i]=tmp3;
B[jj][i+4]=tmp4;
B[jj+1][i+4]=tmp5;
B[jj+2][i+4]=tmp6;
B[jj+3][i+4]=tmp7;
}
for(j=jj;j<jj+4;j++)
{
tmp0=B[j][ii+4];
tmp1=B[j][ii+5];
tmp2=B[j][ii+6];
tmp3=B[j][ii+7];
tmp4=A[ii+4][j];
tmp5=A[ii+5][j];
tmp6=A[ii+6][j];
tmp7=A[ii+7][j];
B[j][ii+4]=tmp4;
B[j][ii+5]=tmp5;
B[j][ii+6]=tmp6;
B[j][ii+7]=tmp7;
B[j+4][ii]=tmp0;
B[j+4][ii+1]=tmp1;
B[j+4][ii+2]=tmp2;
B[j+4][ii+3]=tmp3;
}
for(j=jj+4;j<jj+8;j++)
{
tmp0=A[ii+4][j];
tmp1=A[ii+5][j];
tmp2=A[ii+6][j];
tmp3=A[ii+7][j];
B[j][ii+4]=tmp0;
B[j][ii+5]=tmp1;
B[j][ii+6]=tmp2;
B[j][ii+7]=tmp3;
}
}
}
}
else if(M==61)
{ // 17*17 blocking brings #miss=1950, which is small enough
for(ii=0;ii<67;ii+=17)
{
for(jj=0;jj<61;jj+=17)
{
for(i=ii;i<ii+17&&i<67;i++)
{
for(j=jj;j<jj+17&&j<61;j++)
{
tmp0=A[i][j];
B[j][i]=tmp0;
}
}
}
}
}
}
/*
* You can define additional transpose functions below. We've defined
* a simple one below to help you get started.
*/
/*
* trans - A simple baseline transpose function, not optimized for the cache.
*/
char trans_desc[] = "Simple row-wise scan transpose";
void trans(int M, int N, int A[N][M], int B[M][N])
{
int i, j, tmp;
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
tmp = A[i][j];
B[j][i] = tmp;
}
}
}
char trans_by_block_desc[]="Blocking method";
void trans_by_block(int M, int N, int A[N][M], int B[M][N])
{
int i,j,ii,jj,tmp0,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7;
if(M==32)
{// equivolent to 8*8 blocking
for(ii=0;ii<32;ii+=8)
{
for(jj=0;jj<32;jj+=8)
{
for(i=ii;i<ii+8;i++)
{
tmp0=A[i][jj];
tmp1=A[i][jj+1];
tmp2=A[i][jj+2];
tmp3=A[i][jj+3];
tmp4=A[i][jj+4];
tmp5=A[i][jj+5];
tmp6=A[i][jj+6];
tmp7=A[i][jj+7];
B[jj][i]=tmp0;
B[jj+1][i]=tmp1;
B[jj+2][i]=tmp2;
B[jj+3][i]=tmp3;
B[jj+4][i]=tmp4;
B[jj+5][i]=tmp5;
B[jj+6][i]=tmp6;
B[jj+7][i]=tmp7;
}
}
}
}
else if(M==64)
{ // equivolent to 8*8 blocking and 4*4 sub-blocking
for(ii=0;ii<64;ii+=8)
{
for(jj=0;jj<64;jj+=8)
{
for(i=ii;i<ii+4;i++)
{
tmp0=A[i][jj];
tmp1=A[i][jj+1];
tmp2=A[i][jj+2];
tmp3=A[i][jj+3];
tmp4=A[i][jj+4];
tmp5=A[i][jj+5];
tmp6=A[i][jj+6];
tmp7=A[i][jj+7];
B[jj][i]=tmp0;
B[jj+1][i]=tmp1;
B[jj+2][i]=tmp2;
B[jj+3][i]=tmp3;
B[jj][i+4]=tmp4;
B[jj+1][i+4]=tmp5;
B[jj+2][i+4]=tmp6;
B[jj+3][i+4]=tmp7;
}
for(i=ii+4;i<ii+8;i++)
{
tmp0=A[i][jj];
tmp1=A[i][jj+1];
tmp2=A[i][jj+2];
tmp3=A[i][jj+3];
tmp4=A[i][jj+4];
tmp5=A[i][jj+5];
tmp6=A[i][jj+6];
tmp7=A[i][jj+7];
B[jj+4][i-4]=tmp0;
B[jj+5][i-4]=tmp1;
B[jj+6][i-4]=tmp2;
B[jj+7][i-4]=tmp3;
B[jj+4][i]=tmp4;
B[jj+5][i]=tmp5;
B[jj+6][i]=tmp6;
B[jj+7][i]=tmp7;
}
for(j=jj;j<jj+4;j++)
{
tmp0=B[j][ii+4];
tmp1=B[j][ii+5];
tmp2=B[j][ii+6];
tmp3=B[j][ii+7];
tmp4=B[j+4][ii];
tmp5=B[j+4][ii+1];
tmp6=B[j+4][ii+2];
tmp7=B[j+4][ii+3];
B[j+4][ii]=tmp0;
B[j+4][ii+1]=tmp1;
B[j+4][ii+2]=tmp2;
B[j+4][ii+3]=tmp3;
B[j][ii+4]=tmp4;
B[j][ii+5]=tmp5;
B[j][ii+6]=tmp6;
B[j][ii+7]=tmp7;
}
}
}
}
else if(M==61)
{
for(ii=0;ii<67;ii+=17)
{
for(jj=0;jj<61;jj+=17)
{
for(i=ii;i<ii+17&&i<67;i++)
{
for(j=jj;j<jj+17&&j<61;j++)
{
tmp0=A[i][j];
B[j][i]=tmp0;
}
}
}
}
}
}
/*
* registerFunctions - This function registers your transpose
* functions with the driver. At runtime, the driver will
* evaluate each of the registered functions and summarize their
* performance. This is a handy way to experiment with different
* transpose strategies.
*/
void registerFunctions()
{
/* Register your solution function */
registerTransFunction(transpose_submit, transpose_submit_desc);
/* Register any additional transpose functions */
registerTransFunction(trans, trans_desc);
registerTransFunction(trans_by_block,trans_by_block_desc);
}
/*
* is_transpose - This helper function checks if B is the transpose of
* A. You can check the correctness of your transpose by calling
* it before returning from the transpose function.
*/
int is_transpose(int M, int N, int A[N][M], int B[M][N])
{
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < M; ++j) {
if (A[i][j] != B[j][i]) {
return 0;
}
}
}
return 1;
}