starpu_task_wrapper.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2020-2021 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. /*********************Functions passed in task_submit wrapper***********************/
  27. static PyObject *StarpupyError; /*starpupy error exception*/
  28. static PyObject *asyncio_module; /*python asyncio module*/
  29. static char* starpu_cloudpickle_dumps(PyObject *obj, PyObject **obj_bytes, Py_ssize_t *obj_data_size)
  30. {
  31. PyObject *cloudpickle_module = PyImport_ImportModule("cloudpickle");
  32. if (cloudpickle_module == NULL)
  33. {
  34. printf("can't find cloudpickle module\n");
  35. exit(1);
  36. }
  37. PyObject *dumps = PyObject_GetAttrString(cloudpickle_module, "dumps");
  38. *obj_bytes= PyObject_CallFunctionObjArgs(dumps, obj, NULL);
  39. char* obj_data;
  40. PyBytes_AsStringAndSize(*obj_bytes, &obj_data, obj_data_size);
  41. return obj_data;
  42. }
  43. static PyObject* starpu_cloudpickle_loads(char* pyString, Py_ssize_t pyString_size)
  44. {
  45. PyObject *pickle_module = PyImport_ImportModule("pickle");
  46. if (pickle_module == NULL)
  47. {
  48. printf("can't find pickle module\n");
  49. exit(1);
  50. }
  51. PyObject *loads = PyObject_GetAttrString(pickle_module, "loads");
  52. PyObject *obj_bytes_str = PyBytes_FromStringAndSize(pyString, pyString_size);
  53. PyObject *obj = PyObject_CallFunctionObjArgs(loads, obj_bytes_str, NULL);
  54. Py_DECREF(obj_bytes_str);
  55. return obj;
  56. }
  57. /* prologue_callback_func*/
  58. void prologue_cb_func(void *cl_arg)
  59. {
  60. PyObject *func_data;
  61. size_t func_data_size;
  62. PyObject *argList;
  63. PyObject *fut;
  64. PyObject *loop;
  65. int sb;
  66. /*make sure we own the GIL*/
  67. PyGILState_STATE state = PyGILState_Ensure();
  68. struct starpu_task *task = starpu_task_get_current();
  69. /*Initialize struct starpu_codelet_unpack_arg_data*/
  70. struct starpu_codelet_pack_arg_data data_org;
  71. starpu_codelet_unpack_arg_init(&data_org, &task->cl_arg, &task->cl_arg_size);
  72. /*get func_py char**/
  73. starpu_codelet_pick_arg(&data_org, (void**)&func_data, &func_data_size);
  74. /*get argList*/
  75. starpu_codelet_unpack_arg(&data_org, &argList, sizeof(argList));
  76. /*get fut*/
  77. starpu_codelet_unpack_arg(&data_org, &fut, sizeof(fut));
  78. /*get loop*/
  79. starpu_codelet_unpack_arg(&data_org, &loop, sizeof(loop));
  80. /*get sb*/
  81. starpu_codelet_unpack_arg(&data_org, &sb, sizeof(sb));
  82. /*Repack the data*/
  83. /*Initialize struct starpu_codelet_pack_arg_data*/
  84. struct starpu_codelet_pack_arg_data data;
  85. starpu_codelet_pack_arg_init(&data);
  86. /*repack func_data*/
  87. starpu_codelet_pack_arg(&data, func_data, func_data_size);
  88. /*check if there is Future in argList, if so, get the Future result*/
  89. int i;
  90. for(i=0; i < PyTuple_Size(argList); i++)
  91. {
  92. PyObject *obj=PyTuple_GetItem(argList, i);
  93. const char* tp = Py_TYPE(obj)->tp_name;
  94. if(strcmp(tp, "_asyncio.Future") == 0)
  95. {
  96. /*if one of arguments is Future, get its result*/
  97. PyObject *fut_result = PyObject_CallMethod(obj, "result", NULL);
  98. /*replace the Future argument to its result*/
  99. PyTuple_SetItem(argList, i, fut_result);
  100. }
  101. }
  102. /*use cloudpickle to dump dumps argList*/
  103. Py_ssize_t arg_data_size;
  104. PyObject *arg_bytes;
  105. char* arg_data = starpu_cloudpickle_dumps(argList, &arg_bytes, &arg_data_size);
  106. starpu_codelet_pack_arg(&data, arg_data, arg_data_size);
  107. Py_DECREF(arg_bytes);
  108. /*repack fut*/
  109. starpu_codelet_pack_arg(&data, &fut, sizeof(fut));
  110. /*repack loop*/
  111. starpu_codelet_pack_arg(&data, &loop, sizeof(loop));
  112. /*repack sb*/
  113. starpu_codelet_pack_arg(&data, &sb, sizeof(sb));
  114. /*finish repacking data and store the struct in cl_arg*/
  115. starpu_codelet_pack_arg_fini(&data, &task->cl_arg, &task->cl_arg_size);
  116. /*restore previous GIL state*/
  117. PyGILState_Release(state);
  118. }
  119. /*function passed to starpu_codelet.cpu_func*/
  120. void starpupy_codelet_func(void *buffers[], void *cl_arg)
  121. {
  122. char* func_data;
  123. size_t func_data_size;
  124. PyObject *func_py; /*the python function passed in*/
  125. char* arg_data;
  126. size_t arg_data_size;
  127. PyObject *argList; /*argument list of python function passed in*/
  128. /*make sure we own the GIL*/
  129. PyGILState_STATE state = PyGILState_Ensure();
  130. //struct codelet_args *cst = (struct codelet_args*) cl_arg;
  131. struct starpu_task *task = starpu_task_get_current();
  132. /*Initialize struct starpu_codelet_unpack_arg_data*/
  133. struct starpu_codelet_pack_arg_data data;
  134. starpu_codelet_unpack_arg_init(&data, &task->cl_arg, &task->cl_arg_size);
  135. /*get func_py char**/
  136. starpu_codelet_pick_arg(&data, (void**)&func_data, &func_data_size);
  137. /*get argList char**/
  138. starpu_codelet_pick_arg(&data, (void**)&arg_data, &arg_data_size);
  139. /*skip fut*/
  140. starpu_codelet_unpack_discard_arg(&data);
  141. /*skip loop*/
  142. starpu_codelet_unpack_discard_arg(&data);
  143. /*skip sb*/
  144. starpu_codelet_unpack_discard_arg(&data);
  145. /*use cloudpickle to load function (maybe only function name)*/
  146. PyObject *pFunc=starpu_cloudpickle_loads(func_data, func_data_size);
  147. /* if the function name is passed in*/
  148. const char* tp_func = Py_TYPE(pFunc)->tp_name;
  149. if (strcmp(tp_func, "str")==0)
  150. {
  151. /*getattr(sys.modules[__name__], "<functionname>")*/
  152. /*get sys.modules*/
  153. PyObject *sys_modules = PyImport_GetModuleDict();
  154. /*get sys.modules[__name__]*/
  155. PyObject *sys_modules_name=PyDict_GetItemString(sys_modules,"__main__");
  156. /*get function object*/
  157. func_py=PyObject_GetAttr(sys_modules_name,pFunc);
  158. }
  159. else
  160. {
  161. func_py=pFunc;
  162. }
  163. /*use cloudpickle to load argList*/
  164. argList=starpu_cloudpickle_loads(arg_data, arg_data_size);
  165. /*verify that the function is a proper callable*/
  166. if (!PyCallable_Check(func_py))
  167. {
  168. printf("py_callback: expected a callable function\n");
  169. exit(1);
  170. }
  171. /*call the python function get the return value rv*/
  172. PyObject *rv = PyObject_CallObject(func_py, argList);
  173. /*Initialize struct starpu_codelet_pack_arg_data for return value*/
  174. struct starpu_codelet_pack_arg_data data_ret;
  175. starpu_codelet_pack_arg_init(&data_ret);
  176. /*if the result is None type, pack NULL without using cloudpickle*/
  177. if (rv==Py_None)
  178. {
  179. char* rv_data=NULL;
  180. Py_ssize_t rv_data_size=0;
  181. starpu_codelet_pack_arg(&data_ret, &rv_data_size, sizeof(rv_data_size));
  182. starpu_codelet_pack_arg(&data_ret, &rv_data, sizeof(rv_data));
  183. }
  184. /*else use cloudpickle to dump rv*/
  185. else
  186. {
  187. Py_ssize_t rv_data_size;
  188. PyObject *rv_bytes;
  189. char* rv_data = starpu_cloudpickle_dumps(rv, &rv_bytes, &rv_data_size);
  190. starpu_codelet_pack_arg(&data_ret, &rv_data_size, sizeof(rv_data_size));
  191. starpu_codelet_pack_arg(&data_ret, rv_data, rv_data_size);
  192. Py_DECREF(rv_bytes);
  193. }
  194. /*store the return value in task_>cl_ret*/
  195. starpu_codelet_pack_arg_fini(&data_ret, &task->cl_ret, &task->cl_ret_size);
  196. Py_DECREF(func_py);
  197. Py_DECREF(argList);
  198. /*restore previous GIL state*/
  199. PyGILState_Release(state);
  200. }
  201. /*function passed to starpu_task.callback_func*/
  202. void cb_func(void *v)
  203. {
  204. PyObject *fut; /*asyncio.Future*/
  205. PyObject *loop; /*asyncio.Eventloop*/
  206. char* rv_data;
  207. size_t rv_data_size;
  208. PyObject *rv; /*return value when using PyObject_CallObject call the function f*/
  209. /*make sure we own the GIL*/
  210. PyGILState_STATE state = PyGILState_Ensure();
  211. struct starpu_task *task = starpu_task_get_current();
  212. /*Initialize struct starpu_codelet_unpack_arg_data data*/
  213. struct starpu_codelet_pack_arg_data data;
  214. starpu_codelet_unpack_arg_init(&data, &task->cl_arg, &task->cl_arg_size);
  215. /*skip func_py*/
  216. starpu_codelet_unpack_discard_arg(&data);
  217. /*skip argList*/
  218. starpu_codelet_unpack_discard_arg(&data);
  219. /*get fut*/
  220. starpu_codelet_unpack_arg(&data, &fut, sizeof(fut));
  221. /*get loop*/
  222. starpu_codelet_unpack_arg(&data, &loop, sizeof(loop));
  223. /*skip sb*/
  224. starpu_codelet_unpack_discard_arg(&data);
  225. /*Initialize struct starpu_codelet_unpack_arg_data data*/
  226. struct starpu_codelet_pack_arg_data data_ret;
  227. starpu_codelet_unpack_arg_init(&data_ret, &task->cl_ret, &task->cl_ret_size);
  228. /*get rv_data_size*/
  229. starpu_codelet_unpack_arg(&data_ret, &rv_data_size, sizeof(rv_data_size));
  230. /*if the rv_data_size is 0, the result is None type*/
  231. if (rv_data_size==0)
  232. {
  233. starpu_codelet_unpack_discard_arg(&data_ret);
  234. rv=Py_None;
  235. }
  236. /*else use cloudpickle to load rv*/
  237. else
  238. {
  239. starpu_codelet_pick_arg(&data_ret, (void**)&rv_data, &rv_data_size);
  240. rv=starpu_cloudpickle_loads(rv_data, rv_data_size);
  241. }
  242. /*set the Future result and mark the Future as done*/
  243. PyObject *set_result = PyObject_GetAttrString(fut, "set_result");
  244. PyObject *loop_callback = PyObject_CallMethod(loop, "call_soon_threadsafe", "(O,O)", set_result, rv);
  245. Py_DECREF(loop_callback);
  246. Py_DECREF(set_result);
  247. Py_DECREF(rv);
  248. Py_DECREF(fut);
  249. Py_DECREF(loop);
  250. struct starpu_codelet *func_cl=(struct starpu_codelet *) task->cl;
  251. if (func_cl->model != NULL)
  252. {
  253. struct starpu_perfmodel *perf =(struct starpu_perfmodel *) func_cl->model;
  254. PyObject *perfmodel=PyCapsule_New(perf, "Perf", 0);
  255. Py_DECREF(perfmodel);
  256. }
  257. /*restore previous GIL state*/
  258. PyGILState_Release(state);
  259. /*deallocate task*/
  260. free(task->cl);
  261. free(task->cl_arg);
  262. }
  263. /***********************************************************************************/
  264. /*PyObject*->struct starpu_task**/
  265. static struct starpu_task *PyTask_AsTask(PyObject *obj)
  266. {
  267. return (struct starpu_task *) PyCapsule_GetPointer(obj, "Task");
  268. }
  269. /* destructor function for task */
  270. static void del_Task(PyObject *obj)
  271. {
  272. struct starpu_task *obj_task=PyTask_AsTask(obj);
  273. obj_task->destroy=1; /*XXX we should call starpu task destroy*/
  274. }
  275. /*struct starpu_task*->PyObject**/
  276. static PyObject *PyTask_FromTask(struct starpu_task *task)
  277. {
  278. return PyCapsule_New(task, "Task", del_Task);
  279. }
  280. /***********************************************************************************/
  281. static size_t sizebase (struct starpu_task *task, unsigned nimpl)
  282. {
  283. int sb;
  284. /*Initialize struct starpu_codelet_unpack_arg_data*/
  285. struct starpu_codelet_pack_arg_data data;
  286. starpu_codelet_unpack_arg_init(&data, &task->cl_arg, &task->cl_arg_size);
  287. /*skip func_py*/
  288. //starpu_codelet_unpack_discard_arg(&data);
  289. starpu_codelet_unpack_discard_arg(&data);
  290. /*skip argList*/
  291. //starpu_codelet_unpack_discard_arg(&data);
  292. starpu_codelet_unpack_discard_arg(&data);
  293. /*skip fut*/
  294. starpu_codelet_unpack_discard_arg(&data);
  295. /*skip loop*/
  296. starpu_codelet_unpack_discard_arg(&data);
  297. /*get sb*/
  298. starpu_codelet_unpack_arg(&data, &sb, sizeof(sb));
  299. return sb;
  300. }
  301. /*initialization of perfmodel*/
  302. static PyObject* init_perfmodel(PyObject *self, PyObject *args)
  303. {
  304. char *sym;
  305. if (!PyArg_ParseTuple(args, "s", &sym))
  306. return NULL;
  307. /*allocate a perfmodel structure*/
  308. struct starpu_perfmodel *perf=(struct starpu_perfmodel*)calloc(1, sizeof(struct starpu_perfmodel));
  309. /*get the perfmodel symbol*/
  310. char *p =strdup(sym);
  311. perf->symbol=p;
  312. perf->type=STARPU_HISTORY_BASED;
  313. /*struct perfmodel*->PyObject**/
  314. PyObject *perfmodel=PyCapsule_New(perf, "Perf", NULL);
  315. return perfmodel;
  316. }
  317. /*free perfmodel*/
  318. static PyObject* free_perfmodel(PyObject *self, PyObject *args)
  319. {
  320. PyObject *perfmodel;
  321. if (!PyArg_ParseTuple(args, "O", &perfmodel))
  322. return NULL;
  323. /*PyObject*->struct perfmodel**/
  324. struct starpu_perfmodel *perf=PyCapsule_GetPointer(perfmodel, "Perf");
  325. starpu_save_history_based_model(perf);
  326. //starpu_perfmodel_unload_model(perf);
  327. //free(perf->symbol);
  328. starpu_perfmodel_deinit(perf);
  329. free(perf);
  330. /*return type is void*/
  331. Py_INCREF(Py_None);
  332. return Py_None;
  333. }
  334. static PyObject* starpu_save_history_based_model_wrapper(PyObject *self, PyObject *args)
  335. {
  336. PyObject *perfmodel;
  337. if (!PyArg_ParseTuple(args, "O", &perfmodel))
  338. return NULL;
  339. /*PyObject*->struct perfmodel**/
  340. struct starpu_perfmodel *perf=PyCapsule_GetPointer(perfmodel, "Perf");
  341. starpu_save_history_based_model(perf);
  342. /*return type is void*/
  343. Py_INCREF(Py_None);
  344. return Py_None;
  345. }
  346. /*****************************Wrappers of StarPU methods****************************/
  347. /*wrapper submit method*/
  348. static PyObject* starpu_task_submit_wrapper(PyObject *self, PyObject *args)
  349. {
  350. /*get the running Event loop*/
  351. PyObject *loop = PyObject_CallMethod(asyncio_module, "get_running_loop", NULL);
  352. /*create a asyncio.Future object*/
  353. PyObject *fut = PyObject_CallMethod(loop, "create_future", NULL);
  354. /*first argument in args is always the python function passed in*/
  355. PyObject *func_py = PyTuple_GetItem(args, 0);
  356. Py_INCREF(fut);
  357. Py_INCREF(loop);
  358. Py_INCREF(func_py);
  359. /*allocate a task structure and initialize it with default values*/
  360. struct starpu_task *task=starpu_task_create();
  361. task->destroy=0;
  362. PyObject *PyTask=PyTask_FromTask(task);
  363. /*set one of fut attribute to the task pointer*/
  364. PyObject_SetAttrString(fut, "starpu_task", PyTask);
  365. /*check the arguments of python function passed in*/
  366. int i;
  367. for(i=1; i < PyTuple_Size(args)-1; i++)
  368. {
  369. PyObject *obj=PyTuple_GetItem(args, i);
  370. const char* tp = Py_TYPE(obj)->tp_name;
  371. if(strcmp(tp, "_asyncio.Future") == 0)
  372. {
  373. /*if one of arguments is Future, get its corresponding task*/
  374. PyObject *fut_task=PyObject_GetAttrString(obj, "starpu_task");
  375. /*declare task dependencies between the current task and the corresponding task of Future argument*/
  376. starpu_task_declare_deps(task, 1, PyTask_AsTask(fut_task));
  377. Py_DECREF(fut_task);
  378. }
  379. }
  380. /*allocate a codelet structure*/
  381. struct starpu_codelet *func_cl=(struct starpu_codelet*)malloc(sizeof(struct starpu_codelet));
  382. /*initialize func_cl with default values*/
  383. starpu_codelet_init(func_cl);
  384. func_cl->cpu_funcs[0]=&starpupy_codelet_func;
  385. func_cl->cpu_funcs_name[0]="starpupy_codelet_func";
  386. /*check whether the option perfmodel is None*/
  387. PyObject *dict_option = PyTuple_GetItem(args, PyTuple_Size(args)-1);/*the last argument is the option dictionary*/
  388. PyObject *perfmodel = PyDict_GetItemString(dict_option, "perfmodel");
  389. const char *tp_perf = Py_TYPE(perfmodel)->tp_name;
  390. if (strcmp(tp_perf, "PyCapsule")==0)
  391. {
  392. /*PyObject*->struct perfmodel**/
  393. struct starpu_perfmodel *perf=PyCapsule_GetPointer(perfmodel, "Perf");
  394. func_cl->model=perf;
  395. Py_INCREF(perfmodel);
  396. }
  397. /*Initialize struct starpu_codelet_pack_arg_data*/
  398. struct starpu_codelet_pack_arg_data data;
  399. starpu_codelet_pack_arg_init(&data);
  400. /*argument list of python function passed in*/
  401. PyObject *argList;
  402. /*pass args in argList*/
  403. if (PyTuple_Size(args)==2)/*function no arguments*/
  404. argList = PyTuple_New(0);
  405. else
  406. {
  407. /*function has arguments*/
  408. argList = PyTuple_New(PyTuple_Size(args)-2);
  409. int i;
  410. for(i=0; i < PyTuple_Size(args)-2; i++)
  411. {
  412. PyObject *tmp=PyTuple_GetItem(args, i+1);
  413. PyTuple_SetItem(argList, i, tmp);
  414. Py_INCREF(PyTuple_GetItem(argList, i));
  415. }
  416. }
  417. /*use cloudpickle to dump func_py*/
  418. Py_ssize_t func_data_size;
  419. PyObject *func_bytes;
  420. char* func_data = starpu_cloudpickle_dumps(func_py, &func_bytes, &func_data_size);
  421. starpu_codelet_pack_arg(&data, func_data, func_data_size);
  422. Py_DECREF(func_bytes);
  423. /*pack argList*/
  424. starpu_codelet_pack_arg(&data, &argList, sizeof(argList));
  425. /*pack fut*/
  426. starpu_codelet_pack_arg(&data, &fut, sizeof(fut));
  427. /*pack loop*/
  428. starpu_codelet_pack_arg(&data, &loop, sizeof(loop));
  429. task->cl=func_cl;
  430. /*pass optional values name=None, synchronous=1, priority=0, color=None, flops=None, perfmodel=None, sizebase=0*/
  431. /*const char * name*/
  432. PyObject *PyName = PyDict_GetItemString(dict_option, "name");
  433. if (PyName!=Py_None)
  434. {
  435. const char* name_str = PyUnicode_AsUTF8(PyName);
  436. char* name = strdup(name_str);
  437. //printf("name is %s\n", name);
  438. task->name=name;
  439. }
  440. /*unsigned synchronous:1*/
  441. PyObject *PySync = PyDict_GetItemString(dict_option, "synchronous");
  442. unsigned sync=PyLong_AsUnsignedLong(PySync);
  443. //printf("sync is %u\n", sync);
  444. task->synchronous=sync;
  445. /*int priority*/
  446. PyObject *PyPrio = PyDict_GetItemString(dict_option, "priority");
  447. int prio=PyLong_AsLong(PyPrio);
  448. //printf("prio is %d\n", prio);
  449. task->priority=prio;
  450. /*unsigned color*/
  451. PyObject *PyColor = PyDict_GetItemString(dict_option, "color");
  452. if (PyColor!=Py_None)
  453. {
  454. unsigned color=PyLong_AsUnsignedLong(PyColor);
  455. //printf("color is %u\n", color);
  456. task->color=color;
  457. }
  458. /*double flops*/
  459. PyObject *PyFlops = PyDict_GetItemString(dict_option, "flops");
  460. if (PyFlops!=Py_None)
  461. {
  462. double flops=PyFloat_AsDouble(PyFlops);
  463. //printf("flops is %f\n", flops);
  464. task->flops=flops;
  465. }
  466. /*int sizebase*/
  467. PyObject *PySB = PyDict_GetItemString(dict_option, "sizebase");
  468. int sb=PyLong_AsLong(PySB);
  469. //printf("pack sizebase is %d\n", sb);
  470. /*pack sb*/
  471. starpu_codelet_pack_arg(&data, &sb, sizeof(sb));
  472. /*finish packing data and store the struct in cl_arg*/
  473. starpu_codelet_pack_arg_fini(&data, &task->cl_arg, &task->cl_arg_size);
  474. task->prologue_callback_func=&prologue_cb_func;
  475. task->callback_func=&cb_func;
  476. /*call starpu_task_submit method*/
  477. int ret;
  478. Py_BEGIN_ALLOW_THREADS
  479. ret = starpu_task_submit(task);
  480. Py_END_ALLOW_THREADS
  481. if (ret!=0)
  482. {
  483. PyErr_Format(StarpupyError, "Unexpected value %d returned for starpu_task_submit", ret);
  484. return NULL;
  485. }
  486. if (strcmp(tp_perf, "PyCapsule")==0)
  487. {
  488. struct starpu_perfmodel *perf =(struct starpu_perfmodel *) func_cl->model;
  489. perf->size_base=&sizebase;
  490. }
  491. //printf("the number of reference is %ld\n", Py_REFCNT(func_py));
  492. //_Py_PrintReferences(stderr);
  493. //COUNTREFS();
  494. return fut;
  495. }
  496. /*wrapper wait for all method*/
  497. static PyObject* starpu_task_wait_for_all_wrapper(PyObject *self, PyObject *args)
  498. {
  499. /*call starpu_task_wait_for_all method*/
  500. Py_BEGIN_ALLOW_THREADS
  501. starpu_task_wait_for_all();
  502. Py_END_ALLOW_THREADS
  503. /*return type is void*/
  504. Py_INCREF(Py_None);
  505. return Py_None;
  506. }
  507. /*wrapper pause method*/
  508. static PyObject* starpu_pause_wrapper(PyObject *self, PyObject *args)
  509. {
  510. /*call starpu_pause method*/
  511. starpu_pause();
  512. /*return type is void*/
  513. Py_INCREF(Py_None);
  514. return Py_None;
  515. }
  516. /*wrapper resume method*/
  517. static PyObject* starpu_resume_wrapper(PyObject *self, PyObject *args)
  518. {
  519. /*call starpu_resume method*/
  520. starpu_resume();
  521. /*return type is void*/
  522. Py_INCREF(Py_None);
  523. return Py_None;
  524. }
  525. /*wrapper get count cpu method*/
  526. static PyObject* starpu_cpu_worker_get_count_wrapper(PyObject *self, PyObject *args)
  527. {
  528. /*call starpu_cpu_worker_get_count method*/
  529. int num_cpu=starpu_cpu_worker_get_count();
  530. /*return type is unsigned*/
  531. return Py_BuildValue("I", num_cpu);
  532. }
  533. /*wrapper get min priority method*/
  534. static PyObject* starpu_sched_get_min_priority_wrapper(PyObject *self, PyObject *args)
  535. {
  536. /*call starpu_sched_get_min_priority*/
  537. int min_prio=starpu_sched_get_min_priority();
  538. /*return type is int*/
  539. return Py_BuildValue("i", min_prio);
  540. }
  541. /*wrapper get max priority method*/
  542. static PyObject* starpu_sched_get_max_priority_wrapper(PyObject *self, PyObject *args)
  543. {
  544. /*call starpu_sched_get_max_priority*/
  545. int max_prio=starpu_sched_get_max_priority();
  546. /*return type is int*/
  547. return Py_BuildValue("i", max_prio);
  548. }
  549. /*wrapper get the number of no completed submitted tasks method*/
  550. static PyObject* starpu_task_nsubmitted_wrapper(PyObject *self, PyObject *args)
  551. {
  552. /*call starpu_task_nsubmitted*/
  553. int num_task=starpu_task_nsubmitted();
  554. /*Return the number of submitted tasks which have not completed yet */
  555. return Py_BuildValue("i", num_task);
  556. }
  557. /*wrapper shutdown method*/
  558. static PyObject* starpu_shutdown_wrapper(PyObject *self, PyObject *args)
  559. {
  560. /*call starpu_shutdown method*/
  561. Py_BEGIN_ALLOW_THREADS
  562. starpu_shutdown();
  563. Py_END_ALLOW_THREADS
  564. /*return type is void*/
  565. Py_INCREF(Py_None);
  566. return Py_None;
  567. }
  568. /***********************************************************************************/
  569. /***************The module’s method table and initialization function**************/
  570. /*method table*/
  571. static PyMethodDef starpupyMethods[] =
  572. {
  573. {"_task_submit", starpu_task_submit_wrapper, METH_VARARGS, "submit the task"}, /*submit method*/
  574. {"task_wait_for_all", starpu_task_wait_for_all_wrapper, METH_VARARGS, "wait the task"}, /*wait for all method*/
  575. {"pause", starpu_pause_wrapper, METH_VARARGS, "suspend the processing of new tasks by workers"}, /*pause method*/
  576. {"resume", starpu_resume_wrapper, METH_VARARGS, "resume the workers polling for new tasks"}, /*resume method*/
  577. {"cpu_worker_get_count", starpu_cpu_worker_get_count_wrapper, METH_VARARGS, "return the number of CPUs controlled by StarPU"}, /*get count cpu method*/
  578. {"init_perfmodel", init_perfmodel, METH_VARARGS, "initialize struct starpu_perfmodel"}, /*initialize perfmodel*/
  579. {"free_perfmodel", free_perfmodel, METH_VARARGS, "free struct starpu_perfmodel"}, /*free perfmodel*/
  580. {"save_history_based_model", starpu_save_history_based_model_wrapper, METH_VARARGS, "save the performance model"}, /*save the performance model*/
  581. {"sched_get_min_priority", starpu_sched_get_min_priority_wrapper, METH_VARARGS, "get the number of min priority"}, /*get the number of min priority*/
  582. {"sched_get_max_priority", starpu_sched_get_max_priority_wrapper, METH_VARARGS, "get the number of max priority"}, /*get the number of max priority*/
  583. {"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*/
  584. {"shutdown", starpu_shutdown_wrapper, METH_VARARGS, "shutdown starpu"}, /*shutdown starpu*/
  585. {NULL, NULL}
  586. };
  587. /*deallocation function*/
  588. static void starpupyFree(void *self)
  589. {
  590. starpu_shutdown();
  591. Py_DECREF(asyncio_module);
  592. //COUNTREFS();
  593. }
  594. /*module definition structure*/
  595. static struct PyModuleDef starpupymodule =
  596. {
  597. PyModuleDef_HEAD_INIT,
  598. "starpupy", /*name of module*/
  599. NULL,
  600. -1,
  601. starpupyMethods, /*method table*/
  602. NULL,
  603. NULL,
  604. NULL,
  605. starpupyFree /*deallocation function*/
  606. };
  607. /*initialization function*/
  608. PyMODINIT_FUNC
  609. PyInit_starpupy(void)
  610. {
  611. PyObject *m;
  612. /*module import initialization*/
  613. m = PyModule_Create(&starpupymodule);
  614. if (m == NULL)
  615. return NULL;
  616. StarpupyError = PyErr_NewException("starpupy.error", NULL, NULL);
  617. Py_XINCREF(StarpupyError);
  618. if (PyModule_AddObject(m, "error", StarpupyError) < 0) {
  619. Py_XDECREF(StarpupyError);
  620. Py_CLEAR(StarpupyError);
  621. Py_DECREF(m);
  622. return NULL;
  623. }
  624. #if PY_MAJOR_VERSION < 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9)
  625. PyEval_InitThreads();
  626. #endif
  627. /*starpu initialization*/
  628. int ret;
  629. Py_BEGIN_ALLOW_THREADS
  630. ret = starpu_init(NULL);
  631. Py_END_ALLOW_THREADS
  632. if (ret!=0)
  633. {
  634. PyErr_Format(StarpupyError, "Unexpected value %d returned for starpu_init", ret);
  635. return NULL;
  636. }
  637. /*python asysncio import*/
  638. asyncio_module = PyImport_ImportModule("asyncio");
  639. #ifdef STARPU_PYTHON_HAVE_NUMPY
  640. /*numpy import array*/
  641. import_array();
  642. #endif
  643. return m;
  644. }
  645. /***********************************************************************************/