我应该在 GPU 上还是在 CPU 上计算矩阵? [英] Should I calculate matrices on the GPU or on the CPU?

查看:38
本文介绍了我应该在 GPU 上还是在 CPU 上计算矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该更喜欢在 CPU 还是 GPU 上计算矩阵?

Should I prefer to calculate matrices on the CPU or GPU?

假设我有以下矩阵 P * V * M ,我应该在 CPU 上计算它们以便将最终矩阵发送到 GPU (GLSL) 还是应该发送这三个矩阵矩阵分开到 GPU 以便 GLSL 可以计算最终矩阵?

Let's say I have the following matrices P * V * M , should I calculate them on the CPU so that I can send the final matrix to the GPU (GLSL) or should I send those three matrices separately to the GPU so that GLSL can calculate the final matrix?

我的意思是在这种情况下 GLSL 必须为每个顶点计算 MVP 矩阵,因此在 CPU 上预先计算它可能会更快.

I mean in this case GLSL would have to calculate the MVP matrix for every vertex, so it is probably faster to precompute it on the CPU.

但假设 GLSL 只需要计算一次 MVP 矩阵,GPU 会比 CPU 更快地计算最终矩阵吗?

But let's say that GLSL only has to calculate he MVP matrix once, would the GPU calculate the final matrix faster than the CPU?

推荐答案

一般规则:如果你可以将它以uniform的形式传递给着色器,总是预先计算在 CPU 上;没有例外.着色器端的计算仅对顶点和片段之间变化的值有意义.在整批顶点中保持不变的一切都在 CPU 上得到最有效的处理.

General rule: If you can pass it to a shader in form of a uniform, always precalculate on the CPU; no exceptions. Calculations on the shader side make sense only for values that vary between vertices and fragments. Everything that's constant among a whole batch of vertices is most efficiently dealt with on the CPU.

GPU 并不是可以做任何事情都更快"的魔法机器.在某些任务中,即使对于非常大的数据集,CPU 也可以轻松胜过 GPU.所以一个非常简单的指导方针是:如果你可以将它移到 CPU 上,而不会花费比 GPU 处理它的总开销更多的 CPU 时间来进行计算,那么在 CPU 上进行.单个矩阵的计算属于这些任务之一.

GPUs are not magic "can do faster everything" machines. There are certain tasks where a CPU can easily outperform a GPU, even for very large datasets. So a very simple guideline is: If you can move it to the CPU without spending more CPU time doing the calculation than it takes for the GPU in total overhead to process it, then do it on the CPU. The calculation of a single matrix is among those tasks.

这篇关于我应该在 GPU 上还是在 CPU 上计算矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