-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patherror_handling.f90
57 lines (57 loc) · 1.65 KB
/
error_handling.f90
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
module matrix_mult_mod
implicit none
contains
subroutine mmult_1(a,b,c) ! set ierr with if-elseif: 8 lines
real , intent(in) :: a(:,:) ! (m,n) = shape
real , intent(in) :: b(:,:) ! (n,p)
real , intent(out) :: c(:,:) ! (m,p)
integer :: ierr
ierr = 0
if (size(a,1) /= size(c,1)) then
ierr = 1
else if (size(a,2) /= size(b,1)) then
ierr = 2
else if (size(b,2) /= size(c,2)) then
ierr = 3
end if
if (ierr /= 0) then
print*,"in mmult_1, ierr =",ierr," returning"
return
else
print*,"in mmult_1, ierr = 0, continuing" ! would not appear in a real code
end if
c = matmul(a,b)
end subroutine mmult_1
!
subroutine mmult_2(a,b,c) ! set ierr with findloc: 1 line
! matrix multiplication
real , intent(in) :: a(:,:) ! (m,n) = shape
real , intent(in) :: b(:,:) ! (n,p)
real , intent(out) :: c(:,:) ! (m,p)
integer :: ierr
! set ierr to the position of the first false element if any, otherwise 0
ierr = findloc([size(a,1)==size(c,1),size(a,2)==size(b,1),size(b,2)==size(c,2)],.false.,dim=1)
if (ierr /= 0) then
print*,"in mmult_2, ierr =",ierr," returning"
return
else
print*,"in mmult_2, ierr = 0, continuing" ! would not appear in a real code
end if
c = matmul(a,b)
end subroutine mmult_2
end module matrix_mult_mod
!
program main
use matrix_mult_mod, only: mmult_1, mmult_2
implicit none
integer, parameter :: m = 3, n = 5, p = 10
real :: a(m,n),b(n,p),c(m,p)
call mmult_1(a(2:,:),b,c)
call mmult_2(a(2:,:),b,c)
call mmult_1(a(:,2:),b,c)
call mmult_2(a(:,2:),b,c)
call mmult_1(a,b(:,2:),c)
call mmult_2(a,b(:,2:),c)
call mmult_1(a,b,c)
call mmult_2(a,b,c)
end program main