faster matrix multiplication

This commit is contained in:
biglyderv 2025-02-01 08:40:06 -05:00
parent 6c7b89c9eb
commit 5880b0ff2b
3 changed files with 1756 additions and 58 deletions

1715
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,10 @@
{ {
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"cheerio": "^1.0.0" "cheerio": "^1.0.0",
"gpu.js": "^2.15.0"
},
"overrides": {
"gl": "^8.1.6"
} }
} }

89
rank.js
View file

@ -1,7 +1,8 @@
import { GPU, input, Input } from "gpu.js";
// derived from https://git.dervland.net/biglyderv/new-bigly-chat/src/branch/master/docs/stats.php // derived from https://git.dervland.net/biglyderv/new-bigly-chat/src/branch/master/docs/stats.php
function rankCalc(result, iterations = 10, main = [], domain_mode = false) { function rankCalc(result, iterations = 10, main = [], domain_mode = false) {
let matrixe = {}
let fng = {}; let fng = {};
let fnc = {}; let fnc = {};
let frs = {}; let frs = {};
@ -9,6 +10,13 @@ function rankCalc(result, iterations = 10, main = [], domain_mode = false) {
let pr = {}; let pr = {};
let rl = Object.keys(result).length; let rl = Object.keys(result).length;
let matrixe = new Float32Array(rl ** 2);
for (let i = 0; i < rl ** 2; i += (rl + 1)) {
matrixe[i] = 1;
}
let keys = Object.keys(result);
for (let unn in result) { for (let unn in result) {
let v = true; let v = true;
if (domain_mode) { if (domain_mode) {
@ -21,12 +29,9 @@ function rankCalc(result, iterations = 10, main = [], domain_mode = false) {
} }
} }
if (!v) { if (!v) {
delete result[unn];
continue; continue;
} }
matrixe[unn] = {};
matrixe[unn][unn] = 1;
frs[unn] = result[unn].followers || []; frs[unn] = result[unn].followers || [];
fng[unn] = result[unn].following || []; fng[unn] = result[unn].following || [];
@ -55,76 +60,52 @@ function rankCalc(result, iterations = 10, main = [], domain_mode = false) {
for (let unn in result) { for (let unn in result) {
let fnu = frs[unn]; let fnu = frs[unn];
if (!pr[unn]) pr[unn] = 0;
for (let follow of fnu) { for (let follow of fnu) {
if (follow == unn) continue; if (follow == unn) continue;
let dst = fnc[fnu] || 0; let dst = fnc[fnu] || 0;
matrixe[unn][follow] = 1 + 1 / (dst + 3); let n = (keys.indexOf(unn) || 0) * (rl) + (keys.indexOf(follow) || 0) * 1;
msum_old += matrixe[unn][follow]; matrixe[n] = 1 + 5 / (dst + 3);
} msum_old += matrixe[n];
for (let unn2 in result) {
if (!matrixe[unn][unn2]) {
matrixe[unn][unn2] = 0;
}
} }
} }
let mm = (process.env.matrixIterations || iterations); let mm = (process.env.matrixIterations || iterations);
let gpu = new GPU();
const multiplyMatrix = gpu.createKernel(function (a, b, c) {
let sum = 0;
for (let i = 0; i < c; i++) {
sum += a[this.thread.x / c][i] * b[i][this.thread.x % c];
}
return sum;
}).setOutput([keys.length ** 2,1]);
for (let i = 0; i < mm; i++) { for (let i = 0; i < mm; i++) {
let prold = pr; let prold = pr;
let matrixf = matrixe;
pr = []; pr = [];
matrixe = [];
let msum = 1; let msum = 1;
let intv = Math.pow(0.01 / rl, Math.pow(0.09, i / mm)); let intv = Math.pow(0.01 / rl, Math.pow(0.09, i / mm));
console.log(`Completed ${i} iterations with ${intv} threshold`) console.log(`Completed ${i} iterations with ${intv} threshold`)
matrixe = multiplyMatrix(matrixe, matrixe, keys.length)[0];
let th = -1; for (let h in matrixe) {
for (let una in result) { msum += matrixe[h];
th++; }
pr[una] = 0.1 / rl; for (let h in matrixe) {
matrixe[una] = []; matrixe[h] /= msum / rl;
if (frs[una].length == 0) {
pr[una] = prold[una];
matrixe[una] = matrixf[una];
continue;
} }
let ar = Object.keys(result); for (let una in keys) {
let rf = pr[una]; let una2 = keys[una];
pr[una2] = 0.1 / rl;
if ((frs[una2]).length == 0) continue;
for (let unb of ar) { for (let unb in keys) {
let prb = prold[unb]; if (isNaN(prold[una2])) continue;
pr[una2] += prold[una2] * matrixe[una * rl + unb * 1];
matrixe[una][unb] = 0.03;
if (prb * matrixf[una][unb] < intv || fnc[unb] == 0) {
let mfb = matrixf[una][unb];
if (isNaN(mfb) || !mfb) continue;
pr[una] += prb * mfb;
continue;
}
for (let unc in result) {
let mfc = matrixf[una][unc];
let mfb = matrixf[unc][unb];
if (isNaN(mfc) || isNaN(mfb) || !mfc || !mfb) continue;
matrixe[una][unb] += mfc * mfb;
}
msum += matrixe[una][unb];
pr[una] += prb * matrixe[una][unb];
}
}
for (let una in result) {
if ((frs[una]).length == 0) continue;
for (let unb in result) {
matrixe[una][unb] *= msum_old / msum;
} }
} }