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.
+1
View File
@@ -206,6 +206,7 @@ Note that scalar addition and subtraction act on the \emph{diagonal} only, i.e.\
\lafunc{const typename LA_traits<T>::normtype norm(const T scalar = (T)0) const}{Frobenius norm $\|A-s\,\mathbf 1\|_F$ (BLAS \code{nrm2} when $s=0$).}
\lafunc{const T trace() const}{$\sum_i A_{ii}$ (square matrices).}
\lafunc{const T sum() const; const NRVec<T> rsum() const; const NRVec<T> csum() const}{Sum of all elements; vector of row sums (length \code{nrows()}, sum over the column index); vector of column sums (length \code{ncols()}).}
\lafunc{uint64\_t hash(uint64\_t h=LA\_HASH\_INIT) const; const typename LA\_traits<T>::scalartype checksum(const uint64\_t seed=0) const}{Hash of the exact contents and numerical checksum for comparison within a threshold, see section~\ref{sec:concepts:io}.}
\lafunc{inline const T amax() const; inline const T amin() const}{The element (not its index) with the largest resp.\ smallest absolute value; for complex the measure is $|\mathrm{Re}|+|\mathrm{Im}|$ (BLAS \code{i?amax} convention).}
\lafunc{const typename LA_traits<T>::normtype nonsymmetry() const}{$\sum_{i>j}|A_{ij}-A_{ji}|^2$ (square of the norm, no square root).}
\lafunc{const typename LA_traits<T>::normtype nonhermiticity() const}{Complex types only: $\sum_{i\ge j}|A_{ij}-A_{ji}^*|^2$.}
+1
View File
@@ -166,6 +166,7 @@ generated from the in-place ones.
\lafunc{const typename LA_traits<T>::normtype norm(const T scalar = (T)0) const}{Euclidean norm of the packed data of $S - \mathtt{scalar}\cdot 1$; off-diagonal elements are counted \emph{once}, so this is not the Frobenius norm of the full matrix.}
\lafunc{const T trace() const}{Trace.}
\lafunc{const T sum(bool onlytriangle=false) const}{Sum of all elements of the full matrix, or of the stored triangle only.}
\lafunc{uint64\_t hash(uint64\_t h=LA\_HASH\_INIT) const; const typename LA\_traits<T>::scalartype checksum(const uint64\_t seed=0) const}{Hash of the exact contents and numerical checksum for comparison within a threshold, see section~\ref{sec:concepts:io}. Both use the stored triangle only.}
\lafunc{const NRVec<T> rsum() const; const NRVec<T> csum() const}{Row sums and column sums of the full matrix (equal for real, complex conjugate of each other for hermitian matrices).}
\lafunc{inline const T amax() const; inline const T amin() const}{Stored element with the largest/smallest absolute value (complex: $|\Re|+|\Im|$); floating point types only.}
\lafunc{const T* diagonalof(NRVec<T> &r, const bool divide = 0, bool cache = false) const}{\code{divide=false}: store the diagonal into \code{r} and return \code{&r[0]}; \code{divide=true}: divide \code{r} elementwise by the nonzero diagonal elements and return \code{NULL}. \code{cache} is unused. Interface used by iterative solvers (\ref{ch:iterative}).}
+3
View File
@@ -166,6 +166,9 @@ symmetric tensors, chapter~\ref{ch:tensor}). $s(d,n)=0$ for $n\le0$, $1$ for $d=
recurrence $s(d,n)=s(d,n-1)+s(d-1,n)$ into a table growing on demand; \code{laerror} for $d\ge16$ or $n\ge1024$.}
\lafunc{int inverse_simplicial(int d, unsigned long long s)}{Largest $n$ with \code{simplicial(d,n)}${}\le s$ (Newton
iteration on the polynomial $s(d,n)$ followed by an integer correction); 0 for $s=0$. \code{laerror} for $d\ge16$.}
\lafunc{static inline uint64\_t hash\_mix(const uint64\_t counter, const uint64\_t seed)}{Pseudorandom 64-bit number as a fixed function of its arguments (splitmix64); a generator without a state, independent of \code{la\_random.h}. It generates the weights of \code{checksum} and combines the dimensions into \code{hash} (section~\ref{sec:concepts:io}); it must never be changed.}
\lafunc{static inline double checksum\_weight(const uint64\_t z)}{The number \code{z} mapped to $[-1,1)$.}
\lafunc{uint64\_t hash\_bytes(const void *x, const size\_t nbytes, uint64\_t h=LA\_HASH\_INIT)}{Hash of \code{nbytes} bytes of plain data, taken by words of 8 bytes, continuing from \code{h}; used by \code{hash} of the classes for scalar element types. Frozen as well.}
\end{lareference}
%=====================================================================
+1
View File
@@ -204,6 +204,7 @@ $|\mathrm{Re}|+|\mathrm{Im}|$). Only \code{double} and \code{std::complex<double
\lafunc{const T max() const; const T min() const}{Largest / smallest element (0 for an empty vector). Only real types
(instantiated for the integer types, \code{float}, \code{double}).}
\lafunc{const T sum() const}{$\sum_i x_i$.}
\lafunc{uint64\_t hash(uint64\_t h=LA\_HASH\_INIT) const; const typename LA\_traits<T>::scalartype checksum(const uint64\_t seed=0) const}{Hash of the exact contents and numerical checksum for comparison within a threshold, see section~\ref{sec:concepts:io}.}
\lafunc{const T sumsqr() const}{$\sum_i x_i^2$, 0 for an empty vector. No conjugation for complex types; use \code{norm()} for $\sqrt{\sum_i|x_i|^2}$.}
\lafunc{const T prod() const}{$\prod_i x_i$, 1 for an empty vector.}
\lafunc{NRVec<typename LA_traits<T>::normtype> abs() const}{Vector of $|x_i|$.}