Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[b]How to debug with GDB???[/b] 2

Status
Not open for further replies.

amagrammer

Technical User
Nov 26, 2009
16
DE
HI, Volks,

I am a beginner in Fortran programming. I use Compaq Fortran Compiler 6.6 on Windows Vista. I wrote a program to calculate a PDE with NAG Fortran Library. But it goes awry as it debugging. Command window shows:

forrtl:severe <157>: program exception - access violation
routine line source
unknown unknown unknown

Then I read another thread dealing with similar problem. Following the advice by mikrom, I tried to use GDB-debugger to locate the error. I run the executable under GDB. As it segmentation fault( 0x004a70bc in ??<>) occurred, I typed 'info stack' to trace the stack back. Command windows showed frames of lines like
#1 0x004a70bc in ??<>
#2 0x004027a2 in ??<>
.
.
#433 0x7595fd89 in SetFilePointer <> from c:\windows\system32\kernel32.dll
.
#436 0x75938425 in SetEndOfFile <> from c:\windows\system32\kernel32.dll
.
.
After a while of striking return key, it came to " cannot access memory at address 0x134000"
I don't know what all this means. I am not sure that I use GDB-debugger correctly either. I attach my code below so that someone can replicate my situation. Thanks a lot. :)

regards,
amagrammer

CODE:
Code:
program carbonprice_sp

implicit none

	! Variables

	
	! .. Parameters ..
	INTEGER NOUT
	PARAMETER (NOUT=6)

	!Character(LEN=200), parameter:: inputfile ="C:\\Carbon\\data\\.txt"
	Character(LEN=200), parameter:: outputfile ="C:\\Carbon\\results\\carbonprice.txt"


	! .. External Subroutines ..
	EXTERNAL EX1 

	! .. Executable Statements ..

	open (unit=99, file = outputfile)

	WRITE (99,*) 'Carbon Price Results:'
	CALL EX1
	STOP

	write (NOUT,*) 'Program has been successfully excuted!'
	read (NOUT,*)

end program carbonprice_sp



