Repository navigation
Expand file tree
/
Copy pathnaivematrixmultiplication.cpp
More file actions
119 lines (97 loc) · 3.37 KB
/
Copy pathnaivematrixmultiplication.cpp
File metadata and controls
119 lines (97 loc) · 3.37 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
/**
* Author : Gaurav Maheshwari, CS08B005 (gaurav.m.iitm@gmail.com)
*/
#include "cache.h"
#include "address.h"
#include <stdlib.h>
#include <time.h>
#include <iostream>
#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);
}
void matrixMultiplication (Cache& cache, int dataSize, int m, int n, int p) {
int i, j, k;
// generate a matrix of specified size
int matrix1[m][n];
Address* address1[m][n];
int matrix2[n][p];
Address* address2[n][p];
int matrix3[m][p];
Address* address3[m][p];
int linearAddress = 0;
srand(time(NULL));
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
matrix1[i][j] = rand() % MAX;
address1[i][j] = getAddress(linearAddress, cache.getAssociativity(),
cache.getBlockSize(), cache.getCacheSize(), dataSize);
linearAddress++;
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < p; j++) {
matrix2[i][j] = rand() % MAX;
address2[i][j] = getAddress(linearAddress, cache.getAssociativity(),
cache.getBlockSize(), cache.getCacheSize(), dataSize);
linearAddress++;
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < p; j++) {
address3[i][j] = getAddress(linearAddress, cache.getAssociativity(),
cache.getBlockSize(), cache.getCacheSize(), dataSize);
linearAddress++;
}
}
// perform matrix multiplication using naive algorithm
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < p; k++) {
cache.updateCache(*address1[i][k]);
cache.updateCache(*address2[k][j]);
cache.updateCache(*address3[i][j]);
matrix3[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
int main (int argc, char* argv[]) {
if (argc != 8) {
cout << "Usage: ./exe <cache-associativity> <cache-block-size-in-bytes>"
<< " <cache-size-in-bytes> <data-element-size-in-bytes>"
<< "<m> <n> <p>\n";
exit(1);
}
int associativity = atoi(argv[1]);
int blockSizeInBytes = atoi(argv[2]);
int cacheSizeInBytes = atoi(argv[3]);
int dataSizeInBytes = atoi(argv[4]);
int m = atoi(argv[5]);
int n = atoi(argv[6]);
int p = atoi(argv[7]);
Cache cache(associativity, blockSizeInBytes, cacheSizeInBytes);
Cache fullyAssociativeCache(cacheSizeInBytes/blockSizeInBytes,
blockSizeInBytes, cacheSizeInBytes);
matrixMultiplication(cache, dataSizeInBytes, m, n, p);
matrixMultiplication(fullyAssociativeCache, dataSizeInBytes, m, n, p);
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;
}