Skip to content

Benchmark and add direct nmod_mat products with transposed operands #2797

Description

@edgarcosta

Summary

Add or evaluate public operations for

C = A * B^T
C = A^T * B

without requiring callers to construct explicit transpose matrices.

This was an explicit item in TODO.md, noticed while removing that file in #2795.

The current public nmod_mat API exposes transpose and ordinary multiplication, but no direct transposed-operand products. In the classical ordinary multiplication code, one path already transposes the right operand into temporary contiguous storage. A specialized A * B^T kernel can instead take dot products of contiguous rows directly. The benefit of a specialized A^T * B path is less obvious and should be measured rather than assumed.

Dimensions

The proposed operations should have unambiguous dimension rules:

A * B^T:
    A is m x k
    B is n x k
    C is m x n

A^T * B:
    A is k x m
    B is k x n
    C is m x n

API names and the aliasing policy should be decided before implementation. Names should distinguish left-transpose from right-transpose without relying on ambiguous wording.

Benchmark-first scope

  1. Implement a small prototype or internal benchmark kernel for each product.
  2. Compare it with explicit nmod_mat_transpose followed by nmod_mat_mul.
  3. Measure square and rectangular shapes, small and large moduli, ordinary matrices and windows with nontrivial stride, and builds with and without BLAS.
  4. Separate classical, packed, threaded, Strassen, and BLAS regimes where possible.
  5. Add public APIs and dispatch only for regimes where the specialized path is measurably useful, or where API convenience alone is judged sufficient.

For BLAS-backed paths, transpose flags may avoid an additional transpose after conversion, but exactness bounds and conversion layout must remain unchanged or be re-established explicitly.

Correctness tests

For each operation:

  • Compare with an explicit transpose plus nmod_mat_mul over random dimensions and moduli.
  • Cover zero inner dimension and zero-row/zero-column outputs.
  • Cover square and highly rectangular matrices.
  • Cover matrix windows and nontrivial strides.
  • Test or explicitly reject output aliasing according to the documented policy.
  • Check dimension and modulus compatibility behavior.

Acceptance criteria

  • Benchmark data accompanies the implementation and identifies the dispatch regimes.
  • Specialized paths intended to avoid a transpose do not allocate a full transpose temporary in those regimes.
  • Results agree with explicit transpose plus ordinary multiplication.
  • The API documents dimensions, aliasing, and modulus requirements.

A valid resolution is to close the issue after benchmarking if no caller or regime shows enough benefit to justify maintaining specialized APIs or kernels.

Related: #1508 concerns broader basecase matrix multiplication performance and may be useful for coordinating benchmarks, but it does not provide these transposed-operand APIs.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions