Fortran Why Do Commented Write Statements Affect FORTRAN SDP Output?

  • Thread starter Thread starter kingfisher
  • Start date Start date
  • Tags Tags
    Effects Fortran
AI Thread Summary
The discussion revolves around a stochastic dynamic programming issue in a FORTRAN program, where commenting out certain write statements affects the final output array. The user noted that when specific write statements are commented out, the output changes, which was unexpected. A key point raised is the potential uninitialized state of the array "Of(cin, fst)" before its use in calculations, which could lead to different outputs depending on compiler behavior. It was suggested that some compilers may initialize memory differently, resulting in varying outputs. The resolution involved initializing the array, which clarified the issue. Additionally, one participant could not replicate the behavior, indicating that compiler differences might influence how memory is allocated and initialized.
kingfisher
Messages
2
Reaction score
0
I have a stochastic dynamic program with markov process written in FORTRAN, below. If you comment out the write statement that says "uh, THIS SHOULD MAKE NO DIFFERENCE" and/or some of the other write statements that are just text, no variables being written, you will see a difference in the final array output as compared to when you do not comment any out.

experiment with commenting some of the write statements out/bringing them back in and look at the output array.

can anyone explain this? it happens on more than one computer.


PROGRAM HW4A
IMPLICIT NONE
REAL :: S !current storage
REAL :: LI !previous inflow
REAL :: R !planned current release
REAL :: P !penalty based on planned current release
REAL :: oops !cost of releasing less than planned release
REAL :: f !future costs sum
REAL :: OFP !optimum future costs
REAL :: OCR !optimum current release

INTEGER :: t,st,lin,cin,fst,re !counter indexes for loops and array addresses
!stage, storage, last inflow, future storage, release

!row is current inflow, column is current storage
REAL, DIMENSION(5,0:7) :: Of !best future costs for storage and inflow
REAL, DIMENSION(5,0:7) :: OR !best release for storage and inflow

REAL, DIMENSION(5) :: PIC !probabilities of next inflows




t=20
! WRITE (*,*)"Number of Stages to run?"
! READ(*,*)t
! WRITE (*,*)" Based on",t,"year SDP"
DO !for each t
st=0 !reset current storage


DO !for each S
lin=1 !reset previous inflow


DO !for each I
re=0 !reset current release
LI=lin*10.
OFP=9999999 !reset optimum future cost
CALL INFLOWPROB(LI,PIC)

DO !for each R
R=re*10.
CALL PENALTY (R,P)
f=0 !reset future costs accumulator
cin=1 !reset current inflow

DO !for each current inflow
fst=st-re+cin
IF(fst<0)THEN
oops=5000
fst=0
ELSE
oops=0
END IF
IF(fst>7)fst=7
f=f+PIC(cin)*(Of(cin,fst)+oops)
IF(cin==5)EXIT
cin=cin+1
END DO

IF(OFP>P+f)THEN
OFP=P+f
OCR=R
END IF
IF(re==5)EXIT
re=re+1

END DO
Of(lin,st)=OFP
OR(lin,st)=OCR
! WRITE(*,*)Of(3,3)
IF(lin==5)EXIT
lin=lin+1

END DO

IF(st==7)EXIT
st=st+1

END DO
! WRITE (*,*)" ____________STORAGE____________"
! WRITE (*,*)" 0 10 20 30 40 50 60 70"
! WRITE (*,*)" ________________________________"
! WRITE (*,*)"I 10:",NINT(Of(1,:))
! WRITE (*,*)"N 20:",NINT(Of(2,:))
! WRITE (*,*)"F 30:",NINT(Of(3,:))
! WRITE (*,*)"L 40:",NINT(Of(4,:))
! WRITE (*,*)"O 50:",NINT(Of(5,:))
! WRITE(*,100)Of
!100 FORMAT(1X,5(8F10.0,/),/)
IF(t==1)EXIT
t=t-1
END DO


write(*,*)"uh, THIS SHOULD MAKE NO DIFFERENCE!"
WRITE (*,*)" Advised Releases based on"
WRITE (*,*)" Last Season Inflow and Current Storage"
WRITE (*,*)" ____________STORAGE____________"
WRITE (*,*)" 0 10 20 30 40 50 60 70"
WRITE (*,*)" ________________________________"
WRITE (*,*)"I 10:",OR(1,:)
WRITE (*,*)"N 20:",OR(2,:)
WRITE (*,*)"F 30:",OR(3,:)
WRITE (*,*)"L 40:",OR(4,:)
WRITE (*,*)"O 50:",OR(5,:)


END PROGRAM HW4A

!
SUBROUTINE PENALTY(Q,P)
REAL, INTENT(IN) :: Q
REAL, INTENT(OUT) :: P
REAL :: a = 10
REAL :: b = 2
REAL :: T = 30

! penalty function
IF(Q<T)THEN
P = a*T*(EXP(b*(T-Q)/T)-1)
ELSE
P=0
END IF


END SUBROUTINE PENALTY


!
SUBROUTINE INFLOWPROB(LI,PIC)
REAL, INTENT(IN) :: LI !previous inflow
REAL, DIMENSION(5), INTENT(OUT) :: PIC !current inflow probability

IF(LI == 10)THEN
PIC(1) = 0.8
PIC(2) = 0.1
PIC(3) = 0.1
PIC(4) = 0
PIC(5) = 0
ELSE IF(LI == 20)THEN
PIC(1)= 0.3
PIC(2) = 0.3
PIC(3) = 0.3
PIC(4) = 0.1
PIC(5) = 0
ELSE IF(LI == 30)THEN
PIC(1) = 0.1
PIC(2) = 0.3
PIC(3) = 0.3
PIC(4) = 0.2
PIC(5) = 0.1
ELSE IF(LI == 40)THEN
PIC(1) = 0.1
PIC(2) = 0.3
PIC(3) = 0.3
PIC(4) = 0.2
PIC(5) = 0.1
ELSE IF(LI == 50)THEN
PIC(1) = 0.1
PIC(2) = 0.2
PIC(3) = 0.3
PIC(4) = 0.3
PIC(5) = 0.1
END IF

END SUBROUTINE INFLOWPROB
 
Technology news on Phys.org


Hi kingfisher,

One line that I noticed was this one:

Code:
f=f+PIC(cin)*(Of(cin,fst)+oops)

I was looking at the array Of(cin,fst) and it does not appear to be initialized to any value before it is used. Were you wanting to use array Of here? I think some compilers automatically set things to zero if they are not explicitly initialized, while others just use whatever (garbage) value happens to be at that memory location. Or am I misreading your program?

I don't know how that would give the behavior you are talking about though. Can you give the different outputs that you are getting when the write statement is commented out and active? Also, what compiler are you using?
 


YOU ARE GREAT, AND I THANK YOU

initializing the array was the solution
 


Glad to help! That was an interesting bug; I actually got identical outputs with my compiler whether the write statement was commented out or not, so I could not reproduce the behavior.

But I'm guessing that your compiler chose a different memory location for the variable array Of(,) based on whether the write line was there or not, which caused different "random" values to be used.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
11
Views
3K
Replies
6
Views
3K
Replies
3
Views
3K
Replies
15
Views
33K
Replies
3
Views
3K
Replies
2
Views
9K
Back
Top