-
Notifications
You must be signed in to change notification settings - Fork 0
/
webgl-matmul-window.js
147 lines (131 loc) · 4.99 KB
/
webgl-matmul-window.js
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
function createMatMulProgram(gl, width, height, sharedDim) {
const fragmentShaderSource = `#version 300 es
precision highp float;
in vec2 TexCoord;
out vec4 TexelValue;
// Texture samplers
uniform sampler2D A;
uniform sampler2D B;
uniform float width;
uniform float height;
uniform int startX;
uniform int startY;
void main()
{
float value = 0.0;
int x = startX + int(TexCoord.s * width); // rescale
int y = startY + int(TexCoord.t * height); // rescale
// loop over the shared dim
for(int k=0; k < ${sharedDim}; ++k) {
float a = texelFetch(A, ivec2(k, y), 0).r;
float b = texelFetch(B, ivec2(x, k), 0).r;
value += a * b;
}
TexelValue = vec4(value);
}`;
const program = createProgram(gl, getDefaultVertexShader(gl),
compileShader(gl, fragmentShaderSource, gl.FRAGMENT_SHADER));
return program;
}
function runMatMul(gl, program, texA, texB, width, height, texC, window) {
const handleA = gl.getUniformLocation(program, 'A');
const handleB = gl.getUniformLocation(program, 'B');
const handleW = gl.getUniformLocation(program, 'width');
const handleH = gl.getUniformLocation(program, 'height');
const handleX = gl.getUniformLocation(program, 'startX');
const handleY = gl.getUniformLocation(program, 'startY');
gl.useProgram(program);
attachOutputTexture(gl, texC);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texA);
gl.uniform1i(handleA, 0);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, texB);
gl.uniform1i(handleB, 1);
for(let i = 0; i < Math.ceil(width/window[0]); ++i) {
for(let j = 0; j < Math.ceil(height/window[1]); ++j) {
const startX = i * window[0];
const startY = j * window[1];
const w = Math.min(window[0], width - startX);
const h = Math.min(window[1], height - startY);
//console.info(`viewport: ${startX}, ${startY}, ${w}, ${h}`);
gl.uniform1f(handleW, w);
gl.uniform1f(handleH, h);
gl.uniform1i(handleX, startX);
gl.uniform1i(handleY, startY);
gl.viewport(startX, startY, w, h);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
gl.flush();
}
}
};
// CPU Equivalent for result comparison only
function cpuMatMul(a, shapeA, b, shapeB, c) {
let offset = 0;
for (let i = 0; i < shapeA[0]; i++) {
for (let j = 0; j < shapeB[1]; j++) {
let sum = 0;
for (let k = 0; k < shapeA[1]; k++) {
sum += a[i * shapeA[1] + k] * b[k * shapeB[1] + j];
}
c[offset] = sum;
offset++;
}
}
}
//
// Main
//
function main() {
const canvas = createCanvas(1, 1);
const gl = getContext(canvas);
setupVBO(gl);
createFrameBuffer(gl);
for(let dim0 = 200; dim0 < 2000; dim0 += 500) {
const sharedDim = dim0-100;
const shapeA = [dim0, sharedDim];
const shapeB = [sharedDim, dim0+100];
// const sharedDim = 3;
// const shapeA = [4,sharedDim];
// const shapeB = [sharedDim,4];
console.info(`Running matmul-window for [${shapeA.toString()}]-[${shapeB.toString()}]`);
const width = shapeB[1];
const height = shapeA[0];
const matmulProgram = createMatMulProgram(gl, width, height, sharedDim);
const a = new Float32Array(new Array(shapeA[0] * shapeA[1]).fill(0).map(v=>Math.floor(Math.random()*10)));
//const a = new Float32Array([1,2,2,1,1,2,1,2,3,4,2,3]);
const texA = createTexture(gl, gl.R32F, gl.RED, gl.FLOAT, shapeA[1], shapeA[0], a);
const b = new Float32Array(new Array(shapeB[0] * shapeB[1]).fill(0).map(v=>Math.floor(Math.random()*10)));
//const b = new Float32Array([1,2,3,3,2,1,3,2,2,3,2,1]);
const texB = createTexture(gl, gl.R32F, gl.RED, gl.FLOAT, shapeB[1], shapeB[0], b);
const c = new Float32Array(width * height);
const texC = createTexture(gl, gl.R32F, gl.RED, gl.FLOAT, width, height, null);
const count = 3;
const compare = true;
const windows = [[64,64], [128,128], [256,256]];
for(let k = 0; k < windows.length; ++k) {
console.info(`Running with window size: [${windows[k].toString()}]`);
for (let i = 0; i < count; ++i) {
console.time('matmul-window');
runMatMul(gl, matmulProgram, texA, texB, width, height, texC, windows[k]);
readOutput(gl, width, height, gl.RED, gl.FLOAT, c);
console.timeEnd('matmul-window');
if(i===0 && compare) {
const expected = new Float32Array(width * height);
cpuMatMul(a, shapeA, b, shapeB, expected);
if(!compareOutputs(c, expected, 0.1)) {
console.error('Expected and Actual did not match');
console.log(c);
console.log(expected)
} else {
console.info('Actual and expected matched!')
}
}
}
}
gl.deleteTexture(texA);
gl.deleteTexture(texB);
gl.deleteTexture(texC);
}
}
main();