Python Forum
Trying to debug segfault in ctypes binding to Fortran
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to debug segfault in ctypes binding to Fortran
#1
Hi,

I am just trying to learn creating Python bindings to legacy Fortran using the ctypes module. My code is segfaulting in the Fortran function invocation. I was not able to decipher what gdb was telling me. I would be grateful to be pointed in the right direction here.

A simple Fortran function I am trying to bind is given below. It was compiled using "gfortran -shared euler.f -o euler.so"

       subroutine euler(f, neq, y, t, tout, h)
       implicit none
       real y(neq), t, tout, h, dy(neq), ti
       integer neq, i, stepnum
       stepnum = 1
       ti = t
c      Euler iteration
 10    continue
          call f( neq, t, y, dy )
          do 20 i = 1, neq
             y(i) = y(i) + h*dy(i)
 20       continue
          t = ti + stepnum*h
          stepnum = stepnum + 1
       if ( t < tout ) goto 10
       return
       end
The Python code calling this Fortran subroutine is

import ctypes as ct

flib = ct.CDLL('euler.so')
ode_spec = ct.CFUNCTYPE(None, ct.c_int, ct.c_float, ct.POINTER(ct.c_float), ct.POINTER(ct.c_float))

flib.euler_.restype = None
flib.euler_.argtypes = [
    ct.POINTER(ode_spec),
    ct.c_int,
    ct.POINTER(ct.c_float),
    ct.c_float,
    ct.c_float,
    ct.c_float
]

@ode_spec
def df(neq, t, y, dy):
    dy[0] = y[0]
    return

t = 0.1
to = 0.2
neq = 1
realarr = ct.c_float * neq
y = realarr(0.1)
h = 0.001
flib.euler_(df, len(y), y, t, to, h)
Reply
#2
Finally got it working. The code below worked.

import ctypes as ct

flib = ct.CDLL('euler.so')
ode_spec = ct.CFUNCTYPE(None, ct.POINTER(ct.c_int), ct.POINTER(ct.c_float), ct.POINTER(ct.c_float * 1), ct.POINTER(ct.c_float * 1))

flib.euler_.restype = None
flib.euler_.argtypes = [
    ode_spec,
    ct.POINTER(ct.c_int),
    ct.POINTER(ct.c_float * 1),
    ct.POINTER(ct.c_float),
    ct.POINTER(ct.c_float),
    ct.POINTER(ct.c_float)
]

@ode_spec
def df(neq, t, y, dy):
    dy[0] = y[0]
    return

t = ct.c_float(0.1)
to = ct.c_float(0.2)
neq = ct.c_int(1)
relarr = ct.c_float * 1
y = relarr()
y[0] = 0.1
h = ct.c_float(0.001)
flib.euler_(df, ct.byref(neq) , ct.byref(y), ct.byref(t), ct.byref(to), ct.byref(h))
jefsummers likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using ctypes to use a dll in a python module dauriac 3 387 Mar-06-2024, 04:38 PM
Last Post: dauriac
  Exponeniation and Right Binding PhDoctus 3 546 Feb-15-2024, 07:09 AM
Last Post: Gribouillis
  ctypes juliolop 7 1,402 Apr-20-2023, 03:33 PM
Last Post: Larz60+
  pycharm debug help mg24 1 1,042 Nov-18-2022, 05:38 AM
Last Post: deanhystad
  Python debug suddenly got bad ben1122 3 1,109 Sep-03-2022, 06:20 AM
Last Post: ben1122
  Issue while using ctypes in python GiggsB 6 2,791 Mar-27-2022, 03:38 AM
Last Post: GiggsB
  Ctypes and libffi.so.7 luxedo 1 6,062 Oct-23-2021, 09:24 PM
Last Post: DeaD_EyE
Bug Help Debug please jamesaarr 12 3,881 Jul-28-2021, 11:20 PM
Last Post: Pedroski55
  Binding Complex data to Combobox gcfernando 2 2,087 Sep-14-2020, 03:24 PM
Last Post: gcfernando
  Error in Int object is not subscript-able. How to debug this ? yanDvator 1 2,238 Aug-03-2020, 02:28 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020