Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Matrix as a formal parameter

gryvon
Beginner
564 Views

If an array is described in a subroutine larger than in the main program, it is not transferred correctly. Here is the source.

--------------------------------------------------------

program bpro
implicit none
external :: bsub
integer*8 :: i, j
real*8, dimension(1:3, 1:3) :: a
a(1, 1) = 1.0d0
a(1, 2) = 2.0d0
a(1, 3) = 3.0d0
a(2, 1) = 4.0d0
a(2, 2) = 5.0d0
a(2, 3) = 6.0d0
a(3, 1) = 7.0d0
a(3, 2) = 8.0d0
a(3, 3) = 9.0d0
write(6, '(3D25.16)') ((a(i, j), j = 1, 3), i = 1, 3)
write(6, *)
call bsub(a)
stop
end program bpro
--------------------------------------------------------

subroutine bsub(array)
implicit none
real*8, dimension(1:10, 1:10) :: array
integer*8 i, j
write(6, '(3D25.16)') ((array(i, j), j = 1, 3), i = 1, 3)
return
end subroutine bsub
--------------------------------------------------------

Screen output:

0.1000000000000000D+01 0.2000000000000000D+01 0.3000000000000000D+01
0.4000000000000000D+01 0.5000000000000000D+01 0.6000000000000000D+01
0.7000000000000000D+01 0.8000000000000000D+01 0.9000000000000000D+01

0.1000000000000000D+01 0.0000000000000000D+00 0.0000000000000000D+00
0.4000000000000000D+01 0.0000000000000000D+00 0.0000000000000000D+00
0.7000000000000000D+01 0.0000000000000000D+00 0.0000000000000000D+00

Please comment on this... Sure, I can overcome this by a more careful definition of the matrix, e.g. like

subroutine bsub(array, n)
implicit none
real*8, dimension(1:n, 1:n) :: array

orinanother way. But I expected the simple source above towork properly.

0 Kudos
2 Replies
grg99
Beginner
564 Views
The problem is you explicitly declared "bsub" as "external". If you leave off that declaration then the compiler will complain about a missing definition for "bsub". Then if you define bsub, either by placing it in the same source file, or declaring ti properly in an "interface", then the compiler will be better able to warn you about mismatches like this.

To fix the basic problem, declare bsub as taking a ( : , : )size array. Or as you suggested, pass in the dimensions as a argument.



0 Kudos
Steven_L_Intel1
Employee
564 Views
The compiler can warn you of this coding error if you specify the /gen_interface and /warn:interface options.

t.f90(17) : Error: The storage extent of the dummy argument exceeds that of the
actual argument.
call bsub(a)
----------------^

Otherwise, the compiler doesn't have the information to connect the two and diagnose the error.
0 Kudos
Reply