Why Do Commented Write Statements Affect FORTRAN SDP Output?

  • Context: Fortran 
  • Thread starter Thread starter kingfisher
  • Start date Start date
  • Tags Tags
    Effects Fortran
Click For Summary

Discussion Overview

The discussion centers around the unexpected behavior of a FORTRAN program involving stochastic dynamic programming and Markov processes. Participants explore how commenting out certain write statements affects the output of an array, raising questions about initialization and compiler behavior.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports that commenting out a specific write statement changes the final array output, despite the statement not involving any variable writes.
  • Another participant points out that the array 'Of' may not be initialized before use, suggesting that different compilers might handle uninitialized variables differently, leading to varying outputs.
  • A later reply confirms that initializing the array resolved the issue for the original poster.
  • Another participant mentions that they could not reproduce the behavior, indicating that their compiler produced identical outputs regardless of the write statement's presence.
  • There is speculation that the compiler may allocate different memory locations for the array based on the presence of the write statement, resulting in different values being used.

Areas of Agreement / Disagreement

Participants generally agree that the initialization of the array was crucial to resolving the issue. However, there remains disagreement regarding the behavior across different compilers, as one participant could not replicate the original poster's experience.

Contextual Notes

The discussion highlights potential limitations in understanding compiler behavior and memory management in FORTRAN, particularly regarding uninitialized variables and their impact on program output.

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.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 54 ·
2
Replies
54
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
9
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 10 ·
Replies
10
Views
6K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 13 ·
Replies
13
Views
22K
  • · Replies 3 ·
Replies
3
Views
3K