Repository navigation
Expand file tree
/
Copy pathworker.js
More file actions
75 lines (61 loc) · 2.49 KB
/
Copy pathworker.js
File metadata and controls
75 lines (61 loc) · 2.49 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
/**
* @author Bert Baron
*/
import * as fxp from './fxp.mjs'
import {WorkerContext} from "./workerContext.mjs";
import {MandelbrotFloat} from "./mandelbrotFloat.mjs";
import {MandelbrotFxP} from "./mandelbrotFxP.mjs";
import {MandelbrotPerturbation} from "./mandelbrotPerturbation.mjs";
import {MandelbrotPerturbationExtFloat} from "./mandelbrotPerturbationExtFloat.mjs";
import {MandelbrotWebGPU} from "./mandelbrotWebGPU.mjs";
const ctx = new WorkerContext()
async function initMandelbrotFloat() {
await new Promise(resolve => setTimeout(resolve, 1))
return new MandelbrotFloat(ctx)
}
async function initMandelbrotFxP() {
await new Promise(resolve => setTimeout(resolve, 1))
return new MandelbrotFxP(ctx)
}
async function initMandelbrotPerturbation() {
await new Promise(resolve => setTimeout(resolve, 1))
return new MandelbrotPerturbation(ctx)
}
async function initMandelbrotPerturbationExtFloat() {
await new Promise(resolve => setTimeout(resolve, 1))
return new MandelbrotPerturbationExtFloat(ctx)
}
const mandelbrotFloat = initMandelbrotFloat();
const mandelbrotFxP = initMandelbrotFxP();
const mandelbrotPerturbation = initMandelbrotPerturbation();
const mandelbrotPerturbationExtFloat = initMandelbrotPerturbationExtFloat();
onmessage = handleMessage
// Add some randomnes to have different checkpoints per worker
const STOP_CHECK_INTERVAL = 200 + Math.floor(Math.random() * 100)
async function handleMessage(msg) {
const message = parseMessage(msg)
// console.log(`Received: ${JSON.stringify(msg.data)}`)
if (message.type === 'task') {
const implPromise =
message.requiredPrecision > 1020
? mandelbrotPerturbationExtFloat
: message.requiredPrecision > 58
? mandelbrotPerturbation
: mandelbrotFloat
const impl = await implPromise
// console.log(`Precision ${message.requiredPrecision}, using ${impl.constructor.name}`)
ctx.initTask(message.jobToken)
ctx.resetStats()
const result = await impl.process(message)
postMessage(result)
}
}
function parseMessage(msg) {
if (msg.data.type === 'task') {
msg.data.frameTopLeft[0] = fxp.fromJSON(msg.data.frameTopLeft[0])
msg.data.frameTopLeft[1] = fxp.fromJSON(msg.data.frameTopLeft[1])
msg.data.frameBottomRight[0] = fxp.fromJSON(msg.data.frameBottomRight[0])
msg.data.frameBottomRight[1] = fxp.fromJSON(msg.data.frameBottomRight[1])
}
return msg.data
}