-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcacheaware2.cpp
More file actions
executable file
·243 lines (207 loc) · 7.24 KB
/
Copy pathcacheaware2.cpp
File metadata and controls
executable file
·243 lines (207 loc) · 7.24 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
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
/**
* Author : Mrinal Kumar, CS08B011 (mrinalkumar08@gmail.com)
* Bharat Singh, CS08B025 (bharatsingh430@gmail.com)
*/
#include "cache.h"
#include "address.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <math.h>
#define MAX 100
using namespace std;
Address* getAddress (int linearAddress, int associativity, int blockSize,
int cacheSize, int dataSize) {
int numDataElementsInABlock = blockSize / dataSize;
int numRowsInCache = cacheSize / (blockSize * associativity);
int set = (linearAddress % (numDataElementsInABlock * numRowsInCache)) /
numDataElementsInABlock;
int index = (linearAddress / numDataElementsInABlock) * numDataElementsInABlock;
int offset = linearAddress - index;
return new Address(set, index, offset);
}
int min(int a, int b) {
if(a >= b)
return b;
else
return a;
}
void cacheAwareMatrixMultiplication (Cache& cache, int dataSize, int n, int s) {
int i, j, k, l;
int x, y, z;
// generate a matrix of specified size
int matrix1[n][n];
Address* copied_address1[n][n];
int matrix2[n][n];
Address* copied_address2[n][n];
int matrix3[n][n];
Address* copied_address3[n][n];
int linearAddress = 0;
srand(time(NULL));
//simulating copying matrices to a different location to take care of the fact that elements in the same tile are assigned contiguous linear address so that they are brought together into the ache.
for (i=0; i < n/s; i++) {
for (j = 0; j < n/s; j++) {
//assign linear address of the (i,j)th block in matrix 1, matrix2 and matrix3
for (k = 0; k<s; k++) {
for (l = 0; l<s; l++) {
copied_address1[i*s+k][j*s+l] = getAddress(linearAddress, cache.getAssociativity(),
cache.getBlockSize(), cache.getCacheSize(), dataSize);
matrix1[k][l] = rand() % MAX;
linearAddress++;
}
}
}
}
for (i=0; i < n/s; i++) {
for (j = 0; j < n/s; j++) {
//assign linear address of the (i,j)th block in matrix 1, matrix2 and matrix3
for (k = 0; k<s; k++) {
for (l = 0; l<s; l++) {
copied_address2[i*s+k][j*s+l] = getAddress(linearAddress, cache.getAssociativity(),
cache.getBlockSize(), cache.getCacheSize(), dataSize);
matrix2[k][l] = rand() % MAX;
linearAddress++;
}
}
}
}
for (i=0; i < n/s; i++) {
for (j = 0; j < n/s; j++) {
//assign linear address of the (i,j)th block in matrix 1, matrix2 and matrix3
for (k = 0; k<s; k++) {
for (l = 0; l<s; l++) {
copied_address3[i*s+k][j*s+l] = getAddress(linearAddress, cache.getAssociativity(),
cache.getBlockSize(), cache.getCacheSize(), dataSize);
matrix3[k][l] = rand() % MAX;
linearAddress++;
}
}
}
}
// perform matrix multiplication using tile by tile multiplication
for (i = 0; i < n/s; i++) {
for (j = 0 ; j < n/s; j++) {
for (k = 0; k < n/s; k++) {
//C[i,j] = C[i,j] + A[i,k]*B[k,j]
//blockMultiply(cache, matrix1, matrix2, matrix3, i, j, k, s, p2, p3, m, n, p);
for (x = i*s; x <= min((i+1)*s-1, n-1); x++) {
for (y = j*s; y <= min((j+1)*s-1, n-1); y++) {
for (z = k*s; z <= min((k+1)*s-1, n-1); z++) {
cache.updateCache(*copied_address1[x][z]);
cache.updateCache(*copied_address2[z][y]);
cache.updateCache(*copied_address3[x][y]);
matrix3[x][y] += matrix1[x][z] * matrix2[z][y];
}
}
}
}
}
}
}
void cacheAwareMatrixMultiplication1 (Cache& cache, int dataSize, int n, int s) {
int i, j, k, l;
int x, y, z;
// generate a matrix of specified size
int matrix1[n][n];
Address* copied_address1[n][n];
int matrix2[n][n];
Address* copied_address2[n][n];
int matrix3[n][n];
Address* copied_address3[n][n];
int linearAddress = 0;
// code to fill copied_address1,2,3 according to your scheme
for (i=0; i < n/s; i++) {
for (j = 0; j < n/s; j++) {
//assign linear address of the (i,j)th block in matrix 1, matrix2 and matrix3
for (k = 0; k<s; k++) {
for (l = 0; l<s; l++) {
copied_address1[i*s+k][j*s+l] = getAddress(linearAddress, cache.getAssociativity(),
cache.getBlockSize(), cache.getCacheSize(), dataSize);
matrix1[k][l] = rand() % MAX;
linearAddress++;
}
}
for (k = 0; k<s; k++) {
for (l = 0; l<s; l++) {
copied_address2[i*s+k][j*s+l] = getAddress(linearAddress, cache.getAssociativity(),
cache.getBlockSize(), cache.getCacheSize(), dataSize);
matrix2[k][l] = rand() % MAX;
linearAddress++;
}
}
for (k = 0; k<s; k++) {
for (l = 0; l<s; l++) {
copied_address3[i*s+k][j*s+l] = getAddress(linearAddress, cache.getAssociativity(),
cache.getBlockSize(), cache.getCacheSize(), dataSize);
matrix3[k][l] = rand() % MAX;
linearAddress++;
}
}
}
}
// perform matrix multiplication using tile by tile multiplication
for (i = 0; i < n/s; i++) {
for (j = 0 ; j < n/s; j++) {
for (k = 0; k < n/s; k++) {
//C[i,j] = C[i,j] + A[i,k]*B[k,j]
//blockMultiply(cache, matrix1, matrix2, matrix3, i, j, k, s, p2, p3, m, n, p);
for (x = i*s; x <= min((i+1)*s-1, n-1); x++) {
for (y = j*s; y <= min((j+1)*s-1, n-1); y++) {
for (z = k*s; z <= min((k+1)*s-1, n-1); z++) {
cache.updateCache(*copied_address1[x][z]);
cache.updateCache(*copied_address2[z][y]);
cache.updateCache(*copied_address3[x][y]);
matrix3[x][y] += matrix1[x][z] * matrix2[z][y];
}
}
}
}
}
}
}
int main (int argc, char* argv[]) {
if (argc != 6) {
cout << "Usage: ./exe <cache-associativity> <cache-block-size-in-bytes>"
<< " <cache-size-in-bytes> <data-element-size-in-bytes> <n>\n";
exit(1);
}
int associativity = atoi(argv[1]);
int blockSizeInBytes = atoi(argv[2]);
int cacheSizeInBytes = atoi(argv[3]);
int dataSizeInBytes = atoi(argv[4]);
int n = atoi(argv[5]); //size of matrix
int rows_in_cache = cacheSizeInBytes /(blockSizeInBytes*associativity);
Cache cache(associativity, blockSizeInBytes, cacheSizeInBytes);
Cache fullyAssociativeCache(cacheSizeInBytes/blockSizeInBytes,
blockSizeInBytes, cacheSizeInBytes);
if (rows_in_cache < 3) {
// s : matrix partition will be of size s x s
int s = sqrt(cacheSizeInBytes / (3 * dataSizeInBytes));
if (s > n)
s = n;
cacheAwareMatrixMultiplication(cache, dataSizeInBytes, n, s);
cacheAwareMatrixMultiplication(fullyAssociativeCache, dataSizeInBytes, n, s);
} else {
int rows_by_three = rows_in_cache / 3;
int s = sqrt(rows_by_three * blockSizeInBytes * associativity /dataSizeInBytes);
int i = s;
while(n % i != 0) {
i--;
}
s = i;
cacheAwareMatrixMultiplication1(cache, dataSizeInBytes, n, s);
cacheAwareMatrixMultiplication(fullyAssociativeCache, dataSizeInBytes, n, s);
}
int numHits, numMisses, numColdMisses, numCapacityMisses, numConflictMisses;
numHits = cache.getNumHits();
numMisses = cache.getNumMisses();
numColdMisses = cache.getNumColdMiss();
numCapacityMisses = fullyAssociativeCache.getNumMisses() - numColdMisses;
numConflictMisses = numMisses - numColdMisses - numCapacityMisses;
if (numConflictMisses < 0)
numConflictMisses = 0;
cout << ((float) numHits) / (numHits + numMisses) << ' '
<< numColdMisses << ' ' << numCapacityMisses << ' ' << numConflictMisses << "\n";
return 0;
}