MarkyD43
MarkyD43

Reputation: 467

Intermittent Memory Allocation Error for Fortran Matrix using F2Py

Background:
I have a Python script that uses Fortran code for it's intensive calculations. I'm using F2Py to do this. One particular Fortran subroutine builds a matrix used in later calculations. This subroutine is iterated over in a loop, and solved at each step. A snippet of the code using essential arrays and variables is given below:

for i in xrange(steps):
    x+=dx
    F_Output=Matrix_Build_F2Py.hamiltonian_solve(array_1, array_2, array_3, array_4)
    #Do things with F_Output

SUBROUTINE Hamiltonian_Solve(array_1, array_2, array_3, array_4, output_array)

!N_Long, N_Short are implied, Work, RWork, LWork, INFO
INTEGER, INTENT(IN), DIMENSION(0:N_Long-1)  :: array_1, array_2, array_3 
INTEGER, INTENT(IN), DIMENSION(0:N_Short-1) :: array_4
COMPLEX*16,ALLOCATABLE                      :: Hamiltonian(:,:)
COMPLEX*16, DIMENSION(0:N_Short             :: Complex_Var
DOUBLE PRECISION, INTENT(OUT), DIMENSION(0:N_Short-1)       :: E
INTEGER                                                     :: LWork, INFO, j
COMPLEX*16,       ALLOCATABLE                               :: Work(:)

ALLOCATE(Hamiltonian(0:N_Short-1, 0:N_Short-1))
ALLOCATE(RWork(MAX(1,3*(N_Short-2))))    
ALLOCATE(Work(MAX(1,LWork)))              
ALLOCATE(E(0:N_Short-1)) 

DO h=0, N_Long-1
Hamiltonian(array_1(h),array_2(h))=Hamiltonian(array_1(h),array_2(h))-Complex_Var(h)
END DO


CALL ZHEEV('N','U',N_Short,Hamiltonian,N_Short,E,Work,LWork,RWork,INFO)

DO j=0,N_Short-1
    Output_Array(j)=E(j)
END DO

END SUBROUTINE

However, for some reason this subroutine crashes my Python program, and generates the following malloc error:

error for object 0x1015f9808: incorrect checksum for freed object - object was probably     modified after being freed.

This error is unusual in that it does not occur every time, but only a significant percentage of the time. I have determined that the root of the error lies in the line:

Hamiltonian(array_1(h),array_2(h))=Hamiltonian(array_1(h),array_2(h))-Complex_Var(h)

As if I change it to:

Hamiltonian(array_1(h),array_2(h))=Hamiltonian(array_1(h),array_2(h))

The error stops. However, Complex_Var is essential to the output, otherwise the program simply produces zeroes. This thread bears some similarity to my issue, but that issue seemed to occur after every run, mine does not. I have taken care to ensure the arrays are not mismatched, other arrangements (ie not accounting for numpy's different array formats) immediately creates a segmentation fault, as expected.

Question
Why is Complex_Var breaking the code? Why is the problem intermittent rather than systematic? And are there any obvious (or not so obvious) tips to avoid this?

Any help would be much appreciated!

Upvotes: 0

Views: 375

Answers (2)

user1915639
user1915639

Reputation: 257

It could be because you don't have any deallocate commands. However it is hard to tell with this obviously incomplete code - could you post the actual code (i.e. something that will compile)?

Upvotes: 0

M. S. B.
M. S. B.

Reputation: 29401

updated per first comment and revision of question:

I see that some arrays in the problem expression have upper-dimension N_long-1 (i.e., array_1 and array_2) and array Complex_Var dimension N_short. The loop iterates up to N_Long-1. Do you know that N_Long-1 <= N_short ? If not, you might be accessing an illegal subscript o Complex_var. And do you know that the values in array_1 and array_2 are always legal subscripts for Hamilton? If you write outside the reserved size of that array, you could corrupt the information used by the memory allocator when it created some array, preventing it from freeing that array later.

If this is the problem, using your compiler's option for run-time subscript checking can help you find similar errors.

Upvotes: 1

Related Questions