/ proof-of-concept / plate-dynamics-lab
Sign in
proof-of-concept / plate-dynamics-lab
plate-dynamics-lab / packages / core / src / runner / plain.ts
49 lines · 1.6 KBCodeBlameHistory
2 * Plain, structured-clone-friendly mirror of the numbl values the lab uses.
3 * Tensors keep numbl's column-major layout: element (i, j) of an [m, n] array is
4 * data[i + j*m], and (i, j, k) of an [m, n, p] array is data[i + j*m + k*m*n].
5 */
6export interface PlainTensor {
7 kind: "tensor";
8 shape: number[];
9 data: Float64Array;
10 imag?: Float64Array;
12export interface PlainStruct {
13 kind: "struct";
14 fields: Record<string, PlainValue>;
16export interface PlainCell {
17 kind: "cell";
18 shape: number[];
19 items: PlainValue[];
21export interface PlainUnsupported {
22 kind: "unsupported";
23 description: string;
25export type PlainValue =
26 number | boolean | string | PlainTensor | PlainStruct | PlainCell | PlainUnsupported;
28export function isPlainTensor(v: PlainValue | undefined): v is PlainTensor {
29 return typeof v === "object" && v !== null && v.kind === "tensor";
31export function isPlainStruct(v: PlainValue | undefined): v is PlainStruct {
32 return typeof v === "object" && v !== null && v.kind === "struct";
34export function isPlainCell(v: PlainValue | undefined): v is PlainCell {
35 return typeof v === "object" && v !== null && v.kind === "cell";
38/** All ArrayBuffers inside a value, for zero-copy transfer out of a worker. */
39export function collectBuffers(v: PlainValue, out: ArrayBuffer[] = []): ArrayBuffer[] {
40 if (isPlainTensor(v)) {
41 out.push(v.data.buffer as ArrayBuffer);
42 if (v.imag) out.push(v.imag.buffer as ArrayBuffer);
43 } else if (isPlainStruct(v)) {
44 for (const f of Object.values(v.fields)) collectBuffers(f, out);
45 } else if (isPlainCell(v)) {
46 for (const item of v.items) collectBuffers(item, out);
47 }
48 return out;
↑↓ move↵openescclose