starpu_task_wrapper.c 16 KB

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