1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
! equivalent to openmp_28.f90
subroutine csr_matvec(Ap, Aj, Ax, x, y)
! Compute y = A*x for CSR matrix A and dense vectors x, y
integer, intent(in) :: Ap(:), Aj(:)
real, intent(in) :: Ax(:), x(:)
real, intent(inout) :: y(size(Ap)-1)
integer :: i, j
! Initialize y to zero
y = 0.0
do concurrent (i = 1:size(Ap)-1) shared(Ap, Aj, Ax, x, y) local(j)
do j = Ap(i), Ap(i+1)-1
y(i) = y(i) + Ax(j)*x(Aj(j))
end do
end do
print *, sum(y(1:10))
if (abs(sum(y(1:10)) - 950.00) > 1e-8) error stop
end subroutine
program do_concurrent_11
implicit none
interface
subroutine csr_matvec(Ap, Aj, Ax, x, y)
integer, intent(in) :: Ap(:), Aj(:)
real, intent(in) :: Ax(:), x(:)
real, intent(inout) :: y(size(Ap)-1)
end subroutine
end interface
integer, parameter :: n = 100
integer :: i
real :: x(n), y(n)
integer, allocatable :: Ap(:), Aj(:)
real, allocatable :: Ax(:)
! Initialize CSR matrix A
allocate(Ap(n+1), Aj(3*n), Ax(3*n))
Ap = [(3*(i-1)+1, i=1,n+1)]
Aj = [(mod(i-1, n)+1, i=1,3*n)]
Ax = [(1.0, 2.0, 3.0, i=1,n)]
! Initialize vector x
x = [(i, i=1,n)]
! Initialize y to zero
y = 0.0
! Compute y = A*x
call csr_matvec(Ap, Aj, Ax, x, y)
! Print the result
print *, sum(y)
if (abs(sum(y) - 30300.00) > 1e-8) error stop
end program
|