Skip to content

Commit

Permalink
ants: dynamic worker range based on ant distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheraff committed Dec 8, 2024
1 parent 09dc7ec commit 1e97224
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
23 changes: 21 additions & 2 deletions src/pages/ants/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default function () {
}

let i = 0
let previousAntCount = count
function loop() {
if (!done) rafId = requestAnimationFrame(loop)
if (!data) return
Expand All @@ -62,6 +63,9 @@ export default function () {
let antcount = 0
let foodcount = 0
let untouchedfoodcount = 0
const ranges = [0]
let range = 1
const rangeSize = Math.ceil(previousAntCount / (workers.length + 1))
for (let i = 0; i < data.length; i++) {
const point = data[i]
const isAnt
Expand Down Expand Up @@ -102,13 +106,28 @@ export default function () {
} else {
image.data.set(colors.void, index)
}

if (antcount > rangeSize * range) {
ranges.push(i)
range++
}
}

if (!(i % 100)) {
console.log('antcount', antcount, 'foodcount', foodcount, 'untouchedfoodcount', untouchedfoodcount)
}

previousAntCount = antcount
ctx.putImageData(image, 0, 0)

if (!(i % 100)) {
ranges[workers.length] = data.length
ranges.length = workers.length + 1
console.log('ranges', workers.length, ranges.length, data.length, ranges)
for (let i = 0; i < workers.length; i++) {
post(i, "range", { from: ranges[i], to: ranges[i + 1] })
}
}
}
let rafId = requestAnimationFrame(loop)

Expand All @@ -118,8 +137,8 @@ export default function () {
console.log('parallelism', parallelism)

for (let i = 0; i < parallelism; i++) {
const from = Math.floor(i * height / parallelism)
const to = Math.floor((i + 1) * height / parallelism)
const from = Math.floor(i * height / parallelism) * width
const to = Math.floor((i + 1) * height / parallelism) * width
const whose = i === 0 ? 'main' : 'worker'
console.log(whose, 'from', from, 'to', to)
if (!workers[i]) {
Expand Down
31 changes: 21 additions & 10 deletions src/pages/ants/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ const offsets = {
pheromoneToHill: 8,
}

const range = {
from: 0,
to: 0,
}

export type Incoming =
| { type: "start", data: { height: number, width: number, count: number } }
| { type: "share", data: { buffer: SharedArrayBuffer, width: number, height: number, vision: number, from: number, to: number } }
| { type: "range", data: { from: number, to: number } }

export type Outgoing =
| { type: "started", data: { buffer: SharedArrayBuffer } }
Expand All @@ -41,7 +47,6 @@ self.onmessage = (e: MessageEvent<Incoming>) => handleMessage(e.data)
function postMessage(message: Outgoing) { self.postMessage(message) }

function handleMessage(event: Incoming) {
console.log('handleMessage', event)
if (event.type === "start") {
const buffer = new SharedArrayBuffer(event.data.height * event.data.width * TypedArray.BYTES_PER_ELEMENT)
const array = new TypedArray(buffer)
Expand Down Expand Up @@ -104,7 +109,16 @@ function handleMessage(event: Incoming) {
console.log('onCollected', count)
postMessage({ type: "collected", data: { count } })
}
start({ array, width, height, vision, from, to, onCollected })
range.from = from
range.to = to
start({ array, width, height, vision, range, onCollected })
return
}

if (event.type === "range") {
range.from = event.data.from
range.to = event.data.to
return
}
}

Expand All @@ -115,16 +129,14 @@ async function start({
width,
height,
vision,
from = 0,
to = height,
range,
onCollected,
}: {
array: TypedArray
width: number
height: number
vision: number
from?: number
to?: number
range: { from: number, to: number }
onCollected: (count: number) => void
}) {
let lastPheromoneTick = performance.now()
Expand All @@ -138,9 +150,9 @@ async function start({
const isPheromoneTick = now - lastPheromoneTick > pheromoneTickInterval
const frame = new Promise(resolve => requestAnimationFrame(resolve))
if (isPheromoneTick) lastPheromoneTick = now
for (let y = from; y < to; y++) {
for (let x = 0; x < width; x++) {
const i = y * width + x
for (let i = range.from; i < range.to; i++) {
const y = Math.floor(i / width)
const x = i % width + x
let value = array[i]

// pheromone expiration
Expand Down Expand Up @@ -223,7 +235,6 @@ async function start({
}

array[i] = value
}
}
await frame
if (collectedCount) onCollected(collectedCount)
Expand Down
2 changes: 1 addition & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const ROUTES = {
image: ants_image
},
git: {
lastModified: 1728128584000,
lastModified: 1728137860000,
firstAdded: 1727995709000
},
}
Expand Down

0 comments on commit 1e97224

Please sign in to comment.