You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This code doesn't directly work on the GPU, because we can only _either_ read or write to arrays there. Currently `filters` violates this. However, we could use MatrixLog to _help us_ (note, there is a little thinking that needs to take place) solve this issue by finding the algorithm that `filters` needs.
This code doesn't directly work on the GPU, because we can only _either_ read or write to arrays there. Currently `filters` violates this. However, we could use MatrixLog to _help us_ (note, there is a little thinking that needs to take place) solve this issue by finding the algorithm that `filters` needs.
163
164
164
165
2. Convert the code as follows and see the output below:
165
-
```js
166
-
constMatrixLog=require('./index');
167
-
168
-
constfilters= [
169
-
[0,0],
170
-
[0,0]
171
-
];
172
-
constweights= [
173
-
[1,2,3,4],
174
-
[5,6,7,8],
175
-
[9,10,11,12],
176
-
[13,14,15,16]
177
-
];
178
-
179
-
constmatrixLog=newMatrixLog('filters', 2, 2);
180
-
for (let y =0; y <4; y++) {
181
-
let filterY = y <2?0:1;
182
-
for (let x =0; x <4; x++) {
183
-
let filterX = x <filter.length?0:1;
184
-
filters[filterY][filterX] += weights[y][x];
185
-
matrixLog
186
-
.at({ x, y })
187
-
.add({
188
-
name:'weights',
189
-
x: filterX,
190
-
y: filterY,
191
-
width: weights[0].length,
192
-
height:weights.length
193
-
});
194
-
}
195
-
}
196
-
197
-
console.log(matrixLog.toString('weights'));
198
-
```
199
-
200
-
Gives us the output:
201
-
```
202
-
filters x=0,y=0 weights
203
-
width=2,height=2 width=4,height=4
204
-
[*][ ] [*][*][ ][ ]
205
-
[ ][ ] [*][*][ ][ ]
206
-
[ ][ ][ ][ ]
207
-
[ ][ ][ ][ ]
208
-
209
-
filters x=1,y=0 weights
210
-
width=2,height=2 width=4,height=4
211
-
[ ][*] [ ][ ][*][*]
212
-
[ ][ ] [ ][ ][*][*]
213
-
[ ][ ][ ][ ]
214
-
[ ][ ][ ][ ]
215
-
216
-
filters x=0,y=1 weights
217
-
width=2,height=2 width=4,height=4
218
-
[ ][ ] [ ][ ][ ][ ]
219
-
[*][ ] [ ][ ][ ][ ]
220
-
[*][*][ ][ ]
221
-
[*][*][ ][ ]
222
-
223
-
filters x=1,y=1 weights
224
-
width=2,height=2 width=4,height=4
225
-
[ ][ ] [ ][ ][ ][ ]
226
-
[ ][*] [ ][ ][ ][ ]
227
-
[ ][ ][*][*]
228
-
[ ][ ][*][*]
229
-
```
166
+
```js
167
+
const MatrixLog = require('./index');
168
+
169
+
const filters = [
170
+
[0,0],
171
+
[0,0]
172
+
];
173
+
const weights = [
174
+
[1,2,3,4],
175
+
[5,6,7,8],
176
+
[9,10,11,12],
177
+
[13,14,15,16]
178
+
];
179
+
180
+
const matrixLog = new MatrixLog('filters', 2, 2);
181
+
for (let y = 0; y < 4; y++) {
182
+
let filterY = y < 2 ? 0 : 1;
183
+
for (let x = 0; x < 4; x++) {
184
+
let filterX = x < filter.length ? 0 : 1;
185
+
filters[filterY][filterX] += weights[y][x];
186
+
matrixLog
187
+
.at({ x, y })
188
+
.add({
189
+
name: 'weights',
190
+
x: filterX,
191
+
y: filterY,
192
+
width: weights[0].length,
193
+
height: weights.length
194
+
});
195
+
}
196
+
}
197
+
198
+
console.log(matrixLog.toString('weights'));
199
+
```
200
+
201
+
Gives us the output:
202
+
```
203
+
filters x=0,y=0 weights
204
+
width=2,height=2 width=4,height=4
205
+
[*][ ] [*][*][ ][ ]
206
+
[ ][ ] [*][*][ ][ ]
207
+
[ ][ ][ ][ ]
208
+
[ ][ ][ ][ ]
209
+
210
+
filters x=1,y=0 weights
211
+
width=2,height=2 width=4,height=4
212
+
[ ][*] [ ][ ][*][*]
213
+
[ ][ ] [ ][ ][*][*]
214
+
[ ][ ][ ][ ]
215
+
[ ][ ][ ][ ]
216
+
217
+
filters x=0,y=1 weights
218
+
width=2,height=2 width=4,height=4
219
+
[ ][ ] [ ][ ][ ][ ]
220
+
[*][ ] [ ][ ][ ][ ]
221
+
[*][*][ ][ ]
222
+
[*][*][ ][ ]
223
+
224
+
filters x=1,y=1 weights
225
+
width=2,height=2 width=4,height=4
226
+
[ ][ ] [ ][ ][ ][ ]
227
+
[ ][*] [ ][ ][ ][ ]
228
+
[ ][ ][*][*]
229
+
[ ][ ][*][*]
230
+
```
230
231
231
232
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:
235
-
236
-
```js
237
-
constfilterHeight=2;
238
-
constfilterWidth=2;
239
-
240
-
for (let filterY =0; filterY < filterHeight; filterY++) {
241
-
for (let filterX =0; filterX < filterWidth; filterX++) {
242
-
// NOTE: += filters!
243
-
let sum = filters[filterY][filterX];
244
-
245
-
constyMin= filterHeight * filterY;
246
-
constyMax= yMin + filterHeight;
247
-
constxMin= filterWidth * filterX;
248
-
constxMax= xMin + filterWidth;
249
-
250
-
for (let y = yMin; y < yMax; y++) {
251
-
for (let x = xMin; x < xMax; x++) {
252
-
sum += weights[y][x];
233
+
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`.
234
+
Then to get `filters`@`x=1,y=0`, we seem to increment by two on `weights`.
235
+
If we were to write a loop that emulates this behaviour, it'd look something like this:
236
+
237
+
```js
238
+
const filterHeight = 2;
239
+
const filterWidth = 2;
240
+
241
+
for (let filterY = 0; filterY < filterHeight; filterY++) {
242
+
for (let filterX = 0; filterX < filterWidth; filterX++) {
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" (likeiterationoftheinnermostloopsforbuildingthevalue).
265
-
If that function was just simple Javascript that we imagined might work on a GPU kernel, it'd looks something like this:
5. If we use a GPU environment, such as GPU.js, we could then then convert the kernel so that our algorithm actually works for setting the value of`filters` like this:
0 commit comments