initial implementation of hash and checksum

This commit is contained in:
jiri committed 2026-11-26 20:08:15 +01:00
1 parent f028a2e0fc
commit 8b2b413db7
12 files changed
+228

No files matched your search

+36
View File
@@ -136,6 +136,7 @@ Its main members are:
\begin{lareference}
\lafunc{typedef ... elementtype}{type of the elements (for a scalar the type itself)}
\lafunc{typedef ... normtype}{type of the norm, e.g.\ \code{double} for \code{std::complex<double>} and for \code{NRMat<std::complex<double> >}}
\lafunc{typedef ... scalartype}{type of the numbers at the bottom of a nested object, defined recursively like \code{normtype} but without taking the real type: \code{std::complex<double>} for \code{NRVec<NRMat<std::complex<double> > >}, whereas \code{elementtype} of this object is \code{NRMat<std::complex<double> >}}
\lafunc{typedef ... realtype; typedef ... complextype}{the corresponding real and complex type, e.g.\ \code{NRVec<double>} and \code{NRVec<std::complex<double> >}}
\lafunc{typedef ... producttype}{type of the product of two objects (for \code{NRSMat} it is \code{NRMat})}
\lafunc{static bool is_plaindata()}{true for scalar types, which can be copied by \code{memcpy}, false for the classes}
@@ -143,6 +144,7 @@ Its main members are:
\lafunc{static normtype norm(const T &x); static void axpy(T &s, const T &x, const elementtype c)}{norm and $s \leftarrow s + c\,x$, used by generic algorithms such as \code{matexp.h}}
\lafunc{static void copyonwrite(T &x); static void clearme(T &x); static void deallocate(T &x)}{generic versions of the member functions (no operation for scalars)}
\lafunc{static void put(int fd, const T &x, ...); static void get(int fd, T &x, ...); multiput; multiget}{binary I/O, recursive for nested types}
\lafunc{static uint64\_t hash(const T *x, size\_t n, uint64\_t h); static scalartype checksum(const T \&x, uint64\_t seed)}{hash and numerical checksum (see below), recursive for nested types}
\lafunc{static T conjugate(const T &x); realpart(x); imagpart(x); setrealpart(x,y); setimagpart(x,y)}{complex helpers, trivial for real types}
\lafunc{static bool bigger(const T &x, const T &y); smaller(x,y)}{comparison used for sorting; for complex types it is an error}
\lafunc{static int strassen_cutoff()}{scalar types only: default \code{cutoff} of \code{NRMat::strassen} (section~\ref{sec:mat:strassen}), currently 2048 for all types}
@@ -302,3 +304,37 @@ void get(int fd, bool dimensions=1, bool transp=0, bool orca_format=false);
\code{NRMat} and the sparse matrices.
\end{description}
For storing many objects of the same size in a scratch file, see \code{AuxStorage} (chapter~\ref{ch:utilities}).
\paragraph{Hash and checksum.} Two functions characterize the contents of an object by one number; they are members of
\code{NRVec}, \code{NRMat} and \code{NRSMat} (and of the classes derived from them) and work for nested objects.
\begin{lstlisting}
uint64_t hash(uint64_t h=LA_HASH_INIT) const;
const typename LA_traits<T>::scalartype checksum(const uint64_t seed=0) const;
\end{lstlisting}
\begin{description}
\item[\code{hash}] is computed from the dimensions and from the exact binary representation of the elements. Two
objects with the same hash are bit by bit the same, up to a negligible chance of a coincidence (64 bits); any
change, however small, gives a completely different value. It is meant for integer data, for keys, and for
checking that a file belongs to the data it was derived from. The argument continues a previous hash, so that
several objects can be combined: \code{h=a.hash(); h=b.hash(h);}. The value depends on the byte order of the
machine, like the binary \code{put} and \code{get}; for floating point data $0$ and $-0$ differ. Plain data are
processed by words of 8 bytes (about 4 GB/s).
\item[\code{checksum}] is the sum of the elements multiplied by fixed pseudorandom weights from $[-1,1)$, which are
determined by the position of the element, the dimensions and the seed. It is a linear function of the
elements over the field of the data: its value is complex for complex data,
$\mathrm{checksum}(\alpha x+y)=\alpha\,\mathrm{checksum}(x)+\mathrm{checksum}(y)$ for objects of the same
dimensions, and a change of the elements by $\delta$ changes it by at most $\sum|\delta|$. It is meant for
floating point data (for integer element types it raises an error) and for comparison within a threshold, which
should be taken relative to the norm of the object, e.g.\ of results obtained on different machines or with
different BLAS libraries. Only objects of the same class and dimensions should be compared (an \code{NRMat} and an
\code{NRSMat} holding the same matrix have different checksums). Objects with different elements have close
checksums only by chance: a difference of the norm $d$ shows as less than $td$ with a probability of about $t$,
which can be made negligible by comparing the checksums for two or three seeds. A NaN among the elements gives NaN.
The cost is about 2~ns per element.
\end{description}
The weights are generated by an own generator without a state (splitmix64 applied to the position and the seed,
\code{hash\_mix} in \code{miscfunc.h}, section~\ref{sec:utilities:miscfunc}); they do not depend on the random number generators of the library, which
the user can replace (section~\ref{sec:utilities:random}), and on the order of any previous calls. Both functions are
frozen: their values are meant to be stored in files and will not change in later versions. The method
\code{checksum} is formally a template, so that traits of user-defined element types need not define
\code{scalartype} unless \code{checksum} is called for them.