简体   繁体   English

双重或浮动 - 优化例程

[英]Double or float - optimization routines

I am reading through code for optimization routines (Nelder Mead, SQP...). 我正在阅读优化程序的代码(Nelder Mead,SQP ......)。 Languages are C++, Python. 语言是C ++,Python。 I observe that often conversion from double to float is performed, or methods are duplicated with double resp. 我观察到通常会执行从double到float的转换,或者使用double resp复制方法。 float arguments. 浮动参数。 Why is it profitable in optimization routines code, and is it significant? 为什么它在优化例程代码中有利可图,并且它是否重要? In my own code in C++, should I be careful for types double and float and why? 在我自己的C ++代码中,我应该注意类型double和float以及为什么?

Kind regards. 亲切的问候。

Often the choice between double and float is made more on space demands than speed. 通常, doublefloat之间的选择更多地取决于空间需求而不是速度。 Modern processors are capable of operating on double quite fast. 现代的处理器能够上操作的double相当快。

Floats may be faster than doubles when using SIMD instructions (such as SSE) which can operate on multiple values at a time. 当使用可以一次操作多个值的SIMD指令(例如SSE)时,浮点数可能比双倍更快。 Also if the operations are faster than the memory pipeline, the smaller memory requirements of float will speed things overall. 此外,如果操作比内存管道更快, float的较小内存要求将加速整体事物。

Other times that I've come across the need to consider the choice between double and float types in terms of optimisation include: 在其他时候,我遇到需要考虑在优化方面选择double和float类型包括:

  • Networking. 联网。 Sending double precision data across a socket connection will obviously require more time than sending half that amount of data. 在套接字连接上发送双精度数据显然需要比发送一半数据量更多的时间。
  • Mobile and embedded processors may only be able to handle high speed single precision calculations efficiently on a coprocessor. 移动和嵌入式处理器可能只能在协处理器上有效地处理高速单精度计算。

As mentioned in another answer, modern desktop processors can handle double precision Processing quite fast. 正如另一个答案所提到的,现代桌面处理器可以非常快速地处理双精度处理。 However, you have to ask yourself if the double precision processing is really required. 但是,您必须问自己是否真的需要双精度处理。 I work with audio, and the only time that I can think of where I would need to process double precision data would be when using high order filters where numerical errors can accumulate. 我使用音频,并且我唯一能想到处理双精度数据的地方就是使用高阶滤波器,其中数值误差可以累积。 Most of the time this can be avoided by paying more careful attention to the algorithm design. 大多数情况下,通过更加谨慎地关注算法设计可以避免这种情况。 There are, of course, other scientific or engineering applications where double precision data is required in order to correctly represent a huge dynamic range. 当然,还有其他科学或工程应用需要双精度数据才能正确表示巨大的动态范围。

Even so, the question of how much effort to spend on considering the data type to use really depends on your target platform. 即便如此,在考虑使用数据类型时花费多少精力的问题实际上取决于您的目标平台。 If the platform can crunch through doubles with negligible overhead and you have memory to spare then there is no need to concern yourself. 如果平台能够以微不足道的开销来处理双打并且你有足够的内存,那么就没有必要关心自己了。 Profile small sections of test code to find out. 查找测试代码的小部分以找出答案。

In certain optimization algorithms, the choice between double and float is not made more on space demands than speed. 在某些优化算法中, doublefloat之间的选择不是空间需求而是速度。 For example, with penalty or barrier methods that are used for interior point methods in nonlinear optimization, a float has insufficient precision compared to a double , and using float s in the algorithm will yield garbage. 例如,使用在非线性优化中用于内点法的惩罚或障碍方法, floatdouble精度相比精度不足,并且在算法中使用float将产生垃圾。 For this reason, penalty and barrier methods were not used in the 1960s, but were rediscovered later on with the advent of the double precision data type. 由于这个原因,惩罚和障碍方法在20世纪60年代没有被使用,但随着双精度数据类型的出现,后来被重新发现。 (For more on these methods, consult Nonlinear Programming: Sequential Unconstrained Minimization Techniques (Classics in Applied Mathematics) by Fiacco and McCormick.) (有关这些方法的更多信息,请参阅Fiacco和McCormick的非线性编程:顺序无约束最小化技术(应用数学中的经典) 。)

Another consideration is the conditioning of the underlying linear systems solved in many optimization algorithms. 另一个考虑因素是在许多优化算法中解决的基础线性系统的调节。 If the linear systems you're solving in something like a Newton iteration are sufficiently ill-conditioned, you will not be able to obtain an accurate solution to those systems. 如果您在牛顿迭代中解决的线性系统充分恶劣,那么您将无法获得这些系统的准确解决方案。

Only if the loss in precision will not jeopardize your numerics should you consider replacing double s with float s; 只有当你考虑用float代替double s时,精度的损失不会危害你的数字; even if space constraints force you to do so, you should make sure that the accuracy of your numerical results is not compromised. 即使空间限制迫使您这样做,您也应该确保数值结果的准确性不会受到影响。 Once sufficient accuracy is assured for the problems you're working on, you can then worry about space and performance optimizations. 一旦为您正在处理的问题确保足够的准确性,您就可以担心空间和性能优化。 You can use the CUTEr test set to validate your optimization routines. 您可以使用CUTEr测试集来验证优化例程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM