为大量浮点数生成快速校验和,而不使用任何库?
在 C 语言中(更具体地说,C for CUDA),计算大量浮点数(比如两万个值)的校验和的最佳方法是什么,这很容易用 printf 打印,而不使用任何库?
我可以将所有浮动精度的值相加,但恐怕舍入误差或饱和度或 nan/inf 值会使某些更改无法检测到。
它用于比较同一 GPU 硬件上相同二进制文件运行之间的变量值,并且仅用于调试,而不是用于安全性。
更清楚地说,当数组中的任何浮点值发生变化时,如果校验和的所有数字都发生变化(很有可能),那就太好了,这样校验和就很容易在视觉上进行比较。
In C (more specifically, C for CUDA), what is the best way to compute a checksum of a large array of floats (say twenty thousand values), that is easy to print with printf, without using any libraries?
I could just sum all of the values in floating precision, but I'm afraid roundoff errors or saturation, or nan/inf values, would make some changes un-detectable.
This is being used to compare values of a variable between runs of the same binary on the same gpu hardware, and this is being used for debugging only, not for security.
To be even more clear, it would be nice if all of the digits of the checksum change (with high probability) when any of the floating point values in the array changes, so that checksums are easy to compare visually.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这正是循环冗余检查的用途。 Boost有一个CRC库,并且有几十个网络上的源代码实现。 16 位 CRC 可能最适合您,因为观察结果很容易。但如果您担心误报,您可能需要 32 位 CRC。
This is exactly what Cyclic Redundancy Checks are for. Boost has a CRC library, and there are dozens of source code implementations on the web. Probably a 16-bit CRC is best for you, because eyeballing the result is easy. But you might want a 32-bit CRC if you are paranoid about false positives.
如果您使用 IEEE-754 浮点数,则可以将浮点数转换为指针,然后将该指针重新解释为无符号 int 指针,并以这种方式求和,以避免任何浮点舍入问题。此时,您基本上是在代表浮点数的实际位而不是浮点值本身上创建校验和。
例如:
编辑:正如评论中提到的,这无论如何都不会创建安全校验和......这是一种非常简单的校验和形式,但希望它适用于您的调试目的。
If you are using IEEE-754 floats, you can cast the floats to a pointer that is then re-interpreted as an unsigned int pointer and sum them that way in order to avoid any floating point round-off issues. You're basically at that point creating a checksum on the actual bits representing the floating point numbers rather than the floating point values themselves.
So for example:
Edit: As mentioned in the comments, this does not create a secure checksum by any means ... it's a very simple form of check-summing, but hopefully it would work for your debugging purposes.
也许这对于 stackoverflow 响应来说太大了,但这是我从 pycrc。它包括其他回复中已经提到的一些技术。我最信任 crc 版本,但是当数组应该全为零时,add 和 xor 版本很方便。
Maybe this is too large for a stackoverflow response, but here's the crc.cu file I hacked together from output of pycrc. It includes a couple of techniques already mentioned in other replies. I trust the crc version the most, but the add and xor versions are convenient when arrays should be full of zeros.
适当的 CRC 库可能是您最好的选择,但只是为了潜在的兴趣:您可以使用每个字节(即
*(uint8_t*)&
重新解释)来索引到一个包含 32 位随机数的表中,然后将这些表条目异或在一起。这意味着值中的单个位变化会随机翻转输出中的位。如果不希望拥有与字节数一样多的查找表,则可以通过对已使用的表中的位进行循环移位来获得合理的结果。从概念上讲,它比数学哈希算法简单得多......A proper CRC library is probably your best bet, but just for potential interest: rather than XORing the bits in your values, you can use each byte (i.e. an
*(uint8_t*)&
reinterpretation) to index into a table of say 32-bit random numbers, then XOR those table entries together. This means a single bit of variation in a value randomly flips bits in the output. If it's undesirable to have as many lookup tables as there could be bytes, you can get reasonable results doing a circular shift on the bits in an already-used table. It's a lot simpler conceptually than mathematical hashing algos....编辑:
我相信最好的答案是 TonyK 和 Jason 答案的结合。将
float*
转换为uint32_t*
后,在缓冲区上使用 32 位 CRC。如果您的编译器提供了 uint32 定义,请获取它,或者根据您的平台自行键入定义(通常在 32 位计算机上为 unsigned long,在 64 位计算机上为 unsigned int。)这是一个 良好的 CRC 解释和实现EDITED:
I believe the best answer is the combination of TonyK's and Jason's answers. Use a 32 bit CRC on the buffer, after casting the
float*
to auint32_t*
. Get the uint32 definition from if your compiler supplies it, or typedef it yourself based on your platform (usually unsigned long on 32-bit machines, unsigned int on 64 bit machines.) Here is a good CRC explanation and implementation如果要在 GPU 上计算校验和,请使用 __int_as_float() 和 __float_as_int() 内在函数将浮点数视为整数。 CUDA 4.0 中包含的 Thrust 库使计算此校验和变得容易 - 这是 minmax Thrust 示例,已移植以执行您正在寻找的操作。
If you want to compute the checksum on the GPU, use the __int_as_float() and __float_as_int() intrinsics to treat the floats-as-ints. The Thrust library included with CUDA 4.0 makes computing this checksum easy - here is the minmax Thrust example, ported to do what you are looking for.