starpu_task_wrapper.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #undef NDEBUG
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <starpu.h>
  21. #define PY_SSIZE_T_CLEAN
  22. #include <Python.h>
  23. #ifdef STARPU_PYTHON_HAVE_NUMPY
  24. #include <numpy/arrayobject.h>
  25. #endif
  26. /*macro*/
  27. #if defined(Py_DEBUG) || defined(DEBUG)
  28. extern void _Py_CountReferences(FILE*);
  29. #define CURIOUS(x) { fprintf(stderr, __FILE__ ":%d ", __LINE__); x; }
  30. #else
  31. #define CURIOUS(x)
  32. #endif
  33. #define MARKER() CURIOUS(fprintf(stderr, "\n"))
  34. #define DESCRIBE(x) CURIOUS(fprintf(stderr, " " #x "=%d\n", x))
  35. #define DESCRIBE_HEX(x) CURIOUS(fprintf(stderr, " " #x "=%08x\n", x))
  36. #define COUNTREFS() CURIOUS(_Py_CountReferences(stderr))
  37. /*******/
  38. /*********************Functions passed in task_submit wrapper***********************/
  39. static PyObject *asyncio_module; /*python asyncio module*/
  40. static PyObject *cloudpickle_module; /*cloudpickle module*/
  41. /*structure contains parameters which are passed to starpu_task.cl_arg*/
  42. // struct codelet_args
  43. // {
  44. // PyObject *f; /*the python function passed in*/
  45. // PyObject *argList; /*argument list of python function passed in*/
  46. // PyObject *rv; /*return value when using PyObject_CallObject call the function f*/
  47. // PyObject *fut; /*asyncio.Future*/
  48. // PyObject *lp; /*asyncio.Eventloop*/
  49. // };
  50. static char* starpu_cloudpickle_dumps(PyObject *obj, Py_ssize_t* obj_data_size)
  51. {
  52. PyObject *dumps = PyObject_GetAttrString(cloudpickle_module, "dumps");
  53. PyObject *obj_bytes= PyObject_CallFunctionObjArgs(dumps, obj, NULL);
  54. char* obj_data;
  55. PyBytes_AsStringAndSize(obj_bytes, &obj_data, obj_data_size);
  56. return obj_data;
  57. }
  58. static PyObject* starpu_cloudpickle_loads(char* pyString, Py_ssize_t pyString_size)
  59. {
  60. PyObject *loads = PyObject_GetAttrString(cloudpickle_module, "loads");
  61. PyObject *obj_bytes_str = PyBytes_FromStringAndSize(pyString, pyString_size);
  62. PyObject *obj = PyObject_CallFunctionObjArgs(loads, obj_bytes_str, NULL);
  63. return obj;
  64. }
  65. /*function passed to starpu_codelet.cpu_func*/
  66. void starpupy_codelet_func(void *buffers[], void *cl_arg)
  67. {
  68. char * func_data;
  69. Py_ssize_t func_data_size;
  70. PyObject *func_py; /*the python function passed in*/
  71. char * arg_data;
  72. Py_ssize_t arg_data_size;
  73. PyObject *argList; /*argument list of python function passed in*/
  74. //struct codelet_args *cst = (struct codelet_args*) cl_arg;
  75. struct starpu_task *task = starpu_task_get_current();
  76. /*Initialize struct starpu_codelet_unpack_arg_data*/
  77. struct starpu_codelet_pack_arg_data data;
  78. starpu_codelet_unpack_arg_init(&data, &task->cl_arg, &task->cl_arg_size);
  79. /*get func_py char*/
  80. starpu_codelet_unpack_arg(&data, &func_data_size, sizeof(func_data_size));
  81. func_data = (char *)malloc(func_data_size);
  82. starpu_codelet_unpack_arg(&data, func_data, func_data_size);
  83. //starpu_codelet_unpack_arg(&data, &func_py, sizeof(func_py));
  84. /*get argList char*/
  85. starpu_codelet_unpack_arg(&data, &arg_data_size, sizeof(arg_data_size));
  86. arg_data = (char *)malloc(arg_data_size);
  87. starpu_codelet_unpack_arg(&data, arg_data, arg_data_size);
  88. //starpu_codelet_unpack_arg(&data, &argList, sizeof(argList));
  89. /*skip fut*/
  90. starpu_codelet_unpack_discard_arg(&data);
  91. /*skip loop*/
  92. starpu_codelet_unpack_discard_arg(&data);
  93. /*skip sb*/
  94. starpu_codelet_unpack_discard_arg(&data);
  95. /*make sure we own the GIL*/
  96. PyGILState_STATE state = PyGILState_Ensure();
  97. /*use cloudpickle to load func_py and argList*/
  98. func_py=starpu_cloudpickle_loads(func_data, func_data_size);
  99. argList=starpu_cloudpickle_loads(arg_data, arg_data_size);
  100. /*verify that the function is a proper callable*/
  101. if (!PyCallable_Check(func_py))
  102. {
  103. printf("py_callback: expected a callable function\n");
  104. exit(1);
  105. }
  106. /*check the arguments of python function passed in*/
  107. int i;
  108. for(i=0; i < PyTuple_Size(argList); i++)
  109. {
  110. PyObject *obj = PyTuple_GetItem(argList, i);
  111. const char *tp = Py_TYPE(obj)->tp_name;
  112. if(strcmp(tp, "_asyncio.Future") == 0)
  113. {
  114. /*if one of arguments is Future, get its result*/
  115. PyObject *fut_result = PyObject_CallMethod(obj, "result", NULL);
  116. /*replace the Future argument to its result*/
  117. PyTuple_SetItem(argList, i, fut_result);
  118. }
  119. /*else if (strcmp(tp, "numpy.ndarray")==0)
  120. {
  121. printf("array is %p\n", obj);
  122. }*/
  123. }
  124. /*call the python function get the return value rv*/
  125. //PyObject *pRetVal = PyObject_CallObject(cst->f, cst->argList);
  126. //cst->rv = pRetVal;
  127. PyObject *rv = PyObject_CallObject(func_py, argList);
  128. /*if the result is None type, pack NULL without using cloudpickle*/
  129. if (rv==Py_None)
  130. {
  131. char* rv_data=NULL;
  132. Py_ssize_t rv_data_size=0;
  133. starpu_codelet_pack_arg(&data, &rv_data_size, sizeof(rv_data_size));
  134. starpu_codelet_pack_arg(&data, &rv_data, sizeof(rv_data));
  135. }
  136. /*else use cloudpickle to dump rv*/
  137. else
  138. {
  139. Py_ssize_t rv_data_size;
  140. char* rv_data = starpu_cloudpickle_dumps(rv, &rv_data_size);
  141. starpu_codelet_pack_arg(&data, &rv_data_size, sizeof(rv_data_size));
  142. starpu_codelet_pack_arg(&data, rv_data, rv_data_size);
  143. }
  144. //starpu_codelet_pack_arg(&data, &rv, sizeof(rv));
  145. starpu_codelet_pack_arg_fini(&data, &task->cl_arg, &task->cl_arg_size);
  146. //Py_DECREF(cst->f);
  147. Py_DECREF(func_py);
  148. Py_DECREF(argList);
  149. /*restore previous GIL state*/
  150. PyGILState_Release(state);
  151. }
  152. /*function passed to starpu_task.callback_func*/
  153. void cb_func(void *v)
  154. {
  155. PyObject *fut; /*asyncio.Future*/
  156. PyObject *loop; /*asyncio.Eventloop*/
  157. char * rv_data;
  158. Py_ssize_t rv_data_size;
  159. PyObject *rv; /*return value when using PyObject_CallObject call the function f*/
  160. struct starpu_task *task = starpu_task_get_current();
  161. //struct codelet_args *cst = (struct codelet_args*) task->cl_arg;
  162. /*Initialize struct starpu_codelet_unpack_arg_data*/
  163. struct starpu_codelet_pack_arg_data data;
  164. starpu_codelet_unpack_arg_init(&data, &task->cl_arg, &task->cl_arg_size);
  165. /*skip func_py*/
  166. starpu_codelet_unpack_discard_arg(&data);
  167. starpu_codelet_unpack_discard_arg(&data);
  168. /*skip argList*/
  169. starpu_codelet_unpack_discard_arg(&data);
  170. starpu_codelet_unpack_discard_arg(&data);
  171. /*get fut*/
  172. starpu_codelet_unpack_arg(&data, &fut, sizeof(fut));
  173. /*get loop*/
  174. starpu_codelet_unpack_arg(&data, &loop, sizeof(loop));
  175. /*skip sb*/
  176. starpu_codelet_unpack_discard_arg(&data);
  177. /*get rv char*/
  178. starpu_codelet_unpack_arg(&data, &rv_data_size, sizeof(rv_data_size));
  179. rv_data = (char *)malloc(rv_data_size);
  180. starpu_codelet_unpack_arg(&data, rv_data, rv_data_size);
  181. //starpu_codelet_unpack_arg(&data, &rv, sizeof(rv));
  182. /*make sure we own the GIL*/
  183. PyGILState_STATE state = PyGILState_Ensure();
  184. /*if the rv_data_size is 0, the result is None type*/
  185. if (rv_data_size==0)
  186. {
  187. rv=Py_None;
  188. }
  189. /*else use cloudpickle to load rv*/
  190. else
  191. {
  192. rv=starpu_cloudpickle_loads(rv_data, rv_data_size);
  193. }
  194. /*set the Future result and mark the Future as done*/
  195. PyObject *set_result = PyObject_GetAttrString(fut, "set_result");
  196. PyObject *loop_callback = PyObject_CallMethod(loop, "call_soon_threadsafe", "(O,O)", set_result, rv);
  197. Py_DECREF(loop_callback);
  198. Py_DECREF(set_result);
  199. Py_DECREF(rv);
  200. Py_DECREF(fut);
  201. Py_DECREF(loop);
  202. //Py_DECREF(argList);
  203. //Py_DECREF(perfmodel);
  204. struct starpu_codelet *func_cl=(struct starpu_codelet *) task->cl;
  205. if (func_cl->model != NULL)
  206. {
  207. struct starpu_perfmodel *perf =(struct starpu_perfmodel *) func_cl->model;
  208. PyObject *perfmodel=PyCapsule_New(perf, "Perf", 0);
  209. Py_DECREF(perfmodel);
  210. }
  211. /*restore previous GIL state*/
  212. PyGILState_Release(state);
  213. /*deallocate task*/
  214. free(task->cl);
  215. free(task->cl_arg);
  216. }
  217. /***********************************************************************************/
  218. /*PyObject*->struct starpu_task**/
  219. static struct starpu_task *PyTask_AsTask(PyObject *obj)
  220. {
  221. return (struct starpu_task *) PyCapsule_GetPointer(obj, "Task");
  222. }
  223. /* destructor function for task */
  224. static void del_Task(PyObject *obj)
  225. {
  226. struct starpu_task *obj_task=PyTask_AsTask(obj);
  227. obj_task->destroy=1; /*XXX we should call starpu task destroy*/
  228. }
  229. /*struct starpu_task*->PyObject**/
  230. static PyObject *PyTask_FromTask(struct starpu_task *task)
  231. {
  232. return PyCapsule_New(task, "Task", del_Task);
  233. }
  234. /***********************************************************************************/
  235. static size_t sizebase (struct starpu_task *task, unsigned nimpl)
  236. {
  237. int sb;
  238. //struct codelet_args *cst = (struct codelet_args*) task->cl_arg;
  239. /*Initialize struct starpu_codelet_unpack_arg_data*/
  240. struct starpu_codelet_pack_arg_data data;
  241. starpu_codelet_unpack_arg_init(&data, &task->cl_arg, &task->cl_arg_size);
  242. /*skip func_py*/
  243. starpu_codelet_unpack_discard_arg(&data);
  244. starpu_codelet_unpack_discard_arg(&data);
  245. /*skip argList*/
  246. starpu_codelet_unpack_discard_arg(&data);
  247. starpu_codelet_unpack_discard_arg(&data);
  248. /*skip fut*/
  249. starpu_codelet_unpack_discard_arg(&data);
  250. /*skip loop*/
  251. starpu_codelet_unpack_discard_arg(&data);
  252. /*get sb*/
  253. starpu_codelet_unpack_arg(&data, &sb, sizeof(sb));
  254. /*skip rv*/
  255. starpu_codelet_unpack_discard_arg(&data);
  256. starpu_codelet_unpack_discard_arg(&data);
  257. //starpu_codelet_unpack_args(task_submit->cl_arg, &func_py, &argList, &fut, &loop, &sb, &rv);
  258. return sb;
  259. }
  260. static void del_Perf(PyObject *obj)
  261. {
  262. struct starpu_perfmodel *perf=(struct starpu_perfmodel*)PyCapsule_GetPointer(obj, "Perf");
  263. free(perf);
  264. }
  265. /*initialization of perfmodel*/
  266. static PyObject* init_perfmodel(PyObject *self, PyObject *args)
  267. {
  268. char *sym;
  269. if (!PyArg_ParseTuple(args, "s", &sym))
  270. return NULL;
  271. /*allocate a perfmodel structure*/
  272. struct starpu_perfmodel *perf=(struct starpu_perfmodel*)calloc(1, sizeof(struct starpu_perfmodel));
  273. /*get the perfmodel symbol*/
  274. char *p =strdup(sym);
  275. perf->symbol=p;
  276. perf->type=STARPU_HISTORY_BASED;
  277. /*struct perfmodel*->PyObject**/
  278. PyObject *perfmodel=PyCapsule_New(perf, "Perf", NULL);
  279. return perfmodel;
  280. }
  281. /*free perfmodel*/
  282. static PyObject* free_perfmodel(PyObject *self, PyObject *args)
  283. {
  284. PyObject *perfmodel;
  285. if (!PyArg_ParseTuple(args, "O", &perfmodel))
  286. return NULL;
  287. /*PyObject*->struct perfmodel**/
  288. struct starpu_perfmodel *perf=PyCapsule_GetPointer(perfmodel, "Perf");
  289. starpu_save_history_based_model(perf);
  290. //starpu_perfmodel_unload_model(perf);
  291. //free(perf->symbol);
  292. starpu_perfmodel_deinit(perf);
  293. free(perf);
  294. /*return type is void*/
  295. Py_INCREF(Py_None);
  296. return Py_None;
  297. }
  298. static PyObject* starpu_save_history_based_model_wrapper(PyObject *self, PyObject *args)
  299. {
  300. PyObject *perfmodel;
  301. if (!PyArg_ParseTuple(args, "O", &perfmodel))
  302. return NULL;
  303. /*PyObject*->struct perfmodel**/
  304. struct starpu_perfmodel *perf=PyCapsule_GetPointer(perfmodel, "Perf");
  305. starpu_save_history_based_model(perf);
  306. /*return type is void*/
  307. Py_INCREF(Py_None);
  308. return Py_None;
  309. }
  310. /*****************************Wrappers of StarPU methods****************************/
  311. /*wrapper submit method*/
  312. static PyObject* starpu_task_submit_wrapper(PyObject *self, PyObject *args)
  313. {
  314. /*get the running Event loop*/
  315. PyObject *loop = PyObject_CallMethod(asyncio_module, "get_running_loop", NULL);
  316. /*create a asyncio.Future object*/
  317. PyObject *fut = PyObject_CallMethod(loop, "create_future", NULL);
  318. /*first argument in args is always the python function passed in*/
  319. PyObject *func_py = PyTuple_GetItem(args, 0);
  320. Py_INCREF(fut);
  321. Py_INCREF(loop);
  322. Py_INCREF(func_py);
  323. /*allocate a task structure and initialize it with default values*/
  324. struct starpu_task *task=starpu_task_create();
  325. task->destroy=0;
  326. PyObject *PyTask=PyTask_FromTask(task);
  327. /*set one of fut attribute to the task pointer*/
  328. PyObject_SetAttrString(fut, "starpu_task", PyTask);
  329. /*check the arguments of python function passed in*/
  330. int i;
  331. for(i=1; i < PyTuple_Size(args)-1; i++)
  332. {
  333. PyObject *obj=PyTuple_GetItem(args, i);
  334. const char* tp = Py_TYPE(obj)->tp_name;
  335. if(strcmp(tp, "_asyncio.Future") == 0)
  336. {
  337. /*if one of arguments is Future, get its corresponding task*/
  338. PyObject *fut_task=PyObject_GetAttrString(obj, "starpu_task");
  339. /*declare task dependencies between the current task and the corresponding task of Future argument*/
  340. starpu_task_declare_deps(task, 1, PyTask_AsTask(fut_task));
  341. Py_DECREF(fut_task);
  342. }
  343. }
  344. /*allocate a codelet structure*/
  345. struct starpu_codelet *func_cl=(struct starpu_codelet*)malloc(sizeof(struct starpu_codelet));
  346. /*initialize func_cl with default values*/
  347. starpu_codelet_init(func_cl);
  348. func_cl->cpu_funcs[0]=&starpupy_codelet_func;
  349. func_cl->cpu_funcs_name[0]="starpupy_codelet_func";
  350. /*check whether the option perfmodel is None*/
  351. PyObject *dict_option = PyTuple_GetItem(args, PyTuple_Size(args)-1);/*the last argument is the option dictionary*/
  352. PyObject *perfmodel = PyDict_GetItemString(dict_option, "perfmodel");
  353. const char *tp_perf = Py_TYPE(perfmodel)->tp_name;
  354. if (strcmp(tp_perf, "PyCapsule")==0)
  355. {
  356. /*PyObject*->struct perfmodel**/
  357. struct starpu_perfmodel *perf=PyCapsule_GetPointer(perfmodel, "Perf");
  358. func_cl->model=perf;
  359. Py_INCREF(perfmodel);
  360. }
  361. /*allocate a new codelet structure to pass the python function, asyncio.Future and Event loop*/
  362. //struct codelet_args *cst = (struct codelet_args*)malloc(sizeof(struct codelet_args));
  363. //cst->f = func_py;
  364. //cst->fut = fut;
  365. //cst->lp = loop;
  366. /*Initialize struct starpu_codelet_pack_arg_data*/
  367. struct starpu_codelet_pack_arg_data data;
  368. starpu_codelet_pack_arg_init(&data);
  369. /*argument list of python function passed in*/
  370. PyObject *argList;
  371. /*pass args in argList*/
  372. if (PyTuple_Size(args)==2)/*function no arguments*/
  373. argList = PyTuple_New(0);
  374. else
  375. {/*function has arguments*/
  376. argList = PyTuple_New(PyTuple_Size(args)-2);
  377. int i;
  378. for(i=0; i < PyTuple_Size(args)-2; i++)
  379. {
  380. PyObject *tmp=PyTuple_GetItem(args, i+1);
  381. PyTuple_SetItem(argList, i, tmp);
  382. Py_INCREF(PyTuple_GetItem(argList, i));
  383. }
  384. }
  385. /*use cloudpickle to dump func_py and argList*/
  386. Py_ssize_t func_data_size;
  387. char* func_data = starpu_cloudpickle_dumps(func_py, &func_data_size);
  388. starpu_codelet_pack_arg(&data, &func_data_size, sizeof(func_data_size));
  389. starpu_codelet_pack_arg(&data, func_data, func_data_size);
  390. //starpu_codelet_pack_arg(&data, &func_py, sizeof(func_py));
  391. Py_ssize_t arg_data_size;
  392. char* arg_data = starpu_cloudpickle_dumps(argList, &arg_data_size);
  393. starpu_codelet_pack_arg(&data, &arg_data_size, sizeof(arg_data_size));
  394. starpu_codelet_pack_arg(&data, arg_data, arg_data_size);
  395. //starpu_codelet_pack_arg(&data, &argList, sizeof(argList));
  396. starpu_codelet_pack_arg(&data, &fut, sizeof(fut));
  397. starpu_codelet_pack_arg(&data, &loop, sizeof(loop));
  398. task->cl=func_cl;
  399. //task->cl_arg=cst;
  400. /*pass optional values name=None, synchronous=1, priority=0, color=None, flops=None, perfmodel=None, sizebase=0*/
  401. /*const char * name*/
  402. PyObject *PyName = PyDict_GetItemString(dict_option, "name");
  403. if (PyName!=Py_None)
  404. {
  405. char* name_str = PyUnicode_AsUTF8(PyName);
  406. char* name = strdup(name_str);
  407. //printf("name is %s\n", name);
  408. task->name=name;
  409. }
  410. /*unsigned synchronous:1*/
  411. PyObject *PySync = PyDict_GetItemString(dict_option, "synchronous");
  412. unsigned sync=PyLong_AsUnsignedLong(PySync);
  413. //printf("sync is %u\n", sync);
  414. task->synchronous=sync;
  415. /*int priority*/
  416. PyObject *PyPrio = PyDict_GetItemString(dict_option, "priority");
  417. int prio=PyLong_AsLong(PyPrio);
  418. //printf("prio is %d\n", prio);
  419. task->priority=prio;
  420. /*unsigned color*/
  421. PyObject *PyColor = PyDict_GetItemString(dict_option, "color");
  422. if (PyColor!=Py_None)
  423. {
  424. unsigned color=PyLong_AsUnsignedLong(PyColor);
  425. //printf("color is %u\n", color);
  426. task->color=color;
  427. }
  428. /*double flops*/
  429. PyObject *PyFlops = PyDict_GetItemString(dict_option, "flops");
  430. if (PyFlops!=Py_None)
  431. {
  432. double flops=PyFloat_AsDouble(PyFlops);
  433. //printf("flops is %f\n", flops);
  434. task->flops=flops;
  435. }
  436. /*int sizebase*/
  437. PyObject *PySB = PyDict_GetItemString(dict_option, "sizebase");
  438. int sb=PyLong_AsLong(PySB);
  439. //printf("pack sizebase is %d\n", sb);
  440. starpu_codelet_pack_arg(&data, &sb, sizeof(sb));
  441. starpu_codelet_pack_arg_fini(&data, &task->cl_arg, &task->cl_arg_size);
  442. task->callback_func=&cb_func;
  443. /*call starpu_task_submit method*/
  444. Py_BEGIN_ALLOW_THREADS
  445. int ret = starpu_task_submit(task);
  446. assert(ret==0);
  447. Py_END_ALLOW_THREADS
  448. if (strcmp(tp_perf, "PyCapsule")==0)
  449. {
  450. struct starpu_perfmodel *perf =(struct starpu_perfmodel *) func_cl->model;
  451. perf->size_base=&sizebase;
  452. }
  453. //printf("the number of reference is %ld\n", Py_REFCNT(func_py));
  454. //_Py_PrintReferences(stderr);
  455. //COUNTREFS();
  456. return fut;
  457. }
  458. /*wrapper wait for all method*/
  459. static PyObject* starpu_task_wait_for_all_wrapper(PyObject *self, PyObject *args)
  460. {
  461. /*call starpu_task_wait_for_all method*/
  462. Py_BEGIN_ALLOW_THREADS
  463. starpu_task_wait_for_all();
  464. Py_END_ALLOW_THREADS
  465. /*return type is void*/
  466. Py_INCREF(Py_None);
  467. return Py_None;
  468. }
  469. /*wrapper pause method*/
  470. static PyObject* starpu_pause_wrapper(PyObject *self, PyObject *args)
  471. {
  472. /*call starpu_pause method*/
  473. starpu_pause();
  474. /*return type is void*/
  475. Py_INCREF(Py_None);
  476. return Py_None;
  477. }
  478. /*wrapper resume method*/
  479. static PyObject* starpu_resume_wrapper(PyObject *self, PyObject *args)
  480. {
  481. /*call starpu_resume method*/
  482. starpu_resume();
  483. /*return type is void*/
  484. Py_INCREF(Py_None);
  485. return Py_None;
  486. }
  487. /*wrapper get count cpu method*/
  488. static PyObject* starpu_cpu_worker_get_count_wrapper(PyObject *self, PyObject *args)
  489. {
  490. /*call starpu_cpu_worker_get_count method*/
  491. int num_cpu=starpu_cpu_worker_get_count();
  492. /*return type is unsigned*/
  493. return Py_BuildValue("I", num_cpu);
  494. }
  495. /*wrapper get min priority method*/
  496. static PyObject* starpu_sched_get_min_priority_wrapper(PyObject *self, PyObject *args)
  497. {
  498. /*call starpu_sched_get_min_priority*/
  499. int min_prio=starpu_sched_get_min_priority();
  500. /*return type is int*/
  501. return Py_BuildValue("i", min_prio);
  502. }
  503. /*wrapper get max priority method*/
  504. static PyObject* starpu_sched_get_max_priority_wrapper(PyObject *self, PyObject *args)
  505. {
  506. /*call starpu_sched_get_max_priority*/
  507. int max_prio=starpu_sched_get_max_priority();
  508. /*return type is int*/
  509. return Py_BuildValue("i", max_prio);
  510. }
  511. /*wrapper get the number of no completed submitted tasks method*/
  512. static PyObject* starpu_task_nsubmitted_wrapper(PyObject *self, PyObject *args)
  513. {
  514. /*call starpu_task_nsubmitted*/
  515. int num_task=starpu_task_nsubmitted();
  516. /*Return the number of submitted tasks which have not completed yet */
  517. return Py_BuildValue("i", num_task);
  518. }
  519. /***********************************************************************************/
  520. /***************The module’s method table and initialization function**************/
  521. /*method table*/
  522. static PyMethodDef starpupyMethods[] =
  523. {
  524. {"_task_submit", starpu_task_submit_wrapper, METH_VARARGS, "submit the task"}, /*submit method*/
  525. {"task_wait_for_all", starpu_task_wait_for_all_wrapper, METH_VARARGS, "wait the task"}, /*wait for all method*/
  526. {"pause", starpu_pause_wrapper, METH_VARARGS, "suspend the processing of new tasks by workers"}, /*pause method*/
  527. {"resume", starpu_resume_wrapper, METH_VARARGS, "resume the workers polling for new tasks"}, /*resume method*/
  528. {"cpu_worker_get_count", starpu_cpu_worker_get_count_wrapper, METH_VARARGS, "return the number of CPUs controlled by StarPU"}, /*get count cpu method*/
  529. {"init_perfmodel", init_perfmodel, METH_VARARGS, "initialize struct starpu_perfmodel"}, /*initialize perfmodel*/
  530. {"free_perfmodel", free_perfmodel, METH_VARARGS, "free struct starpu_perfmodel"}, /*free perfmodel*/
  531. {"save_history_based_model", starpu_save_history_based_model_wrapper, METH_VARARGS, "save the performance model"}, /*save the performance model*/
  532. {"sched_get_min_priority", starpu_sched_get_min_priority_wrapper, METH_VARARGS, "get the number of min priority"}, /*get the number of min priority*/
  533. {"sched_get_max_priority", starpu_sched_get_max_priority_wrapper, METH_VARARGS, "get the number of max priority"}, /*get the number of max priority*/
  534. {"task_nsubmitted", starpu_task_nsubmitted_wrapper, METH_VARARGS, "get the number of submitted tasks which have not completed yet"}, /*get the number of submitted tasks which have not completed yet*/
  535. {NULL, NULL}
  536. };
  537. /*deallocation function*/
  538. static void starpupyFree(void *self)
  539. {
  540. starpu_shutdown();
  541. Py_DECREF(asyncio_module);
  542. //COUNTREFS();
  543. }
  544. /*module definition structure*/
  545. static struct PyModuleDef starpupymodule =
  546. {
  547. PyModuleDef_HEAD_INIT,
  548. "starpupy", /*name of module*/
  549. NULL,
  550. -1,
  551. starpupyMethods, /*method table*/
  552. NULL,
  553. NULL,
  554. NULL,
  555. starpupyFree /*deallocation function*/
  556. };
  557. /*initialization function*/
  558. PyMODINIT_FUNC
  559. PyInit_starpupy(void)
  560. {
  561. PyEval_InitThreads();
  562. /*starpu initialization*/
  563. int ret = starpu_init(NULL);
  564. assert(ret==0);
  565. /*python asysncio import*/
  566. asyncio_module = PyImport_ImportModule("asyncio");
  567. /*cloudpickle import*/
  568. cloudpickle_module = PyImport_ImportModule("cloudpickle");
  569. #ifdef STARPU_PYTHON_HAVE_NUMPY
  570. /*numpy import array*/
  571. import_array();
  572. #endif
  573. /*module import initialization*/
  574. return PyModule_Create(&starpupymodule);
  575. }
  576. /***********************************************************************************/