-
Notifications
You must be signed in to change notification settings - Fork 182
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
feat: extend intrinsic matmul
#951
Open
wassup05
wants to merge
11
commits into
fortran-lang:master
Choose a base branch
from
wassup05:matmul
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f06f556
add interface and procedures
wassup05 fed4d73
add implementation for 3,4,5 matrices
wassup05 27911ae
add very basic example
wassup05 a7f645c
fix typo
wassup05 cc77dee
a bit efficient
wassup05 3958018
refactor algorithm
wassup05 35a5a28
add new interface
wassup05 ebf92d7
add helper functions
wassup05 5f5c5a9
add implementation, refactor select to if clauses
wassup05 06ce735
slightly better examples
wassup05 e709f83
replace all matmul's by gemm
wassup05 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
ADD_EXAMPLE(sum) | ||
ADD_EXAMPLE(dot_product) | ||
ADD_EXAMPLE(dot_product) | ||
ADD_EXAMPLE(matmul) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
program example_matmul | ||
use stdlib_intrinsics, only: stdlib_matmul | ||
complex :: x(2, 2), y(2, 2) | ||
real :: r1(50, 100), r2(100, 40), r3(40, 50) | ||
real, allocatable :: res(:, :) | ||
x = reshape([(0, 0), (1, 0), (1, 0), (0, 0)], [2, 2]) | ||
y = reshape([(0, 0), (0, 1), (0, -1), (0, 0)], [2, 2]) ! pauli y-matrix | ||
|
||
print *, stdlib_matmul(y, y, y) ! should be y | ||
print *, stdlib_matmul(x, x, y, x) ! should be -i x sigma_z | ||
|
||
call random_seed() | ||
call random_number(r1) | ||
call random_number(r2) | ||
call random_number(r3) | ||
|
||
res = stdlib_matmul(r1, r2, r3) ! 50x50 matrix | ||
print *, shape(res) | ||
end program example_matmul |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
#:include "common.fypp" | ||
#:set I_KINDS_TYPES = list(zip(INT_KINDS, INT_TYPES, INT_KINDS)) | ||
#:set R_KINDS_TYPES = list(zip(REAL_KINDS, REAL_TYPES, REAL_SUFFIX)) | ||
#:set C_KINDS_TYPES = list(zip(CMPLX_KINDS, CMPLX_TYPES, CMPLX_SUFFIX)) | ||
|
||
submodule (stdlib_intrinsics) stdlib_intrinsics_matmul | ||
use stdlib_linalg_blas, only: gemm | ||
use stdlib_constants | ||
implicit none | ||
|
||
contains | ||
|
||
! Algorithm for the optimal parenthesization of matrices | ||
! Reference: Cormen, "Introduction to Algorithms", 4ed, ch-14, section-2 | ||
! Internal use only! | ||
pure function matmul_chain_order(p) result(s) | ||
integer, intent(in) :: p(:) | ||
integer :: s(1:size(p) - 2, 2:size(p) - 1), m(1:size(p) - 1, 1:size(p) - 1) | ||
integer :: n, l, i, j, k, q | ||
n = size(p) - 1 | ||
m(:,:) = 0 | ||
s(:,:) = 0 | ||
|
||
do l = 2, n | ||
do i = 1, n - l + 1 | ||
j = i + l - 1 | ||
m(i,j) = huge(1) | ||
|
||
do k = i, j - 1 | ||
q = m(i,k) + m(k+1,j) + p(i)*p(k+1)*p(j+1) | ||
|
||
if (q < m(i, j)) then | ||
m(i,j) = q | ||
s(i,j) = k | ||
end if | ||
end do | ||
end do | ||
end do | ||
end function matmul_chain_order | ||
|
||
#:for k, t, s in R_KINDS_TYPES + C_KINDS_TYPES | ||
|
||
pure function matmul_chain_mult_${s}$_3 (m1, m2, m3, start, s, p) result(r) | ||
${t}$, intent(in) :: m1(:,:), m2(:,:), m3(:,:) | ||
integer, intent(in) :: start, s(:,2:), p(:) | ||
${t}$, allocatable :: r(:,:), temp(:,:) | ||
integer :: ord, m, n, k | ||
ord = s(start, start + 2) | ||
allocate(r(p(start), p(start + 3))) | ||
|
||
if (ord == start) then | ||
! m1*(m2*m3) | ||
m = p(start + 1) | ||
n = p(start + 3) | ||
k = p(start + 2) | ||
allocate(temp(m,n)) | ||
call gemm('N', 'N', m, n, k, one_${s}$, m2, m, m3, k, zero_${s}$, temp, m) | ||
m = p(start) | ||
n = p(start + 3) | ||
k = p(start + 1) | ||
call gemm('N', 'N', m, n, k, one_${s}$, m1, m, temp, k, zero_${s}$, r, m) | ||
else if (ord == start + 1) then | ||
! (m1*m2)*m3 | ||
m = p(start) | ||
n = p(start + 2) | ||
k = p(start + 1) | ||
allocate(temp(m, n)) | ||
call gemm('N', 'N', m, n, k, one_${s}$, m1, m, m2, k, zero_${s}$, temp, m) | ||
m = p(start) | ||
n = p(start + 3) | ||
k = p(start + 1) | ||
call gemm('N', 'N', m, n, k, one_${s}$, temp, m, m3, k, zero_${s}$, r, m) | ||
else | ||
error stop "stdlib_matmul: error: unexpected s(i,j)" | ||
end if | ||
|
||
end function matmul_chain_mult_${s}$_3 | ||
|
||
pure function matmul_chain_mult_${s}$_4 (m1, m2, m3, m4, start, s, p) result(r) | ||
${t}$, intent(in) :: m1(:,:), m2(:,:), m3(:,:), m4(:,:) | ||
integer, intent(in) :: start, s(:,2:), p(:) | ||
${t}$, allocatable :: r(:,:), temp(:,:), temp1(:,:) | ||
integer :: ord, m, n, k | ||
ord = s(start, start + 3) | ||
allocate(r(p(start), p(start + 4))) | ||
|
||
if (ord == start) then | ||
! m1*(m2*m3*m4) | ||
temp = matmul_chain_mult_${s}$_3(m2, m3, m4, start + 1, s, p) | ||
m = p(start) | ||
n = p(start + 4) | ||
k = p(start + 1) | ||
call gemm('N', 'N', m, n, k, one_${s}$, m1, m, temp, k, zero_${s}$, r, m) | ||
else if (ord == start + 1) then | ||
! (m1*m2)*(m3*m4) | ||
m = p(start) | ||
n = p(start + 2) | ||
k = p(start + 1) | ||
allocate(temp(m,n)) | ||
call gemm('N', 'N', m, n, k, one_${s}$, m1, m, m2, k, zero_${s}$, temp, m) | ||
|
||
m = p(start + 2) | ||
n = p(start + 4) | ||
k = p(start + 3) | ||
allocate(temp1(m,n)) | ||
call gemm('N', 'N', m, n, k, one_${s}$, m3, m, m4, k, zero_${s}$, temp1, m) | ||
|
||
m = p(start) | ||
n = p(start + 4) | ||
k = p(start + 2) | ||
call gemm('N', 'N', m, n, k, one_${s}$, temp, m, temp1, k, zero_${s}$, r, m) | ||
else if (ord == start + 2) then | ||
! (m1*m2*m3)*m4 | ||
temp = matmul_chain_mult_${s}$_3(m1, m2, m3, start, s, p) | ||
m = p(start) | ||
n = p(start + 4) | ||
k = p(start + 3) | ||
call gemm('N', 'N', m, n, k, one_${s}$, temp, m, m4, k, zero_${s}$, r, m) | ||
else | ||
error stop "stdlib_matmul: error: unexpected s(i,j)" | ||
end if | ||
|
||
end function matmul_chain_mult_${s}$_4 | ||
|
||
pure module function stdlib_matmul_${s}$ (m1, m2, m3, m4, m5) result(r) | ||
${t}$, intent(in) :: m1(:,:), m2(:,:) | ||
${t}$, intent(in), optional :: m3(:,:), m4(:,:), m5(:,:) | ||
${t}$, allocatable :: r(:,:), temp(:,:), temp1(:,:) | ||
integer :: p(6), num_present, m, n, k | ||
integer, allocatable :: s(:,:) | ||
|
||
p(1) = size(m1, 1) | ||
p(2) = size(m2, 1) | ||
p(3) = size(m2, 2) | ||
|
||
num_present = 2 | ||
if (present(m3)) then | ||
p(3) = size(m3, 1) | ||
p(4) = size(m3, 2) | ||
num_present = num_present + 1 | ||
end if | ||
if (present(m4)) then | ||
p(4) = size(m4, 1) | ||
p(5) = size(m4, 2) | ||
num_present = num_present + 1 | ||
end if | ||
if (present(m5)) then | ||
p(5) = size(m5, 1) | ||
p(6) = size(m5, 2) | ||
num_present = num_present + 1 | ||
end if | ||
|
||
allocate(r(p(1), p(num_present + 1))) | ||
|
||
if (num_present == 2) then | ||
m = p(1) | ||
n = p(3) | ||
k = p(2) | ||
call gemm('N', 'N', m, n, k, one_${s}$, m1, m, m2, k, zero_${s}$, r, m) | ||
return | ||
end if | ||
|
||
! Now num_present >= 3 | ||
allocate(s(1:num_present - 1, 2:num_present)) | ||
|
||
s = matmul_chain_order(p(1: num_present + 1)) | ||
|
||
if (num_present == 3) then | ||
r = matmul_chain_mult_${s}$_3(m1, m2, m3, 1, s, p(1:4)) | ||
return | ||
else if (num_present == 4) then | ||
r = matmul_chain_mult_${s}$_4(m1, m2, m3, m4, 1, s, p(1:5)) | ||
return | ||
end if | ||
|
||
! Now num_present is 5 | ||
|
||
select case (s(1, 5)) | ||
case (1) | ||
! m1*(m2*m3*m4*m5) | ||
temp = matmul_chain_mult_${s}$_4(m2, m3, m4, m5, 2, s, p) | ||
m = p(1) | ||
n = p(6) | ||
k = p(2) | ||
call gemm('N', 'N', m, n, k, one_${s}$, m1, m, temp, k, zero_${s}$, r, m) | ||
case (2) | ||
! (m1*m2)*(m3*m4*m5) | ||
m = p(1) | ||
n = p(3) | ||
k = p(2) | ||
allocate(temp(m,n)) | ||
call gemm('N', 'N', m, n, k, one_${s}$, m1, m, m2, k, zero_${s}$, temp, m) | ||
|
||
temp1 = matmul_chain_mult_${s}$_3(m3, m4, m5, 3, s, p) | ||
|
||
k = n | ||
n = p(6) | ||
call gemm('N', 'N', m, n, k, one_${s}$, temp, m, temp1, k, zero_${s}$, r, m) | ||
case (3) | ||
! (m1*m2*m3)*(m4*m5) | ||
temp = matmul_chain_mult_${s}$_3(m1, m2, m3, 3, s, p) | ||
|
||
m = p(4) | ||
n = p(6) | ||
k = p(5) | ||
allocate(temp1(m,n)) | ||
call gemm('N', 'N', m, n, k, one_${s}$, m4, m, m5, k, zero_${s}$, temp1, m) | ||
|
||
k = m | ||
m = p(1) | ||
call gemm('N', 'N', m, n, k, one_${s}$, temp, m, temp1, k, zero_${s}$, r, m) | ||
case (4) | ||
! (m1*m2*m3*m4)*m5 | ||
temp = matmul_chain_mult_${s}$_4(m1, m2, m3, m4, 1, s, p) | ||
m = p(1) | ||
n = p(6) | ||
k = p(5) | ||
call gemm('N', 'N', m, n, k, one_${s}$, temp, m, m5, k, zero_${s}$, r, m) | ||
case default | ||
error stop "stdlib_matmul: error: unexpected s(i,j)" | ||
end select | ||
|
||
end function stdlib_matmul_${s}$ | ||
|
||
#:endfor | ||
end submodule stdlib_intrinsics_matmul |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good progress @wassup05, thank you! Imho this PR is almost ready to be merged. As you suggest, it would be good to have a nice wrapper for
gemm
. It has been discussed before. I would like to suggest that all calls togemm
are also wrapped into astdlib_matmul
function - now with two matrices only. This would give stdlib fully functional matmul functionality.Here I suggest two possible APIs, and I will ask @jalvesz @jvdp1 @loiseaujc to discuss that together:
The first would be similar to
gemm
and could use the matrix state definitions already in use for the sparse operations
stdlib/src/stdlib_sparse_constants.fypp
Lines 16 to 18 in 5c64ee6
The second would be more ambitious and essentially zero-overhead, it would wrap the operation in a derived type: (to be templated of course)
Then we could define a templated base interface
So the user writing code would have it clear:
we could even make it an operator:
without it triggering any actual data movement.