SUBROUTINE EX1
	! .. Parameters ..
	INTEGER NOUT
	PARAMETER (NOUT=6)

	INTEGER MXLEV, NPDE, NPTS
	PARAMETER (MXLEV=3,NPDE=1,NPTS=5.0D4)    

	INTEGER LENIWK, LENRWK, LENLWK
	PARAMETER (LENIWK=NPTS*(5*MXLEV+14)+2+7*MXLEV,&
			   LENRWK=NPTS*NPDE*(5*MXLEV+9+18*NPDE)+NPTS*2,&
			   LENLWK=NPTS+1)


	! .. Scalars in Common ..
	DOUBLE PRECISION PENALTY, ENDOWMENT, MACOST, INTEREST, MU, SIGMA
	INTEGER IOUT

	! .. Arrays in Common ..
	DOUBLE PRECISION TWANT(2)


	! .. Local Scalars ..
	DOUBLE PRECISION TOLS, TOLT, TOUT, TS, XMAX, XMIN, YMAX, YMIN
	INTEGER I, IFAIL, IND, ITRACE, J, MAXLEV, NX, NY


	! .. Local Arrays ..
	DOUBLE PRECISION DT(3), OPTR(3,NPDE), RWK(LENRWK)
	INTEGER IWK(LENIWK), OPTI(4)


	LOGICAL LWK(LENLWK)

	! .. External Subroutines ..
	EXTERNAL D03RAF, MONIT1, PDEF1, PDEIV1  ! BNDRY1  ! NO Boundary conditions

	! .. Intrinsic Functions ..
	INTRINSIC EXP

	! .. Common blocks ..
	COMMON /OTIME1/TWANT, IOUT
	COMMON /PARAM1/PENALTY, ENDOWMENT, MACOST, INTEREST, MU, SIGMA

	! .. Executable Statements ..
	WRITE (99,*)
	WRITE (99,*)
	WRITE (99,*) 'Carbon Emission Allowance Prices w.r.t. time, emission amount(X) and rate(Y)'
	WRITE (99,*)

	!
	! Problem Parameters
	!
	PENALTY		= 100.0D0
	ENDOWMENT	= 66935.0D0
	MACOST		= 0.24D0
	INTEREST	= 0.05D0
	MU			= 0.01D0
	SIGMA		= 0.15D0
	 

	IND = 0
	ITRACE = 1
	TS = 0.0D0
	DT(1) = 1.0D-2		  
	DT(2) = 0.0D0
	DT(3) = 0.0D0
	TOUT  = 8.0D0           
	TWANT(1) = 2.0D-2      
	TWANT(2) = 8.0D0       
	XMIN = 65754.0D0
	XMAX = 67135.0D0
	YMIN = 1500.0D0
	YMAX = 2500.0D0
	NX = 151      
	NY = 101	
	TOLS = 10.0D0     
	TOLT = 0.01D0  

	
	OPTI(1) = 3
	DO 20 I = 2, 4
		OPTI(I) = 10
	20 CONTINUE

	DO 60 J = 1, NPDE
		DO 40 I = 1, 3
			OPTR(I,J) = 1.0D0
		40 CONTINUE
	60 CONTINUE


	DO 120 IOUT = 1, 2
		IFAIL = -1      
		TOUT = TWANT(IOUT)
		
		CALL D03RAF(NPDE,TS,TOUT,DT,XMIN,XMAX,YMIN,YMAX,NX,NY,TOLS,&
		 TOLT,PDEF1,BNDRY1,PDEIV1,MONIT1,OPTI,OPTR,RWK,&
		 LENRWK,IWK,LENIWK,LWK,LENLWK,ITRACE,IND,IFAIL)
	!
	! Print statistics

		WRITE (99,'('' Statistics:'')')
		WRITE (99,'('' Time = '',F8.4)') TS
		WRITE (99,'('' Total number of accepted timesteps ='', I5)') IWK(1)
		WRITE (99,'('' Total number of rejected timesteps ='', I5)') IWK(2)
		WRITE (99,*)
		WRITE (99, '('' T o t a l n u m b e r o f '')')
		WRITE (99, '('' Residual Jacobian Newton '' , '' Lin sys'')' )
		WRITE (99, '('' evals evals iters '' , '' iters'')')
		WRITE (99,'('' At level '')')
		
		MAXLEV = 3
		
			DO 80 J = 1, MAXLEV
				IF (IWK(J+2).NE.0) THEN 
				WRITE (99,'(I8,4I10)') J, IWK(J+2), IWK(J+2+MAXLEV), IWK(J+2+2*MAXLEV),IWK(J+2+3*MAXLEV) 
				END IF
			
			80 CONTINUE
		WRITE (99,*)
		WRITE (99, '('' M a x i m u m n u m b e r '', '' o f'')')
		WRITE (99, '('' Newton iters Lin sys iters '')')
		WRITE (99,'('' At level '')')
		
		DO 100 J = 1, MAXLEV
			
			IF (IWK(J+2).NE.0)  THEN 
				WRITE (99,'(I8,2I14)') J, &
				IWK(J+2+4*MAXLEV), IWK(J+2+5*MAXLEV)  !max number of newton, linear solvers of any time step at J
			END IF

		100 CONTINUE
		WRITE (99,*) '------+++++++++++++++++-------------+++++++++++++-------------------'

	120 CONTINUE

	RETURN
	END

	
	! The initial valuess
	SUBROUTINE PDEIV1(NPTS,NPDE,T,X,Y,U)

		! .. Scalar Arguments ..
		DOUBLE PRECISION T, ENDOWMENT, PENALTY
		INTEGER NPDE, NPTS

		! .. Array Arguments ..
		DOUBLE PRECISION U(NPTS,NPDE), X(NPTS), Y(NPTS)

		! ...common blocks... I added, not all variables used. MU, SIGMA, INTEREST may be removed

		COMMON /PARAM1/PENALTY, ENDOWMENT, MACOST, INTEREST, MU, SIGMA

		! .. Local Scalars ..
		DOUBLE PRECISION TOL		!
		INTEGER I

		! .. Executable Statements ..

		TOL = 10.D0*X02AJF()

		DO 20 I = 1, NPTS
			IF( (X(I)-ENDOWMENT) .GE. TOL) THEN
			!write(*,*) 'emission amount=', X(I), 'endowment=', ENDOWMENT, 'penalty= ', PENALTY
			!read(*,*)
			U(I,1) = (X(I)-ENDOWMENT)*PENALTY
			!write(*,*) 'penalty payment= ', U(I,1) 
			!read(*,*)
			ELSE 
			U(I,1) = 0.0D0 
			END IF        
		20 CONTINUE

		RETURN
	END


	!Definition of the PDE, equation (1) in the Document
	SUBROUTINE PDEF1(NPTS,NPDE,T,X,Y,U,UT,UX,UY,UXX,UXY,UYY,RES)

		! .. Scalar Arguments ..

		DOUBLE PRECISION T
		INTEGER NPDE, NPTS

		! .. Array Arguments ..
		DOUBLE PRECISION RES(NPTS,NPDE), U(NPTS,NPDE), UT(NPTS,NPDE),&
						 UX(NPTS,NPDE), UXX(NPTS,NPDE), UXY(NPTS,NPDE),&
						 UY(NPTS,NPDE), UYY(NPTS,NPDE), X(NPTS), Y(NPTS)

		! .. Scalars in Common ..
		DOUBLE PRECISION PENALTY, ENDOWMENT, MACOST, INTEREST, MU, SIGMA

		! .. Local Scalars ..
		INTEGER I

		! .. Intrinsic Functions ..
		INTRINSIC EXP

		! .. Common blocks ..
		COMMON /PARAM1/PENALTY, ENDOWMENT, MACOST, INTEREST, MU, SIGMA

		! .. Executable Statements ..
		DO 20 I = 1, NPTS
			RES(I,1) = UT(I,1) - INTEREST*U(I,1) - 0.5D0*UX(I,1)*UX(I,1)/MACOST + Y(I)*UX(I,1) + MU*Y(I)*UY(I,1) + 0.5D0*UYY(I,1)*SIGMA*SIGMA*Y(I)*Y(I)
		20 CONTINUE


		RETURN
	END


