Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the intrinsic matmul to handle more than just two matrices. #931

Open
loiseaujc opened this issue Feb 6, 2025 · 0 comments
Open
Labels
idea Proposition of an idea and opening an issue to discuss it

Comments

@loiseaujc
Copy link

Motivation

The Fortran intrinisc matmul can handle only two matrices, i.e. result = matmul(A, B). While working on problems involving multiple low-rank matrices, I've been faced recently with expressions like result = A * B * C, or result = A * B * C * D, where A, B, C and D tend to be tall or wide matrices. Depending on how you evaluate these matrix-matrix multiplications, you can either have a pretty fast code or a terribly slow one.

Except for myself, would it be of interest to anyone to extend the intrinsic matmul to handle expressions involving multiple matrices (say between 2 and 5, I don't think there are situations where you need much more than that). The corresponding interfaces would simply be

result = matmul(A, B) ! Intrinsic matmul
result = matmul(A, B, C)
result = matmul(A, B, C, D)
result = matmul(A, B, C, D, E)

Given the dimensions of each array, the optimal order in which to perform the operations can be determined by solving the corresponding optimization problem using dynamic programming. Some details can be found here.

Prior Art

  • numpy exposes the numpy.linalg.multi_dot(arrays, *, out=None) function which chains calls to numpy.dot using the optimal parenthesization of the matrices.

  • I'm pretty sure julia has a similar feature but a quick search didn't bring actual references to it

As far as I know, Fortran does not allow for arbitrary long lists of dummy arguments (except for intrisic max and min and a couple of others I suppose). In practice, I don't think the extended matmul interface would need to handle more than say 5-ish arrays though.

Additional Information

Pinging @perazz, @jvdp1 and @jalvesz.

@loiseaujc loiseaujc added the idea Proposition of an idea and opening an issue to discuss it label Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idea Proposition of an idea and opening an issue to discuss it
Projects
None yet
Development

No branches or pull requests

1 participant