Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a quick checksum for a large array of floats, without using any libraries?

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.

like image 315
Andrew Wagner Avatar asked Jan 28 '26 07:01

Andrew Wagner


1 Answers

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.

like image 190
TonyK Avatar answered Jan 30 '26 21:01

TonyK