-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl-matmul-multi-output.js
210 lines (188 loc) · 7.16 KB
/
webgl-matmul-multi-output.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
function createMatMulProgram(gl, tileWidth, tileHeight, sharedDim, tileCount) {
const valueLines = [];
for(let i = 0; i < tileCount; ++i) {
valueLines.push(`TexelValue[${i}] += valueA * texelFetch(B, ivec2(x + ${tileWidth*i}, k), 0).r;`);
}
const fragmentShaderSource = `#version 300 es
precision highp float;
in vec2 TexCoord;
uniform sampler2D A;
uniform sampler2D B;
layout(location = 0) out vec4 TexelValue[${tileCount}];
void main()
{
int x = int(TexCoord.s * ${tileWidth}.0); // rescale
int y = int(TexCoord.t * ${tileHeight}.0); // rescale
for(int k=0; k < ${sharedDim}; ++k) {
float valueA = texelFetch(A, ivec2(k, y), 0).r;
${valueLines.join('\n')}
}
}`;
const program = createProgram(gl, getDefaultVertexShader(gl),
compileShader(gl, fragmentShaderSource, gl.FRAGMENT_SHADER));
return program;
}
function createCombineProgram(gl, width, height, tileCount) {
const tileWidth = Math.ceil(width / tileCount);
const valueLines = [];
for(let i=0; i < tileCount; ++i) {
valueLines.push(`
if(i == ${i}) {
TexelValue = texelFetch(Tiles[${i}], tileCoords, 0);
return;
}
`);
}
const fragmentShaderSource = `#version 300 es
precision highp float;
in vec2 TexCoord;
out vec4 TexelValue;
uniform sampler2D Tiles[${tileCount}];
void main()
{
int x = int(TexCoord.s * ${width}.0); // rescale
int y = int(TexCoord.t * ${height}.0); // rescale
int i = x / ${tileWidth};
ivec2 tileCoords = ivec2(x - i * ${tileWidth}, y);
${valueLines.join('\n')}
}`;
const program = createProgram(gl, getDefaultVertexShader(gl),
compileShader(gl, fragmentShaderSource, gl.FRAGMENT_SHADER));
return program;
}
async function runMatMul(gl, texA, texB, width, height, sharedDim, textures) {
const tileWidth = Math.ceil(width / textures.length);
const tileHeight = height;
const matmulKey = `matmul-multi-output-${width}-${height}-${textures.length}-${sharedDim}`;
let program = getProgram(matmulKey);
if(!program) {
program = createMatMulProgram(gl, tileWidth, height, sharedDim, textures.length);
cacheProgram(matmulKey, program);
}
const handleA = gl.getUniformLocation(program, 'A');
const handleB = gl.getUniformLocation(program, 'B');
gl.useProgram(program);
gl.viewport(0, 0, tileWidth, tileHeight);
const drawBuffers = [];
for (let i = 0; i < textures.length; ++i) {
gl.bindTexture(gl.TEXTURE_2D, textures[i]);
// attach texture to framebuffer
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i,
gl.TEXTURE_2D, textures[i], 0);
drawBuffers.push(gl.COLOR_ATTACHMENT0 + i);
}
gl.drawBuffers(drawBuffers);
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);
//checkError(gl); // make sure we have bound all input/output properly
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
await waitForSync(gl);
}
async function runCombine(gl, textures, width, height, texC) {
// Combining phase
const mergeKey = `Merge-${width}-${height}-${textures.length}`;
let mergeProgram = getProgram(mergeKey);
if(!mergeProgram) {
mergeProgram = createCombineProgram(gl, width, height, textures.length);
cacheProgram(mergeKey, mergeProgram);
}
gl.useProgram(mergeProgram);
gl.viewport(0, 0, width, height);
//gl.clearBufferiv(gl.COLOR, 0, drawBuffers);
createFrameBuffer(gl);
attachOutputTexture(gl, texC);
const uniformIndices = [];
for (let i = 0; i < textures.length; ++i) {
gl.activeTexture(gl.TEXTURE0 + i);
gl.bindTexture(gl.TEXTURE_2D, textures[i]);
uniformIndices.push(i);
}
gl.uniform1iv(gl.getUniformLocation(mergeProgram, 'Tiles[0]'), uniformIndices);
//checkError(gl); // make sure we have bound all input/output properly
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
await waitForSync(gl);
};
// 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
//
async 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];
console.info(`Running matmul-multi-output for [${shapeA.toString()}]-[${shapeB.toString()}]`)
const width = shapeB[1];
const height = shapeA[0];
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,3,4,5,6,6,5,4,3,2,1,1,3,5,2,4,6,2,4,6,1,3,5]);
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([6,4,2,1,3,5,1,3,5,2,4,6,1,2,3,3,2,1,6,5,4,4,5,6]);
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 = 5;
const compare = true;
const dops = [2,4,8];
for(let j = 0; j < dops.length; ++j) {
const tileWidth = Math.ceil(width / dops[j]);
const textures = [];
for (let i = 0; i < dops[j]; ++i) {
const tex = createTexture(gl, gl.R32F, gl.RED, gl.FLOAT, tileWidth, height, null);
textures.push(tex);
}
console.log(`Running with degree of parallelism: ${dops[j]}`);
for (let i = 0; i < count; ++i) {
console.time('matmul-multi');
await runMatMul(gl, texA, texB, width, height, sharedDim, textures);
console.timeEnd('matmul-multi');
console.time('combine');
await runCombine(gl, textures, width, height, texC);
console.timeEnd('combine');
console.time('readpixels');
readOutput(gl, width, height, gl.RED, gl.FLOAT, c);
console.timeEnd('readpixels');
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!')
}
}
}
for (let i = 0; i < dops[j]; ++i) {
gl.deleteTexture(textures[i]);
}
}
gl.deleteTexture(texA);
gl.deleteTexture(texB);
gl.deleteTexture(texC);
}
}
main();