cmsismodule.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Python Wrapper
  3. * Title: cmsismodule.c
  4. * Description: C code for the CMSIS-DSP Python wrapper
  5. *
  6. * $Date: 25. March 2019
  7. * $Revision: V0.0.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #define NPY_NO_DEPRECATED_API NPY_1_15_API_VERSION
  29. #ifdef WIN
  30. #pragma warning( disable : 4013 )
  31. #pragma warning( disable : 4244 )
  32. #endif
  33. #include <Python.h>
  34. #define MAX(A,B) (A) < (B) ? (B) : (A)
  35. #define CAT1(A,B) A##B
  36. #define CAT(A,B) CAT1(A,B)
  37. #ifdef CMSISDSP
  38. #include "arm_math.h"
  39. #define MODNAME "cmsisdsp"
  40. #define MODINITNAME cmsisdsp
  41. #endif
  42. #include <numpy/arrayobject.h>
  43. #include <numpy/ndarraytypes.h>
  44. #if PY_MAJOR_VERSION >= 3
  45. #define IS_PY3K
  46. #endif
  47. struct module_state {
  48. PyObject *error;
  49. };
  50. #if PY_MAJOR_VERSION >= 3
  51. #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
  52. #else
  53. #define GETSTATE(m) (&_state)
  54. static struct module_state _state;
  55. #endif
  56. static PyObject *
  57. error_out(PyObject *m) {
  58. struct module_state *st = GETSTATE(m);
  59. PyErr_SetString(st->error, "something bad happened");
  60. return NULL;
  61. }
  62. #define MLTYPE(name,thenewfunc,deallocfunc,initfunc,methods)\
  63. static PyTypeObject ml_##name##Type = { \
  64. PyVarObject_HEAD_INIT(NULL, 0) \
  65. .tp_name=MODNAME".##name", \
  66. .tp_basicsize = sizeof(ml_##name##Object), \
  67. .tp_itemsize = 0, \
  68. .tp_dealloc = (destructor)deallocfunc, \
  69. .tp_flags = Py_TPFLAGS_DEFAULT, \
  70. .tp_doc = #name, \
  71. .tp_init = (initproc)initfunc, \
  72. .tp_new = (newfunc)thenewfunc, \
  73. .tp_methods = methods \
  74. };
  75. #define MEMCPY(DST,SRC,NB,FORMAT) \
  76. for(memCpyIndex = 0; memCpyIndex < (NB) ; memCpyIndex++)\
  77. { \
  78. (DST)[memCpyIndex] = (FORMAT)(SRC)[memCpyIndex]; \
  79. }
  80. #define GETFIELD(NAME,FIELD,FORMAT) \
  81. static PyObject * \
  82. Method_##NAME##_##FIELD(ml_##NAME##Object *self, PyObject *ignored)\
  83. { \
  84. return(Py_BuildValue(FORMAT,self->instance->FIELD)); \
  85. }
  86. #define GETFIELDARRAY(NAME,FIELD,FORMAT) \
  87. static PyObject * \
  88. Method_##NAME##_##FIELD(ml_##NAME##Object *self, PyObject *ignored)\
  89. { \
  90. return(specific_##NAME##_##FIELD(self->instance)); \
  91. }
  92. #define INITARRAYFIELD(FIELD,FORMAT,SRCFORMAT,DSTFORMAT) \
  93. if (FIELD) \
  94. { \
  95. PyArray_Descr *desct=PyArray_DescrFromType(FORMAT); \
  96. PyArrayObject *FIELD##c = (PyArrayObject *)PyArray_FromAny(FIELD,desct,\
  97. 1,0,NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED | NPY_ARRAY_FORCECAST, \
  98. NULL); \
  99. if (FIELD##c) \
  100. { \
  101. uint32_t memCpyIndex; \
  102. SRCFORMAT *f=(SRCFORMAT*)PyArray_DATA(FIELD##c); \
  103. uint32_t n = PyArray_SIZE(FIELD##c); \
  104. self->instance->FIELD =PyMem_Malloc(sizeof(DSTFORMAT)*n); \
  105. MEMCPY(self->instance->FIELD ,f,n,DSTFORMAT); \
  106. Py_DECREF(FIELD##c); \
  107. } \
  108. }
  109. #define GETCARRAY(PYVAR,CVAR,FORMAT,SRCFORMAT,DSTFORMAT) \
  110. if (PYVAR) \
  111. { \
  112. PyArray_Descr *desct=PyArray_DescrFromType(FORMAT); \
  113. PyArrayObject *PYVAR##c = (PyArrayObject *)PyArray_FromAny(PYVAR,desct,\
  114. 1,0,NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED | NPY_ARRAY_FORCECAST, \
  115. NULL); \
  116. if (PYVAR##c) \
  117. { \
  118. uint32_t memCpyIndex; \
  119. SRCFORMAT *f=(SRCFORMAT*)PyArray_DATA(PYVAR##c); \
  120. uint32_t n = PyArray_SIZE(PYVAR##c); \
  121. CVAR =PyMem_Malloc(sizeof(DSTFORMAT)*n); \
  122. MEMCPY(CVAR ,f,n,DSTFORMAT); \
  123. Py_DECREF(PYVAR##c); \
  124. } \
  125. }
  126. #define GETARGUMENT(FIELD,FORMAT,SRCFORMAT,DSTFORMAT) \
  127. uint32_t arraySize##FIELD=0; \
  128. if (FIELD) \
  129. { \
  130. PyArray_Descr *desct=PyArray_DescrFromType(FORMAT); \
  131. PyArrayObject *FIELD##c = (PyArrayObject *)PyArray_FromAny(FIELD,desct, \
  132. 1,0,NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED | NPY_ARRAY_FORCECAST, \
  133. NULL); \
  134. if (FIELD##c) \
  135. { \
  136. uint32_t memCpyIndex; \
  137. SRCFORMAT *f=(SRCFORMAT*)PyArray_DATA(FIELD##c); \
  138. arraySize##FIELD = PyArray_SIZE(FIELD##c); \
  139. FIELD##_converted =PyMem_Malloc(sizeof(DSTFORMAT)*arraySize##FIELD);\
  140. MEMCPY(FIELD##_converted ,f,arraySize##FIELD,DSTFORMAT); \
  141. Py_DECREF(FIELD##c); \
  142. } \
  143. }
  144. #define FREEARGUMENT(FIELD) \
  145. PyMem_Free(FIELD)
  146. #ifdef IS_PY3K
  147. #define ADDTYPE(name) \
  148. if (PyType_Ready(&ml_##name##Type) < 0) \
  149. return; \
  150. \
  151. Py_INCREF(&ml_##name##Type); \
  152. PyModule_AddObject(module, #name, (PyObject *)&ml_##name##Type);
  153. #else
  154. #define ADDTYPE(name) \
  155. if (PyType_Ready(&ml_##name##Type) < 0) \
  156. return; \
  157. \
  158. Py_INCREF(&ml_##name##Type); \
  159. PyModule_AddObject(module, #name, (PyObject *)&ml_##name##Type);
  160. #endif
  161. #define FLOATARRAY2(OBJ,NB1,NB2,DATA) \
  162. npy_intp dims[2]; \
  163. dims[0]=NB1; \
  164. dims[1]=NB2; \
  165. const int ND=2; \
  166. PyObject *OBJ=PyArray_SimpleNewFromData(ND, dims, NPY_FLOAT, DATA);
  167. #define FLOATARRAY1(OBJ,NB1,DATA) \
  168. npy_intp dims[1]; \
  169. dims[0]=NB1; \
  170. const int ND=1; \
  171. PyObject *OBJ=PyArray_SimpleNewFromData(ND, dims, NPY_FLOAT, DATA);
  172. #define FLOAT64ARRAY1(OBJ,NB1,DATA) \
  173. npy_intp dims[1]; \
  174. dims[0]=NB1; \
  175. const int ND=1; \
  176. PyObject *OBJ=PyArray_SimpleNewFromData(ND, dims, NPY_DOUBLE, DATA);
  177. #define UINT32ARRAY1(OBJ,NB1,DATA) \
  178. npy_intp dims[1]; \
  179. dims[0]=NB1; \
  180. const int ND=1; \
  181. PyObject *OBJ=PyArray_SimpleNewFromData(ND, dims, NPY_UINT32, DATA);
  182. #define INT32ARRAY1(OBJ,NB1,DATA) \
  183. npy_intp dims[1]; \
  184. dims[0]=NB1; \
  185. const int ND=1; \
  186. PyObject *OBJ=PyArray_SimpleNewFromData(ND, dims, NPY_INT32, DATA);
  187. #define INT16ARRAY1(OBJ,NB1,DATA) \
  188. npy_intp dims[1]; \
  189. dims[0]=NB1; \
  190. const int ND=1; \
  191. PyObject *OBJ=PyArray_SimpleNewFromData(ND, dims, NPY_INT16, DATA);
  192. #define INT8ARRAY1(OBJ,NB1,DATA) \
  193. npy_intp dims[1]; \
  194. dims[0]=NB1; \
  195. const int ND=1; \
  196. PyObject *OBJ=PyArray_SimpleNewFromData(ND, dims, NPY_BYTE, DATA);
  197. #define MATRIXFROMNUMPY(EXT,TYP,SRCTYPE,NUMPYTYPE) \
  198. arm_matrix_instance_##EXT *EXT##MatrixFromNumpy(PyObject *o) \
  199. { \
  200. arm_matrix_instance_##EXT *s; \
  201. \
  202. s=PyMem_Malloc(sizeof(arm_matrix_instance_##EXT)); \
  203. s->pData=NULL; \
  204. s->numRows=0; \
  205. s->numCols=0; \
  206. \
  207. PyArray_Descr *desct=PyArray_DescrFromType(NUMPYTYPE); \
  208. PyArrayObject *cdata = (PyArrayObject *)PyArray_FromAny(o,desct, \
  209. 1,0,NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED | NPY_ARRAY_FORCECAST, \
  210. NULL); \
  211. if (cdata) \
  212. { \
  213. uint32_t memCpyIndex; \
  214. SRCTYPE *f=(SRCTYPE*)PyArray_DATA(cdata); \
  215. s->numRows=PyArray_DIM(cdata,0); \
  216. s->numCols=PyArray_DIM(cdata,1); \
  217. uint32_t nb = PyArray_SIZE(cdata); \
  218. s->pData = PyMem_Malloc(sizeof(TYP)*nb); \
  219. MEMCPY(s->pData ,f,nb,TYP); \
  220. Py_DECREF(cdata); \
  221. } \
  222. \
  223. \
  224. return(s); \
  225. \
  226. }
  227. MATRIXFROMNUMPY(f32,float32_t,double,NPY_DOUBLE);
  228. MATRIXFROMNUMPY(f64,float64_t,double,NPY_DOUBLE);
  229. MATRIXFROMNUMPY(q31,q31_t,int32_t,NPY_INT32);
  230. MATRIXFROMNUMPY(q15,q15_t,int16_t,NPY_INT16);
  231. #define CREATEMATRIX(EXT,TYP) \
  232. arm_matrix_instance_##EXT *create##EXT##Matrix(uint32_t r,uint32_t c)\
  233. { \
  234. arm_matrix_instance_##EXT *s; \
  235. \
  236. s=PyMem_Malloc(sizeof(arm_matrix_instance_##EXT)); \
  237. s->pData=PyMem_Malloc(sizeof(TYP)*r*c); \
  238. s->numRows=r; \
  239. s->numCols=c; \
  240. return(s); \
  241. }
  242. CREATEMATRIX(f32,float32_t);
  243. CREATEMATRIX(f64,float64_t);
  244. CREATEMATRIX(q31,q31_t);
  245. CREATEMATRIX(q15,q15_t);
  246. #define NUMPYARRAYFROMMATRIX(EXT,NUMPYTYPE_FROMC) \
  247. PyObject *NumpyArrayFrom##EXT##Matrix(arm_matrix_instance_##EXT *mat) \
  248. { \
  249. npy_intp dims[2]; \
  250. dims[0]=mat->numRows; \
  251. dims[1]=mat->numCols; \
  252. const int ND=2; \
  253. PyObject *OBJ=PyArray_SimpleNewFromData(ND, dims, NUMPYTYPE_FROMC, mat->pData);\
  254. return(OBJ); \
  255. }
  256. NUMPYARRAYFROMMATRIX(f32,NPY_FLOAT);
  257. NUMPYARRAYFROMMATRIX(f64,NPY_DOUBLE);
  258. NUMPYARRAYFROMMATRIX(q31,NPY_INT32);
  259. NUMPYARRAYFROMMATRIX(q15,NPY_INT16);
  260. //#include "specific.h"
  261. #include "cmsismodule.h"
  262. #if 0
  263. static PyObject *cmsisml_test(PyObject *obj, PyObject *args)
  264. {
  265. ml_arm_svm_linear_instance_f32Object *self=NULL;
  266. PyObject *svm, *vector=NULL;
  267. if (!PyArg_ParseTuple(args, "OO", &svm,&vector))
  268. return NULL;
  269. self=(ml_arm_svm_linear_instance_f32Object*)svm;
  270. if (self)
  271. {
  272. if (self->instance)
  273. {
  274. int result;
  275. float32_t *input=NULL;
  276. GETCARRAY(vector,input,NPY_DOUBLE,double,float32_t);
  277. arm_svm_linear_predict_f32(self->instance,input,&result);
  278. /*
  279. printf("Dual\n");
  280. for(int i = 0 ; i < self->instance->nbOfSupportVectors ; i++)
  281. {
  282. printf("%f\n",self->instance->dualCoefficients[i]);
  283. }
  284. printf("Vectors\n");
  285. int k=0;
  286. for(int i = 0 ; i < self->instance->nbOfSupportVectors ; i++)
  287. {
  288. printf("Vector %d\n",i);
  289. for(int j = 0 ; j < self->instance->vectorDimension ; j++)
  290. {
  291. printf("%f\n",self->instance->supportVectors[k]);
  292. k++;
  293. }
  294. }
  295. printf("Classes\n");
  296. for(int i = 0 ; i < 2 ; i++)
  297. {
  298. printf("%d\n",self->instance->classes[i]);
  299. }
  300. printf("Intercept %f\n",self->instance->intercept);
  301. */
  302. PyMem_Free(input);
  303. return(Py_BuildValue("i",result));
  304. }
  305. }
  306. return(Py_BuildValue("i",-1));
  307. }
  308. #endif
  309. #ifdef IS_PY3K
  310. static int cmsisml_traverse(PyObject *m, visitproc visit, void *arg) {
  311. Py_VISIT(GETSTATE(m)->error);
  312. return 0;
  313. }
  314. static int cmsisml_clear(PyObject *m) {
  315. Py_CLEAR(GETSTATE(m)->error);
  316. return 0;
  317. }
  318. static struct PyModuleDef moduledef = {
  319. PyModuleDef_HEAD_INIT,
  320. MODNAME,
  321. NULL,
  322. sizeof(struct module_state),
  323. CMSISMLMethods,
  324. NULL,
  325. cmsisml_traverse,
  326. cmsisml_clear,
  327. NULL
  328. };
  329. #define INITERROR return NULL
  330. PyMODINIT_FUNC
  331. CAT(PyInit_,MODINITNAME)(void)
  332. #else
  333. #define INITERROR return
  334. void CAT(init,MODINITNAME)(void)
  335. #endif
  336. {
  337. import_array();
  338. #ifdef IS_PY3K
  339. PyObject *module = PyModule_Create(&moduledef);
  340. #else
  341. PyObject *module = Py_InitModule(MODNAME, CMSISMLMethods);
  342. #endif
  343. if (module == NULL)
  344. INITERROR;
  345. struct module_state *st = GETSTATE(module);
  346. st->error = PyErr_NewException(MODNAME".Error", NULL, NULL);
  347. if (st->error == NULL) {
  348. Py_DECREF(module);
  349. INITERROR;
  350. }
  351. typeRegistration(module);
  352. #ifdef IS_PY3K
  353. return module;
  354. #endif
  355. }