5.6.4 : The wrapper module source : astericshpc.cpp

Now, we have to write the module file astericshpc.cpp. It will define the python module and the wrapped function to be called.

First, we have to activate the numpy we disabled in the previous files. Otherwise, you will get a segmentation fault out of knowhere.
1
2
3
4
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#ifndef DISABLE_COOL_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL core_ARRAY_API
#endif
Then, we include all the previous wrapper and the python stuff, of course.
1
2
3
4
5
6
7
8
9
#include <Python.h>
#include "structmember.h"
#include <numpy/arrayobject.h>

#include <string>

#include "allocTableWrapper.h"
#include "allocMatrixWrapper.h"
#include "timerWrapper.h"
It is always good to have a documentation of your function. The folowing will appear when you call a function with '?' or shift-tab in jupyter-notebook :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::string allocTable_docstring = "Allocate a table of float with a padding\n\
Parameters :\n\
	nbElement : number of elements of the table\n\
Return :\n\
	1 dimentional aligned numpy array initialised to 0";

std::string allocMatrix_docstring = "Allocate a matrix of float with a pitch\n\
Parameters :\n\
	nbRow : number of rows of the matrix\n\
	nbCol : number of colmuns of the matrix\n\
Return :\n\
	2 dimentional aligned numpy array initialised to 0 with a pitch";

std::string timerWrapper_docString = "Get the number of cycles since the begining of the program\n\
Return :\n\
	number of cycles since the begining of the program in uint64";
We define the callable method of our wrapper :
1
2
3
4
5
6
7
static PyMethodDef _astericshpc_methods[] = {
	{"allocTable", (PyCFunction)allocTableWrapper, METH_VARARGS, allocTable_docstring.c_str()},
	{"allocMatrix", (PyCFunction)allocMatrixWrapper, METH_VARARGS, allocMatrix_docstring.c_str()},
	{"rdtsc", (PyCFunction)timerWrapper, METH_NOARGS, timerWrapper_docString.c_str()},

	{NULL, NULL}
};
	Notice that you can expose the functions you want, not all the functions you developed.
Now, we have to define the python module itself :
1
2
3
4
5
6
7
8
9
10
11
static PyModuleDef _astericshpc_module = {
	PyModuleDef_HEAD_INIT,
	"astericshpc",
	"",
	-1,
	_astericshpc_methods,
	NULL,
	NULL,
	NULL,
	NULL
};
Now we define the function which will be called on the import of our module.
	Notice you must call this function PyInit_NameOfYouModule where NameOfYouModule is the name of THIS file.
We create the module, which is a PyObject.
	Do not forget to call import_array otherwise you will get a warning on the best cases and a segmentation fault on your module import.
1
2
3
4
5
6
7
8
9
10
11
12
13
///Create the python module astericshpc
/**	@return python module astericshpc
*/
PyMODINIT_FUNC PyInit_astericshpc(void){
	PyObject *m;
	import_array();
	
	m = PyModule_Create(&_astericshpc_module);
	if(m == NULL){
		return NULL;
	}
	return m;
}
The full astericshpc.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/***************************************
	Auteur : Pierre Aubert
	Mail : aubertp7@gmail.com
	Licence : CeCILL-C
****************************************/

#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#ifndef DISABLE_COOL_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL core_ARRAY_API
#endif

#include <Python.h>
#include "structmember.h"
#include <numpy/arrayobject.h>

#include <string>

#include "allocTableWrapper.h"
#include "allocMatrixWrapper.h"
#include "timerWrapper.h"

std::string allocTable_docstring = "Allocate a table of float with a padding\n\
Parameters :\n\
	nbElement : number of elements of the table\n\
Return :\n\
	1 dimentional aligned numpy array initialised to 0";

std::string allocMatrix_docstring = "Allocate a matrix of float with a pitch\n\
Parameters :\n\
	nbRow : number of rows of the matrix\n\
	nbCol : number of colmuns of the matrix\n\
Return :\n\
	2 dimentional aligned numpy array initialised to 0 with a pitch";

std::string timerWrapper_docString = "Get the number of cycles since the begining of the program\n\
Return :\n\
	number of cycles since the begining of the program in uint64";

static PyMethodDef _astericshpc_methods[] = {
	{"allocTable", (PyCFunction)allocTableWrapper, METH_VARARGS, allocTable_docstring.c_str()},
	{"allocMatrix", (PyCFunction)allocMatrixWrapper, METH_VARARGS, allocMatrix_docstring.c_str()},
	{"rdtsc", (PyCFunction)timerWrapper, METH_NOARGS, timerWrapper_docString.c_str()},

	{NULL, NULL}
};

static PyModuleDef _astericshpc_module = {
	PyModuleDef_HEAD_INIT,
	"astericshpc",
	"",
	-1,
	_astericshpc_methods,
	NULL,
	NULL,
	NULL,
	NULL
};

///Create the python module astericshpc
/**	@return python module astericshpc
*/
PyMODINIT_FUNC PyInit_astericshpc(void){
	PyObject *m;
	import_array();
	
	m = PyModule_Create(&_astericshpc_module);
	if(m == NULL){
		return NULL;
	}
	return m;
}
You can download it here.