@rulecad/fea 0.3.0 → 0.4.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/fea",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
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, and frames of beams (static, buckling and modes), in plain JavaScript.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -47,7 +47,7 @@
47
47
  "ground-structure"
48
48
  ],
49
49
  "engines": {
50
- "node": ">=20"
50
+ "node": ">=22.13"
51
51
  },
52
52
  "sideEffects": false,
53
53
  "repository": {
package/threads.mjs CHANGED
@@ -10,7 +10,8 @@ import { jsBackend, pattern, assemble } from './fea.mjs';
10
10
 
11
11
  // FEA_THREADS: a whole number of worker threads, 1 to 8; anything else is refused by name (2.5 was taken as 2.5)
12
12
  const asked = process.env.FEA_THREADS;
13
- if (asked != null && asked !== '' && !/^[1-8]$/.test(asked.trim())) throw new Error(`FEA_THREADS is "${asked}": give a whole number of threads from 1 to 8, or leave it unset`);
13
+ if (asked != null && asked !== '' && !/^[1-8]$/.test(asked.trim()))
14
+ throw new Error(`FEA_THREADS is "${asked}": give a whole number of threads from 1 to 8, or leave it unset`);
14
15
  const THREADS = asked ? Number(asked) : Math.max(1, Math.min(8, os.cpus().length - 1));
15
16
  // How many operators each worker keeps: an optimisation step registers one per multigrid level (four or so), and
16
17
  // their arrays are large (the bin's: 160 MB), so the workers let go of all but the last few. Before this, a run
@@ -24,8 +25,13 @@ function workers() {
24
25
  if (pool) return pool;
25
26
  // ctl: [0] the product generation, [1] the operator id, [2] workers done, [3] registrations acknowledged, [4] a
26
27
  // product asked of an operator a worker no longer holds
27
- const ctl = new Int32Array(new SharedArrayBuffer(20)), list = [];
28
- 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); }
28
+ const ctl = new Int32Array(new SharedArrayBuffer(20)),
29
+ list = [];
30
+ for (let w = 0; w < THREADS; w++) {
31
+ const worker = new Worker(new URL(import.meta.url), { workerData: { ctl, index: w, count: THREADS } });
32
+ worker.unref();
33
+ list.push(worker);
34
+ }
29
35
  let nextId = 1;
