5.6.1 : Wrapper of the timer
The timer will be wrapped in two files :
- timerWrapper.h
- timerWrapper.cpp
The timerWrapper.h file is quite simple.
We just have to include some python stuff and define our wrapped function.
1 2 3 4 5 6 7 8 9 |
#ifndef __TIMER_WRAPPER_H__ #define __TIMER_WRAPPER_H__ #include <Python.h> #include "structmember.h" PyObject * timerWrapper(PyObject *self, PyObject *args); #endif |
The prototype of the function is standard for wrappers :
- Two PyObject pointer as input
- One PyObject pointer as output
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 do classial includes. First for our c++ timer :
1 |
#include "timer.h"
|
Then for our wrapped timer :
1 |
#include "timerWrapper.h"
|
Then we call our rdtsc function and create a PyObject for long unsigned int or size_t with the function Py_BuildValue :
1 2 3 4 5 6 7 8 9 |
///Allocate an aligned matrix of float with a pitch /** @param self : pointer to the parent object if it exist * @param args : arguments passed to the program * @return result of rdtsc function */ PyObject * timerWrapper(PyObject *self, PyObject *args){ size_t res(rdtsc()); return Py_BuildValue("k", res); } |
The full timerWrapper.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 |
/*************************************** Auteur : Pierre Aubert Mail : pierre.aubert@lapp.in2p3.fr 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 "timer.h" #include "timerWrapper.h" ///Allocate an aligned matrix of float with a pitch /** @param self : pointer to the parent object if it exist * @param args : arguments passed to the program * @return result of rdtsc function */ PyObject * timerWrapper(PyObject *self, PyObject *args){ size_t res(rdtsc()); return Py_BuildValue("k", res); } |
You can download it here.