To ease profiling and testing, I have wrapped the library with SWIG.
Category: SWIG
Everything related to SWIG use (mainly with Python)
Sometimes, a C or C++ array structure must be used in Python, and it’s always better to be able to use the underlying array to do some Numpy computations. To that purpose, Numpy proposes the array interface.
I will now expose an efficient way to use SWIG to generate the array interface and exposing the __array_struct__ property.
This question was asked on the Scipy mailing-list last year (well, one week ago). Nathan Bell proposed a skeleton that I used to create an out typemap for SWIG.
%typemap(out) std::vector<double> {
int length = $1.size();
$result = PyArray_FromDims(1, &length, NPY_DOUBLE);
memcpy(PyArray_DATA((PyArrayObject*)$result),&((*(&$1))[0]),sizeof(double)*length);
}
This typemap uses obviously Numpy, so don’t forget to initialize the module and to import it. Then there is a strange instruction in memcpy. &((*(&$1))[0]) takes the address of the array of the vector, but as it is wrapped by SWIG, one has to get to the std::vector by dereferencing the SWIG wrapper. Then one can get the first element in the vector and take the address.
Edit on May 2017: This is my most recent trials with this.
%typemap(out) std::vector<float> {
npy_intp length = $1.size();
$result = PyArray_SimpleNew(1, &length, NPY_FLOAT);
memcpy(PyArray_DATA((PyArrayObject*)$result),$1.data(),sizeof(float)*length);
}
Some times ago, I proposed an optional build for SWIG if the SWIG binary was not found on the system. Here I propose an enhancement, a new library builder that will be registered in the environment env as PythonModule. It takes the same arguments as a classical SharedLibrary, but it does some additional steps :
- It forces SWIG to create a Python wrapper (flag -python)
- It checks if SWIG is present at all
- It suppresses every prefix that the system might need (as lib in Linux)
- On Windows and for Python >= 2.5, it changes the extension as pyd
I was looking for some days in SWIG documentation how I could release the GIL (Global Interpreter Lock) with SWIG. There were some macros defined in the generated code, but none was used in any place.
In fact, I just had to enable the thread support with an additional argument (-threads) and now every wrapped function releases the GIL before it is called, but that does not satisfy me. Indeed, some of my wrappers must retain the GIL while they are used (see this item). So here are the features that can be used :
- nothread enables or disables the whole thread lock for a function :
- %nothread activates the nothread feature
- %thread disables the feature
- %clearnothread clears the feature
- nothreadblock enables or disables the block thread lock for a function :
- %nothreadblock activates the nothreadblock feature
- %threadblock disables the feature
- %clearnothreadblock clears the feature
- nothreadallow enables or disables the allow thread lock for a function :
- %nothreadallow activates the nothreadallow feature
- %threadallow disables the feature
- %clearnothreadallow clears the feature
When the whole thread lock is enabled, the GIL is locked when entering the C function (with the macro SWIG_PYTHON_THREAD_BEGIN_BLOCK). Then it is released before the call to the function (with SWIG_PYTHON_THREAD_BEGIN_ALLOW), retained after the end (SWIG_PYTHON_THREAD_END_ALLOW) and finally it is released when exiting the function (SWIG_PYTHON_THREAD_END_BLOCK), after all Python result variables are created and/or modified.
When moving to Python, the real big problem that arises is the transformation of a Python array into the C++ container the team used for years.
Let’s set some hypothesis :
- there is a separation between the class containing the data and the class that uses the data (iterators, …)
- the containing class can be changed (policy or strategy pattern)
The first hypothesis is derived from the responsibility principle, the two classes have two distinct responsibilities, the first allocates the data space and allows simple access to it, the second allows usual operations (assignation, comparison tests or iterations for instance).
The second one will be the heart of the wrapper. It allows to change the way data is stored and accessed in a simple way.
I now regularly use Scons as a cross-platform software construction tool. It is easy, written in Python, and I know Python, so no problem learning a new language as for CMake. In some cases when I use SWIG, the target platform does not have the SWIG executable. But when compiling a module, Scons must use this executable, whatever you try to do. In this case, one need to create a new SharedLibrary builder, so that this attribute will determine if SWIG is present or if the generated .c or .cpp files must be used instead.