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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

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