uses MtxExpr,
MtxVec,
MtxExprInt,
MtxForLoop;
var Data, cmpData, nearDistance: Matrix;
nearIdx: MatrixInt;
threads:
TMtxForLoop;
begin
// 1.) Create once ahead of time. This is the thread pool and is slow to create.
threads := TMtxForLoop.Create;
threads.ThreadCount := Controller.CpuCores*2;
//hyperthreaded core count
threads.BlockGranularity := 4;
//required
// 2.) This sets the "minimum" required. Also slow and needs to be set upfront and
// it can only be done before any allocations are made by MtxVec.
Controller.ThreadDimension :=
Max(threads.ThreadCount + 1, Controller.ThreadDimension);
// 3.) Compiler assertions switch needs to be "off", when compling the source code
data := [[1,2,3],
[2,3,1],
[3,2,1]];
cmpData := [[2,2,3],
[2,3,4]];
ClusteredKNN(threads, Data, cmpData, 2, nearIdx, nearDistance);
// Results:
// nearIdx = [[0, 1], // data rows 0 and 1 are closest vectors to cmpData row 0.
// [0, 1]] // data rows 0 and 1 are closest vectors to cmpData row 1.
// nearDistance = [[1, 5], // [0,0] = sqr(2-1) + sqr(2-2) + sqr(3-3), distance from cmpData row 0 to Data row 0 is 1
// [3, 9]] // [1,0] = sqr(2-1) + sqr(3-2) + sqr(4-3), distance from cmpData row 1 to Data row 0 is 3
threads.Free;