What does the mtimes function do in MATLAB / RunMat?
mtimes(A, B) implements MATLAB's matrix multiplication operator (A * B). It supports
scalars, vectors, matrices, and complex tensors while preserving MATLAB's column-major
layout and dimension rules.
How does the mtimes function behave in MATLAB / RunMat?
- The inner dimensions must match:
size(A, 2) == size(B, 1)for 2-D arrays. N-D tensors flatten the leading two dimensions into a matrix slice so MATLAB-style broadcasting semantics stay intact. - Row vectors (
1×N) times column vectors (N×1) evaluate to a scalar; column-by-row produces the familiar outer product (N×1·1×M→N×M). - Scalars multiply every element of the other operand without changing shape; logical inputs are first
converted to double precision (
true → 1,false → 0). - Complex scalars, matrices, and tensors use full complex arithmetic, including real/complex mixes that promote the result to a complex tensor when necessary.
- Empty matrices follow MATLAB semantics: multiplying an
m×0by0×nyields anm×nzero matrix and any mismatch in inner dimensions raisesInner matrix dimensions must agree. - When either input is GPU-resident, RunMat consults the active acceleration provider; if its
matmulhook supports the operands the computation stays on device, otherwise the runtime gathers inputs and executes the CPU fallback transparently.
mtimes Function GPU Execution Behaviour
- The native auto-offload planner checks the active acceleration provider. When a backend with a
matmulhook (for example, the WGPU provider) is registered, RunMat dispatches the operation there, keeping gpuArray inputs and the result resident on the device. - Mixed-residency calls automatically upload host tensors to the provider before invoking
matmul, while pure scalar operands use the provider'sscalar_mulhook to avoid unnecessary transfers. - If no GPU provider is registered or the backend declines the request (unsupported precision, shape,
or size), RunMat gathers any gpuArray inputs, executes the CPU fallback in this module, and returns a
host tensor. Reapply
gpuArrayif you need the result back on the device.
Examples of using the mtimes function in MATLAB / RunMat
Multiply two 2-D matrices
A = [1 2 3; 4 5 6];
B = [7 8; 9 10; 11 12];
C = A * B;
Expected output:
C = [58 64; 139 154];
Compute a dot product with row and column vectors
u = [1 2 3];
v = [4; 5; 6];
dotVal = u * v;
Expected output:
dotVal = 32;
Scale a matrix by a scalar using mtimes
S = 0.5 * eye(3);
Expected output:
S =
0.5000 0 0
0 0.5000 0
0 0 0.5000
Multiply complex matrices
A = [1+2i 3-4i; 5+6i 7+8i];
B = [1-1i; 2+2i];
C = A * B;
Expected output:
C =
17 - 1i
9 + 31i
Perform matrix multiplication on GPU arrays
G1 = gpuArray([1 2; 3 4]);
G2 = gpuArray([5; 6]);
G = G1 * G2;
result = gather(G);
Expected output:
isa(G, 'gpuArray') % logical 1
result =
17
39
Dimension mismatch raises a MATLAB-style error
A = rand(2, 3);
B = rand(4, 2);
C = A * B;
Expected output:
Error using *
Inner matrix dimensions must agree.
GPU residency in RunMat (Do I need gpuArray?)
When both operands already live on the GPU, the provider keeps intermediate buffers and the final
result on the device. If RunMat needs to fall back to the CPU it gathers any gpuArray inputs,
performs the multiplication, and returns a host tensor—apply gpuArray to the result if subsequent
steps must stay on the device. Auto-offload heuristics will continue to expand, so explicit residency
control is rarely required.
FAQ
How is mtimes different from times (.*)?
mtimes performs matrix multiplication (dot products, GEMM). Use .* for element-wise products with
implicit expansion.
What happens when inner dimensions do not match?
RunMat raises Inner matrix dimensions must agree, matching MATLAB's error identifier and message.
Does mtimes support scalars and matrices together?
Yes. Scalars multiply every element of the matrix, returning a matrix of the same size.
Are complex numbers fully supported?
Yes. Mixed real/complex operands produce complex outputs using MATLAB's arithmetic rules.
Will results stay on the GPU?
When a provider implements matmul, results remain device-resident. Otherwise RunMat gathers data,
computes on the CPU, and returns a host tensor.
Do vectors need to be explicitly shaped?
Like MATLAB, row vectors must be 1×N and column vectors N×1. Use .' or (:) to reshape when needed.
Does RunMat use BLAS?
Yes. The host implementation uses RunMat's optimized inner loops today and will leverage BLAS/LAPACK when the optional feature is enabled.
Can mtimes fuse with other GPU ops?
Providers may fuse GEMM with adjacent operations; otherwise fusion falls back to the standard kernels.
See Also
eye, zeros, ones, sum, gpuArray, gather
Source & Feedback
- Implementation:
crates/runmat-runtime/src/builtins/math/linalg/ops/mtimes.rs - Found a bug or behavioural difference? Please open an issue with a minimal repro.