0

I'm fairly new to Fortran and I am having trouble understanding why a subroutine is working fine with a matrix defined statically at compile time, but is not working for a similar matrix created with allocate at runtime.

For all I can tell, the matrices should be very similar: same type, size, sizeof and values. The question is not so much about this specific example, but about why and when they would behalve differently at all.

A 'minimum' working example at pastebin (updated here!), and what I think is the essential part here:

! static 'allocation'
real(dp), dimension(fN, fN) :: fH
! static call
call ZHPADM(pade_deg, fN, dt, fH, fN, fwsp, flwsp, fipiv, iexph, ns, f)

! dynamic allocation
real(dp), allocatable, dimension(:, :) :: dH
allocate(dH(dN, dN))
! dynamic call
call ZHPADM(pade_deg, dN, dt, dH, dN, dwsp, dlwsp, dipiv, iexph, ns, f) ! full dynamic call
call ZHPADM(pade_deg, fN, dt, dH, fN, fwsp, flwsp, fipiv, iexph, ns, f) ! only fH->dH to show that it is the matrix that causes the error

Making any of the other (0D/1D) paramters dynamic works just fine. The routine is ZHPADM from expokit and the error Program received signal 8 (SIGFPE): Floating-point exception. but as said, a general explanation is preferred.

EDIT 1: I forgot to mention some info, sorry for that! Calling ZHPADM with all relevant argument dynamic gives the same error. I just changed some back to static to show that it is the matrix being dynamic that causes the problem. A few lines near the end became inconsistent because of this, sorry. The values for the static and dynamic variables are the same.

EDIT 2: The exception occurs in line 77 in the new pastebin, the dynamic ZHPADM call (commenting that line stops the exception). I'm using gfortran 4.6.3 on Ubuntu like this gfortran demo.f90 -lexpokit -lblas -llapack (and usually some warning flags).

7
  • I see several errors in your code at pastebin... E.g. dPP = reshape(dwsp(iexph:iexph+dN**2-1), [dN, dN]): dwsp is not initialized! Are dN and fN the same? Don't you need to reset fipiv? Apart from that it's too little information! Please, use all debugging options of your compiler to fix your code until you get no more complaints, and then give the full error description including the line where the exception occured. Commented Jul 16, 2014 at 11:55
  • It's probably worth checking the status flag returned by allocate. Commented Jul 16, 2014 at 12:01
  • @AlexanderVogt you're right, sorry. The dPP = part was based on calling ZHPADM with all dynamic arguments, but I later found out that only dH gives a problem, so changed it to be minimal. Should have removed the dPP line. Commented Jul 16, 2014 at 12:19
  • @HighPerformanceMark Good idea indeed, I should do that in general I guess. However I checked and allocation went okay in this case. Commented Jul 16, 2014 at 12:36
  • In which line does the exception occur? Commented Jul 16, 2014 at 13:20

1 Answer 1

1

In dynamic call of ZHPADM, you pass fN as order of H, but you have allocated only dN items. If fN .ne dN program probably do some operations on unallocated memory positions resulting to undefined behaviour.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer! That's certainly reasonable but in this case fN = dN, I'm sorry for not mentioning that before, I updated the question now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.