!	SUBROUTINE BNDRY1(NPTS,NPDE,T,X,Y,U,UT,UX,UY,NBPTS,LBND,RES)

		! .. Scalar Arguments ..
!		DOUBLE PRECISION T
!		INTEGER NBPTS, NPDE, NPTS

		!.. Array Arguments ..
!		DOUBLE PRECISION RES(NPTS,NPDE), U(NPTS,NPDE), UT(NPTS,NPDE),&
!						 UX(NPTS,NPDE), UY(NPTS,NPDE), X(NPTS), Y(NPTS)

!		INTEGER LBND(NBPTS)

		! .. Local Scalars ..
!		DOUBLE PRECISION TOL
!		INTEGER I, J
!
		! .. External Functions ..
!		DOUBLE PRECISION X02AJF
!		EXTERNAL X02AJF            ! The machine precision

		! .. Intrinsic Functions ..

!		INTRINSIC ABS

		! .. Executable Statements ..

!		TOL = 10.D0*X02AJF()

!		DO 20 I = 1, NBPTS
!			J = LBND(I)
			
!			IF (ABS(X(J)).LE.TOL) THEN
!				RES(J,1) = UX(J,1)
!			ELSE IF (ABS(X(J)-1.0D0).LE.TOL) THEN
!			RES(J,1) = U(J,1) - 1.0D0
!			ELSE IF (ABS(Y(J)).LE.TOL) THEN
!				RES(J,1) = UY(J,1)
!			ELSE IF (ABS(Y(J)-1.0D0).LE.TOL) THEN
!				RES(J,1) = U(J,1) - 1.0D0
!			END IF
!		20 CONTINUE
!
!		RETURN
!	END

	SUBROUTINE MONIT1(NPDE,T,DT,DTNEW,TLAST,NLEV,NGPTS,XPTS,YPTS,LSOL,SOL,IERR)

		! .. Parameters ..
		INTEGER NOUT
		PARAMETER (NOUT=6)

		! .. Scalar Arguments ..
		DOUBLE PRECISION DT, DTNEW, T
		INTEGER IERR, NLEV, NPDE
		LOGICAL TLAST

		! .. Array Arguments ..
		DOUBLE PRECISION SOL(*), XPTS(*), YPTS(*)
		INTEGER LSOL(NLEV), NGPTS(NLEV)

		! .. Scalars in Common ..
		INTEGER IOUT

		! .. Arrays in Common ..
		DOUBLE PRECISION TWANT(2)

		! .. Local Scalars ..
		INTEGER I, IPSOL, IPT, LEVEL, NPTS

		! .. Common blocks ..
		COMMON /OTIME1/TWANT, IOUT

		! .. Executable Statements ..
		IF (TLAST) THEN

		! Print solution at the time T controlled by IOUT 

			IF (IOUT.EQ.1) THEN
				WRITE (99,'('' Solution at every 4th grid point '', ''in level 1 at time '', F8.4,'':'')') T
				WRITE (99,*)
				WRITE (99,'(7X,''x'',10X,''y'',8X,''approx u'')')
				WRITE (99,*)
				LEVEL = 1
				NPTS = NGPTS(LEVEL)   ! total points on the first level. grid levels are in the y direction
				IPSOL = LSOL(LEVEL)
				IPT = 1

				DO 20 I = 1, NPTS, 4
				WRITE (99,'(3(1X,E11.4))') XPTS(NGPTS(1)+IPT+I-1),YPTS(NGPTS(1)+IPT+I-1), SOL(IPSOL+I)

				20 CONTINUE
				WRITE (99,*)
			END IF

		 END IF

    	RETURN


	END
 
