Skip to content

Commit 3fd9fb9

Browse files
Fix typos, edit example to better relate to readme, and provide link to example
1 parent a3c4048 commit 3fd9fb9

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ width=2,height=2,depth=3 width=4,height=4,depth=2
133133
```
134134

135135
## What does this log mean?
136+
If you prefer code to learn take a look at the [included example](example.js)
136137
1. Suppose we had a (contrived) block of CPU code we'd like to run on the GPU:
137138
```js
138139
const filters = [
@@ -227,7 +228,10 @@ width=2,height=2 width=4,height=4
227228
[ ][ ][*][*]
228229
```
229230

230-
3. Now we have enough logic to visibly see how to build out our algorythm that will work on the GPU. For `filters`@`x=0,y=0` we can see we need the values from `weights`@`x=0,y=0`,`x=1,y=0`,`x=0,y=1`, and `x=1,y=1`. Then to get `filters`@`x=1,y=0`, we seem to increment by two on `weights`. If we were to write a loop that emilates this behaviour, it'd look something like this:
231+
3. Now we have enough logic to visibly see how to build out our algorythm that will work on the GPU.
232+
For `filters`@`x=0,y=0` we can see we need the values from `weights`@`x=0,y=0`,`x=1,y=0`,`x=0,y=1`, and `x=1,y=1`.
233+
Then to get `filters`@`x=1,y=0`, we seem to increment by two on `weights`.
234+
If we were to write a loop that emulates this behaviour, it'd look something like this:
231235

232236
```js
233237
const filterHeight = 2;
@@ -257,7 +261,8 @@ for (let filterY = 0; filterY < filterHeight; filterY++) {
257261
console.log(filters); // -> [ [ 14, 22 ], [ 46, 54 ] ]
258262
```
259263

260-
4. On the GPU we are writing from a kernel, which acts like the `filters` loop already, so we can ommit that and pretend that the function will run in its own "fragment" (like iteration of the inner most loops for building the value). If that function was just simple Javascript that we imagined might work on a GPU kernel, it'd looks something like this:
264+
4. On the GPU we are writing from a kernel, which acts like the `filters` loop already, so we can omit that and pretend that the function will run in its own "fragment" (like iteration of the inner most loops for building the value).
265+
If that function was just simple Javascript that we imagined might work on a GPU kernel, it'd looks something like this:
261266

262267
```js
263268
function filterKernel(filters, filterX, filterY, filterWidth, filterHeight, weights) {

example.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,27 @@ const weights = [
99
];
1010

1111
const targetValue = step1CPU();
12-
assert.deepStrictEqual(step2GPUStyleLoops(), targetValue);
13-
assert.deepStrictEqual(step3GPUStyleKernel(), targetValue);
14-
assert.deepStrictEqual(step4GPUKernel(), targetValue);
12+
assert.deepStrictEqual(step2CPUWithMatrixLog(), targetValue);
13+
assert.deepStrictEqual(step3GPUStyleLoops(), targetValue);
14+
assert.deepStrictEqual(step4GPUStyleKernel(), targetValue);
15+
assert.deepStrictEqual(step5GPUKernel(), targetValue);
1516

1617
function step1CPU() {
18+
const filters = [
19+
[0,0],
20+
[0,0]
21+
];
22+
for (let y = 0; y < 4; y++) {
23+
let filterY = y < 2 ? 0 : 1;
24+
for (let x = 0; x < 4; x++) {
25+
let filterX = x < 2 ? 0 : 1;
26+
filters[filterY][filterX] += weights[y][x];
27+
}
28+
}
29+
return filters;
30+
}
31+
32+
function step2CPUWithMatrixLog() {
1733
const filters = [
1834
[0,0],
1935
[0,0]
@@ -50,7 +66,7 @@ function step1CPU() {
5066
return filters;
5167
}
5268

53-
function step2GPUStyleLoops() {
69+
function step3GPUStyleLoops() {
5470
const filters = [
5571
[0,0],
5672
[0,0]
@@ -82,7 +98,7 @@ function step2GPUStyleLoops() {
8298
return filters;
8399
}
84100

85-
function step3GPUStyleKernel() {
101+
function step4GPUStyleKernel() {
86102
const filters = [
87103
[0,0],
88104
[0,0]
@@ -113,7 +129,7 @@ function step3GPUStyleKernel() {
113129
return filters;
114130
}
115131

116-
function step4GPUKernel() {
132+
function step5GPUKernel() {
117133
const filters = [
118134
[0,0],
119135
[0,0]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "matrix-log.js",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "A matrix log dependency utility. Useful for converting and testing algorithm behavior of CPU code to GPU code.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)