-
Notifications
You must be signed in to change notification settings - Fork 191
feat: extend intrinsic matmul
#951
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
Open
wassup05
wants to merge
29
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 5 commits
Commits
Show all changes
29 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 cf5f030
add error handling in a better way
wassup05 b6d07e6
Update src/stdlib_intrinsics_matmul.fypp
perazz 5e3b588
Update src/stdlib_intrinsics_matmul.fypp
perazz 7d2130a
Update src/stdlib_intrinsics_matmul.fypp
perazz 61851fc
Update src/stdlib_intrinsics_matmul.fypp
perazz ee7da8d
Update src/stdlib_intrinsics_matmul.fypp
perazz 195c57e
Update src/stdlib_intrinsics_matmul.fypp
perazz e71b9bb
Update src/stdlib_intrinsics_matmul.fypp
perazz 00c4461
Update src/stdlib_intrinsics_matmul.fypp
perazz 5c2bbc5
Update src/stdlib_intrinsics_matmul.fypp
perazz 79113da
Update src/stdlib_intrinsics_matmul.fypp
perazz 72dc641
added specs
wassup05 cee5bba
added tests
wassup05 a052599
modified example and slight changes
wassup05 24c5787
Merge branch 'fortran-lang:master' into matmul
wassup05 0174145
Merge branch 'master' of github.com:wassup05/stdlib into matmul
wassup05 2d0d9ca
reduce size, increase tolerance
wassup05 ca3e470
Merge remote-tracking branch 'origin/matmul' into matmul
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 hidden or 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 hidden or 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,7 @@ | ||
program example_matmul | ||
use stdlib_intrinsics, only: stdlib_matmul | ||
complex :: a(2,2) | ||
a = reshape([(0, 0), (0, -1), (0, 1), (0, 0)], [2, 2]) ! pauli y-matrix | ||
|
||
print *, stdlib_matmul(a, a, a, a, a) ! should be sigma_y | ||
end program example_matmul |
This file contains hidden or 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 hidden or 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 hidden or 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,133 @@ | ||
#: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 | ||
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(n, p) result(s) | ||
integer, intent(in) :: n, p(:) | ||
integer :: s(1:n-1, 2:n), m(1:n, 1:n), l, i, j, k, q | ||
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 I_KINDS_TYPES + R_KINDS_TYPES + C_KINDS_TYPES | ||
|
||
pure module function stdlib_matmul_${s}$_3 (a, b, c) result(d) | ||
${t}$, intent(in) :: a(:,:), b(:,:), c(:,:) | ||
${t}$, allocatable :: d(:,:) | ||
integer :: sa(2), sb(2), sc(2), cost1, cost2 | ||
sa = shape(a) | ||
sb = shape(b) | ||
sc = shape(c) | ||
|
||
if ((sa(2) /= sb(1)) .or. (sb(2) /= sc(1))) then | ||
error stop "stdlib_matmul: Incompatible array shapes" | ||
end if | ||
|
||
! computes the cost (number of scalar multiplications required) | ||
! cost(A, B) = shape(A)(1) * shape(A)(2) * shape(B)(2) | ||
cost1 = sa(1) * sa(2) * sb(2) + sa(1) * sb(2) * sc(2) ! ((AB)C) | ||
cost2 = sb(1) * sb(2) * sc(2) + sa(1) * sa(2) * sc(2) ! (A(BC)) | ||
|
||
if (cost1 < cost2) then | ||
d = matmul(matmul(a, b), c) | ||
else | ||
d = matmul(a, matmul(b, c)) | ||
end if | ||
end function stdlib_matmul_${s}$_3 | ||
|
||
pure module function stdlib_matmul_${s}$_4 (a, b, c, d) result(e) | ||
${t}$, intent(in) :: a(:,:), b(:,:), c(:,:), d(:,:) | ||
${t}$, allocatable :: e(:,:) | ||
integer :: p(5), i | ||
integer :: s(3,2:4) | ||
|
||
p(1) = size(a, 1) | ||
p(2) = size(b, 1) | ||
p(3) = size(c, 1) | ||
p(4) = size(d, 1) | ||
p(5) = size(d, 2) | ||
|
||
s = matmul_chain_order(4, p) | ||
|
||
select case (s(1,4)) | ||
case (1) | ||
select case (s(2, 4)) | ||
case (2) | ||
e = matmul(a, matmul(b, matmul(c, d))) | ||
case (3) | ||
e = matmul(a, matmul(matmul(b, c), d)) | ||
case default | ||
error stop "stdlib_matmul: unexpected error unexpected s(i,j)" | ||
end select | ||
case (2) | ||
e = matmul(matmul(a, b), matmul(c, d)) | ||
case (3) | ||
select case (s(1, 3)) | ||
case (1) | ||
e = matmul(matmul(a, matmul(b, c)), d) | ||
case (2) | ||
e = matmul(matmul(matmul(a, b), c), d) | ||
case default | ||
error stop "stdlib_matmul: unexpected error unexpected s(i,j)" | ||
end select | ||
case default | ||
error stop "stdlib_matmul: unexpected error unexpected s(i,j)" | ||
end select | ||
end function stdlib_matmul_${s}$_4 | ||
|
||
pure module function stdlib_matmul_${s}$_5 (a, b, c, d, e) result(f) | ||
${t}$, intent(in) :: a(:,:), b(:,:), c(:,:), d(:,:), e(:,:) | ||
${t}$, allocatable :: f(:,:) | ||
integer :: p(6), i | ||
integer :: s(4,2:5) | ||
|
||
p(1) = size(a, 1) | ||
p(2) = size(b, 1) | ||
p(3) = size(c, 1) | ||
p(4) = size(d, 1) | ||
p(5) = size(e, 1) | ||
p(6) = size(e, 2) | ||
|
||
s = matmul_chain_order(5, p) | ||
|
||
select case (s(1,5)) | ||
case (1) | ||
f = matmul(a, stdlib_matmul(b, c, d, e)) | ||
case (2) | ||
f = matmul(matmul(a, b), stdlib_matmul(c, d, e)) | ||
case (3) | ||
f = matmul(stdlib_matmul(a, b ,c), matmul(d, e)) | ||
case (4) | ||
f = matmul(stdlib_matmul(a, b, c, d), e) | ||
case default | ||
error stop "stdlib_matmul: unexpected error unexpected s(i,j)" | ||
end select | ||
end function stdlib_matmul_${s}$_5 | ||
|
||
#: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.
Uh oh!
There was an error while loading. Please reload this page.