如何在gpu.js中的组合内实现循环

发布于 2025-02-02 10:50:43 字数 1164 浏览 2 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路黄昏 2025-02-09 10:50:43

您可以在combineKernels代码(包括循环)中执行您喜欢的任何事情。它是在CPU上运行的常规JS代码,而不是GPU。

来自 docs for code> combineKernels

有时您想在没有GPU上进行多次数学操作
从CPU到GPU再到CPU到GPU的数据传输的往返罚款,
等。

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:

Sometimes you want to do multiple math operations on the gpu without
the round trip penalty of data transfer from cpu to gpu to cpu to gpu,
etc.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文