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

Intent(in) variable is changing without any error or warning

forcpp
New Contributor I
1,667 Views
I am working on an optimization problem and using Harwell subroutine to solve it. I am facing a strange bug in my programs. The difficulty is that an intent(in) variable changes its values without any warning or error.
[fortran]subroutine optimize(aij, X, wt)

implicit none 
real(kind=8), dimension(4, 10), intent(in) :: aij
real(kind=8), dimension(10), intent(out) :: X
real(kind=8), dimension(4), intent(in) :: wt

print*, "a",wt(1), wt(2), wt(3), wt(4)

CALL LA04AD(A,IA,IRN,IP,M,N,B,C,BND,KB,LB,JOB,CNTL,IX,JX,X,Z,G, &
&           RINFO,WS,LWS,IWS,LIWS)
print*, "b",wt(1), wt(2), wt(3), wt(4)

end subroutine optimize[/fortran]
when i compile and run, the following output is coming:
[bash]a 1.50000000000000    0.500000000000000    2.000000000000000 0.000000000000000[/bash]
[bash]b 0.00000000000000 0.000000000000000 0.000000000000000 0.0000000000000000[/bash]
I can't understand why the call statement "CALL LA04AD()" is changing the values of wt array which is declared as intent(in).
I also checked with pointer and find the values at the location of wt array is changing.
0 Kudos
1 Solution
mecej4
Honored Contributor III
1,667 Views
You have a misconception as to what INTENT(IN), etc. imply.

The declaration of intent tells the compiler that, with regard to an argument to your subroutine OPTIMIZE with INTENT(IN), it is your intention that it will not be changed in the subroutine (and, possibly, other subroutines and functions to which that variable is handed over by OPTIMIZE). There is no requirement that the compiler check for violations of the intention in a different subprogram than the one containing the INTENT declaration. In other words, if you give false information to the compiler, you will be rewarded with consequences.

The compiler will refuse to produce an object file if it can observe a violation of the declared intention in OPTIMIZE.

However, Fortran allows separate compilation and implicit interfaces; for older code such as LA04, you may have available only an implicit interface. In such cases, the code in LA04 knows nothing about your intentions as declared in its caller and, if you do not pass it a correct and consistent set of arguments, INTENT(IN) variables in the caller can get modified in LA04. Nevertheless, many current compilers allow compile time and/or run-time checks across subroutines, but you need to specify appropriate compiler options and accept the reduced performance that results from the extra checking code.

What you saw about changes to WT indicates that X or AIJ may not have correct sizes or shapes; subscript overruns in these arrays can cause spillover writes into WT.

If you provide a complete example, or at least the declarations of all the arguments to LA04AD, I'll be happy to take a look.

View solution in original post

0 Kudos
2 Replies
mecej4
Honored Contributor III
1,668 Views
You have a misconception as to what INTENT(IN), etc. imply.

The declaration of intent tells the compiler that, with regard to an argument to your subroutine OPTIMIZE with INTENT(IN), it is your intention that it will not be changed in the subroutine (and, possibly, other subroutines and functions to which that variable is handed over by OPTIMIZE). There is no requirement that the compiler check for violations of the intention in a different subprogram than the one containing the INTENT declaration. In other words, if you give false information to the compiler, you will be rewarded with consequences.

The compiler will refuse to produce an object file if it can observe a violation of the declared intention in OPTIMIZE.

However, Fortran allows separate compilation and implicit interfaces; for older code such as LA04, you may have available only an implicit interface. In such cases, the code in LA04 knows nothing about your intentions as declared in its caller and, if you do not pass it a correct and consistent set of arguments, INTENT(IN) variables in the caller can get modified in LA04. Nevertheless, many current compilers allow compile time and/or run-time checks across subroutines, but you need to specify appropriate compiler options and accept the reduced performance that results from the extra checking code.

What you saw about changes to WT indicates that X or AIJ may not have correct sizes or shapes; subscript overruns in these arrays can cause spillover writes into WT.

If you provide a complete example, or at least the declarations of all the arguments to LA04AD, I'll be happy to take a look.
0 Kudos
forcpp
New Contributor I
1,667 Views
Thank you very muchmecej4.Actually, I was giving the dimension of x array 10 as in my problem only 10 distinct variables are present. But it should be minimally 14(=4+10) as required by LA04D.
0 Kudos
Reply