-1

I'm trying to speed up an integration (scipy.integrate.quad) using Ctypes. I have never used C and don't understand the Ctypes documentation. Could someone give a basic explanation of what Ctypes is actually doing using as few computing terms as possible. Essentially please explain it like I'm 5!

Thanks

imp
  • 17
  • 1
  • http://stackoverflow.com/a/5082294/742269 – Avión Jul 02 '15 at 09:27
  • Hi I've seen this. I'm not looking for how to execute I'm looking to qualitatively understand what it does. – imp Jul 02 '15 at 09:33
  • 1
    For [`scipy.integrate.quad`](http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html) ctypes only provides the address of a native machine-code function to integrate, which gets passed to a routine in the Fortran QUADPACK library. Scipy is *not using* most of what ctypes is designed to do, i.e. convert between Python and C data types and call C functions. – Eryk Sun Jul 02 '15 at 10:28

1 Answers1

1

A computer runs any program by following very simple steps, known as machine code or native code. At that level, anything is a number of a handful of widths, and there are millions of memory slots to store them in. When writing a program, a higher level of abstraction is usually desired, allowing us to name variables and subroutines, keep track of what memory holds what value, and so on. Native code itself does not reveal that information, but stored program files (whether libraries or executables) often have some clues, such as subroutine names. C source code supplies this information in declarations, and some libraries like SciPy have wrappers to preserve the information another layer up, in this case Python. Python objects always hold information on their types, unlike C variables. Ctypes permits to look up names and describe the missing type information, so native variables and subroutines can be accessed from Python. In the scipy.integrate.quad example, Ctypes is used to create a Python function object from a native subroutine named func.

>>> import ctypes
>>> lib = ctypes.CDLL('/home/.../testlib.*') #use absolute path
>>> lib.func.restype = ctypes.c_double
>>> lib.func.argtypes = (ctypes.c_int,ctypes.c_double)

In C terms, this function is declared as extern double func(int, double);. In general, native routines are faster than Python ones, because Python has to figure out what to do with each operation by the objects it handles, while that information is statically determined in C. A middle ground can be reached with just in time compilers, of which PyPy is a good example.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
  • 'CPython wrapper', 'Python interpreted bytecode' not really understandable for a 5 year old – imp Jul 02 '15 at 09:41
  • The `argtypes` spec in this case is fake (but used indirectly) and really nonsense since the 2nd argument is a `double *` for an array parameter. It doesn't matter because `scipy.integrate` isn't using ctypes to convert arguments or even to call the function. ctypes is pretty much only used to get the address of the function. – Eryk Sun Jul 02 '15 at 10:32
  • Not sure what 5 year old you're talking to who does integration using multiple programming languages, but I did a bit of a rewrite. – Yann Vernier Jul 02 '15 at 10:33