Skip to content

Commit 6e06a5a

Browse files
committed
feat(directConvolution): add optional output argument
Allows to reuse an output array to do many convolutions of the same size.
1 parent e42523e commit 6e06a5a

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,41 @@ Current results suggest that from a kernel size of 512, fft convolution should b
3131

3232
| Data x Kernel | fft [ops/s] | direct [ops/s] |
3333
| ------------- | --- | ------ |
34-
| 128 x 4 | 17625 | 729087 |
3534
| 128 x 16 | 17640 | 256329 |
3635
| 128 x 32 | 17590 | 138211 |
3736
| 128 x 128 | 17295 | 35486 |
3837
| 128 x 512 | 4515 | 9308 |
3938
| 128 x 1024 | 2157 | 4695 |
40-
| 512 x 4 | 4539 | 191679 |
4139
| 512 x 16 | 4527 | 66017 |
4240
| 512 x 32 | 4556 | 35356 |
4341
| 512 x 128 | 3848 | 8134 |
4442
| 512 x 512 | 3217 | 2311 |
4543
| 512 x 1024 | 2079 | 1072 |
46-
| 2048 x 4 | 703 | 39038 |
4744
| 2048 x 16 | 681 | 15864 |
4845
| 2048 x 32 | 977 | 8749 |
4946
| 2048 x 128 | 1098 | 2164 |
5047
| 2048 x 512 | 1089 | 591 |
5148
| 2048 x 1024 | 1088 | 298 |
52-
| 4096 x 4 | 507 | 23076 |
5349
| 4096 x 16 | 473 | 8082 |
5450
| 4096 x 32 | 404 | 4272 |
5551
| 4096 x 128 | 489 | 1128 |
5652
| 4096 x 512 | 510 | 296 |
5753
| 4096 x 1024 | 506 | 149 |
58-
| 16384 x 4 | 81 | 6190 |
5954
| 16384 x 16 | 80 | 2112 |
6055
| 16384 x 32 | 72 | 1107 |
6156
| 16384 x 128 | 78 | 282 |
6257
| 16384 x 512 | 79 | 74 |
6358
| 16384 x 1024 | 77 | 37 |
64-
| 65536 x 4 | 18 | 1257 |
6559
| 65536 x 16 | 18 | 491 |
6660
| 65536 x 32 | 18 | 270 |
6761
| 65536 x 128 | 18 | 70 |
6862
| 65536 x 512 | 17 | 18 |
6963
| 65536 x 1024 | 19 | 9 |
70-
| 262144 x 4 | 3 | 327 |
7164
| 262144 x 16 | 4 | 125 |
7265
| 262144 x 32 | 4 | 67 |
7366
| 262144 x 128 | 4 | 17 |
7467
| 262144 x 512 | 4 | 5 |
7568
| 262144 x 1024 | 4 | 2 |
76-
| 1048576 x 4 | 1 | 62 |
7769
| 1048576 x 16 | 1 | 31 |
7870
| 1048576 x 32 | 1 | 15 |
7971
| 1048576 x 128 | 1 | 4 |

benchmark/convolution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77

88
const tests = {
99
data: [128, 512, 2048, 4096, 16384, 65536, 262144, 1048576],
10-
kernel: [4, 16, 32, 128, 512, 1024]
10+
kernel: [16, 32, 128, 512, 1024]
1111
};
1212

1313
function test(dataLength, kernelLength) {

src/__tests__/directConvolution.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,11 @@ describe('direct convolution', () => {
1111
expect(directConvolution([0, 1, 2, 3], [1, 1, 1])).toEqual([0, 1, 3, 6, 5, 3]);
1212
expect(directConvolution([0, 1, 2, 3], [-1, 1, -1])).toEqual([0, -1, -1, -2, 1, -3]);
1313
});
14+
15+
it('with existing output array', () => {
16+
const output = new Array(6);
17+
const result = directConvolution([0, 1, 2, 3], [-1, 1, -1], output);
18+
expect(result).toBe(output);
19+
expect(output).toEqual([0, -1, -1, -2, 1, -3]);
20+
});
1421
});

src/directConvolution.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
export default function directConvolution(input, kernel) {
2-
const length = input.length + kernel.length - 1;
3-
const output = new Array(length);
4-
output.fill(0);
1+
export default function directConvolution(input, kernel, output) {
2+
if (output === undefined) {
3+
const length = input.length + kernel.length - 1;
4+
output = new Array(length);
5+
}
6+
fill(output);
57
for (var i = 0; i < input.length; i++) {
68
for (var j = 0; j < kernel.length; j++) {
79
output[i + j] += input[i] * kernel[j];
810
}
911
}
1012
return output;
1113
}
14+
15+
function fill(array) {
16+
for (var i = 0; i < array.length; i++) {
17+
array[i] = 0;
18+
}
19+
}

0 commit comments

Comments
 (0)