-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrans.c
More file actions
155 lines (146 loc) · 4.77 KB
/
trans.c
File metadata and controls
155 lines (146 loc) · 4.77 KB
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
/**
* 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; //index of row
int j; //index of column
int r; //rows
int c; //columns
int transpose = 0;
int temp = 0;
//We will use a switch to determine the case by sorting by N.
//There are three test matrices used. There is 32x32, 64x64, and 61x67.
//This solution will not work on other sizes.
switch(N){
case 64:
//we will loop through the columns then rows
//but increasing by 4 to simulate a block
for (c = 0; c < N; c += 4)
{
for (r = 0; r < N; r += 4)
{
for (i = r; i < r + 4; i++)
{
for (j = c; j < c + 4; j++)
{
if (i != j) {
B[j][i] = A[i][j];
}
else{
temp = A[i][j];
transpose = i;
}
}
if (r == c)
{
B[transpose][transpose] = temp;
}
}
}
}
break;
case 32:
//first loop through columns
//incrementing c by 8 is best way for 32
for (c = 0; c < N; c += 8)
{
//and through rows
for (r = 0; r < N; r += 8)
{
for (i = r; i < r + 8; i++)
{
for (j = c; j < c + 8; j++)
{
if (i != j)
{
B[j][i] = A[i][j];
}
else{
temp = A[i][j];
transpose = i;
}
}
if (r == c)
{
B[transpose][transpose] = temp;
}
}
}
}
break;
default:
for (c = 0; c < M; c += 16)
{
for (r = 0; r < N; r += 16)
{
for (i = r; (i < r + 16) && (i < N); i++)
{
for (j = c; (j < c + 16) && (j < M); j++)
{
if (i != j)
{
B[j][i] = A[i][j];
}
else
{
temp = A[i][j];
transpose = i;
}
}
if (r == c)
{
B[transpose][transpose] = temp;
}
}
}
}
} //end switch
} //end function
/**
* 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;
}
}
}
/**
* 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);
}