Skip to content

Commit

Permalink
Perform distributed skip filling, meaning time waiting on primary worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumbub committed Mar 30, 2022
1 parent 7d9e1c3 commit e3d355e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/logic/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export type Skips = Uint8Array;

export const SKIP_MULTIPLYER = 8;

export const fillSkips = (skips: Skips, start: number, end: number) => {
return skips.fill(SKIP, skipI(start), skipI(end));
};

export const skipI = (i: number) => {
return Math.floor(i / SKIP_MULTIPLYER);
};

export const newBoard = (viewWidth: number, viewHeight: number) => {
if (!littleEndian()) {
alert('Browser uses incorrect bit endianness');
Expand Down
26 changes: 24 additions & 2 deletions src/logic/next.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { createJobSignals, notifyStartJobs, requestJobToProcess, waitForAllJobsToComplete } from '../workers/jobs.js';
import { BootMessage } from '../workers/secondary.worker.js';
import { Board, Cells, DONT_SKIP, flipBoardIo, getBoardIo, SKIP, Skips, SKIP_MULTIPLYER } from './board.js';
import {
Board,
Cells,
DONT_SKIP,
fillSkips,
flipBoardIo,
getBoardIo,
SKIP,
skipI,
Skips,
SKIP_MULTIPLYER,
} from './board.js';
import { assignBoardPadding } from './padding.js';
import { PROBABLY_OPTIMAL_JOB_COUNT } from './threads.js';

Expand Down Expand Up @@ -41,6 +52,7 @@ export const nextBoardSection = (
inSkip: Skips,
outSkip: Skips,
) => {
fillSkips(outSkip, i + width - 1, endI - width + 1);
while (i < endI) {
while (inSkip[Math.floor(i / SKIP_MULTIPLYER)]) i += SKIP_MULTIPLYER;

Expand Down Expand Up @@ -89,6 +101,16 @@ const createJobs = (segments: number, width: number, height: number): [number, n
});
};

const setSkipBorders = (board: Board, jobs: [number, number][]) => {
const { width, height } = board;
const { outSkips } = getBoardIo(board);

for (let i = 0; i < jobs.length; i++) {
const [beginI] = jobs[i];
fillSkips(outSkips, beginI - width - 1, beginI + width - 1);
}
};

export const startNextBoardLoop = (generationsAndMax: Uint32Array, board: Board, workers: Worker[]) => {
const jobs = createJobs(PROBABLY_OPTIMAL_JOB_COUNT, board.width, board.height);
const signals = createJobSignals(jobs.length);
Expand All @@ -112,7 +134,7 @@ export const startNextBoardLoop = (generationsAndMax: Uint32Array, board: Board,
while (generationsAndMax[0] < generationsAndMax[1]) {
// Pre-processing
flipBoardIo(board);
board.skips[1 - board.skipsInput[0]].fill(SKIP);
setSkipBorders(board, jobs);

// Processing
notifyStartJobs(signals);
Expand Down

0 comments on commit e3d355e

Please sign in to comment.