30
36
  pool = {
31
37
  register(arrays) {
@@ -33,15 +39,21 @@ function workers() {
33
39
  Atomics.store(ctl, 3, 0);
34
40
  for (const w of list) w.postMessage({ id, ...arrays });
35
41
  // Wake the workers to take the message (a product with no operator), then wait for every one to hold the arrays
36
- pool.multiply(0); Atomics.store(ctl, 4, 0);
42
+ pool.multiply(0);
43
+ Atomics.store(ctl, 4, 0);
37
44
  while (Atomics.load(ctl, 3) < THREADS) Atomics.wait(ctl, 3, Atomics.load(ctl, 3), 50);
38
45
  return id;
39
46
  },
40
47
  multiply(id) {
41
- Atomics.store(ctl, 2, 0); Atomics.store(ctl, 1, id);
42
- Atomics.add(ctl, 0, 1); Atomics.notify(ctl, 0);
48
+ Atomics.store(ctl, 2, 0);
49
+ Atomics.store(ctl, 1, id);
50
+ Atomics.add(ctl, 0, 1);
51
+ Atomics.notify(ctl, 0);
43
52
  while (Atomics.load(ctl, 2) < THREADS) Atomics.wait(ctl, 2, Atomics.load(ctl, 2), 50);
44
- if (Atomics.load(ctl, 4)) { Atomics.store(ctl, 4, 0); return false; } // (an operator the workers had let go: the caller registers it again)
53
+ if (Atomics.load(ctl, 4)) {
54
+ Atomics.store(ctl, 4, 0);
55
+ return false;
56
+ } // (an operator the workers had let go: the caller registers it again)
45
57
  return true;
46
58
  },
47
59
  };
@@ -51,38 +63,68 @@ function workers() {
51
63
  // On Node 20 and older the products came out wrong now and then (a few solves in sixty, more with more threads):
52
64
  // letting go of an operator's shared memory there corrupts work still running on it, and Node 20 is past its end of
53
65
  // life. There, this backend is plain JavaScript under another name: right, only slower. (Node 22 and later: threads)
54
- const NODE = Number(process.versions.node.split('.')[0]), SHARED_OK = NODE >= 22;
66
+ const NODE = Number(process.versions.node.split('.')[0]),
67
+ SHARED_OK = NODE >= 22;
55
68
 
56
- export const threadsBackend = !SHARED_OK ? { ...jsBackend, name: `js (threads need Node 22; this is Node ${NODE})` } : {
57
- name: `threads (${THREADS})`,
58
- precision: 64,
59
- operator(g, ke, cellMatrices) {
60
- const pat = pattern(g), { dim } = g, bb = dim * dim, n = g.nodes;
61
- const xs = new Float64Array(new SharedArrayBuffer(g.dofs * 8)), ys = new Float64Array(new SharedArrayBuffer(g.dofs * 8));
62
- let values = cellMatrices ? assemble(g, pat, cellMatrices, null, null) : new Float64Array(new SharedArrayBuffer(pat.cols.length * bb * 8));
63
- let assembledFor = cellMatrices ? undefined : {}, id = null;
64
- const ready = (scale) => {
65
- if (!cellMatrices && assembledFor !== scale) { assemble(g, pat, null, ke, scale, values); assembledFor = scale; }
66
- if (id === null) id = workers().register({ dim, nodes: n, rows: pat.rows, cols: pat.cols, values, xs, ys });
67
- };
68
- return {
69
- // (a solve's again() kept past several others finds its operator let go by the workers, who keep the last few:
70
- // registered again, from the arrays this side still holds, and the product taken)
71
- multiply(scale, x, y) { ready(scale); xs.set(x); if (!workers().multiply(id)) { id = workers().register({ dim, nodes: n, rows: pat.rows, cols: pat.cols, values, xs, ys }); workers().multiply(id); } y.set(ys); },
72
- diagonal(scale, d) {
73
- ready(scale);
74
- const { rows, self } = pat;
75
- for (let i = 0; i < n; i++) { const o = (rows[i] + self[i]) * bb; for (let q = 0; q < dim; q++) d[dim * i + q] = values[o + q * dim + q]; }
69
+ export const threadsBackend = !SHARED_OK
70
+ ? { ...jsBackend, name: `js (threads need Node 22; this is Node ${NODE})` }
71
+ : {
72
+ name: `threads (${THREADS})`,
73
+ precision: 64,
74
+ operator(g, ke, cellMatrices) {
75
+ const pat = pattern(g),
76
+ { dim } = g,
77
+ bb = dim * dim,
78
+ n = g.nodes;
79
+ const xs = new Float64Array(new SharedArrayBuffer(g.dofs * 8)),
80
+ ys = new Float64Array(new SharedArrayBuffer(g.dofs * 8));
81
+ let values = cellMatrices
82
+ ? assemble(g, pat, cellMatrices, null, null)
83
+ : new Float64Array(new SharedArrayBuffer(pat.cols.length * bb * 8));
84
+ let assembledFor = cellMatrices ? undefined : {},
85
+ id = null;
86
+ const ready = (scale) => {
87
+ if (!cellMatrices && assembledFor !== scale) {
88
+ assemble(g, pat, null, ke, scale, values);
89
+ assembledFor = scale;
90
+ }
91
+ if (id === null) id = workers().register({ dim, nodes: n, rows: pat.rows, cols: pat.cols, values, xs, ys });
92
+ };
93
+ return {
94
+ // (a solve's again() kept past several others finds its operator let go by the workers, who keep the last few:
95
+ // registered again, from the arrays this side still holds, and the product taken)
96
+ multiply(scale, x, y) {
97
+ ready(scale);
98
+ xs.set(x);
99
+ if (!workers().multiply(id)) {
100
+ id = workers().register({ dim, nodes: n, rows: pat.rows, cols: pat.cols, values, xs, ys });
101
+ workers().multiply(id);
102
+ }
103
+ y.set(ys);
104
+ },
105
+ diagonal(scale, d) {
106
+ ready(scale);
107
+ const { rows, self } = pat;
108
+ for (let i = 0; i < n; i++) {
109
+ const o = (rows[i] + self[i]) * bb;
110
+ for (let q = 0; q < dim; q++) d[dim * i + q] = values[o + q * dim + q];
111
+ }
112
+ },
113
+ };
76
114
  },
115
+ dot: jsBackend.dot,
116
+ axpy: jsBackend.axpy,
77
117
  };
78
- },
79
- dot: jsBackend.dot,
80
- axpy: jsBackend.axpy,
81
- };
82
118
 
83
119
  if (!isMainThread && workerData?.ctl) {
84
- const { ctl, index, count } = workerData, ops = new Map();
85
- 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); });
120
+ const { ctl, index, count } = workerData,
121
+ ops = new Map();
122
+ parentPort.on('message', (m) => {
123
+ ops.set(m.id, m);
124
+ while (ops.size > KEPT) ops.delete(ops.keys().next().value);
125
+ Atomics.add(ctl, 3, 1);
126
+ Atomics.notify(ctl, 3);
127
+ });
86
128
  // Products: wait for the generation to change, sweep this worker's share of the nodes, say so
87
129
  let seen = 0;
88
130
  const run = () => {
@@ -90,29 +132,56 @@ if (!isMainThread && workerData?.ctl) {
90
132
  const r = Atomics.wait(ctl, 0, seen, 1000); // (timed, so a message gets through now and then regardless)
91
133
  if (r === 'timed-out') return setImmediate(run);
92
134
  seen = Atomics.load(ctl, 0);
93
- const id = Atomics.load(ctl, 1), op = ops.get(id);
94
- if (!id) { Atomics.add(ctl, 2, 1); Atomics.notify(ctl, 2); return setImmediate(run); } // woken to take a message
135
+ const id = Atomics.load(ctl, 1),
136
+ op = ops.get(id);
137
+ if (!id) {
138
+ Atomics.add(ctl, 2, 1);
139
+ Atomics.notify(ctl, 2);
140
+ return setImmediate(run);
141
+ } // woken to take a message
95
142
  if (id && !op) Atomics.store(ctl, 4, 1);
96
143
  if (op) {
97
- 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);
144
+ const { dim, nodes, rows, cols, values: v, xs: x, ys: y } = op,
145
+ from = Math.floor((nodes * index) / count),
146
+ to = Math.floor((nodes * (index + 1)) / count);
98
147
  if (dim === 3) {
99
148
  for (let i = from; i < to; i++) {
100
- let y0 = 0, y1 = 0, y2 = 0;
149
+ let y0 = 0,
150
+ y1 = 0,
151
+ y2 = 0;
101
152
  for (let t = rows[i], end = rows[i + 1]; t < end; t++) {
102
- const j = cols[t] * 3, o = t * 9, x0 = x[j], x1 = x[j + 1], x2 = x[j + 2];
103
- y0 += v[o] * x0 + v[o + 1] * x1 + v[o + 2] * x2; y1 += v[o + 3] * x0 + v[o + 4] * x1 + v[o + 5] * x2; y2 += v[o + 6] * x0 + v[o + 7] * x1 + v[o + 8] * x2;
153
+ const j = cols[t] * 3,
154
+ o = t * 9,
155
+ x0 = x[j],
156
+ x1 = x[j + 1],
157
+ x2 = x[j + 2];
158
+ y0 += v[o] * x0 + v[o + 1] * x1 + v[o + 2] * x2;
159
+ y1 += v[o + 3] * x0 + v[o + 4] * x1 + v[o + 5] * x2;
160
+ y2 += v[o + 6] * x0 + v[o + 7] * x1 + v[o + 8] * x2;
104
161
  }
105
- y[3 * i] = y0; y[3 * i + 1] = y1; y[3 * i + 2] = y2;
162
+ y[3 * i] = y0;
163
+ y[3 * i + 1] = y1;
164
+ y[3 * i + 2] = y2;
106
165
  }
107
166
  } else {
108
167
  for (let i = from; i < to; i++) {
109
- let y0 = 0, y1 = 0;
110
- for (let t = rows[i], end = rows[i + 1]; t < end; t++) { const j = cols[t] * 2, o = t * 4, x0 = x[j], x1 = x[j + 1]; y0 += v[o] * x0 + v[o + 1] * x1; y1 += v[o + 2] * x0 + v[o + 3] * x1; }
111
- y[2 * i] = y0; y[2 * i + 1] = y1;
168
+ let y0 = 0,
169
+ y1 = 0;
170
+ for (let t = rows[i], end = rows[i + 1]; t < end; t++) {
171
+ const j = cols[t] * 2,
172
+ o = t * 4,
173
+ x0 = x[j],
174
+ x1 = x[j + 1];
175
+ y0 += v[o] * x0 + v[o + 1] * x1;
176
+ y1 += v[o + 2] * x0 + v[o + 3] * x1;
177
+ }
178
+ y[2 * i] = y0;
179
+ y[2 * i + 1] = y1;
112
180
  }
113
181
  }
114
182
  }
115
- Atomics.add(ctl, 2, 1); Atomics.notify(ctl, 2);
183
+ Atomics.add(ctl, 2, 1);
184
+ Atomics.notify(ctl, 2);
116
185
  }
117
186
  };
118
187
  run();