I have a fundamental question regarding the possibility of creating a hybrid dynamical model using python and C via cython, cffi or ctypes.
There is a complicated fluid dynamical solver written in C developed by me and one of my colleagues has developed a machine learning package in python. At the moment, we are trying to integrate these two codes into a comprehensive package in python or C.
I found quite a few approaches to calling C functions from python via cython, ctypes or cffi and had successfully implemented two approaches myself to call C functions from python. However, none of these approaches really solved my problem.
The idea is that we train the AI system in Python on-fly by feeding the solutions of the fluid dynamical system in sequence as we integrate it in time. Due to the large volume of the solutions for the fluid problem, we cannot save them on the hard disk. Hence, we plan to design a Python or C package which constantly alternates the calls between Python and C routines. However, technically, it seems to be a huge challenge for us. Because before doing the numerical computation of my fluid dynamical solver, a few very large arrays must be allocated, such as the memory space for storing the inertial term and MPI buffers. Till now I still do not know the feasible/easy way of how to allocate these arrays in C and reuse them for computation at the next time step after calling the AI training routines in Python. Please bear in mind that for our application, memory management is a major challenge.
Could anyone give us suggestions?
Perhaps, a better way to do this is to declare and manage all arrays in Python and pass the reference of the allocated memory to C function for computation. My major concern is to avoid constantly copying a big chunk of data between C and Python. For plan B, is the ctypes, cffi or cython preferred?
My knowledge of Python is really limited. I appreciate that if you could point me to a specific example for learning. Many thanks in advance!!!!
Juan