You are here: Symbol Reference > MtxVec Namespace > Classes > TSmallMatrixMultiply Class
MtxVec VCL
ContentsIndex
PreviousUpNext
TSmallMatrixMultiply Class

Provides interface for multiplying small matrices by using the lapack's dgemm api standard.)

MtxVec_TSmallMatrixMultiply
Pascal
TSmallMatrixMultiply = class;

The matrix multiplication code is generated on the fly to achieve maximum possible performance. Additionally all the error checking on each consecutive call is also absent. This is most usefull, when the matrices to be multiplied are very small: 2x2 or 3x3, (100x) but there are still considerable benefits up to size 50x50 (1.3x) and more. 

Typically this object would be created, then Init method is to be called and finally, one of the six Mul methods can be called multiple times.The class provides four variants of Multiply methods:

It is most safe to use the first variant. Performance will increase with the parameterless Mul method (2x) and best performance can be obtained with the third variant (3x). Only switch to using the third variant when you are confident, that your algorithm works correctly. 

Please note that 

TMtx.Mul(A,B); 

will use object cache to store JIT-ed kernels up to user specified matrix size for square matrices. The default limit is set at size 32x32. The kernel will be created on first call.

Three different options to setup matrix multiplication of small matrices

uses MtxVec, MtxExpr, Math387; procedure TForm84.Button2Click(Sender: TObject); var am, bm, cm, cmRef: Matrix; jit: TSmallMatrixMultiply; a,b,c: TDoubleArray; begin jit := TSmallMatrixMultiply.Create; try am := [ [1 , 2], [2 , 3] ]; bm := [ [0.5, 1.5], [2.5, 3.5] ]; // Compute with TMtx cmRef.Mul(am, bm); //standard method optimized for large matrices jit.Init(am, bm, cm, opNone, opNone, 1, 0); // Option 1: jit.Mul; //reuse memory allocation by am, cm and bm if not cmRef.Equal(cm) then ERaise('Problem'); // Option 2: jit.Mul(am, bm, cm); // am, cm, bm can use different memory on each call, but a bit slower if not cmRef.Equal(cm) then ERaise('Problem'); // Option 3: a := [1 , 2, 2 , 3]; b := [ 0.5, 1.5, 2.5, 3.5 ]; jit.Init(2, 2, 2, 2, mvDouble, opNone, opNone, 1, 0); SetLength(c, 4); //allocate storage to hold the result jit.MulKernelDouble(jit.Jitter, a, b, c); if not cmRef.Equal(cm) then ERaise('Problem'); //alternative finally jit.Free; end; end;
Examples on GitHub
Copyright (c) 1999-2025 by Dew Research. All rights reserved.
What do you think about this topic? Send feedback!