-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwebgpuJacobiSolverScript.js
More file actions
156 lines (137 loc) · 5.05 KB
/
webgpuJacobiSolverScript.js
File metadata and controls
156 lines (137 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* ════════════════════════════════════════════════════════════════
* FEAScript Core Library
* Lightweight Finite Element Simulation in JavaScript
* Version: 0.2.0 | https://feascript.com
* MIT License © 2023–2026 FEAScript
* ════════════════════════════════════════════════════════════════
*/
// External imports
import * as ti from "../vendor/taichi.esm.js";
// Internal imports
import { debugLog, errorLog } from "../utilities/loggingScript.js";
/**
* Class to provide GPU-accelerated Jacobi solver using Taichi.js/WebGPU
* Offloads iterative linear algebra to the GPU for improved performance on large systems
*/
export class WebGPUComputeEngine {
/**
* Constructor to creates a WebGPUComputeEngine instance
* The engine remains uninitialized until initialize() is called
*/
constructor() {
this.initialized = false;
this.extractDiagonalKernel = null;
this.jacobiStepKernel = null;
this.swapSolutionKernel = null;
this.cachedSize = null;
this.fields = null;
}
/**
* Function to initialize the WebGPU compute engine
* @returns {Promise<void>} Resolves when Taichi.js has finished binding to WebGPU
*/
async initialize() {
if (this.initialized) {
return;
}
await ti.init();
this.initialized = true;
}
/**
* Function to solve a system of linear equations using the Jacobi iterative method (GPU asynchronous version)
* @param {number[][]} A - The system matrix (dense, n×n)
* @param {number[]} b - The right-hand side vector (length n)
* @param {number[]} x0 - Initial guess for solution vector (length n)
* @param {number} maxIter - Maximum number of iterations
* @param {number} tol - Convergence tolerance for the residual norm
* @returns {Promise<object>} Result object containing the solution, iteration count, and convergence flag
*/
async webgpuJacobiSolver(A, b, x0, maxIter, tol) {
await this.initialize();
const n = b.length;
const flatA = A.flat();
if (!this.fields || this.cachedSize !== n) {
this.fields = {
AField: ti.field(ti.f32, [n * n]),
bField: ti.field(ti.f32, [n]),
xField: ti.field(ti.f32, [n]),
xNewField: ti.field(ti.f32, [n]),
diagField: ti.field(ti.f32, [n]),
maxResidualField: ti.field(ti.f32, [1]),
};
this.cachedSize = n;
}
const { AField, bField, xField, xNewField, diagField, maxResidualField } = this.fields;
AField.fromArray(flatA);
bField.fromArray(b);
xField.fromArray(x0);
xNewField.fromArray(x0);
ti.addToKernelScope({ AField, bField, xField, xNewField, diagField, maxResidualField });
if (!this.extractDiagonalKernel) {
this.extractDiagonalKernel = ti.kernel((size) => {
for (let i of ti.ndrange(size)) {
diagField[i] = AField[ti.i32(i) * ti.i32(size) + ti.i32(i)];
}
});
this.jacobiStepKernel = ti.kernel((size) => {
maxResidualField[0] = 0.0;
for (let i of ti.ndrange(size)) {
let sum = 0.0;
for (let j of ti.ndrange(size)) {
sum += AField[ti.i32(i) * ti.i32(size) + ti.i32(j)] * xField[j];
}
const residual = bField[i] - sum;
xNewField[i] = xField[i] + residual / diagField[i];
ti.atomicMax(maxResidualField[0], ti.abs(residual));
}
});
this.swapSolutionKernel = ti.kernel((size) => {
for (let i of ti.ndrange(size)) {
xField[i] = xNewField[i];
}
});
}
this.extractDiagonalKernel(n);
const residualCheckInterval = Math.max(1, Math.min(10, Math.floor(maxIter / 4) || 1));
let iterations = maxIter;
let converged = false;
for (let iter = 0; iter < maxIter; iter++) {
this.jacobiStepKernel(n);
this.swapSolutionKernel(n);
const shouldCheckResidual = (iter + 1) % residualCheckInterval === 0 || iter === maxIter - 1;
if (!shouldCheckResidual) {
continue;
}
const rnorm = (await maxResidualField.toArray())[0];
iterations = iter + 1;
debugLog(`Jacobi: Iteration ${iterations}, residual norm: ${rnorm}`);
if (rnorm < tol) {
converged = true;
break;
}
}
if (!converged) {
errorLog(`Jacobi: Did not converge in ${maxIter} iterations`);
}
return { solutionVector: await xField.toArray(), iterations, converged };
}
/**
* Function to destroy the compute engine and clean up resources
* @returns {Promise<void>} Resolves when GPU resources have been released
*/
async destroy() {
if (!this.initialized) {
return;
}
if (typeof ti.destroy === "function") {
await ti.destroy();
}
this.initialized = false;
this.extractDiagonalKernel = null;
this.jacobiStepKernel = null;
this.swapSolutionKernel = null;
this.cachedSize = null;
this.fields = null;
}
}