-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
136 lines (116 loc) · 4.88 KB
/
main.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
// epiphron-web
// Devon McKee, 2024
const delay = ms => new Promise(res => setTimeout(res, ms));
async function cpuAtomicTest(contention=1, padding=1) {
console.log(`Testing CPU atomics with contention ${contention} and padding ${padding}...`);
// Setup SharedArrayBuffer for atomics
let bufLen = Uint32Array.BYTES_PER_ELEMENT * Math.ceil(navigator.hardwareConcurrency * padding / contention);
const buffer = new SharedArrayBuffer(bufLen);
const bufArray = new Uint32Array(buffer);
for (let i = 0; i < bufArray.length; i++) {
Atomics.store(bufArray, i, 0);
}
//console.log(bufArray);
// Initialize workers
const cpuWorkers = [];
for (let i = 0; i < navigator.hardwareConcurrency; i++) {
cpuWorkers.push(new Worker('cpuWorker.js'));
}
// Start timer and workers
let start_time = performance.now();
for (let i = 0; i < navigator.hardwareConcurrency; i++) {
cpuWorkers[i].postMessage({buffer : buffer, i : Math.floor(i / contention * padding)});
}
await delay(3000);
// End workers
cpuWorkers.forEach((w) => w.terminate());
let end_time = performance.now();
let test_time = end_time - start_time;
console.log(`Workers terminated after ${test_time} ms.`);
let res = 0;
for (let i = 0; i < bufArray.length; i++) {
res += Atomics.load(bufArray, i);
}
console.log(`Performed ${res} operations.`);
console.log(`Throughput: ${res / test_time / 1000} atomic ops/microsecond`);
return res / test_time / 1000;
}
async function cpuAtomicSweep(contention = 8, padding = 8) {
console.log(`Detected ${navigator.hardwareConcurrency} logical cores...`);
console.log(`Sweeping through CPU atomics with contention from 1-${contention} and padding from 1-${padding}`);
$('#heatmap-progress').css('display', 'block');
$('#heatmap-progress').prop('value', 0);
$('#heatmap-progress').prop('max', (Math.log2(contention) + 1) * (Math.log2(padding) + 1));
let x = [];
let y = [];
let z = [];
for (let c = 1; c <= contention; c *= 2) x.push(c);
let layout = {
//title: 'CPU Atomic Results',
xaxis: { type : 'category', title: 'Contention' },
yaxis: { type : 'category', title : 'Padding' },
annotations : []
};
let test_num = 0;
for (let p = 1; p <= padding; p *= 2) {
y.push(p);
let row = [];
for (let c = 1; c <= contention; c *= 2) {
let res = await cpuAtomicTest(c, p);
row.push(res);
layout.annotations.push({
x : Math.log2(c), y : Math.log2(p),
text : parseFloat(res).toFixed(2),
font : {
color: 'white',
},
showarrow: false,
bgcolor: 'black'
});
test_num++;
$('#heatmap-progress').prop('value', test_num);
}
z.push(row);
}
Plotly.newPlot('cpu-heatmap', [{
x : x,
y : y,
z : z,
type : 'heatmap',
colorscale : 'Viridis',
colorbar : {title : { text : 'Atomic Operations per Microsecond', side : 'right'}}
}], layout);
}
async function initializeGPU() {
if (!navigator.gpu) throw Error("WebGPU not supported.");
const adapter = await navigator.gpu.requestAdapter();
if (!adapter) throw Error("Couldn't request WebGPU adapter.");
const device = await adapter.requestDevice();
if (!device) throw Error("Couldn't request WebGPU device.");
return { adapter, device };
}
async function gpuAtomicTest(contention=1, padding=1) {
}
async function gpuAtomicSweep(contention=1, padding=1) {
console.log(`Sweeping through GPU atomics with contention from 1-${contention} and padding from 1-${padding}`);
let adapter, device;
try {
({ adapter, device } = await initializeGPU());
} catch (e) {
alert('Error initializing WebGPU - check console for more info.');
console.error(e);
return;
}
}
document.addEventListener('DOMContentLoaded', async () => {
//await cpuAtomicSweep(navigator.hardwareConcurrency, 16);
//await gpuAtomicSweep(16, 16);
$('#start-tests').on('click', async () => {
$('#start-tests').prop('disabled', true);
$('#num-threads').text(`Detected ${navigator.hardwareConcurrency} logical threads on CPU.`);
$('#cpu-baseline').text(`${parseFloat((await cpuAtomicTest(1, 1)).toFixed(2))} atomic ops/microsecond`);
$('#cpu-contention').text(`${parseFloat((await cpuAtomicTest(2, 1)).toFixed(2))} atomic ops/microsecond`);
$('#cpu-padding').text(`${parseFloat((await cpuAtomicTest(1, 16)).toFixed(2))} atomic ops/microsecond`);
await cpuAtomicSweep(navigator.hardwareConcurrency, 16);
});
});