starpu_task_wrapper.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 library*/
  40. /*structure contains parameters which are passed to starpu_task.cl_arg*/
  41. struct codelet_args
  42. {
  43. PyObject *f; /*the python function passed in*/
  44. PyObject *argList; /*argument list of python function passed in*/
  45. PyObject *rv; /*return value when using PyObject_CallObject call the function f*/
  46. PyObject *fut; /*asyncio.Future*/
  47. PyObject *lp; /*asyncio.Eventloop*/
  48. };
  49. /*function passed to starpu_codelet.cpu_func*/
  50. void codelet_func(void *buffers[], void *cl_arg)
  51. {
  52. struct codelet_args *cst = (struct codelet_args*) cl_arg;
  53. /*make sure we own the GIL*/
  54. PyGILState_STATE state = PyGILState_Ensure();
  55. /*verify that the function is a proper callable*/
  56. if (!PyCallable_Check(cst->f))
  57. {
  58. printf("py_callback: expected a callable function\n");
  59. exit(1);
  60. }
  61. /*check the arguments of python function passed in*/
  62. int i;
  63. for(i=0; i < PyTuple_Size(cst->argList); i++)
  64. {
  65. PyObject *obj = PyTuple_GetItem(cst->argList, i);
  66. const char *tp = Py_TYPE(obj)->tp_name;
  67. if(strcmp(tp, "_asyncio.Future") == 0)
  68. {
  69. /*if one of arguments is Future, get its result*/
  70. PyObject *fut_result = PyObject_CallMethod(obj, "result", NULL);
  71. /*replace the Future argument to its result*/
  72. PyTuple_SetItem(cst->argList, i, fut_result);
  73. }
  74. /*else if (strcmp(tp, "numpy.ndarray")==0)
  75. {
  76. printf("array is %p\n", obj);
  77. }*/
  78. }
  79. /*call the python function*/
  80. PyObject *pRetVal = PyObject_CallObject(cst->f, cst->argList);
  81. //const char *tp = Py_TYPE(pRetVal)->tp_name;
  82. //printf("return value type is %s\n", tp);
  83. cst->rv = pRetVal;
  84. //Py_DECREF(cst->f);
  85. /*restore previous GIL state*/
  86. PyGILState_Release(state);
  87. }
  88. /*function passed to starpu_task.callback_func*/
  89. void cb_func(void *v)
  90. {
  91. struct starpu_task *task = starpu_task_get_current();
  92. struct codelet_args *cst = (struct codelet_args*) task->cl_arg;
  93. /*make sure we own the GIL*/
  94. PyGILState_STATE state = PyGILState_Ensure();
  95. /*set the Future result and mark the Future as done*/
  96. PyObject *set_result = PyObject_GetAttrString(cst->fut, "set_result");
  97. PyObject *loop_callback = PyObject_CallMethod(cst->lp, "call_soon_threadsafe", "(O,O)", set_result, cst->rv);
  98. Py_DECREF(loop_callback);
  99. Py_DECREF(set_result);
  100. Py_DECREF(cst->rv);
  101. Py_DECREF(cst->fut);
  102. Py_DECREF(cst->lp);
  103. Py_DECREF(cst->argList);
  104. //Py_DECREF(perfmodel);
  105. struct starpu_codelet *func_cl=(struct starpu_codelet *) task->cl;
  106. if (func_cl->model != NULL)
  107. {
  108. struct starpu_perfmodel *perf =(struct starpu_perfmodel *) func_cl->model;
  109. PyObject *perfmodel=PyCapsule_New(perf, "Perf", 0);
  110. Py_DECREF(perfmodel);
  111. }
  112. /*restore previous GIL state*/
  113. PyGILState_Release(state);
  114. /*deallocate task*/
  115. free(task->cl);
  116. free(task->cl_arg);
  117. }
  118. /***********************************************************************************/
  119. /*PyObject*->struct starpu_task**/
  120. static struct starpu_task *PyTask_AsTask(PyObject *obj)
  121. {
  122. return (struct starpu_task *) PyCapsule_GetPointer(obj, "Task");
  123. }
  124. /* destructor function for task */
  125. static void del_Task(PyObject *obj)
  126. {
  127. struct starpu_task *obj_task=PyTask_AsTask(obj);
  128. obj_task->destroy=1; /*XXX we should call starpu task destroy*/
  129. }
  130. /*struct starpu_task*->PyObject**/
  131. static PyObject *PyTask_FromTask(struct starpu_task *task)
  132. {
  133. return PyCapsule_New(task, "Task", del_Task);
  134. }
  135. /***********************************************************************************/
  136. static size_t sizebase (struct starpu_task *task, unsigned nimpl)
  137. {
  138. int n=0;
  139. struct codelet_args *cst = (struct codelet_args*) task->cl_arg;
  140. /*get the result of function*/
  141. PyObject *obj=cst->rv;
  142. /*get the length of result*/
  143. const char *tp = Py_TYPE(obj)->tp_name;
  144. #ifdef STARPU_PYTHON_HAVE_NUMPY
  145. /*if the result is a numpy array*/
  146. if (strcmp(tp, "numpy.ndarray")==0)
  147. n = PyArray_SIZE(obj);
  148. else
  149. #endif
  150. /*if the result is a list*/
  151. if (strcmp(tp, "list")==0)
  152. n = PyList_Size(obj);
  153. /*else error*/
  154. else
  155. {
  156. printf("starpu_perfmodel::size_base: the type of function result is unrecognized\n");
  157. exit(1);
  158. }
  159. return n;
  160. }
  161. static void del_Perf(PyObject *obj)
  162. {
  163. struct starpu_perfmodel *perf=(struct starpu_perfmodel*)PyCapsule_GetPointer(obj, "Perf");
  164. free(perf);
  165. }
  166. /*initialization of perfmodel*/
  167. static PyObject* init_perfmodel(PyObject *self, PyObject *args)
  168. {
  169. char *sym;
  170. if (!PyArg_ParseTuple(args, "s", &sym))
  171. return NULL;
  172. /*allocate a perfmodel structure*/
  173. struct starpu_perfmodel *perf=(struct starpu_perfmodel*)calloc(1, sizeof(struct starpu_perfmodel));
  174. /*get the perfmodel symbol*/
  175. char *p =strdup(sym);
  176. perf->symbol=p;
  177. perf->type=STARPU_HISTORY_BASED;
  178. /*struct perfmodel*->PyObject**/
  179. PyObject *perfmodel=PyCapsule_New(perf, "Perf", NULL);
  180. return perfmodel;
  181. }
  182. /*free perfmodel*/
  183. static PyObject* free_perfmodel(PyObject *self, PyObject *args)
  184. {
  185. PyObject *perfmodel;
  186. if (!PyArg_ParseTuple(args, "O", &perfmodel))
  187. return NULL;
  188. /*PyObject*->struct perfmodel**/
  189. struct starpu_perfmodel *perf=PyCapsule_GetPointer(perfmodel, "Perf");
  190. starpu_save_history_based_model(perf);
  191. //starpu_perfmodel_unload_model(perf);
  192. //free(perf->symbol);
  193. starpu_perfmodel_deinit(perf);
  194. free(perf);
  195. /*return type is void*/
  196. Py_INCREF(Py_None);
  197. return Py_None;
  198. }
  199. static PyObject* starpu_save_history_based_model_wrapper(PyObject *self, PyObject *args)
  200. {
  201. PyObject *perfmodel;
  202. if (!PyArg_ParseTuple(args, "O", &perfmodel))
  203. return NULL;
  204. /*PyObject*->struct perfmodel**/
  205. struct starpu_perfmodel *perf=PyCapsule_GetPointer(perfmodel, "Perf");
  206. starpu_save_history_based_model(perf);
  207. /*return type is void*/
  208. Py_INCREF(Py_None);
  209. return Py_None;
  210. }
  211. /*****************************Wrappers of StarPU methods****************************/
  212. /*wrapper submit method*/
  213. static PyObject* starpu_task_submit_wrapper(PyObject *self, PyObject *args)
  214. {
  215. /*get the running Event loop*/
  216. PyObject *loop = PyObject_CallMethod(asyncio_module, "get_running_loop", NULL);
  217. /*create a asyncio.Future object*/
  218. PyObject *fut = PyObject_CallMethod(loop, "create_future", NULL);
  219. /*first argument in args is always the python function passed in*/
  220. PyObject *func_py = PyTuple_GetItem(args, 0);
  221. Py_INCREF(func_py);
  222. /*allocate a task structure and initialize it with default values*/
  223. struct starpu_task *task=starpu_task_create();
  224. task->destroy=0;
  225. PyObject *PyTask=PyTask_FromTask(task);
  226. /*set one of fut attribute to the task pointer*/
  227. PyObject_SetAttrString(fut, "starpu_task", PyTask);
  228. /*check the arguments of python function passed in*/
  229. int i;
  230. for(i=1; i < PyTuple_Size(args)-1; i++)
  231. {
  232. PyObject *obj=PyTuple_GetItem(args, i);
  233. const char* tp = Py_TYPE(obj)->tp_name;
  234. if(strcmp(tp, "_asyncio.Future") == 0)
  235. {
  236. /*if one of arguments is Future, get its corresponding task*/
  237. PyObject *fut_task=PyObject_GetAttrString(obj, "starpu_task");
  238. /*declare task dependencies between the current task and the corresponding task of Future argument*/
  239. starpu_task_declare_deps(task, 1, PyTask_AsTask(fut_task));
  240. Py_DECREF(fut_task);
  241. }
  242. }
  243. /*allocate a codelet structure*/
  244. struct starpu_codelet *func_cl=(struct starpu_codelet*)malloc(sizeof(struct starpu_codelet));
  245. /*initialize func_cl with default values*/
  246. starpu_codelet_init(func_cl);
  247. func_cl->cpu_funcs[0]=&codelet_func;
  248. /*check whether the option perfmodel is None*/
  249. PyObject *dict_option = PyTuple_GetItem(args, PyTuple_Size(args)-1);/*the last argument is the option dictionary*/
  250. PyObject *perfmodel = PyDict_GetItemString(dict_option, "perfmodel");
  251. const char *tp_perf = Py_TYPE(perfmodel)->tp_name;
  252. if (strcmp(tp_perf, "PyCapsule")==0)
  253. {
  254. /*PyObject*->struct perfmodel**/
  255. struct starpu_perfmodel *perf=PyCapsule_GetPointer(perfmodel, "Perf");
  256. func_cl->model=perf;
  257. Py_INCREF(perfmodel);
  258. }
  259. /*allocate a new codelet structure to pass the python function, asyncio.Future and Event loop*/
  260. struct codelet_args *cst = (struct codelet_args*)malloc(sizeof(struct codelet_args));
  261. cst->f = func_py;
  262. cst->fut = fut;
  263. cst->lp = loop;
  264. Py_INCREF(fut);
  265. Py_INCREF(loop);
  266. /*pass args in argList*/
  267. if (PyTuple_Size(args)==2)/*function no arguments*/
  268. cst->argList = PyTuple_New(0);
  269. else
  270. {/*function has arguments*/
  271. cst->argList = PyTuple_New(PyTuple_Size(args)-2);
  272. int i;
  273. for(i=0; i < PyTuple_Size(args)-2; i++)
  274. {
  275. PyObject *tmp=PyTuple_GetItem(args, i+1);
  276. PyTuple_SetItem(cst->argList, i, tmp);
  277. Py_INCREF(PyTuple_GetItem(cst->argList, i));
  278. }
  279. }
  280. task->cl=func_cl;
  281. task->cl_arg=cst;
  282. /*pass optional values name=None, synchronous=1, priority=0, color=None, flops=None, perfmodel=None*/
  283. /*const char * name*/
  284. PyObject *PyName = PyDict_GetItemString(dict_option, "name");
  285. const char *name_type = Py_TYPE(PyName)->tp_name;
  286. if (strcmp(name_type, "NoneType")!=0)
  287. {
  288. PyObject *pStrObj = PyUnicode_AsUTF8String(PyName);
  289. char* name_str = PyBytes_AsString(pStrObj);
  290. char* name = strdup(name_str);
  291. //printf("name is %s\n", name);
  292. task->name=name;
  293. Py_DECREF(pStrObj);
  294. }
  295. /*unsigned synchronous:1*/
  296. PyObject *PySync = PyDict_GetItemString(dict_option, "synchronous");
  297. unsigned sync=PyLong_AsUnsignedLong(PySync);
  298. //printf("sync is %u\n", sync);
  299. task->synchronous=sync;
  300. /*int priority*/
  301. PyObject *PyPrio = PyDict_GetItemString(dict_option, "priority");
  302. int prio=PyLong_AsLong(PyPrio);
  303. //printf("prio is %d\n", prio);
  304. task->priority=prio;
  305. /*unsigned color*/
  306. PyObject *PyColor = PyDict_GetItemString(dict_option, "color");
  307. const char *color_type = Py_TYPE(PyColor)->tp_name;
  308. if (strcmp(color_type, "NoneType")!=0)
  309. {
  310. unsigned color=PyLong_AsUnsignedLong(PyColor);
  311. //printf("color is %u\n", color);
  312. task->color=color;
  313. }
  314. /*double flops*/
  315. PyObject *PyFlops = PyDict_GetItemString(dict_option, "flops");
  316. const char *flops_type = Py_TYPE(PyFlops)->tp_name;
  317. if (strcmp(flops_type, "NoneType")!=0)
  318. {
  319. double flops=PyFloat_AsDouble(PyFlops);
  320. //printf("flops is %f\n", flop);
  321. task->flops=flops;
  322. }
  323. task->callback_func=&cb_func;
  324. /*call starpu_task_submit method*/
  325. Py_BEGIN_ALLOW_THREADS
  326. int ret = starpu_task_submit(task);
  327. assert(ret==0);
  328. Py_END_ALLOW_THREADS
  329. if (strcmp(tp_perf, "PyCapsule")==0)
  330. {
  331. struct starpu_perfmodel *perf =(struct starpu_perfmodel *) func_cl->model;
  332. perf->size_base=&sizebase;
  333. }
  334. //printf("the number of reference is %ld\n", Py_REFCNT(func_py));
  335. //_Py_PrintReferences(stderr);
  336. //COUNTREFS();
  337. return fut;
  338. }
  339. /*wrapper wait for all method*/
  340. static PyObject* starpu_task_wait_for_all_wrapper(PyObject *self, PyObject *args)
  341. {
  342. /*call starpu_task_wait_for_all method*/
  343. Py_BEGIN_ALLOW_THREADS
  344. starpu_task_wait_for_all();
  345. Py_END_ALLOW_THREADS
  346. /*return type is void*/
  347. Py_INCREF(Py_None);
  348. return Py_None;
  349. }
  350. /*wrapper pause method*/
  351. static PyObject* starpu_pause_wrapper(PyObject *self, PyObject *args)
  352. {
  353. /*call starpu_pause method*/
  354. starpu_pause();
  355. /*return type is void*/
  356. Py_INCREF(Py_None);
  357. return Py_None;
  358. }
  359. /*wrapper resume method*/
  360. static PyObject* starpu_resume_wrapper(PyObject *self, PyObject *args)
  361. {
  362. /*call starpu_resume method*/
  363. starpu_resume();
  364. /*return type is void*/
  365. Py_INCREF(Py_None);
  366. return Py_None;
  367. }
  368. /*wrapper get count cpu method*/
  369. static PyObject* starpu_cpu_worker_get_count_wrapper(PyObject *self, PyObject *args)
  370. {
  371. /*call starpu_cpu_worker_get_count method*/
  372. int num_cpu=starpu_cpu_worker_get_count();
  373. /*return type is unsigned*/
  374. return Py_BuildValue("I", num_cpu);
  375. }
  376. /*wrapper get min priority method*/
  377. static PyObject* starpu_sched_get_min_priority_wrapper(PyObject *self, PyObject *args)
  378. {
  379. /*call starpu_sched_get_min_priority*/
  380. int min_prio=starpu_sched_get_min_priority();
  381. /*return type is int*/
  382. return Py_BuildValue("i", min_prio);
  383. }
  384. /*wrapper get max priority method*/
  385. static PyObject* starpu_sched_get_max_priority_wrapper(PyObject *self, PyObject *args)
  386. {
  387. /*call starpu_sched_get_max_priority*/
  388. int max_prio=starpu_sched_get_max_priority();
  389. /*return type is int*/
  390. return Py_BuildValue("i", max_prio);
  391. }
  392. /*wrapper get the number of no completed submitted tasks method*/
  393. static PyObject* starpu_task_nsubmitted_wrapper(PyObject *self, PyObject *args)
  394. {
  395. /*call starpu_task_nsubmitted*/
  396. int num_task=starpu_task_nsubmitted();
  397. /*Return the number of submitted tasks which have not completed yet */
  398. return Py_BuildValue("i", num_task);
  399. }
  400. /***********************************************************************************/
  401. /***************The module’s method table and initialization function**************/
  402. /*method table*/
  403. static PyMethodDef starpupyMethods[] =
  404. {
  405. {"_task_submit", starpu_task_submit_wrapper, METH_VARARGS, "submit the task"}, /*submit method*/
  406. {"task_wait_for_all", starpu_task_wait_for_all_wrapper, METH_VARARGS, "wait the task"}, /*wait for all method*/
  407. {"pause", starpu_pause_wrapper, METH_VARARGS, "suspend the processing of new tasks by workers"}, /*pause method*/
  408. {"resume", starpu_resume_wrapper, METH_VARARGS, "resume the workers polling for new tasks"}, /*resume method*/
  409. {"cpu_worker_get_count", starpu_cpu_worker_get_count_wrapper, METH_VARARGS, "return the number of CPUs controlled by StarPU"}, /*get count cpu method*/
  410. {"init_perfmodel", init_perfmodel, METH_VARARGS, "initialize struct starpu_perfmodel"}, /*initialize perfmodel*/
  411. {"free_perfmodel", free_perfmodel, METH_VARARGS, "free struct starpu_perfmodel"}, /*free perfmodel*/
  412. {"save_history_based_model", starpu_save_history_based_model_wrapper, METH_VARARGS, "save the performance model"}, /*save the performance model*/
  413. {"sched_get_min_priority", starpu_sched_get_min_priority_wrapper, METH_VARARGS, "get the number of min priority"}, /*get the number of min priority*/
  414. {"sched_get_max_priority", starpu_sched_get_max_priority_wrapper, METH_VARARGS, "get the number of max priority"}, /*get the number of max priority*/
  415. {"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*/
  416. {NULL, NULL}
  417. };
  418. /*deallocation function*/
  419. static void starpupyFree(void *self)
  420. {
  421. starpu_shutdown();
  422. Py_DECREF(asyncio_module);
  423. //COUNTREFS();
  424. }
  425. /*module definition structure*/
  426. static struct PyModuleDef starpupymodule =
  427. {
  428. PyModuleDef_HEAD_INIT,
  429. "starpupy", /*name of module*/
  430. NULL,
  431. -1,
  432. starpupyMethods, /*method table*/
  433. NULL,
  434. NULL,
  435. NULL,
  436. starpupyFree /*deallocation function*/
  437. };
  438. /*initialization function*/
  439. PyMODINIT_FUNC
  440. PyInit_starpupy(void)
  441. {
  442. PyEval_InitThreads();
  443. /*starpu initialization*/
  444. int ret = starpu_init(NULL);
  445. assert(ret==0);
  446. /*python asysncio import*/
  447. asyncio_module = PyImport_ImportModule("asyncio");
  448. #ifdef STARPU_PYTHON_HAVE_NUMPY
  449. /*numpy import array*/
  450. import_array();
  451. #endif
  452. /*module import initialization*/
  453. return PyModule_Create(&starpupymodule);
  454. }
  455. /***********************************************************************************/