什么时候使用 TMP 预计算值才真正有用?

发布于 2024-10-16 06:29:16 字数 316 浏览 0 评论 0原文

Scott Meyers 在“Effective C++”中指出,在编译器中执行矩阵运算等功能是在模板类/函数中实现某些算法的原因。但显然,这些函数不能对运行时确定的参数进行操作——它们只适用于写入程序的数字或最多作为编译器的参数给出的数字。程序编译后,每次运行时都将使用相同的输出值。在这种情况下,为什么不直接使用常规(非模板化)程序计算该值,并在必要时将其写入原始程序?例如,计算 1000 分并不更快。编译器中的 fft 肯定比常规程序中的 fft 好。

我能想到的最好办法是,如果您需要为不同的客户端编译不同版本的程序,那么 TMP 可能会节省您一些时间。但这真的需要发生吗?

Scott Meyers in "Effective C++" points at the ability to do e.g. matrix operations in the compiler as a reason for implementing some of your algorithms in template classes/functions. But these functions can't operate on arguments that are determined at run-time, obviously--they only work for numbers that are written into the program or at best given as arguments to the compiler. Once the program is compiled, it will be using the same output value, every time it is run. In that case why not just calculate that value with a regular (non-templated) program, and just write it in to the original program where necessary? It's not faster to calculate e.g. a 1000-pt. fft in the compiler than it is with a regular program surely.

The best I can come up with is if you need to compile different versions of your program for different clients, then TMP might save you some time. But does this need every actually arise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小糖芽 2024-10-23 06:29:16

TMP 在矩阵运算方面的主要优势不是能够预先计算矩阵运算的结果,而是能够优化生成的代码以在运行时进行实际的矩阵计算。你是对的 - 你不太可能想要在程序中预先计算矩阵 - 但在程序开始运行之前想要在编译时优化矩阵数学是很正常的。例如,考虑以下代码:

 Matrix a, b, c;
 /* ... Initialize these matrices ... */
 Matrix d = a + b + c;

最后一行使用一些重载运算符来计算矩阵表达式。使用传统的 C++ 编程技术,其工作原理如下:

  1. 计算 b * c,返回保存副本的临时矩阵对象。
  2. 计算 a + b + c,再次返回临时副本。
  3. 将结果复制到d中。

这很慢 - 没有充分的理由在这里复制任何值。相反,我们应该只对矩阵中的所有索引进行循环,并对我们找到的所有值求和。但是,使用称为表达式模板的 TMP 技术,可以以实际执行此操作的方式实现这些运算符以智能、优化的方式而不是缓慢、标准的方式进行计算。我认为迈耶斯在书中提到的就是这一系列技术。

TMP 最著名的示例是在编译时预先计算值的简单程序,但实际上,实际使用的技术要复杂得多。

The main advantage of TMP when it comes to matrix operations is not the ability to precompute the result of a matrix operation, but rather the ability to optimize the generated code for doing the actual matrix computation at runtime. You are correct - it would be pretty unlikely that you'd ever want to precompute a matrix in the program - but it's salmon to want to optimize matrix math at compile-time before the program begins running. For example, consider this code:

 Matrix a, b, c;
 /* ... Initialize these matrices ... */
 Matrix d = a + b + c;

This last line uses some overloaded operators to compute a matrix expression. Using traditional C++ programming techniques, this would work as follows:

  1. Compute b * c, returning a temporary matrix object holding the copy.
  2. Compute a + b + c, again returning a temporary copy.
  3. Copy the result into d.

This is slow - there's no good reason to make any copies of any values here. instead we should just for loop over all indices in the matrices and sum up all the values we find. However, using a TMP technique called expression templates, it's possible to implement these operators in a way that actually does this computation in the intelligent, optimized way rather than the slow, standard way. It's this family of techniques that I think Meyers was referring to in the book.

The most well-known examples of TMP are simple programs to precompute values at compile time, but in practice it's much more complex techniques like these that actually get used in practice.

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