How to implement a for loop inside a combineKernel in GPU.js

158 Views Asked by At

I'm new to gpu.js and I need help. Basically I want to access my two kernel functions but I don't know how without sacrificing the expense of calling createKernel each time. I know there is combineKernel but I haven't seen an example where it uses a for loop inside.

So I have my first function which generates a square matrix from a vector

const functionA = gpu.createKernel(function(vector:number[]) {
   // just body of the code
}).setOutput([somethingLength, somethingLength])

Then from the resulting square matrix, I add each of its columns, and this return a vector. So I have another function for that

const functionB = gpu.createKernel(function (squareMatrix: number[][], initialVector: number[]) {
        let sum = initialVector[this.thread.x]
        for (let i = 0; i < squareMatrixLength ; i++) {
            sum += squareMatrix[i][this.thread.x]
        }
        return sum;
}).setOutput([initialVector.length])

I also have a combineKernel for both of them

const functionC = gpu.combineKernels(functionA as any, functionB as any, function (initialVector: number[], vector: number[]) {
        return functionB(functionA(vector), initialVector)
    })

Now, I have a number of vectors that I want to undergo functionA and then functionB, I want to do something like the code below inside the combineKernel to avoid the expense of cpu to gpu but I am having trouble how to figure it out. I want the final result to be a 2D matrix.

for(let i=0; i<vectorsLength; i++){
   newVector = functionB(functionA(vectors[i]), initialVector)
}
return newVector
0

There are 0 best solutions below