TSmallMatrixMultiply Class

Source: MtxVec.cs · Assembly: Dew.Math

public class TSmallMatrixMultiply

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

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:

  • Mul(a,b,c: TMtx);
  • Mul;
  • MulKernel
  • MulKernelFloat, MulKernelDouble, MulKernelComplexFloat, MulKernelComplexDouble

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.

Examples

csharp
procedure TForm84.Button2Click(Sender: TObject);
Matrix am;
Matrix bm;
Matrix cm;
Matrix cmRef;
TSmallMatrixMultiply jit;
TDoubleArray a;
TDoubleArray b;
TDoubleArray c;
jit = TSmallMatrixMultiply.Create;
try
    {
        am = [ new double[] {1  ,   2}, new double[] {2  ,   3} ];
        bm = [ new double[] {0.5, 1.5}, new double[] {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 !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 !cmRef.Equal(cm) then ERaise(@"Problem");

        // Option 3:
        a = new double[] {1  ,   2, 2  ,   3};
        b = new double[] { 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 !cmRef.Equal(cm) then ERaise(@"Problem");

        //alternative
    }
finally
    {
        jit.Free;
    }

Properties

NameTypeDescription
cColsInt32Field computed by Init method. Holds the row count of the matrix to hold the result.
cRowsInt32Field computed by Init method. Holds the row count of the matrix to hold the result.

Methods

NameDescription
FreeThe object is required to be freed manually.
FreeKernelFrees the internally jitted kernel
Init (4)Initialize matrix multiplication
Mul (2)Performs matrix multiplication optimized for small matrices