0

I am currently wrapping fortran code with f2py. However, even some simple tests will trigger malloc error. For example:

! file name is simple.F90
SUBROUTINE test(aout, ain, rank)
    INTEGER :: rank
    COMPLEX(kind=16) :: ain(rank, rank)
    COMPLEX(kind=16), INTENT(out) :: aout(rank, rank)
    print *, ain
    aout = ain
END SUBROUTINE test

I wrapped this code with f2py -m simple -c simple.F90 and used it:

import simple
import numpy as np
ain = np.array([[1,0],[0,1]],dtype="complex")
simple.test(ain,2)

The output is rather confusing:

(2.98323129689261721470924741607413318E-4947,0.00000000000000000000000000000000000) (0.00000000000000000000000000000000000,2.98323129689261721470924741607413318E-4947) (5.43913343041483495234482095052061699E-4937,1.19445898260724927749750105155089048E-4946) (2.03058027046167518900674768173335535E-4945,5.42471021397611822645684765587671432E-4937)
Python(9481,0x7fff7ad40000) malloc: \*** error for object 0x7fabcaa1adc8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

Fortran subroutine is not getting the correct array and something's wrong with the memory.

More information:

  • OS: OS X
  • python: 3.5.1
atbug
  • 818
  • 6
  • 26
  • I think it should be `kind=8` not `kind=16` (it looks to specify the precision of each part of the complex number). However, I don't know if that's what is causing your error. – DavidW Mar 11 '16 at 16:16
  • 1
    There are many answers here on SO which point out that the kind number is an arbitrary integer value. When it comes to complex numbers it is almost certain that a kind number of `16` WILL NOT map to a complex number with 2 8-byte elements. The kind of a complex number is the same as the kind of a real number capable of representing each component of the complex number. i.e. if you think of a complex number as being 2 reals of `kind=3` (say) then that complex number ought also to have `kind=3`. – High Performance Mark Mar 11 '16 at 16:40
  • Obligatory links http://stackoverflow.com/questions/838310/fortran-90-kind-parameter http://stackoverflow.com/questions/3170239/fortran-integer4-vs-integer4-vs-integerkind-4?lq=1 Any `(kind=number)` is a code smell. Use named constants. If you omit the `kind=` it will still be shorter. – Vladimir F Героям слава Mar 11 '16 at 18:18

1 Answers1

0

As all the comments pointed out, kind is what to be blamed here. All problems resolved after I use the following convention.

INTEGER, PARAMETER :: dp = kind(1.0d0)
REAL(kind=dp) :: a_real_number
COMPLEX(kind=dp) :: a_complex_number
atbug
  • 818
  • 6
  • 26