Class ReDeMatMultDispatcher

java.lang.Object
org.flag4j.linalg.ops.dispatch.BiTensorOpDispatcher<Matrix,Matrix,Matrix>
org.flag4j.linalg.ops.dispatch.ReDeMatMultDispatcher

public final class ReDeMatMultDispatcher extends BiTensorOpDispatcher<Matrix,Matrix,Matrix>

A dispatcher that selects the most suitable matrix multiplication kernel for two real dense matrices.

This class implements a threshold- and shape-based decision tree to optimize performance for various matrix multiplication scenarios (e.g., small matrices, square matrices, wide/tall matrices, matrix-vector products, etc.). It maintains a cache of recently used shape/kernels to further improve performance for repeated multiplication patterns.

Usage:

  • Use dispatch(Matrix, Matrix) to multiply two matrices, dynamically choosing the best kernel.
  • This class is a singleton; call getInstance() to retrieve the instance if you need direct access to the underlying dispatcher.

Configuration:

The dispatcher reads various thresholds (e.g., ASPECT_THRESH, SML_THRESH, etc.) from ReDeMatMultDispatchConfigs, allowing external tuning without modifying code.

Thread Safety:

  • The dispatcher itself does not maintain mutable state beyond a cached kernel lookup, which is also thread-safe.
  • dispatch(Matrix, Matrix) is safe to call from multiple threads.
See Also:
  • Method Details

    • validateShapes

      protected void validateShapes(Shape aShape, Shape bShape)
      Validates the shapes are valid for the operation.
      Specified by:
      validateShapes in class BiTensorOpDispatcher<Matrix,Matrix,Matrix>
      Parameters:
      aShape - Shape of first tensor in the operation.
      bShape - Shape of second tensor in the operation.
    • getInstance

      public static ReDeMatMultDispatcher getInstance()
      Gets the singleton instance of this class. If this class has not been instanced, a new instance will be created.
      Returns:
      The singleton instance of this class.
    • dispatch

      public static Matrix dispatch(Matrix a, Matrix b)
      Dispatches the multiplication of two matrices to an appropriate implementation based on the shapes of the two matrices.
      Parameters:
      a - Left matrix in the matrix multiplication.
      b - Right matrix in the matrix multiplication.
      Returns:
      The matrix product of a and b.
    • getFunc

      protected BiFunction<Matrix,Matrix,Matrix> getFunc(Shape aShape, Shape bShape, int data1Length, int data2Length)
      Computes the appropriate function to use when computing the matrix multiplication between two matrices.
      Specified by:
      getFunc in class BiTensorOpDispatcher<Matrix,Matrix,Matrix>
      Parameters:
      aShape - Shape of the first matrix in the matrix multiplication problem.
      bShape - Shape of the second matrix in the matrix multiplication problem.
      data1Length - Full length of the data array within the first matrix.
      data2Length - Full length of the data array within the second matrix.
      Returns:
      The appropriate function to use when computing the matrix multiplication between two matrices.