Mark
Mark

Reputation: 19997

Fortran routine only works for statically allocated matrix

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).

Upvotes: 0

Views: 90

Answers (1)

Peter Petrik
Peter Petrik

Reputation: 10195

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.

Upvotes: 1

Related Questions