The aim of this post is to demonstrate the use of the ctypes package to accelerate the solution of a problem by invoking a C function for a computationally intensive task. I will not discuss ctypes in excruciating detail (one should consult the documentation for in-depth information about the package), but instead will provide the basic introduction needed for a user to begin experimenting with ctypes. I selected the problem for this example in the hopes that it would be complex enough to expose some key components of ctypes, but not so complex that it distracts from the main point.
In this post I will demonstrate how to write a function in C, compile it into a shared library, and invoke it from Python using ctypes. The function in question will take multiple parameters, including pointers. It will not return any value, instead relying on the user to provide a pointer to pre-allocated memory for the output. One may be tempted to allocate memory in the C function, and return a pointer to this memory instead, but due to the way the memory is managed, this can cause problems. This is not to say that it is impossible to do so, (after all, often times you don’t know the size of the output of a function before invoking it) but it should be done carefully to avoid memory leaks. I will cover how to do this in a future post.