THE INNER LOOP OF A SINGLE-SERVER LATTICE PIR ANSWER.
A PIR server answers a query by multiplying its whole database, read as a
matrix of 32-bit words, by the client's query vector. Every element of the
matrix is touched once per query and nothing is reused, so the cost of a
lookup is the size of the database divided by the rate at which memory can
be read, and the arithmetic has to keep up with that rather than set it.
Two products are needed:
MatVec Result[i] := sum over j of Matrix[i,j] * Vector[j]
MatVecT Result[j] := sum over i of Matrix[i,j] * Vector[i]
Both are taken modulo 2^32, which is what a UInt32 multiply and a UInt32
addition already do. That is not a convenience: the schemes these serve
choose q = 2^32 precisely so that the modular reduction is the machine's
own wrap-around and never a division. The unit is compiled with overflow
and range checking off (the Q- and R- above) because the wrap IS the
arithmetic; a build that switched them on would raise on correct input.
THE MATRIX IS ROW-MAJOR, and both products read it strictly in storage
order. MatVec walks one row at a time and keeps a scalar accumulator, so
the vector it reads is the short one and stays in cache. MatVecT walks the
same bytes in the same order and instead accumulates into a result vector
of one element per column, which is the array that must stay in cache. So
a single layout serves both directions and neither ever strides. The
operand that has to be cache-resident differs - the vector for MatVec, the
result for MatVecT - and both are short beside the matrix.
Addition modulo 2^32 is associative and commutative, so the order in which
the products of a row are summed does not affect the result. That is what
licenses both the vectorisation of these loops and their partitioning
across threads: MatVec rows and MatVecT columns can be split without
synchronisation, and every arrangement yields the same words.
MEMORY IS THE LIMIT, NOT ARITHMETIC - EVENTUALLY. Measured on a development
box whose memory system delivers about 50 GB/s to four threads, the portable
loop reaches 9 to 10 GB/s per thread under dcc64 and 21 to 26 GB/s per thread
under dcclinux64, whose back end vectorises it on its own. So at low thread
counts it is the scalar loop and not the memory that binds on Win64, which is
what the AVX2 bodies below are for.
WHAT THE AVX2 ARM IS WORTH DEPENDS ON THE MACHINE, and on Linux64 it depends
on it entirely, because there the portable loop is already vectorised. Where
something else binds first - memory bandwidth, or a vCPU shared with another
tenant - the two arms measure the same to within the noise. On a core with
the memory system to itself the AVX2 arm is worth about a fifth. Neither
figure generalises, which is why the harness reports both arms and the demo
measures them on the machine in front of you rather than quoting a number.
A rate below the machine's bandwidth at high thread counts is not about
either arm: look at the traffic before the loop. Reading the matrix once per
query is inherent, reading it twice is not.
THE SCAN ON AVX2. Both products have AVX2 bodies, one row at a time, in
PirAvx2RowDot.inc and PirAvx2RowAxpy.inc. Each body is written once against a
neutral register assignment and is included by three hosts:
Delphi Win64, Delphi 11 or later - built-in assembler below. Earlier
dcc64 releases do not know the AVX instruction set.
FPC on System V x86-64 - FPC assembler below.
Delphi Linux64 - dcclinux64 has no assembler, so the bodies come from
stPirAvx2Func.o, assembled by FPC from stPirAvx2Func.pas and linked by
declaration. The object must be on the object path (-O, DCC_ObjPath)
beside stAESNIFunc.o; see the LINUX64 notes in stAESNI.pas for why
neither $L nor $LINK can be used. That object is a build product of the
.inc files, so an edit to either must be followed by a rebuild of the
object with the FPC steps in stPirAvx2Func.pas, or Delphi Linux64 keeps
running the old code.
The availability check is MLKEMAvx2Probe.inc, which asks the processor and
the operating system about AVX2 and the YMM state and is not specific to the
scheme it is named for.
AVX2 is not part of the x86-64 baseline. Initialization runs that check and
the portable Pascal code runs wherever it fails. ST4_NO_AVX2 - or the
library-wide ST4_NO_ASM - compiles the AVX2 code out, which also removes the
object from a Linux64 link. All targets produce identical results: the lanes
are summed in a different order from the portable loop, which changes nothing
because addition modulo 2^32 is associative and commutative.Feature notes: Private information retrieval notes (SimplePIR / DoublePIR)
inherits from Exception
Root of the PIR exception hierarchy. Declared in this unit because it is the deepest of the StreamSec.PIR units, so every other one already reaches it without a new dependency.
(no public members)
class procedure MatVec(const aMatrix: pPirWord; aRowCount, aColCount: Integer; const aVector : pPirWord; const aResult : pPirWord); overload; static;
class procedure MatVecT(const aMatrix: pPirWord; aRowCount, aColCount: Integer; const aVector : pPirWord; const aResult : pPirWord); overload; static;
class procedure MatVec(const aMatrix: tPirWordArray; aRowCount, aColCount: Integer; const aVector : tPirWordArray; var aResult : tPirWordArray); overload; static;
class procedure MatVecT(const aMatrix: tPirWordArray; aRowCount, aColCount: Integer; const aVector : tPirWordArray; var aResult : tPirWordArray); overload; static;
class function Avx2Available: Boolean; static;
class function GetUseAvx2: Boolean; static;
class procedure SetUseAvx2(Value: Boolean); static;
The word the schemes compute in. q = 2^32 is not a parameter of this unit: the reduction is the type's own wrap-around, so a different modulus would be a different kernel rather than a different constant.