-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
311 lines (295 loc) · 9.14 KB
/
index.html
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
306
307
308
309
310
311
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>WASM Performance</title>
<meta name="description" content="WASM performance example">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script type="text/javascript" src="d3.min.js"></script>
<script type="text/javascript" src="graph.js"></script>
<script async type="text/javascript" src="mmm3.js"></script>
<style>
.line {
fill: none;
stroke: #FFF;
stroke-width: 2;
}
.xaxis, .yaxis {
font-size: 12px;
}
.axisLabel {
font-size:18px;
}
.legend{
font-size: 28px;
}
#ijkjs {
/*fill: #ffa500;*/
stroke: #ffa500;
}
#kjijs {
/*fill: #66cccc;*/
stroke: #66cccc;
}
#bmmjs {
/*fill: #008000;*/
stroke: #008000;
}
#ijkc {
/*fill: #000000;*/
stroke: #000000;
}
#kjic {
/*fill: #8a2be2;*/
stroke: #8a2be2;
}
#bmmc {
/*fill: #ff4040;*/
stroke: #ff4040;
}
.grid line {
stroke: lightgrey;
stroke-opacity: 0.7;
shape-rendering: crispEdges;
}
</style>
</head>
<body>
<p id="progress">Running...</p>
<script>
/**
* Initializes Web Assembly environment and load the .wasm file
*
*/
async function createWebAssembly(path, importObject) {
const bytes = await window.fetch(path).then(x => x.arrayBuffer());
return WebAssembly.instantiate(bytes, importObject);
}
/**
* intializes matrices with random values
* clearBit == 0 -> creates a zero matrix
*
*/
function initialize(matrix, clearBit, length) {
for(let i = 0; i < length; i++){
for (let j = 0; j < length; j++) {
matrix[i * length + j] = (Math.floor(Math.random() * (1001 - 1)) + 1001) * clearBit;
}
}
}
/**
* clears cache by loading a new matrix into memory
*
*/
function flush(matrix) {
let sum = 0;
for(let i = 0; i < length; i++){
for (let j = 0; j < length; j++) {
sum += matrix[i * length + j];
//console.log(matrix[i * length + j]);
}
}
}
/**
* Baseline matrix mulitplication
*
*/
function mmmijk(matrixA, matrixB, matrixC, length) {
for(let i = 0; i < length; i++){
for(let j = 0; j < length; j++) {
for(let k = 0; k < length; k++) {
matrixC[i * length + j] += (matrixA[i * length + k] * matrixB[j * length + k]);
}
}
}
}
/**
* Matrix multiplication with high cache misses
*
*/
function mmmkji(matrixA, matrixB, matrixC, length) {
for(let k = 0; k < length; k++) {
for(let j = 0; j < length; j++) {
for(let i = 0; i < length; i++){
matrixC[i * length + j] += (matrixA[i * length + k] * matrixB[j * length + k]);
}
}
}
}
/**
* Matrix Multiplication with less cache misses
*
*/
function bmm(matrixA, matrixB, matrixC, length, blockSize) {
let block = blockSize * (length/blockSize);
let sum;
for(let kk = 0; kk < block; kk += blockSize) {
for(let jj = 0; jj < block; jj += blockSize) {
for(let i = 0; i < length; i++) {
for(let j = jj; j < jj + blockSize; j++) {
sum = matrixC[i * length + j];
for(let k = kk; k < kk + blockSize; k++) {
sum += matrixA[i * length + j] * matrixB[i * length + j];
}
matrixC[i * length + j] = sum;
}
}
}
}
}
/**
* computes avg
*
*/
function getAvg(array, x){
let len = array.length;
for (let i = 0; i < len; i++){
array[i] /= x;
}
}
function main(){
let progress = document.getElementById("progress");
const memory = new WebAssembly.Memory({initial: 256, maximum: 256});
const env = {
'abortStackOverflow': _ => { throw new Error('overflow'); },
'table': new WebAssembly.Table({initial: 0, maximum: 0, element: 'anyfunc'}),
'tableBase': 0,
'memory': memory,
'memoryBase': 1024,
'STACKTOP': 0,
'STACK_MAX': memory.buffer.byteLength,
};
const importObject = {env};
createWebAssembly('mmm3.wasm', importObject).then(wasm => {
// some constants used through out
const ghz = 2.8; // GHZ - adjust to your cpu
const msToS = 1000; // ms n 1 sec
// experiement to figure out best blocking size for higher cache hits
const blockSize = 32;
const maxLength = (1024 * 1024);
const arrayLen = 3;
const runs = 1; // controls how many run for averging out noise
const exports = wasm.instance.exports;
// array size 1 to get rid of noise
const length = [1, 64, 128, 256, 512, 1024]; // matrix sizes: n x n
const totalSize = 100 * 100;
// set up all the matrices
let bufferA = new ArrayBuffer(maxLength);
let bufferB = new ArrayBuffer(maxLength);
let bufferC = new ArrayBuffer(maxLength);
let bufferFlush = new ArrayBuffer(maxLength);
let matrixA = new Float64Array(bufferA);
let matrixB = new Float64Array(bufferB);
let matrixC = new Float64Array(bufferC);
let cache = new Float64Array(bufferFlush);
let mmmijkResult = [0,0,0,0,0,0];
let mmmkjiResult = [0,0,0,0,0,0];
let bmmResult = [0,0,0,0,0,0];
let cmmmijkResult = [0,0,0,0,0,0];
let cmmmkjiResult = [0,0,0,0,0,0];
let cbmmResult = [0,0,0,0,0,0];
let yaxisarray = []; // used to find max to both cpi and time
initialize(cache, 1, 512);
for (let j = 0; j < runs; j++){
for (let i = 2; i < arrayLen; i++) {
console.log(length[i]);
initialize(matrixA, 1, length[i]);
initialize(matrixB, 1, length[i]);
initialize(matrixC, 0, length[i]);
flush(cache);
let start = window.performance.now();
mmmijk(matrixA, matrixB, matrixC, length[i]);
let end = window.performance.now();
let mmmijkcpi = (ghz * (end - start)) / (length[i] ^ 3);
yaxisarray.push(mmmijkcpi);
if(i !== 0) mmmijkResult[i] += (mmmijkcpi);
console.log( "cpi " + (mmmijkcpi).toString());
console.log(end-start);
flush(cache);
initialize(matrixC, 0);
start = window.performance.now();
mmmkji(matrixA, matrixB, matrixC, length[i]);
end = window.performance.now();
let mmmkjicpi = (ghz * (end - start)) / (length[i] ^ 3);
yaxisarray.push(mmmkjicpi);
if(i !== 0) mmmkjiResult[i] += (mmmkjicpi);
console.log( "cpi " + ((ghz * (end - start)) / (length[i] ^ 3)).toString());
console.log(end-start);
flush(cache);
initialize(matrixC, 0);
start = window.performance.now();
bmm(matrixA, matrixB, matrixC, length[i], blockSize);
end = window.performance.now();
let bmmcpi = (ghz * (end - start)) / (length[i] ^ 3);
yaxisarray.push(bmmcpi);
if(i !== 0) bmmResult[i] += (bmmcpi);
console.log( "cpi " + ((ghz * (end - start)) / (length[i] ^ 3)).toString());
console.log(end-start);
flush(cache);
// WASM code
console.info('got exports', exports);
start = window.performance.now();
exports._bmm(matrixA, matrixB, matrixC, length[i], blockSize);
end = window.performance.now();
let cbmmcpi = (ghz * (end - start)) / (length[i] ^ 3);
yaxisarray.push(cbmmcpi);
if(i !== 0) cbmmResult[i] += (cbmmcpi);
console.log( "cpi " + ((ghz * (end - start)) / (length[i] ^ 3)).toString());
console.log("bmm " + (end - start).toString());
flush(cache);
start = window.performance.now();
exports._mmmijk(matrixA, matrixB, matrixC, length[i]);
end = window.performance.now();
let cmmmijkcpi = (ghz * (end - start)) / (length[i] ^ 3);
yaxisarray.push(cmmmijkcpi);
if(i !== 0) cmmmijkResult[i] += (cmmmijkcpi);
console.log( "cpi " + ((ghz * (end - start)) / (length[i] ^ 3)).toString());
console.log("ijk " + (end - start).toString());
flush(cache);
start = window.performance.now();
exports._mmmkji(matrixA, matrixB, matrixC, length[i]);
end = window.performance.now();
let cmmmkjicpi = (ghz * (end - start)) / (length[i] ^ 3);
yaxisarray.push(cmmmkjicpi);
if(i !== 0) cmmmkjiResult[i] += (cmmmkjicpi);
console.log( "cpi " + ((ghz * (end - start)) / (length[i] ^ 3)).toString());
console.log("kji " + (end - start).toString());
}
}
getAvg(mmmijkResult, runs);
getAvg(mmmkjiResult, runs);
getAvg(bmmResult, runs);
getAvg(cmmmijkResult, runs);
getAvg(cmmmkjiResult, runs);
getAvg(cbmmResult, runs);
let mmmijkFinalResult = [];
let mmmkjiFinalResult = [];
let bmmFinalResult = [];
let cmmmijkFinalResult = [];
let cmmmkjiFinalResult = [];
let cbmmFinalResult = [];
for (let i = 0; i < arrayLen; i++) {
mmmijkFinalResult.push([length[i], mmmijkResult[i]]);
mmmkjiFinalResult.push([length[i], mmmkjiResult[i]]);
bmmFinalResult.push([length[i], bmmResult[i]]);
cmmmijkFinalResult.push([length[i], cmmmijkResult[i]]);
cmmmkjiFinalResult.push([length[i], cmmmkjiResult[i]]);
cbmmFinalResult.push([length[i], cbmmResult[i]]);
}
let results = {
"ijkJS" : mmmijkFinalResult,
"kjiJS" : mmmkjiFinalResult,
"bmmJS" : bmmFinalResult,
"ijkC" : cmmmijkFinalResult,
"kjiC" : cmmmkjiFinalResult,
"bmmC" : cbmmFinalResult
};
yaxisarray.sort(function(a,b){return a - b});
let maxY = yaxisarray[yaxisarray.length - 1];
graph(results, arrayLen, length, maxY);
progress.style.display = "none";
}).catch(err => console.warn('err loading wasm', err));
}
main();
</script>
</body></html>