如何在gpu.js中的组合内实现循环
我是GPU.JS的新手,我需要帮助。基本上,我想访问我的两个内核函数,但我不知道每次都会致电CreateKernel的费用。我知道有组合kernel,但我还没有看到它在内部使用循环的示例。
因此,我的第一个函数从向量生成一个方形矩阵
const functionA = gpu.createKernel(function(vector:number[]) {
// just body of the code
}).setOutput([somethingLength, somethingLength])
,然后从结果的方形矩阵中添加其每个列,然后返回向量。因此,我现在还有另一个功能,
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])
现在我都有一个组合
const functionC = gpu.combineKernels(functionA as any, functionB as any, function (initialVector: number[], vector: number[]) {
return functionB(functionA(vector), initialVector)
})
,我现在有许多我想使用功能的向量,然后函数b,我想在组合内部的下面执行类似下面的代码以避免使用费用以避免使用费用CPU到GPU,但我很难解决。我希望最终结果是2D矩阵。
for(let i=0; i<vectorsLength; i++){
newVector = functionB(functionA(vectors[i]), initialVector)
}
return newVector
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在
combineKernels
代码(包括循环)中执行您喜欢的任何事情。它是在CPU上运行的常规JS代码,而不是GPU。来自 docs for
code> combineKernels
:combineKernels 只是做
因此,实际上会有上下文从GPU转换为CPU再到GPU,但是数据将作为GPU中的纹理保留。
You can do whatever you like in your
combineKernels
code, including loops. It is regular JS code running on your CPU, not GPU.From the docs for
combineKernels
:combineKernels
is just a helper for doing pipelines: your kernels will be configured to produce their outputs as references to textures inside the GPU, without converting them to arrays. Texture references and arrays are accepted as inputs by any kernel interchangeably.So there will actually be context switching from GPU to CPU back to GPU but the data will remain as textures in the GPU.