8.6.1 : The wrapper function

The header file reductionWrapper.h is very simple :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/***************************************
	Auteur : Pierre Aubert
	Mail : aubertp7@gmail.com
	Licence : CeCILL-C
****************************************/

#ifndef __REDUCTION_WRAPPER_H__
#define __REDUCTION_WRAPPER_H__

#include <Python.h>
#include "structmember.h"

PyObject * reductionWrapper(PyObject *self, PyObject *args);

#endif
You can download it here.

Now, let's write the reductionWrapper.cpp file :

Again in this example, since we are using numpy in the module, we have to define the NO_IMPORT_ARRAY to avoid multiple definitions of the same numpy function. And we also have to specify the version of the numpy API to avoid warnings :
1
2
3
4
5
6
#define NO_IMPORT_ARRAY
#ifndef DISABLE_COOL_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL core_ARRAY_API
#endif

#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
Then we include appropriate files. Do not forget to include the asterics_alloc.h file to use our allocation function :
1
2
3
4
5
#include <iostream>
#include <numpy/arrayobject.h>
#include <bytearrayobject.h>

#include "reduction_intrinsics_interleave8.h"
To parse static parameters, we have to use the function PyArg_ParseTuple, this function works the same as the scanf function from the C standard library.

Here, we parse the output table and the two input tables with 'O' which corresponds to a PyObject type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
///Do the reduction computation
/**	@param self : parent of the function if it exist
 * 	@param args : arguments passed to the function
 * 	@return result of the reduction result
*/
PyObject * reductionWrapper(PyObject *self, PyObject *args){
	PyArrayObject *objTabX = NULL;
	if(!PyArg_ParseTuple(args, "O", &objTabX)){
		PyErr_SetString(PyExc_RuntimeError, "reductionWrapper : wrong set of arguments. Expect tabX\n");
		return NULL;
	}
	if(PyArray_NDIM(objTabX) != 1){
		PyErr_SetString(PyExc_TypeError, "reductionWrapper : input table must be a one dimension array");
		return NULL;
	}
	long unsigned int sizeElement(PyArray_DIMS(objTabX)[0]);
	
	const float * tabX = (const float*)PyArray_DATA(objTabX);
	float res(reduction(tabX, sizeElement));
	
	return Py_BuildValue("f", res);
}
The full reductionWrapper.cpp file :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/***************************************
	Auteur : Pierre Aubert
	Mail : aubertp7@gmail.com
	Licence : CeCILL-C
****************************************/

#define NO_IMPORT_ARRAY
#ifndef DISABLE_COOL_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL core_ARRAY_API
#endif

#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

#include <iostream>
#include <numpy/arrayobject.h>
#include <bytearrayobject.h>

#include "reduction_intrinsics_interleave8.h"

///Do the reduction computation
/**	@param self : parent of the function if it exist
 * 	@param args : arguments passed to the function
 * 	@return result of the reduction result
*/
PyObject * reductionWrapper(PyObject *self, PyObject *args){
	PyArrayObject *objTabX = NULL;
	if(!PyArg_ParseTuple(args, "O", &objTabX)){
		PyErr_SetString(PyExc_RuntimeError, "reductionWrapper : wrong set of arguments. Expect tabX\n");
		return NULL;
	}
	if(PyArray_NDIM(objTabX) != 1){
		PyErr_SetString(PyExc_TypeError, "reductionWrapper : input table must be a one dimension array");
		return NULL;
	}
	long unsigned int sizeElement(PyArray_DIMS(objTabX)[0]);
	
	const float * tabX = (const float*)PyArray_DATA(objTabX);
	float res(reduction(tabX, sizeElement));
	
	return Py_BuildValue("f", res);
}
You can download it here.