At a guess, your file is opened for reading so you can't write to it. Add STATUS='UNKNOWN' in your open statement.
 
thank you, xwb, for the quick suggestion,

I tried the proposal. It doesn't seem to be the problem. I got the same error messages and stack information. But, thanks.












 
Hi amagrammer,

I could not compile your program because of external subroutine D03RAF and the function X02AJF() - so I cancelled their usage in the program:
Code:
!CALL D03RAF(NPDE,TS,...
...
...
!TOL = 10.D0*X02AJF()
TOL = 5e-10

Now I can compile it with g95 and gfortran.

g95 gives me a warning
Code:
$ g95 carbonprice_sp.f95 -o carbonprice_sp   
In file carbonprice_sp.f95:188

    COMMON /PARAM1/PENALTY, ENDOWMENT, MACOST, INTEREST, MU, SIGMA
                1
In file carbonprice_sp.f95:76

    COMMON /PARAM1/PENALTY, ENDOWMENT, MACOST, INTEREST, MU, SIGMA
            2
Warning (121): COMMON block 'param1' is 32 bytes at (1) and 48 bytes at (2)
I looked at the both lines but common block seems to be the same.

gfortran compiles without warning.

In both cases the created executable runs and produces this file
carbonprice.txt
Code:
 Carbon Price Results:


 Carbon Emission Allowance Prices w.r.t. time, emission amount(X) and rate(Y)

 Statistics:
 Time =   0.0000
 Total number of accepted timesteps =    0
 Total number of rejected timesteps =    0

 T o t a l n u m b e r o f 
 Residual Jacobian Newton  Lin sys
 evals evals iters  iters
 At level 

 M a x i m u m n u m b e r  o f
 Newton iters Lin sys iters 
 At level 
 ------+++++++++++++++++-------------+++++++++++++-------------------
 Statistics:
 Time =   0.0000
 Total number of accepted timesteps =    0
 Total number of rejected timesteps =    0

 T o t a l n u m b e r o f 
 Residual Jacobian Newton  Lin sys
 evals evals iters  iters
 At level 

 M a x i m u m n u m b e r  o f
 Newton iters Lin sys iters 
 At level 
 ------+++++++++++++++++-------------+++++++++++++-------------------

Try to find out if the external subroutine D03RAF or the function X02AJF is problematic.
 
Hi,mikrom,
thank you for looking into this. D03RAF is a NAGroutine and X02AJF is the machine precision defined in NAG. I don't have the problem with them.

I added some more content in this program and the problem just disappeared. I don't know how. But the problem is gone. Still don't know how to use GDB...
 
Hi amagrammer,

I'm using gdb with the GUI called insight

To use it, you need first to compile your program with oprion -g, e.g.
Code:
$ g95 carbonprice_sp.f95 -g -o carbonpirce_sp
and then start your programn in insight
Code:
$ insight carbonpirce_sp
After the GUI opens, you see some assembly code:
Click on Run icon (or press the key R) and then click on the Step icon (or press S).
Now you see the fortran source code:
Step through the source by clicking on the Step icon (or pressing S) or define a breakpoint on a line by left-clicking on the line before a line number and then run the program to the defined breakpoint by clicking on the Next icon (or by pressing N)...etc.
You can look at local variables by clicking on the Local variables icon (or pressing Ctrl-L) or watching an expression by clicking on the icon Watch Expressions (or by pressing Ctrl-W)...etc.
 
Hi, mikrom,

I would like to try this insight. How do I install it on my Windows? I don't see any .exe file on the download page:
ftp://sourceware.org/pub/insight/releases/
 
Hi amagrammer,
First I installed MinGW and MSYS and then I downloaded the binary insight-6.6-mingw.tar.bz2 from here.
You need to unpack the directory tree contained in the tar.bz2 archive into mingw directory tree, e.g. c:\msys\1.0\mingw\
(Note: The version of MinGW I have installed contains only g77, so I installed g95 and gfortran additionally - maybe there is a newer version now which comes with gfortran)
 
hello, mikrom,

Sorry for so many questions. But I feel to be on the right track.

I am referring to your post at 8 Dec 09 10:03. After installing MinGW and unpacking insight-6.6-mingw under my directory c:\MinGW\. I tried to follow compilation and debugging steps. My question is: where shall I run the codes you gave?
Because either directly in windows command console or under GDB(I typed command "c:\mingw\bin\gdb.exe"), the code
Code:
$ g95 carbonprice_sp.f95 -g -o carbonpirce_sp
returned "undefined command"!

BTW, my source file is of the type .f90. I don't know if this matters.

 
It seems that you installed only MinGW without MSYS. I run the commands in MSYS - it's like minimalistic Unix shell in Windows.
As I said, I installed g95 aditionally - it's not part of MinGW. Have you installed g95 too?
Have you c:\mingw\bin\ in your path ?
 
HI, mikrom,

now I installed everything. And I can start compiling carbonprice_sp.f90 using g95 under MSYS. But the compilation was not successful. g95 found some error in the file:
Code:
warning<115>:Line 267 of carbonprice_sp.f90 is being truncated
warning<115>:Line 348 of carbonprice_sp.f90 is being truncated
Error:Syntax error in argument list at <1>
I checked the referred lines in my source file. They are correct to me. Line 267 reads
Code:
RES(I,1) = UT(I,1) - INTEREST*U(I,1) - 0.5*UX(I,1)*UX(I,1)/MACOST + Y(I)*UX(I,1) + MU*Y(I)*UY(I,1) &
			           + 0.5*UYY(I,1)*SIGMA*SIGMA*Y(I)*Y(I) + TINY*TINY*UXX(I,1)
But I don't think this line was referred because after I added several comment lines above this line, command console still told me "line 267 and line 348 are being truncated". I am really confused now...
 
There is possibly a limit on line length. Try something like
Code:
RES(I,1) = UT(I,1) - INTEREST*U(I,1) - 0.5*UX(I,1)*UX(I,1)/ &
           MACOST + Y(I)*UX(I,1) + MU*Y(I)*UY(I,1) + &
           0.5*UYY(I,1)*SIGMA*SIGMA*Y(I)*Y(I) + &
           TINY*TINY*UXX(I,1)
If it is not that, you may have a carriage return/line feed problem. Make sure all lines either end with LF only or CRLF but not with both.
 
Thank you. This is it! Actually as you can see, I already chopped the line into two. But it seems still to be too long. Yes, thanks!
 
One problem solved. Another comes. This time, the compiler now gives other error messages:
Code:
C:/msys/1.0/projects/carbonprice_sp.f90:215 undefined reference to '_x02ajf_'
C:/msys/1.0/projects/carbonprice_sp.f90:148 undefined reference to '_d03raf_'
Those are both subroutine or function in NAG library. I guess that the libraries are not linked under MSYS, or... If libraries are not properly linked, how can I do that?
 
Hi amagrammer,

As I first tried to compile your program, I had the same problem with it as now you, first I divided the long lines into two or more shorter (as xwb suggested) and the I commented out the usage of the two functions, because I don't have the NAG library installed.

Here I found two links about using NAG with gfortran and g95

I never used NAG library, so I have a question too:
Is the NAG library commercial? Are there no sources for the particular subroutines of NAG library available?
 
Hello, mikrom,
This maybe is the crux of the problem. NAG library is commercial. I searched in my installation folder and find no sources of any subroutines, only a few example programs. Users' Notes are not helpful in this,either. I wrote an email to NAG support asking this. Now I am waiting and trying some stuffs.
 
Yes then that's the problem.
Have you looked at any open source alternatives or are you contrained to use this commercial library ?
The function X02AJF only computes machine epsilon. It could be substituted by your own function (I posted a simple example for macheps here: or e.g. BLAS contains such procedures (But the subroutine D03RAF should compute a system of time-dependent PDEs - I never computed systems of PDEs, so I don't know if there is an open source alternative or not.
 
Hi, mikrom, I did a bit searching in web. And D03raf is the only routine I found for 3-dimensional time-dependent PDE solver. I would like to know some alternatives for my purpose.
 
Not sure what this would do
Code:
    INTEGER MXLEV, NPDE, NPTS
    PARAMETER (MXLEV=3,NPDE=1,NPTS=5.0D4)
It should be
Code:
    INTEGER MXLEV, NPDE, NPTS
    PARAMETER (MXLEV=3,NPDE=1,NPTS=50000)
You may have a typo somewhere. Try putting IMPLICIT NONE at the head of every routine.
 
Hi, xwb, Changing 5.0D4 to 50000 doesn't change anything. But your suggestion about input arguments is good. I am checking all the arguments. Maybe there is a typo or an argument making no sense.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top