faster matrix multiplication
This commit is contained in:
parent
6c7b89c9eb
commit
5880b0ff2b
3 changed files with 1756 additions and 58 deletions
1715
package-lock.json
generated
1715
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"cheerio": "^1.0.0"
|
||||
"cheerio": "^1.0.0",
|
||||
"gpu.js": "^2.15.0"
|
||||
},
|
||||
"overrides": {
|
||||
"gl": "^8.1.6"
|
||||
}
|
||||
}
|
||||
}
|
91
rank.js
91
rank.js
|
@ -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
|
||||
function rankCalc(result, iterations = 10, main = [], domain_mode = false) {
|
||||
let matrixe = {}
|
||||
|
||||
let fng = {};
|
||||
let fnc = {};
|
||||
let frs = {};
|
||||
|
@ -9,6 +10,13 @@ function rankCalc(result, iterations = 10, main = [], domain_mode = false) {
|
|||
let pr = {};
|
||||
|
||||
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) {
|
||||
let v = true;
|
||||
if (domain_mode) {
|
||||
|
@ -21,13 +29,10 @@ function rankCalc(result, iterations = 10, main = [], domain_mode = false) {
|
|||
}
|
||||
}
|
||||
if (!v) {
|
||||
delete result[unn];
|
||||
continue;
|
||||
}
|
||||
|
||||
matrixe[unn] = {};
|
||||
matrixe[unn][unn] = 1;
|
||||
frs[unn] = result[unn].followers|| [];
|
||||
frs[unn] = result[unn].followers || [];
|
||||
fng[unn] = result[unn].following || [];
|
||||
|
||||
let lf = Object.keys(fng[unn]).length;
|
||||
|
@ -55,76 +60,52 @@ function rankCalc(result, iterations = 10, main = [], domain_mode = false) {
|
|||
|
||||
for (let unn in result) {
|
||||
let fnu = frs[unn];
|
||||
if (!pr[unn]) pr[unn] = 0;
|
||||
for (let follow of fnu) {
|
||||
if (follow == unn) continue;
|
||||
let dst = fnc[fnu] || 0;
|
||||
matrixe[unn][follow] = 1 + 1 / (dst + 3);
|
||||
msum_old += matrixe[unn][follow];
|
||||
}
|
||||
for (let unn2 in result) {
|
||||
if (!matrixe[unn][unn2]) {
|
||||
matrixe[unn][unn2] = 0;
|
||||
}
|
||||
let n = (keys.indexOf(unn) || 0) * (rl) + (keys.indexOf(follow) || 0) * 1;
|
||||
matrixe[n] = 1 + 5 / (dst + 3);
|
||||
msum_old += matrixe[n];
|
||||
}
|
||||
}
|
||||
|
||||
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++) {
|
||||
let prold = pr;
|
||||
let matrixf = matrixe;
|
||||
|
||||
pr = [];
|
||||
matrixe = [];
|
||||
|
||||
let msum = 1;
|
||||
let intv = Math.pow(0.01 / rl, Math.pow(0.09, i / mm));
|
||||
console.log(`Completed ${i} iterations with ${intv} threshold`)
|
||||
matrixe = multiplyMatrix(matrixe, matrixe, keys.length)[0];
|
||||
|
||||
let th = -1;
|
||||
for (let una in result) {
|
||||
th++;
|
||||
pr[una] = 0.1 / rl;
|
||||
matrixe[una] = [];
|
||||
if (frs[una].length == 0) {
|
||||
pr[una] = prold[una];
|
||||
matrixe[una] = matrixf[una];
|
||||
continue;
|
||||
}
|
||||
|
||||
let ar = Object.keys(result);
|
||||
let rf = pr[una];
|
||||
|
||||
for (let unb of ar) {
|
||||
let prb = prold[unb];
|
||||
|
||||
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 h in matrixe) {
|
||||
msum += matrixe[h];
|
||||
}
|
||||
for (let h in matrixe) {
|
||||
matrixe[h] /= msum / rl;
|
||||
}
|
||||
|
||||
for (let una in keys) {
|
||||
let una2 = keys[una];
|
||||
pr[una2] = 0.1 / rl;
|
||||
if ((frs[una2]).length == 0) continue;
|
||||
|
||||
for (let una in result) {
|
||||
if ((frs[una]).length == 0) continue;
|
||||
|
||||
for (let unb in result) {
|
||||
matrixe[una][unb] *= msum_old / msum;
|
||||
for (let unb in keys) {
|
||||
if (isNaN(prold[una2])) continue;
|
||||
pr[una2] += prold[una2] * matrixe[una * rl + unb * 1];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue