-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
136 lines (124 loc) · 4.39 KB
/
index.js
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
var normSortingBtn = document.getElementById('norm-sorting-btn');
var distSortingBtn = document.getElementById('dist-sorting-btn');
var normStatus = document.getElementById('norm-status');
var distStatus = document.getElementById('dist-status');
var statuses = ['Not running', 'Creating Array', 'Running', 'Completed!'];
var normStats = {
arraySize: null,
startTime: null,
endTime: null,
totalTime: null
};
var dStats = {
arraySize: null,
startTime: null,
endTime: null,
totalTime: null,
workerStat: false
};
function promisifyWorker(worker, data) {
return new Promise((resolve, reject) => {
worker.onmessage = function(e) {
console.log('resolved');
resolve(e);
};
worker.onerror = function(err) {
reject(err);
};
worker.postMessage(data);
});
}
function randomFiller(view, minValue, maxValue) {
for (var i = 0; i < view.length; i++) {
view[i] = Math.floor(Math.random() * (maxValue - minValue) + minValue);
}
}
function startDistributed() {
distStatus.innerText = statuses[1];
var sab = new SharedArrayBuffer(20000000);
var uint16View = new Uint16Array(sab);
randomFiller(uint16View, 10, 32767);
var worker1 = new Worker('worker.js');
var worker2 = new Worker('worker.js');
var worker3 = new Worker('worker.js');
var worker4 = new Worker('worker.js');
var mWorker1 = new Worker('merge-worker.js');
var mWorker2 = new Worker('merge-worker.js');
performance.mark('merge-start');
distStatus.innerText = statuses[2];
dStats.arraySize = uint16View.length;
dStats.startTime = Date.now();
Promise.all([
promisifyWorker(worker1, [sab, 0, 2499999]),
promisifyWorker(worker2, [sab, 2500000, 4999999]),
promisifyWorker(worker3, [sab, 5000000, 7499999]),
promisifyWorker(worker4, [sab, 7500000, 9999999])
])
.then(function() {
return Promise.all([
promisifyWorker(mWorker1, [uint16View, 0, 2499999, 4999999]),
promisifyWorker(mWorker2, [uint16View, 5000000, 7499999, 9999999])
]);
})
.then(function(d) {
merge(uint16View, 0, 4999999, 9999999);
dStats.endTime = Date.now();
dStats.totalTime = dStats.endTime - dStats.startTime;
performance.mark('merge-end');
distStatus.innerText = statuses[3];
updateDStats();
distSortingBtn.removeAttribute('disabled');
performance.measure('d-merge-sort', 'merge-start', 'merge-end');
});
}
function updateNormStats() {
document.getElementById('norm-stats').style.display = 'block';
document.getElementById('n-start-time').innerText = normStats.startTime;
document.getElementById('n-end-time').innerText = normStats.endTime;
document.getElementById('n-total-time').innerText =
normStats.totalTime + ' (' + normStats.totalTime / 1000.0 + 's)';
document.getElementById('n-array-size').innerText = normStats.arraySize;
}
function updateDStats() {
document.getElementById('dist-stats').style.display = 'block';
document.getElementById('d-start-time').innerText = dStats.startTime;
document.getElementById('d-end-time').innerText = dStats.endTime;
document.getElementById('d-total-time').innerText =
dStats.totalTime + ' (' + dStats.totalTime / 1000.0 + 's)';
document.getElementById('d-array-size').innerText = dStats.arraySize;
}
function startNormalMergeSort() {
normStatus.innerText = statuses[1];
var sab = new SharedArrayBuffer(20000000);
var uint16View = new Uint16Array(sab);
randomFiller(uint16View, 10, 32767);
normStatus.innerText = statuses[2];
setTimeout(function() {
performance.mark('merge-start');
normStats.startTime = Date.now();
mergeSort(uint16View, 0, uint16View.length - 1);
normStats.endTime = Date.now();
performance.mark('merge-end');
performance.measure('merge-sort', 'merge-start', 'merge-end');
normStatus.innerText = statuses[3];
normStats.arraySize = uint16View.length;
normStats.totalTime = normStats.endTime - normStats.startTime;
updateNormStats();
normSortingBtn.removeAttribute('disabled');
}, 500);
}
normSortingBtn.addEventListener('click', function() {
this.setAttribute('disabled', 'disabled');
// Set to not running
normStatus.innerHTML = statuses[0];
setTimeout(function() {
startNormalMergeSort();
}, 200);
});
distSortingBtn.addEventListener('click', function() {
this.setAttribute('disabled', 'disabled');
distStatus.innerHTML = statuses[0];
setTimeout(function() {
startDistributed();
}, 200);
});