graphwise 1.8.0 → 1.9.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/dist/adjacency-map-BtKzcuJq.js +229 -0
- package/dist/adjacency-map-BtKzcuJq.js.map +1 -0
- package/dist/adjacency-map-JqBnMNkF.cjs +234 -0
- package/dist/adjacency-map-JqBnMNkF.cjs.map +1 -0
- package/dist/async/index.cjs +15 -242
- package/dist/async/index.js +2 -229
- package/dist/expansion/index.cjs +43 -0
- package/dist/expansion/index.js +2 -0
- package/dist/expansion-ClDhlMK8.js +1704 -0
- package/dist/expansion-ClDhlMK8.js.map +1 -0
- package/dist/expansion-DaTroIyv.cjs +1949 -0
- package/dist/expansion-DaTroIyv.cjs.map +1 -0
- package/dist/extraction/index.cjs +630 -0
- package/dist/extraction/index.cjs.map +1 -0
- package/dist/extraction/index.js +621 -0
- package/dist/extraction/index.js.map +1 -0
- package/dist/gpu/csr.d.ts +29 -30
- package/dist/gpu/csr.d.ts.map +1 -1
- package/dist/gpu/dispatch.d.ts +31 -0
- package/dist/gpu/dispatch.d.ts.map +1 -0
- package/dist/gpu/dispatch.unit.test.d.ts +5 -0
- package/dist/gpu/dispatch.unit.test.d.ts.map +1 -0
- package/dist/gpu/index.cjs +15 -410
- package/dist/gpu/index.d.ts +3 -1
- package/dist/gpu/index.d.ts.map +1 -1
- package/dist/gpu/index.js +2 -400
- package/dist/gpu/kernels/bfs/kernel.d.ts +59 -0
- package/dist/gpu/kernels/bfs/kernel.d.ts.map +1 -0
- package/dist/gpu/kernels/bfs/logic.d.ts +47 -0
- package/dist/gpu/kernels/bfs/logic.d.ts.map +1 -0
- package/dist/gpu/kernels/bfs/logic.unit.test.d.ts +2 -0
- package/dist/gpu/kernels/bfs/logic.unit.test.d.ts.map +1 -0
- package/dist/gpu/kernels/degree-histogram/kernel.d.ts +32 -0
- package/dist/gpu/kernels/degree-histogram/kernel.d.ts.map +1 -0
- package/dist/gpu/kernels/degree-histogram/logic.d.ts +45 -0
- package/dist/gpu/kernels/degree-histogram/logic.d.ts.map +1 -0
- package/dist/gpu/kernels/degree-histogram/logic.unit.test.d.ts +2 -0
- package/dist/gpu/kernels/degree-histogram/logic.unit.test.d.ts.map +1 -0
- package/dist/gpu/kernels/jaccard/kernel.d.ts +40 -0
- package/dist/gpu/kernels/jaccard/kernel.d.ts.map +1 -0
- package/dist/gpu/kernels/jaccard/logic.d.ts +43 -0
- package/dist/gpu/kernels/jaccard/logic.d.ts.map +1 -0
- package/dist/gpu/kernels/jaccard/logic.unit.test.d.ts +2 -0
- package/dist/gpu/kernels/jaccard/logic.unit.test.d.ts.map +1 -0
- package/dist/gpu/kernels/pagerank/kernel.d.ts +44 -0
- package/dist/gpu/kernels/pagerank/kernel.d.ts.map +1 -0
- package/dist/gpu/kernels/pagerank/logic.d.ts +50 -0
- package/dist/gpu/kernels/pagerank/logic.d.ts.map +1 -0
- package/dist/gpu/kernels/pagerank/logic.unit.test.d.ts +2 -0
- package/dist/gpu/kernels/pagerank/logic.unit.test.d.ts.map +1 -0
- package/dist/gpu/kernels/spmv/kernel.d.ts +43 -0
- package/dist/gpu/kernels/spmv/kernel.d.ts.map +1 -0
- package/dist/gpu/kernels/spmv/logic.d.ts +31 -0
- package/dist/gpu/kernels/spmv/logic.d.ts.map +1 -0
- package/dist/gpu/kernels/spmv/logic.unit.test.d.ts +2 -0
- package/dist/gpu/kernels/spmv/logic.unit.test.d.ts.map +1 -0
- package/dist/gpu/operations.d.ts +76 -0
- package/dist/gpu/operations.d.ts.map +1 -0
- package/dist/gpu/operations.unit.test.d.ts +5 -0
- package/dist/gpu/operations.unit.test.d.ts.map +1 -0
- package/dist/gpu/root.d.ts +53 -0
- package/dist/gpu/root.d.ts.map +1 -0
- package/dist/gpu/root.unit.test.d.ts +2 -0
- package/dist/gpu/root.unit.test.d.ts.map +1 -0
- package/dist/gpu/types.d.ts +3 -8
- package/dist/gpu/types.d.ts.map +1 -1
- package/dist/gpu-CHiCN0wa.js +16945 -0
- package/dist/gpu-CHiCN0wa.js.map +1 -0
- package/dist/gpu-Y6owRVMi.cjs +17028 -0
- package/dist/gpu-Y6owRVMi.cjs.map +1 -0
- package/dist/graph/index.cjs +2 -229
- package/dist/graph/index.js +1 -228
- package/dist/index/index.cjs +141 -4040
- package/dist/index/index.js +15 -3917
- package/dist/jaccard-3rCdilwm.js +39 -0
- package/dist/jaccard-3rCdilwm.js.map +1 -0
- package/dist/jaccard-Bys9_dGW.cjs +50 -0
- package/dist/jaccard-Bys9_dGW.cjs.map +1 -0
- package/dist/{kmeans-BIgSyGKu.cjs → kmeans-B8x9D1kt.cjs} +1 -1
- package/dist/{kmeans-BIgSyGKu.cjs.map → kmeans-B8x9D1kt.cjs.map} +1 -1
- package/dist/{kmeans-87ExSUNZ.js → kmeans-DKkL9rAN.js} +1 -1
- package/dist/{kmeans-87ExSUNZ.js.map → kmeans-DKkL9rAN.js.map} +1 -1
- package/dist/ops-djAsQQSh.cjs +277 -0
- package/dist/ops-djAsQQSh.cjs.map +1 -0
- package/dist/ops-upIi6JIi.js +212 -0
- package/dist/ops-upIi6JIi.js.map +1 -0
- package/dist/priority-queue-BIiD1L0k.cjs +148 -0
- package/dist/priority-queue-BIiD1L0k.cjs.map +1 -0
- package/dist/priority-queue-CFDd5cBg.js +143 -0
- package/dist/priority-queue-CFDd5cBg.js.map +1 -0
- package/dist/ranking/index.cjs +43 -0
- package/dist/ranking/index.js +4 -0
- package/dist/ranking/mi/index.cjs +581 -0
- package/dist/ranking/mi/index.cjs.map +1 -0
- package/dist/ranking/mi/index.js +555 -0
- package/dist/ranking/mi/index.js.map +1 -0
- package/dist/ranking-3ez5m67U.js +1016 -0
- package/dist/ranking-3ez5m67U.js.map +1 -0
- package/dist/ranking-DVvajgUZ.cjs +1093 -0
- package/dist/ranking-DVvajgUZ.cjs.map +1 -0
- package/dist/seeds/index.cjs +1 -1
- package/dist/seeds/index.js +1 -1
- package/dist/structures/index.cjs +2 -143
- package/dist/structures/index.js +1 -142
- package/dist/utils/index.cjs +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils-BodeE2Mo.js +22 -0
- package/dist/utils-BodeE2Mo.js.map +1 -0
- package/dist/utils-CDtCcsyF.cjs +33 -0
- package/dist/utils-CDtCcsyF.cjs.map +1 -0
- package/package.json +3 -1
- package/dist/async/index.cjs.map +0 -1
- package/dist/async/index.js.map +0 -1
- package/dist/gpu/context.d.ts +0 -118
- package/dist/gpu/context.d.ts.map +0 -1
- package/dist/gpu/context.unit.test.d.ts +0 -2
- package/dist/gpu/context.unit.test.d.ts.map +0 -1
- package/dist/gpu/index.cjs.map +0 -1
- package/dist/gpu/index.js.map +0 -1
- package/dist/graph/index.cjs.map +0 -1
- package/dist/graph/index.js.map +0 -1
- package/dist/index/index.cjs.map +0 -1
- package/dist/index/index.js.map +0 -1
- package/dist/structures/index.cjs.map +0 -1
- package/dist/structures/index.js.map +0 -1
package/dist/gpu/index.cjs
CHANGED
|
@@ -1,411 +1,16 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
* For undirected graphs, each edge is stored twice (once in each direction).
|
|
18
|
-
* For directed graphs, edges are stored in the out-direction by default.
|
|
19
|
-
*
|
|
20
|
-
* @param graph - The graph to convert
|
|
21
|
-
* @param direction - Edge direction to include (default: 'out' for directed, 'both' for undirected)
|
|
22
|
-
* @returns CSR representation with index mapping
|
|
23
|
-
*/
|
|
24
|
-
function graphToCSR(graph, direction = graph.directed ? "out" : "both") {
|
|
25
|
-
const nodeToIndex = /* @__PURE__ */ new Map();
|
|
26
|
-
const indexToNode = [];
|
|
27
|
-
for (const nodeId of graph.nodeIds()) {
|
|
28
|
-
const index = indexToNode.length;
|
|
29
|
-
nodeToIndex.set(nodeId, index);
|
|
30
|
-
indexToNode.push(nodeId);
|
|
31
|
-
}
|
|
32
|
-
const nodeCount = indexToNode.length;
|
|
33
|
-
const degrees = new Uint32Array(nodeCount);
|
|
34
|
-
for (const nodeId of graph.nodeIds()) {
|
|
35
|
-
const srcIndex = nodeToIndex.get(nodeId);
|
|
36
|
-
if (srcIndex === void 0) continue;
|
|
37
|
-
degrees[srcIndex] = graph.degree(nodeId, direction);
|
|
38
|
-
}
|
|
39
|
-
let totalEdges = 0;
|
|
40
|
-
for (let i = 0; i < nodeCount; i++) totalEdges += degrees[i] ?? 0;
|
|
41
|
-
const rowOffsets = new Uint32Array(nodeCount + 1);
|
|
42
|
-
for (let i = 0; i < nodeCount; i++) rowOffsets[i + 1] = (rowOffsets[i] ?? 0) + (degrees[i] ?? 0);
|
|
43
|
-
const colIndices = new Uint32Array(totalEdges);
|
|
44
|
-
const values = new Float32Array(totalEdges);
|
|
45
|
-
for (const nodeId of graph.nodeIds()) {
|
|
46
|
-
const srcIndex = nodeToIndex.get(nodeId);
|
|
47
|
-
if (srcIndex === void 0) continue;
|
|
48
|
-
const baseOffset = rowOffsets[srcIndex] ?? 0;
|
|
49
|
-
let localOffset = 0;
|
|
50
|
-
for (const neighbourId of graph.neighbours(nodeId, direction)) {
|
|
51
|
-
const dstIndex = nodeToIndex.get(neighbourId);
|
|
52
|
-
if (dstIndex === void 0) continue;
|
|
53
|
-
const edgeIdx = baseOffset + localOffset;
|
|
54
|
-
colIndices[edgeIdx] = dstIndex;
|
|
55
|
-
values[edgeIdx] = graph.getEdge(nodeId, neighbourId)?.weight ?? 1;
|
|
56
|
-
localOffset++;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return {
|
|
60
|
-
csr: {
|
|
61
|
-
rowOffsets,
|
|
62
|
-
colIndices,
|
|
63
|
-
values,
|
|
64
|
-
nodeCount,
|
|
65
|
-
edgeCount: graph.directed ? graph.edgeCount : graph.edgeCount * 2
|
|
66
|
-
},
|
|
67
|
-
indexMap: {
|
|
68
|
-
nodeToIndex,
|
|
69
|
-
indexToNode
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Create GPU buffers from a CSR matrix.
|
|
75
|
-
*
|
|
76
|
-
* Buffers are created with:
|
|
77
|
-
* - rowOffsets/colIndices: STORAGE | COPY_DST
|
|
78
|
-
* - values: STORAGE | COPY_DST (if present)
|
|
79
|
-
*
|
|
80
|
-
* @param device - GPU device to create buffers on
|
|
81
|
-
* @param csr - CSR matrix to upload
|
|
82
|
-
* @returns GPU buffer group
|
|
83
|
-
*/
|
|
84
|
-
function csrToGPUBuffers(device, csr) {
|
|
85
|
-
const rowOffsetsBuffer = device.createBuffer({
|
|
86
|
-
size: csr.rowOffsets.byteLength,
|
|
87
|
-
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
88
|
-
mappedAtCreation: false
|
|
89
|
-
});
|
|
90
|
-
device.queue.writeBuffer(rowOffsetsBuffer, 0, csr.rowOffsets);
|
|
91
|
-
const colIndicesBuffer = device.createBuffer({
|
|
92
|
-
size: csr.colIndices.byteLength,
|
|
93
|
-
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
94
|
-
mappedAtCreation: false
|
|
95
|
-
});
|
|
96
|
-
device.queue.writeBuffer(colIndicesBuffer, 0, csr.colIndices);
|
|
97
|
-
let valuesBuffer;
|
|
98
|
-
if (csr.values !== void 0) {
|
|
99
|
-
valuesBuffer = device.createBuffer({
|
|
100
|
-
size: csr.values.byteLength,
|
|
101
|
-
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
102
|
-
mappedAtCreation: false
|
|
103
|
-
});
|
|
104
|
-
device.queue.writeBuffer(valuesBuffer, 0, csr.values);
|
|
105
|
-
}
|
|
106
|
-
if (valuesBuffer !== void 0) return {
|
|
107
|
-
rowOffsets: rowOffsetsBuffer,
|
|
108
|
-
colIndices: colIndicesBuffer,
|
|
109
|
-
values: valuesBuffer,
|
|
110
|
-
nodeCount: csr.nodeCount,
|
|
111
|
-
edgeCount: csr.edgeCount
|
|
112
|
-
};
|
|
113
|
-
return {
|
|
114
|
-
rowOffsets: rowOffsetsBuffer,
|
|
115
|
-
colIndices: colIndicesBuffer,
|
|
116
|
-
nodeCount: csr.nodeCount,
|
|
117
|
-
edgeCount: csr.edgeCount
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Create a result buffer for reading compute output.
|
|
122
|
-
*
|
|
123
|
-
* @param device - GPU device
|
|
124
|
-
* @param byteLength - Size of the buffer in bytes
|
|
125
|
-
* @returns GPU buffer configured for map reading
|
|
126
|
-
*/
|
|
127
|
-
function createResultBuffer(device, byteLength) {
|
|
128
|
-
return device.createBuffer({
|
|
129
|
-
size: byteLength,
|
|
130
|
-
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.MAP_READ
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Read data from a GPU buffer to CPU.
|
|
135
|
-
*
|
|
136
|
-
* @param device - GPU device
|
|
137
|
-
* @param buffer - Buffer to read from
|
|
138
|
-
* @returns ArrayBuffer containing the buffer data
|
|
139
|
-
*/
|
|
140
|
-
async function readBufferToCPU(device, buffer) {
|
|
141
|
-
await buffer.mapAsync(GPUMapMode.READ);
|
|
142
|
-
const data = buffer.getMappedRange().slice(0);
|
|
143
|
-
buffer.unmap();
|
|
144
|
-
return data;
|
|
145
|
-
}
|
|
146
|
-
//#endregion
|
|
147
|
-
//#region src/gpu/detect.ts
|
|
148
|
-
/**
|
|
149
|
-
* Detect WebGPU availability in the current environment.
|
|
150
|
-
*
|
|
151
|
-
* Checks for:
|
|
152
|
-
* - Browser: navigator.gpu
|
|
153
|
-
* - Node.js: global GPU constructor (Node.js 21+ with --experimental-webgpu)
|
|
154
|
-
* - Deno: global GPU constructor
|
|
155
|
-
*
|
|
156
|
-
* @returns Detection result with availability status and reason
|
|
157
|
-
*/
|
|
158
|
-
function detectWebGPU() {
|
|
159
|
-
if (typeof navigator !== "undefined" && "gpu" in navigator) return { available: true };
|
|
160
|
-
if (typeof globalThis !== "undefined" && "GPU" in globalThis) return { available: true };
|
|
161
|
-
const reasons = [];
|
|
162
|
-
if (typeof navigator === "undefined" && typeof globalThis === "undefined") reasons.push("no global scope detected");
|
|
163
|
-
else if (typeof navigator !== "undefined" && !("gpu" in navigator)) reasons.push("navigator.gpu not present (browser may not support WebGPU)");
|
|
164
|
-
else if (typeof globalThis !== "undefined" && !("GPU" in globalThis)) reasons.push("global GPU not present (Node.js requires v21+ with --experimental-webgpu flag)");
|
|
165
|
-
return {
|
|
166
|
-
available: false,
|
|
167
|
-
reason: reasons.length > 0 ? reasons.join("; ") : "unknown environment"
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Check if WebGPU is available (convenience function).
|
|
172
|
-
*
|
|
173
|
-
* @returns true if WebGPU is available, false otherwise
|
|
174
|
-
*/
|
|
175
|
-
function isWebGPUAvailable() {
|
|
176
|
-
return detectWebGPU().available;
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Assert that WebGPU is available, throwing an error if not.
|
|
180
|
-
*
|
|
181
|
-
* @throws Error if WebGPU is not available
|
|
182
|
-
*/
|
|
183
|
-
function assertWebGPUAvailable() {
|
|
184
|
-
const result = detectWebGPU();
|
|
185
|
-
if (!result.available) throw new Error(`WebGPU required but not available: ${result.reason ?? "unknown reason"}`);
|
|
186
|
-
}
|
|
187
|
-
//#endregion
|
|
188
|
-
//#region src/gpu/context.ts
|
|
189
|
-
/**
|
|
190
|
-
* GPU context management for WebGPU compute operations.
|
|
191
|
-
*
|
|
192
|
-
* Provides device acquisition, buffer pooling, and shader compilation
|
|
193
|
-
* with caching for efficient GPU resource management.
|
|
194
|
-
*/
|
|
195
|
-
/**
|
|
196
|
-
* Type guard to check if an object has a WebGPU-compatible requestAdapter method.
|
|
197
|
-
*/
|
|
198
|
-
function hasRequestAdapter(obj) {
|
|
199
|
-
if (typeof obj !== "object" || obj === null) return false;
|
|
200
|
-
if (!("requestAdapter" in obj)) return false;
|
|
201
|
-
const descriptor = Object.getOwnPropertyDescriptor(obj, "requestAdapter");
|
|
202
|
-
return descriptor !== void 0 && typeof descriptor.value === "function";
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Manages GPU device, buffers, and compiled shaders.
|
|
206
|
-
*
|
|
207
|
-
* Use the singleton pattern via getGPUContext() for most use cases,
|
|
208
|
-
* or create separate contexts for isolated GPU resource pools.
|
|
209
|
-
*/
|
|
210
|
-
var GPUContext = class {
|
|
211
|
-
device = null;
|
|
212
|
-
shaderCache = /* @__PURE__ */ new Map();
|
|
213
|
-
bufferPool = [];
|
|
214
|
-
destroyedBuffers = /* @__PURE__ */ new WeakSet();
|
|
215
|
-
/**
|
|
216
|
-
* Check if this context has an acquired GPU device.
|
|
217
|
-
*/
|
|
218
|
-
get isReady() {
|
|
219
|
-
return this.device !== null;
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Get the GPU device, throwing if not acquired.
|
|
223
|
-
*/
|
|
224
|
-
getDevice() {
|
|
225
|
-
if (this.device === null) throw new Error("GPUContext not initialised. Call acquireDevice() first.");
|
|
226
|
-
return this.device;
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Acquire a GPU device from the adapter.
|
|
230
|
-
*
|
|
231
|
-
* @param options - Context creation options
|
|
232
|
-
* @returns true if device was acquired successfully
|
|
233
|
-
* @throws Error if WebGPU is unavailable or device request fails
|
|
234
|
-
*/
|
|
235
|
-
async acquireDevice(options = {}) {
|
|
236
|
-
const detection = detectWebGPU();
|
|
237
|
-
if (!detection.available) throw new Error(`WebGPU unavailable: ${detection.reason ?? "unknown reason"}`);
|
|
238
|
-
let adapter = null;
|
|
239
|
-
const adapterOpts = options.powerPreference !== void 0 ? { powerPreference: options.powerPreference } : {};
|
|
240
|
-
if (typeof navigator !== "undefined" && "gpu" in navigator) adapter = await navigator.gpu.requestAdapter(adapterOpts);
|
|
241
|
-
if (adapter === null && typeof globalThis !== "undefined" && "GPU" in globalThis) {
|
|
242
|
-
const gpuDescriptor = Object.getOwnPropertyDescriptor(globalThis, "gpu");
|
|
243
|
-
if (gpuDescriptor !== void 0 && hasRequestAdapter(gpuDescriptor.value)) adapter = await gpuDescriptor.value.requestAdapter(adapterOpts);
|
|
244
|
-
}
|
|
245
|
-
if (adapter === null) throw new Error("No GPU adapter found");
|
|
246
|
-
this.device = await adapter.requestDevice();
|
|
247
|
-
this.device.lost.then((info) => {
|
|
248
|
-
console.error(`GPU device lost: ${info.message}`);
|
|
249
|
-
this.device = null;
|
|
250
|
-
this.clearCache();
|
|
251
|
-
});
|
|
252
|
-
return true;
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Compile a WGSL shader, using cache if available.
|
|
256
|
-
*
|
|
257
|
-
* @param code - WGSL shader code
|
|
258
|
-
* @param key - Optional cache key (defaults to code hash)
|
|
259
|
-
* @returns Compiled shader module
|
|
260
|
-
*/
|
|
261
|
-
compileShader(code, key) {
|
|
262
|
-
const cacheKey = key ?? this.hashCode(code);
|
|
263
|
-
const cached = this.shaderCache.get(cacheKey);
|
|
264
|
-
if (cached?.code === code) return cached.module;
|
|
265
|
-
const module = this.getDevice().createShaderModule({ code });
|
|
266
|
-
this.shaderCache.set(cacheKey, {
|
|
267
|
-
module,
|
|
268
|
-
code
|
|
269
|
-
});
|
|
270
|
-
return module;
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Create a GPU buffer, optionally reusing from pool.
|
|
274
|
-
*
|
|
275
|
-
* @param size - Buffer size in bytes
|
|
276
|
-
* @param usage - Buffer usage flags
|
|
277
|
-
* @returns GPU buffer
|
|
278
|
-
*/
|
|
279
|
-
createBuffer(size, usage) {
|
|
280
|
-
for (let i = 0; i < this.bufferPool.length; i++) {
|
|
281
|
-
const buffer = this.bufferPool[i];
|
|
282
|
-
if (buffer !== void 0 && !this.destroyedBuffers.has(buffer) && buffer.size >= size && (buffer.usage & usage) === usage) {
|
|
283
|
-
this.bufferPool.splice(i, 1);
|
|
284
|
-
return buffer;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
return this.getDevice().createBuffer({
|
|
288
|
-
size,
|
|
289
|
-
usage
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* Return a buffer to the pool for reuse.
|
|
294
|
-
*
|
|
295
|
-
* @param buffer - Buffer to recycle
|
|
296
|
-
*/
|
|
297
|
-
recycleBuffer(buffer) {
|
|
298
|
-
if (!this.destroyedBuffers.has(buffer)) this.bufferPool.push(buffer);
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* Mark a buffer as destroyed (call before GPUBuffer.destroy()).
|
|
302
|
-
*
|
|
303
|
-
* @param buffer - Buffer to mark as destroyed
|
|
304
|
-
*/
|
|
305
|
-
markDestroyed(buffer) {
|
|
306
|
-
this.destroyedBuffers.add(buffer);
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Create a compute pipeline from shader code.
|
|
310
|
-
*
|
|
311
|
-
* @param code - WGSL compute shader code
|
|
312
|
-
* @param entryPoint - Entry point function name
|
|
313
|
-
* @returns Compute pipeline
|
|
314
|
-
*/
|
|
315
|
-
createComputePipeline(code, entryPoint = "main") {
|
|
316
|
-
const device = this.getDevice();
|
|
317
|
-
const module = this.compileShader(code);
|
|
318
|
-
return device.createComputePipeline({
|
|
319
|
-
layout: "auto",
|
|
320
|
-
compute: {
|
|
321
|
-
module,
|
|
322
|
-
entryPoint
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
/**
|
|
327
|
-
* Create a compute pipeline asynchronously (preferred for performance).
|
|
328
|
-
*
|
|
329
|
-
* @param code - WGSL compute shader code
|
|
330
|
-
* @param entryPoint - Entry point function name
|
|
331
|
-
* @returns Promise resolving to compute pipeline
|
|
332
|
-
*/
|
|
333
|
-
async createComputePipelineAsync(code, entryPoint = "main") {
|
|
334
|
-
const device = this.getDevice();
|
|
335
|
-
const module = this.compileShader(code);
|
|
336
|
-
return device.createComputePipelineAsync({
|
|
337
|
-
layout: "auto",
|
|
338
|
-
compute: {
|
|
339
|
-
module,
|
|
340
|
-
entryPoint
|
|
341
|
-
}
|
|
342
|
-
});
|
|
343
|
-
}
|
|
344
|
-
/**
|
|
345
|
-
* Clear all cached resources.
|
|
346
|
-
*/
|
|
347
|
-
clearCache() {
|
|
348
|
-
this.shaderCache.clear();
|
|
349
|
-
for (const buffer of this.bufferPool) buffer.destroy();
|
|
350
|
-
this.bufferPool.length = 0;
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* Destroy the context and release all resources.
|
|
354
|
-
*/
|
|
355
|
-
destroy() {
|
|
356
|
-
this.clearCache();
|
|
357
|
-
if (this.device !== null) {
|
|
358
|
-
this.device.destroy();
|
|
359
|
-
this.device = null;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
/**
|
|
363
|
-
* Simple string hash for cache keys.
|
|
364
|
-
*/
|
|
365
|
-
hashCode(str) {
|
|
366
|
-
let hash = 0;
|
|
367
|
-
for (let i = 0; i < str.length; i++) {
|
|
368
|
-
const char = str.charCodeAt(i);
|
|
369
|
-
hash = (hash << 5) - hash + char | 0;
|
|
370
|
-
}
|
|
371
|
-
return hash.toString(16);
|
|
372
|
-
}
|
|
373
|
-
};
|
|
374
|
-
var defaultContext = null;
|
|
375
|
-
/**
|
|
376
|
-
* Get or create the default GPU context.
|
|
377
|
-
*
|
|
378
|
-
* This is a lazy singleton - the device is not acquired until
|
|
379
|
-
* acquireDevice() is called.
|
|
380
|
-
*
|
|
381
|
-
* @param options - Context creation options
|
|
382
|
-
* @returns GPU context instance
|
|
383
|
-
*/
|
|
384
|
-
function getGPUContext(options) {
|
|
385
|
-
if (defaultContext === null || (options?.forceNew ?? false)) defaultContext = new GPUContext();
|
|
386
|
-
return defaultContext;
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* Create a new isolated GPU context.
|
|
390
|
-
*
|
|
391
|
-
* Use this when you need separate resource pools or device management.
|
|
392
|
-
*
|
|
393
|
-
* @returns New GPU context instance
|
|
394
|
-
*/
|
|
395
|
-
function createGPUContext() {
|
|
396
|
-
return new GPUContext();
|
|
397
|
-
}
|
|
398
|
-
//#endregion
|
|
399
|
-
exports.GPUContext = GPUContext;
|
|
400
|
-
exports.GPUNotAvailableError = GPUNotAvailableError;
|
|
401
|
-
exports.assertWebGPUAvailable = assertWebGPUAvailable;
|
|
402
|
-
exports.createGPUContext = createGPUContext;
|
|
403
|
-
exports.createResultBuffer = createResultBuffer;
|
|
404
|
-
exports.csrToGPUBuffers = csrToGPUBuffers;
|
|
405
|
-
exports.detectWebGPU = detectWebGPU;
|
|
406
|
-
exports.getGPUContext = getGPUContext;
|
|
407
|
-
exports.graphToCSR = graphToCSR;
|
|
408
|
-
exports.isWebGPUAvailable = isWebGPUAvailable;
|
|
409
|
-
exports.readBufferToCPU = readBufferToCPU;
|
|
410
|
-
|
|
411
|
-
//# sourceMappingURL=index.cjs.map
|
|
2
|
+
const require_gpu = require("../gpu-Y6owRVMi.cjs");
|
|
3
|
+
exports.GPUNotAvailableError = require_gpu.GPUNotAvailableError;
|
|
4
|
+
exports.assertWebGPUAvailable = require_gpu.assertWebGPUAvailable;
|
|
5
|
+
exports.csrToTypedBuffers = require_gpu.csrToTypedBuffers;
|
|
6
|
+
exports.detectWebGPU = require_gpu.detectWebGPU;
|
|
7
|
+
exports.gpuBfsLevels = require_gpu.gpuBfsLevels;
|
|
8
|
+
exports.gpuDegreeHistogram = require_gpu.gpuDegreeHistogram;
|
|
9
|
+
exports.gpuJaccardBatch = require_gpu.gpuJaccardBatch;
|
|
10
|
+
exports.gpuPageRank = require_gpu.gpuPageRank;
|
|
11
|
+
exports.gpuSpmv = require_gpu.gpuSpmv;
|
|
12
|
+
exports.graphToCSR = require_gpu.graphToCSR;
|
|
13
|
+
exports.initGPU = require_gpu.initGPU;
|
|
14
|
+
exports.initGPUFromDevice = require_gpu.initGPUFromDevice;
|
|
15
|
+
exports.isWebGPUAvailable = require_gpu.isWebGPUAvailable;
|
|
16
|
+
exports.withBackend = require_gpu.withBackend;
|
package/dist/gpu/index.d.ts
CHANGED
package/dist/gpu/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/gpu/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/gpu/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC"}
|