@rulecad/fea 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/threads.mjs +10 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/fea",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "How far a part bends under load: finite elements on a grid of squares (2D) or cubes (3D), with a multigrid solver, in plain JavaScript.",
5
5
  "type": "module",
6
6
  "exports": {
package/threads.mjs CHANGED
@@ -9,14 +9,19 @@ import { Worker, isMainThread, parentPort, workerData } from 'node:worker_thread
9
9
  import { jsBackend, pattern, assemble } from './fea.mjs';
10
10
 
11
11
  const THREADS = Math.max(1, Math.min(8, Number(process.env.FEA_THREADS) || os.cpus().length - 1));
12
+ // How many operators each worker keeps: an optimisation step registers one per multigrid level (four or so), and
13
+ // their arrays are large (the bin's: 160 MB), so the workers let go of all but the last few. Before this, a run
14
+ // kept every step's and swapped itself to a crawl.
15
+ const KEPT = 16;
12
16
 
13
17
  // The pool: made on first use, kept for the process (unref'd, so it doesn't keep the process alive). Each operator
14
18
  // registers its arrays with every worker under an id; a product then signals the id and waits for every worker's done.
15
19
  let pool = null;
16
20
  function workers() {
17
21
  if (pool) return pool;
18
- // ctl: [0] the product generation, [1] the operator id, [2] workers done, [3] registrations acknowledged
19
- const ctl = new Int32Array(new SharedArrayBuffer(16)), list = [];
22
+ // ctl: [0] the product generation, [1] the operator id, [2] workers done, [3] registrations acknowledged, [4] a
23
+ // product asked of an operator a worker no longer holds
24
+ const ctl = new Int32Array(new SharedArrayBuffer(20)), list = [];
20
25
  for (let w = 0; w < THREADS; w++) { const worker = new Worker(new URL(import.meta.url), { workerData: { ctl, index: w, count: THREADS } }); worker.unref(); list.push(worker); }
21
26
  let nextId = 1;
22
27
  pool = {
@@ -33,6 +38,7 @@ function workers() {
33
38
  Atomics.store(ctl, 2, 0); Atomics.store(ctl, 1, id);
34
39
  Atomics.add(ctl, 0, 1); Atomics.notify(ctl, 0);
35
40
  while (Atomics.load(ctl, 2) < THREADS) Atomics.wait(ctl, 2, Atomics.load(ctl, 2), 50);
41
+ if (Atomics.load(ctl, 4)) { Atomics.store(ctl, 4, 0); throw new Error(`the threads backend was asked for a product on an operator its workers had let go (${KEPT} are kept): a solve reused too long after others`); }
36
42
  },
37
43
  };
38
44
  return pool;
@@ -65,7 +71,7 @@ export const threadsBackend = {
65
71
 
66
72
  if (!isMainThread && workerData?.ctl) {
67
73
  const { ctl, index, count } = workerData, ops = new Map();
68
- parentPort.on('message', (m) => { ops.set(m.id, m); Atomics.add(ctl, 3, 1); Atomics.notify(ctl, 3); });
74
+ parentPort.on('message', (m) => { ops.set(m.id, m); while (ops.size > KEPT) ops.delete(ops.keys().next().value); Atomics.add(ctl, 3, 1); Atomics.notify(ctl, 3); });
69
75
  // Products: wait for the generation to change, sweep this worker's share of the nodes, say so
70
76
  let seen = 0;
71
77
  const run = () => {
@@ -75,6 +81,7 @@ if (!isMainThread && workerData?.ctl) {
75
81
  seen = Atomics.load(ctl, 0);
76
82
  const id = Atomics.load(ctl, 1), op = ops.get(id);
77
83
  if (!id) { Atomics.add(ctl, 2, 1); Atomics.notify(ctl, 2); return setImmediate(run); } // woken to take a message
84
+ if (id && !op) Atomics.store(ctl, 4, 1);
78
85
  if (op) {
79
86
  const { dim, nodes, rows, cols, values: v, xs: x, ys: y } = op, from = Math.floor((nodes * index) / count), to = Math.floor((nodes * (index + 1)) / count);
80
87
  if (dim === 3) {