workers.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
  5. * Copyright (C) 2010, 2011 INRIA
  6. * Copyright (C) 2011 Télécom-SudParis
  7. * Copyright (C) 2011-2012, 2016, 2017 INRIA
  8. * Copyright (C) 2016 Uppsala University
  9. *
  10. * StarPU is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation; either version 2.1 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * StarPU is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. *
  19. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  20. */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #ifdef __linux__
  24. #include <sys/utsname.h>
  25. #endif
  26. #include <common/config.h>
  27. #include <common/utils.h>
  28. #include <common/graph.h>
  29. #include <core/progress_hook.h>
  30. #include <core/workers.h>
  31. #include <core/debug.h>
  32. #include <core/disk.h>
  33. #include <core/task.h>
  34. #include <datawizard/malloc.h>
  35. #include <profiling/profiling.h>
  36. #include <sched_policies/sched_component.h>
  37. #include <datawizard/memory_nodes.h>
  38. #include <top/starpu_top_core.h>
  39. #include <drivers/mp_common/sink_common.h>
  40. #include <drivers/scc/driver_scc_common.h>
  41. #include <drivers/mpi/driver_mpi_common.h>
  42. #include <drivers/cpu/driver_cpu.h>
  43. #include <drivers/cuda/driver_cuda.h>
  44. #include <drivers/opencl/driver_opencl.h>
  45. #ifdef STARPU_SIMGRID
  46. #include <core/simgrid.h>
  47. #endif
  48. #if defined(_WIN32) && !defined(__CYGWIN__)
  49. #include <windows.h>
  50. #endif
  51. /* acquire/release semantic for concurrent initialization/de-initialization */
  52. static starpu_pthread_mutex_t init_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  53. static starpu_pthread_cond_t init_cond = STARPU_PTHREAD_COND_INITIALIZER;
  54. static int init_count = 0;
  55. static enum initialization initialized = UNINITIALIZED;
  56. int _starpu_keys_initialized STARPU_ATTRIBUTE_INTERNAL;
  57. starpu_pthread_key_t _starpu_worker_key STARPU_ATTRIBUTE_INTERNAL;
  58. starpu_pthread_key_t _starpu_worker_set_key STARPU_ATTRIBUTE_INTERNAL;
  59. struct _starpu_machine_config _starpu_config STARPU_ATTRIBUTE_INTERNAL;
  60. static int check_entire_platform;
  61. /* Pointers to argc and argv
  62. */
  63. static int *my_argc = 0;
  64. static char ***my_argv = NULL;
  65. /* Initialize value of static argc and argv, called when the process begins
  66. */
  67. void _starpu_set_argc_argv(int *argc_param, char ***argv_param)
  68. {
  69. my_argc = argc_param;
  70. my_argv = argv_param;
  71. }
  72. int *_starpu_get_argc()
  73. {
  74. return my_argc;
  75. }
  76. char ***_starpu_get_argv()
  77. {
  78. return my_argv;
  79. }
  80. int _starpu_is_initialized(void)
  81. {
  82. return initialized == INITIALIZED;
  83. }
  84. /* Makes sure that at least one of the workers of type <arch> can execute
  85. * <task>, for at least one of its implementations. */
  86. static uint32_t _starpu_worker_exists_and_can_execute(struct starpu_task *task,
  87. enum starpu_worker_archtype arch)
  88. {
  89. _starpu_codelet_check_deprecated_fields(task->cl);
  90. /* make sure there is a worker on the machine able to execute the
  91. task, independent of the sched_ctx, this latter may receive latter on
  92. the necessary worker - the user or the hypervisor should take care this happens */
  93. struct _starpu_sched_ctx *sched_ctx = check_entire_platform == 1 ? _starpu_get_initial_sched_ctx() : _starpu_get_sched_ctx_struct(task->sched_ctx);
  94. struct starpu_worker_collection *workers = sched_ctx->workers;
  95. struct starpu_sched_ctx_iterator it;
  96. workers->init_iterator(workers, &it);
  97. while(workers->has_next(workers, &it))
  98. {
  99. int i = workers->get_next(workers, &it);
  100. if (starpu_worker_get_type(i) != arch)
  101. continue;
  102. unsigned impl;
  103. for (impl = 0; impl < STARPU_MAXIMPLEMENTATIONS; impl++)
  104. {
  105. /* We could call task->cl->can_execute(i, task, impl)
  106. here, it would definitely work. It is probably
  107. cheaper to check whether it is necessary in order to
  108. avoid a useless function call, though. */
  109. unsigned test_implementation = 0;
  110. switch (arch)
  111. {
  112. case STARPU_CPU_WORKER:
  113. if (task->cl->cpu_funcs[impl] != NULL)
  114. test_implementation = 1;
  115. break;
  116. case STARPU_CUDA_WORKER:
  117. if (task->cl->cuda_funcs[impl] != NULL)
  118. test_implementation = 1;
  119. break;
  120. case STARPU_OPENCL_WORKER:
  121. if (task->cl->opencl_funcs[impl] != NULL)
  122. test_implementation = 1;
  123. break;
  124. case STARPU_MIC_WORKER:
  125. if (task->cl->cpu_funcs_name[impl] != NULL || task->cl->mic_funcs[impl] != NULL)
  126. test_implementation = 1;
  127. break;
  128. case STARPU_MPI_MS_WORKER:
  129. if (task->cl->cpu_funcs_name[impl] != NULL || task->cl->mpi_ms_funcs[impl] != NULL)
  130. test_implementation = 1;
  131. break;
  132. case STARPU_SCC_WORKER:
  133. if (task->cl->cpu_funcs_name[impl] != NULL || task->cl->scc_funcs[impl] != NULL)
  134. test_implementation = 1;
  135. break;
  136. default:
  137. STARPU_ABORT();
  138. }
  139. if (!test_implementation)
  140. continue;
  141. if (task->cl->can_execute)
  142. return task->cl->can_execute(i, task, impl);
  143. if(test_implementation)
  144. return 1;
  145. }
  146. }
  147. return 0;
  148. }
  149. /* in case a task is submitted, we may check whether there exists a worker
  150. that may execute the task or not */
  151. uint32_t _starpu_worker_exists(struct starpu_task *task)
  152. {
  153. _starpu_codelet_check_deprecated_fields(task->cl);
  154. if (task->where == STARPU_NOWHERE)
  155. return 1;
  156. /* if the task belongs to the init context we can
  157. check out all the worker mask of the machine
  158. if not we should iterate on the workers of the ctx
  159. and verify if it exists a worker able to exec the task */
  160. if(task->sched_ctx == 0)
  161. {
  162. if (!(task->where & _starpu_config.worker_mask))
  163. return 0;
  164. if (!task->cl->can_execute)
  165. return 1;
  166. }
  167. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  168. if ((task->where & STARPU_CPU) &&
  169. _starpu_worker_exists_and_can_execute(task, STARPU_CPU_WORKER))
  170. return 1;
  171. #endif
  172. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  173. if ((task->where & STARPU_CUDA) &&
  174. _starpu_worker_exists_and_can_execute(task, STARPU_CUDA_WORKER))
  175. return 1;
  176. #endif
  177. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  178. if ((task->where & STARPU_OPENCL) &&
  179. _starpu_worker_exists_and_can_execute(task, STARPU_OPENCL_WORKER))
  180. return 1;
  181. #endif
  182. #ifdef STARPU_USE_MIC
  183. if ((task->where & STARPU_MIC) &&
  184. _starpu_worker_exists_and_can_execute(task, STARPU_MIC_WORKER))
  185. return 1;
  186. #endif
  187. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  188. if ((task->where & STARPU_MPI_MS) &&
  189. _starpu_worker_exists_and_can_execute(task, STARPU_MPI_MS_WORKER))
  190. return 1;
  191. #endif
  192. #ifdef STARPU_USE_SCC
  193. if ((task->where & STARPU_SCC) &&
  194. _starpu_worker_exists_and_can_execute(task, STARPU_SCC_WORKER))
  195. return 1;
  196. #endif
  197. return 0;
  198. }
  199. uint32_t _starpu_can_submit_cuda_task(void)
  200. {
  201. return (STARPU_CUDA & _starpu_config.worker_mask);
  202. }
  203. uint32_t _starpu_can_submit_cpu_task(void)
  204. {
  205. return (STARPU_CPU & _starpu_config.worker_mask);
  206. }
  207. uint32_t _starpu_can_submit_opencl_task(void)
  208. {
  209. return (STARPU_OPENCL & _starpu_config.worker_mask);
  210. }
  211. uint32_t _starpu_can_submit_scc_task(void)
  212. {
  213. return (STARPU_SCC & _starpu_config.worker_mask);
  214. }
  215. static inline int _starpu_can_use_nth_implementation(enum starpu_worker_archtype arch, struct starpu_codelet *cl, unsigned nimpl)
  216. {
  217. switch(arch)
  218. {
  219. case STARPU_ANY_WORKER:
  220. {
  221. int cpu_func_enabled=1, cuda_func_enabled=1, opencl_func_enabled=1;
  222. /* TODO: MIC/SCC */
  223. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  224. starpu_cpu_func_t cpu_func = _starpu_task_get_cpu_nth_implementation(cl, nimpl);
  225. cpu_func_enabled = cpu_func != NULL && starpu_cpu_worker_get_count();
  226. #endif
  227. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  228. starpu_cuda_func_t cuda_func = _starpu_task_get_cuda_nth_implementation(cl, nimpl);
  229. cuda_func_enabled = cuda_func != NULL && starpu_cuda_worker_get_count();
  230. #endif
  231. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  232. starpu_opencl_func_t opencl_func = _starpu_task_get_opencl_nth_implementation(cl, nimpl);
  233. opencl_func_enabled = opencl_func != NULL && starpu_opencl_worker_get_count();
  234. #endif
  235. return (cpu_func_enabled && cuda_func_enabled && opencl_func_enabled);
  236. }
  237. case STARPU_CPU_WORKER:
  238. {
  239. starpu_cpu_func_t func = _starpu_task_get_cpu_nth_implementation(cl, nimpl);
  240. return func != NULL;
  241. }
  242. case STARPU_CUDA_WORKER:
  243. {
  244. starpu_cuda_func_t func = _starpu_task_get_cuda_nth_implementation(cl, nimpl);
  245. return func != NULL;
  246. }
  247. case STARPU_OPENCL_WORKER:
  248. {
  249. starpu_opencl_func_t func = _starpu_task_get_opencl_nth_implementation(cl, nimpl);
  250. return func != NULL;
  251. }
  252. case STARPU_MIC_WORKER:
  253. {
  254. starpu_mic_func_t func = _starpu_task_get_mic_nth_implementation(cl, nimpl);
  255. const char *func_name = _starpu_task_get_cpu_name_nth_implementation(cl, nimpl);
  256. return func != NULL || func_name != NULL;
  257. }
  258. case STARPU_MPI_MS_WORKER:
  259. {
  260. starpu_mpi_ms_func_t func = _starpu_task_get_mpi_ms_nth_implementation(cl, nimpl);
  261. const char *func_name = _starpu_task_get_cpu_name_nth_implementation(cl, nimpl);
  262. return func != NULL || func_name != NULL;
  263. }
  264. case STARPU_SCC_WORKER:
  265. {
  266. starpu_scc_func_t func = _starpu_task_get_scc_nth_implementation(cl, nimpl);
  267. const char *func_name = _starpu_task_get_cpu_name_nth_implementation(cl, nimpl);
  268. return func != NULL || func_name != NULL;
  269. }
  270. default:
  271. STARPU_ASSERT_MSG(0, "Unknown arch type %d", arch);
  272. }
  273. return 0;
  274. }
  275. /* must be called with sched_mutex locked to protect state_blocked_in_parallel */
  276. int starpu_worker_can_execute_task(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  277. {
  278. /* if the worker is blocked in a parallel ctx don't submit tasks on it */
  279. #ifdef STARPU_DEVEL
  280. #warning FIXME: this is very expensive, while can_execute is supposed to be not very costly so schedulers can call it a lot
  281. #endif
  282. if(starpu_worker_is_blocked_in_parallel(workerid))
  283. return 0;
  284. /* TODO: check that the task operand sizes will fit on that device */
  285. return (task->where & _starpu_config.workers[workerid].worker_mask) &&
  286. _starpu_can_use_nth_implementation(_starpu_config.workers[workerid].arch, task->cl, nimpl) &&
  287. (!task->cl->can_execute || task->cl->can_execute(workerid, task, nimpl));
  288. }
  289. /* must be called with sched_mutex locked to protect state_blocked_in_parallel */
  290. int starpu_worker_can_execute_task_impl(unsigned workerid, struct starpu_task *task, unsigned *impl_mask)
  291. {
  292. /* if the worker is blocked in a parallel ctx don't submit tasks on it */
  293. if(starpu_worker_is_blocked_in_parallel(workerid))
  294. return 0;
  295. unsigned mask;
  296. int i;
  297. enum starpu_worker_archtype arch;
  298. struct starpu_codelet *cl;
  299. /* TODO: check that the task operand sizes will fit on that device */
  300. cl = task->cl;
  301. if (!(task->where & _starpu_config.workers[workerid].worker_mask)) return 0;
  302. mask = 0;
  303. arch = _starpu_config.workers[workerid].arch;
  304. if (!task->cl->can_execute)
  305. {
  306. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  307. if (_starpu_can_use_nth_implementation(arch, cl, i))
  308. {
  309. mask |= 1U << i;
  310. if (!impl_mask)
  311. break;
  312. }
  313. }
  314. else
  315. {
  316. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  317. if (_starpu_can_use_nth_implementation(arch, cl, i)
  318. && (!task->cl->can_execute || task->cl->can_execute(workerid, task, i)))
  319. {
  320. mask |= 1U << i;
  321. if (!impl_mask)
  322. break;
  323. }
  324. }
  325. if (impl_mask)
  326. *impl_mask = mask;
  327. return mask != 0;
  328. }
  329. /* must be called with sched_mutex locked to protect state_blocked */
  330. int starpu_worker_can_execute_task_first_impl(unsigned workerid, struct starpu_task *task, unsigned *nimpl)
  331. {
  332. /* if the worker is blocked in a parallel ctx don't submit tasks on it */
  333. if(starpu_worker_is_blocked_in_parallel(workerid))
  334. return 0;
  335. int i;
  336. enum starpu_worker_archtype arch;
  337. struct starpu_codelet *cl;
  338. /* TODO: check that the task operand sizes will fit on that device */
  339. cl = task->cl;
  340. if (!(task->where & _starpu_config.workers[workerid].worker_mask)) return 0;
  341. arch = _starpu_config.workers[workerid].arch;
  342. if (!task->cl->can_execute)
  343. {
  344. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  345. if (_starpu_can_use_nth_implementation(arch, cl, i))
  346. {
  347. if (nimpl)
  348. *nimpl = i;
  349. return 1;
  350. }
  351. }
  352. else
  353. {
  354. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  355. if (_starpu_can_use_nth_implementation(arch, cl, i)
  356. && (task->cl->can_execute(workerid, task, i)))
  357. {
  358. if (nimpl)
  359. *nimpl = i;
  360. return 1;
  361. }
  362. }
  363. return 0;
  364. }
  365. int starpu_combined_worker_can_execute_task(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  366. {
  367. /* TODO: check that the task operand sizes will fit on that device */
  368. struct starpu_codelet *cl = task->cl;
  369. unsigned nworkers = _starpu_config.topology.nworkers;
  370. /* Is this a parallel worker ? */
  371. if (workerid < nworkers)
  372. {
  373. return !!((task->where & _starpu_config.workers[workerid].worker_mask) &&
  374. _starpu_can_use_nth_implementation(_starpu_config.workers[workerid].arch, task->cl, nimpl) &&
  375. (!task->cl->can_execute || task->cl->can_execute(workerid, task, nimpl)));
  376. }
  377. else
  378. {
  379. if (cl->type == STARPU_SPMD
  380. #ifdef STARPU_HAVE_HWLOC
  381. || cl->type == STARPU_FORKJOIN
  382. #else
  383. #ifdef __GLIBC__
  384. || cl->type == STARPU_FORKJOIN
  385. #endif
  386. #endif
  387. )
  388. {
  389. /* TODO we should add other types of constraints */
  390. /* Is the worker larger than requested ? */
  391. int worker_size = (int)_starpu_config.combined_workers[workerid - nworkers].worker_size;
  392. int worker0 = _starpu_config.combined_workers[workerid - nworkers].combined_workerid[0];
  393. return !!((worker_size <= task->cl->max_parallelism) &&
  394. _starpu_can_use_nth_implementation(_starpu_config.workers[worker0].arch, task->cl, nimpl) &&
  395. (!task->cl->can_execute || task->cl->can_execute(workerid, task, nimpl)));
  396. }
  397. else
  398. {
  399. /* We have a sequential task but a parallel worker */
  400. return 0;
  401. }
  402. }
  403. }
  404. /*
  405. * Runtime initialization methods
  406. */
  407. static void _starpu_init_worker_queue(struct _starpu_worker *worker)
  408. {
  409. _starpu_memory_node_register_condition(worker, &worker->sched_cond, worker->memory_node);
  410. }
  411. /*
  412. * Returns 0 if the given driver is one of the drivers that must be launched by
  413. * the application itself, and not by StarPU, 1 otherwise.
  414. */
  415. static unsigned _starpu_may_launch_driver(struct starpu_conf *conf,
  416. struct starpu_driver *d)
  417. {
  418. if (conf->n_not_launched_drivers == 0 || conf->not_launched_drivers == NULL)
  419. return 1;
  420. /* Is <d> in conf->not_launched_drivers ? */
  421. unsigned i;
  422. for (i = 0; i < conf->n_not_launched_drivers; i++)
  423. {
  424. if (d->type != conf->not_launched_drivers[i].type)
  425. continue;
  426. switch (d->type)
  427. {
  428. case STARPU_CPU_WORKER:
  429. if (d->id.cpu_id == conf->not_launched_drivers[i].id.cpu_id)
  430. return 0;
  431. break;
  432. case STARPU_CUDA_WORKER:
  433. if (d->id.cuda_id == conf->not_launched_drivers[i].id.cuda_id)
  434. return 0;
  435. break;
  436. #ifdef STARPU_USE_OPENCL
  437. case STARPU_OPENCL_WORKER:
  438. if (d->id.opencl_id == conf->not_launched_drivers[i].id.opencl_id)
  439. return 0;
  440. break;
  441. #endif
  442. default:
  443. STARPU_ABORT();
  444. }
  445. }
  446. return 1;
  447. }
  448. #ifdef STARPU_PERF_DEBUG
  449. struct itimerval prof_itimer;
  450. #endif
  451. static void _starpu_worker_init(struct _starpu_worker *workerarg, struct _starpu_machine_config *pconfig)
  452. {
  453. workerarg->config = pconfig;
  454. STARPU_PTHREAD_MUTEX_INIT(&workerarg->mutex, NULL);
  455. /* arch initialized by topology.c */
  456. /* worker_mask initialized by topology.c */
  457. /* perf_arch initialized by topology.c */
  458. /* worker_thread initialized by _starpu_launch_drivers */
  459. /* devid initialized by topology.c */
  460. /* subworkerid initialized by topology.c */
  461. /* bindid initialized by topology.c */
  462. /* workerid initialized by topology.c */
  463. workerarg->combined_workerid = workerarg->workerid;
  464. workerarg->current_rank = 0;
  465. workerarg->worker_size = 1;
  466. STARPU_PTHREAD_COND_INIT(&workerarg->started_cond, NULL);
  467. STARPU_PTHREAD_COND_INIT(&workerarg->ready_cond, NULL);
  468. /* memory_node initialized by topology.c */
  469. STARPU_PTHREAD_COND_INIT(&workerarg->sched_cond, NULL);
  470. STARPU_PTHREAD_MUTEX_INIT(&workerarg->sched_mutex, NULL);
  471. starpu_task_list_init(&workerarg->local_tasks);
  472. _starpu_ctx_change_list_init(&workerarg->ctx_change_list);
  473. workerarg->local_ordered_tasks = NULL;
  474. workerarg->local_ordered_tasks_size = 0;
  475. workerarg->current_ordered_task = 0;
  476. workerarg->current_ordered_task_order = 1;
  477. workerarg->current_task = NULL;
  478. #ifdef STARPU_SIMGRID
  479. starpu_pthread_wait_init(&workerarg->wait);
  480. starpu_pthread_queue_register(&workerarg->wait, &_starpu_simgrid_task_queue[workerarg->workerid]);
  481. #endif
  482. workerarg->task_transferring = NULL;
  483. workerarg->nb_buffers_transferred = 0;
  484. workerarg->nb_buffers_totransfer = 0;
  485. workerarg->first_task = 0;
  486. workerarg->ntasks = 0;
  487. /* set initialized by topology.c */
  488. workerarg->pipeline_length = 0;
  489. workerarg->pipeline_stuck = 0;
  490. workerarg->worker_is_running = 0;
  491. workerarg->worker_is_initialized = 0;
  492. workerarg->status = STATUS_INITIALIZING;
  493. workerarg->state_keep_awake = 0;
  494. /* name initialized by driver */
  495. /* short_name initialized by driver */
  496. workerarg->run_by_starpu = 1;
  497. workerarg->driver_ops = NULL;
  498. workerarg->sched_ctx_list = NULL;
  499. workerarg->tmp_sched_ctx = -1;
  500. workerarg->nsched_ctxs = 0;
  501. _starpu_barrier_counter_init(&workerarg->tasks_barrier, 0);
  502. workerarg->has_prev_init = 0;
  503. int ctx;
  504. for(ctx = 0; ctx < STARPU_NMAX_SCHED_CTXS; ctx++)
  505. workerarg->removed_from_ctx[ctx] = 0;
  506. workerarg->spinning_backoff = 1;
  507. for(ctx = 0; ctx < STARPU_NMAX_SCHED_CTXS; ctx++)
  508. {
  509. workerarg->shares_tasks_lists[ctx] = 0;
  510. workerarg->poped_in_ctx[ctx] = 0;
  511. }
  512. workerarg->reverse_phase[0] = 0;
  513. workerarg->reverse_phase[1] = 0;
  514. workerarg->pop_ctx_priority = 1;
  515. workerarg->is_slave_somewhere = 0;
  516. workerarg->state_relax_refcnt = 1;
  517. #ifdef STARPU_SPINLOCK_CHECK
  518. workerarg->relax_on_file = __FILE__;
  519. workerarg->relax_on_line = __LINE__;
  520. workerarg->relax_on_func = __starpu_func__;
  521. workerarg->relax_off_file = NULL;
  522. workerarg->relax_off_line = 0;
  523. workerarg->relax_off_func = NULL;
  524. #endif
  525. workerarg->state_sched_op_pending = 0;
  526. workerarg->state_changing_ctx_waiting = 0;
  527. workerarg->state_changing_ctx_notice = 0;
  528. workerarg->state_blocked_in_parallel_observed = 0;
  529. workerarg->state_blocked_in_parallel = 0;
  530. workerarg->state_block_in_parallel_req = 0;
  531. workerarg->state_block_in_parallel_ack = 0;
  532. workerarg->state_unblock_in_parallel_req = 0;
  533. workerarg->state_unblock_in_parallel_ack = 0;
  534. workerarg->block_in_parallel_ref_count = 0;
  535. /* cpu_set/hwloc_cpu_set initialized in topology.c */
  536. }
  537. static void _starpu_worker_deinit(struct _starpu_worker *workerarg)
  538. {
  539. (void) workerarg;
  540. #ifdef STARPU_SIMGRID
  541. starpu_pthread_queue_unregister(&workerarg->wait, &_starpu_simgrid_task_queue[workerarg->workerid]);
  542. starpu_pthread_wait_destroy(&workerarg->wait);
  543. #endif
  544. }
  545. #ifdef STARPU_USE_FXT
  546. void _starpu_worker_start(struct _starpu_worker *worker, unsigned fut_key, unsigned sync)
  547. {
  548. unsigned devid = worker->devid;
  549. unsigned memnode = worker->memory_node;
  550. _STARPU_TRACE_WORKER_INIT_START(fut_key, worker->workerid, devid, memnode, worker->bindid, sync);
  551. }
  552. #endif
  553. void _starpu_driver_start(struct _starpu_worker *worker, unsigned fut_key, unsigned sync STARPU_ATTRIBUTE_UNUSED)
  554. {
  555. (void) fut_key;
  556. int devid = worker->devid;
  557. (void) devid;
  558. #ifdef STARPU_USE_FXT
  559. _starpu_fxt_register_thread(worker->bindid);
  560. _starpu_worker_start(worker, fut_key, sync);
  561. #endif
  562. _starpu_set_local_worker_key(worker);
  563. STARPU_PTHREAD_MUTEX_LOCK(&worker->mutex);
  564. worker->worker_is_running = 1;
  565. STARPU_PTHREAD_COND_SIGNAL(&worker->started_cond);
  566. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->mutex);
  567. _starpu_bind_thread_on_cpu(worker->config, worker->bindid, worker->workerid);
  568. #if defined(STARPU_PERF_DEBUG) && !defined(STARPU_SIMGRID)
  569. setitimer(ITIMER_PROF, &prof_itimer, NULL);
  570. #endif
  571. _STARPU_DEBUG("worker %p %d for dev %d is ready on logical cpu %d\n", worker, worker->workerid, devid, worker->bindid);
  572. #ifdef STARPU_HAVE_HWLOC
  573. _STARPU_DEBUG("worker %p %d cpuset start at %d\n", worker, worker->workerid, hwloc_bitmap_first(worker->hwloc_cpu_set));
  574. #endif
  575. }
  576. static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
  577. {
  578. pconfig->running = 1;
  579. pconfig->pause_depth = 0;
  580. pconfig->submitting = 1;
  581. STARPU_HG_DISABLE_CHECKING(pconfig->watchdog_ok);
  582. unsigned nworkers = pconfig->topology.nworkers;
  583. unsigned worker;
  584. #if defined(STARPU_PERF_DEBUG) && !defined(STARPU_SIMGRID)
  585. /* Get itimer of the main thread, to set it for the worker threads */
  586. getitimer(ITIMER_PROF, &prof_itimer);
  587. #endif
  588. STARPU_AYU_INIT();
  589. /* Launch workers asynchronously */
  590. for (worker = 0; worker < nworkers; worker++)
  591. {
  592. struct _starpu_worker *workerarg = &pconfig->workers[worker];
  593. unsigned devid = workerarg->devid;
  594. #if defined(STARPU_USE_MIC) || defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID) || defined(STARPU_USE_MPI_MASTER_SLAVE)
  595. struct _starpu_worker_set *worker_set = workerarg->set;
  596. #endif
  597. _STARPU_DEBUG("initialising worker %u/%u\n", worker, nworkers);
  598. _starpu_init_worker_queue(workerarg);
  599. struct starpu_driver driver;
  600. driver.type = workerarg->arch;
  601. switch (workerarg->arch)
  602. {
  603. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  604. case STARPU_CPU_WORKER:
  605. driver.id.cpu_id = devid;
  606. workerarg->driver_ops = &_starpu_driver_cpu_ops;
  607. if (_starpu_may_launch_driver(&pconfig->conf, &driver))
  608. {
  609. STARPU_PTHREAD_CREATE_ON(
  610. "CPU",
  611. &workerarg->worker_thread,
  612. NULL,
  613. _starpu_cpu_worker,
  614. workerarg,
  615. _starpu_simgrid_get_host_by_worker(workerarg));
  616. #ifdef STARPU_USE_FXT
  617. /* In tracing mode, make sure the
  618. * thread is really started before
  619. * starting another one, to make sure
  620. * they appear in order in the trace.
  621. */
  622. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  623. while (!workerarg->worker_is_running)
  624. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  625. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  626. #endif
  627. }
  628. else
  629. {
  630. workerarg->run_by_starpu = 0;
  631. }
  632. break;
  633. #endif
  634. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  635. case STARPU_CUDA_WORKER:
  636. driver.id.cuda_id = devid;
  637. workerarg->driver_ops = &_starpu_driver_cuda_ops;
  638. if (worker_set->workers != workerarg)
  639. /* We are not the first worker of the
  640. * set, don't start a thread for it. */
  641. break;
  642. worker_set->set_is_initialized = 0;
  643. if (!_starpu_may_launch_driver(&pconfig->conf, &driver))
  644. {
  645. workerarg->run_by_starpu = 0;
  646. break;
  647. }
  648. STARPU_PTHREAD_CREATE_ON(
  649. "CUDA",
  650. &worker_set->worker_thread,
  651. NULL,
  652. _starpu_cuda_worker,
  653. worker_set,
  654. _starpu_simgrid_get_host_by_worker(workerarg));
  655. #ifdef STARPU_USE_FXT
  656. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  657. while (!workerarg->worker_is_running)
  658. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  659. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  660. #endif
  661. break;
  662. #endif
  663. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  664. case STARPU_OPENCL_WORKER:
  665. #ifndef STARPU_SIMGRID
  666. starpu_opencl_get_device(devid, &driver.id.opencl_id);
  667. workerarg->driver_ops = &_starpu_driver_opencl_ops;
  668. if (!_starpu_may_launch_driver(&pconfig->conf, &driver))
  669. {
  670. workerarg->run_by_starpu = 0;
  671. break;
  672. }
  673. #endif
  674. STARPU_PTHREAD_CREATE_ON(
  675. "OpenCL",
  676. &workerarg->worker_thread,
  677. NULL,
  678. _starpu_opencl_worker,
  679. workerarg,
  680. _starpu_simgrid_get_host_by_worker(workerarg));
  681. #ifdef STARPU_USE_FXT
  682. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  683. while (!workerarg->worker_is_running)
  684. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  685. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  686. #endif
  687. break;
  688. #endif
  689. #ifdef STARPU_USE_MIC
  690. case STARPU_MIC_WORKER:
  691. /* We spawn only one thread
  692. * per MIC device, which will control all MIC
  693. * workers of this device. (by using a worker set). */
  694. if (worker_set->workers != workerarg)
  695. break;
  696. worker_set->set_is_initialized = 0;
  697. STARPU_PTHREAD_CREATE_ON(
  698. "MIC",
  699. &worker_set->worker_thread,
  700. NULL,
  701. _starpu_mic_src_worker,
  702. worker_set,
  703. _starpu_simgrid_get_host_by_worker(workerarg));
  704. #ifdef STARPU_USE_FXT
  705. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  706. while (!workerarg->worker_is_running)
  707. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  708. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  709. #endif
  710. STARPU_PTHREAD_MUTEX_LOCK(&worker_set->mutex);
  711. while (!worker_set->set_is_initialized)
  712. STARPU_PTHREAD_COND_WAIT(&worker_set->ready_cond,
  713. &worker_set->mutex);
  714. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_set->mutex);
  715. worker_set->started = 1;
  716. break;
  717. #endif /* STARPU_USE_MIC */
  718. #ifdef STARPU_USE_SCC
  719. case STARPU_SCC_WORKER:
  720. workerarg->worker_is_initialized = 0;
  721. STARPU_PTHREAD_CREATE_ON(
  722. "SCC",
  723. &workerarg->worker_thread,
  724. NULL,
  725. _starpu_scc_src_worker,
  726. workerarg,
  727. _starpu_simgrid_get_host_by_worker(workerarg));
  728. #ifdef STARPU_USE_FXT
  729. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  730. while (!workerarg->worker_is_running)
  731. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  732. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  733. #endif
  734. break;
  735. #endif /* STARPU_USE_SCC */
  736. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  737. case STARPU_MPI_MS_WORKER:
  738. /* We spawn only one thread
  739. * per MPI device, which will control all MPI
  740. * workers of this device. (by using a worker set). */
  741. if (worker_set->workers != workerarg)
  742. break;
  743. worker_set->set_is_initialized = 0;
  744. #ifdef STARPU_MPI_MASTER_SLAVE_MULTIPLE_THREAD
  745. /* if MPI has multiple threads supports
  746. * we launch 1 thread per device
  747. * else
  748. * we launch one thread for all devices
  749. */
  750. STARPU_PTHREAD_CREATE_ON(
  751. "MPI MS",
  752. &worker_set->worker_thread,
  753. NULL,
  754. _starpu_mpi_src_worker,
  755. worker_set,
  756. _starpu_simgrid_get_host_by_worker(workerarg));
  757. #ifdef STARPU_USE_FXT
  758. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  759. while (!workerarg->worker_is_running)
  760. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  761. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  762. #endif
  763. STARPU_PTHREAD_MUTEX_LOCK(&worker_set->mutex);
  764. while (!worker_set->set_is_initialized)
  765. STARPU_PTHREAD_COND_WAIT(&worker_set->ready_cond,
  766. &worker_set->mutex);
  767. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_set->mutex);
  768. worker_set->started = 1;
  769. #endif /* STARPU_MPI_MASTER_SLAVE_MULTIPLE_THREAD */
  770. break;
  771. #endif /* STARPU_USE_MPI_MASTER_SLAVE */
  772. default:
  773. STARPU_ABORT();
  774. }
  775. }
  776. #if defined(STARPU_USE_MPI_MASTER_SLAVE) && !defined(STARPU_MPI_MASTER_SLAVE_MULTIPLE_THREAD)
  777. if (pconfig->topology.nmpidevices > 0)
  778. {
  779. struct _starpu_worker_set * worker_set_zero = &mpi_worker_set[0];
  780. struct _starpu_worker * worker_zero = &worker_set_zero->workers[0];
  781. STARPU_PTHREAD_CREATE_ON(
  782. "zero",
  783. &worker_set_zero->worker_thread,
  784. NULL,
  785. _starpu_mpi_src_worker,
  786. &mpi_worker_set,
  787. _starpu_simgrid_get_host_by_worker(worker_zero));
  788. /* We use the first worker to know if everything are finished */
  789. #ifdef STARPU_USE_FXT
  790. STARPU_PTHREAD_MUTEX_LOCK(&worker_zero->mutex);
  791. while (!worker_zero->worker_is_running)
  792. STARPU_PTHREAD_COND_WAIT(&worker_zero->started_cond, &worker_zero->mutex);
  793. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_zero->mutex);
  794. #endif
  795. STARPU_PTHREAD_MUTEX_LOCK(&worker_set_zero->mutex);
  796. while (!worker_set_zero->set_is_initialized)
  797. STARPU_PTHREAD_COND_WAIT(&worker_set_zero->ready_cond,
  798. &worker_set_zero->mutex);
  799. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_set_zero->mutex);
  800. worker_set_zero->started = 1;
  801. worker_set_zero->worker_thread = mpi_worker_set[0].worker_thread;
  802. }
  803. #endif
  804. for (worker = 0; worker < nworkers; worker++)
  805. {
  806. struct _starpu_worker *workerarg = &pconfig->workers[worker];
  807. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  808. #ifndef STARPU_SIMGRID
  809. if (!workerarg->run_by_starpu)
  810. break;
  811. #endif
  812. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  813. if (workerarg->arch == STARPU_CUDA_WORKER)
  814. {
  815. struct _starpu_worker_set *worker_set = workerarg->set;
  816. STARPU_PTHREAD_MUTEX_LOCK(&worker_set->mutex);
  817. while (!worker_set->set_is_initialized)
  818. STARPU_PTHREAD_COND_WAIT(&worker_set->ready_cond,
  819. &worker_set->mutex);
  820. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_set->mutex);
  821. worker_set->started = 1;
  822. }
  823. else
  824. #endif
  825. if (workerarg->arch != STARPU_CUDA_WORKER && workerarg->arch != STARPU_MPI_MS_WORKER && workerarg->arch != STARPU_MIC_WORKER)
  826. {
  827. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  828. while (!workerarg->worker_is_initialized)
  829. STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  830. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  831. }
  832. }
  833. _STARPU_DEBUG("finished launching drivers\n");
  834. }
  835. /* Initialize the starpu_conf with default values */
  836. int starpu_conf_init(struct starpu_conf *conf)
  837. {
  838. if (!conf)
  839. return -EINVAL;
  840. memset(conf, 0, sizeof(*conf));
  841. conf->magic = 42;
  842. conf->sched_policy_name = starpu_getenv("STARPU_SCHED");
  843. conf->sched_policy = NULL;
  844. conf->global_sched_ctx_min_priority = starpu_get_env_number("STARPU_MIN_PRIO");
  845. conf->global_sched_ctx_max_priority = starpu_get_env_number("STARPU_MAX_PRIO");
  846. /* Note that starpu_get_env_number returns -1 in case the variable is
  847. * not defined */
  848. /* Backward compatibility: check the value of STARPU_NCPUS if
  849. * STARPU_NCPU is not set. */
  850. conf->ncpus = starpu_get_env_number("STARPU_NCPU");
  851. if (conf->ncpus == -1)
  852. conf->ncpus = starpu_get_env_number("STARPU_NCPUS");
  853. conf->ncuda = starpu_get_env_number("STARPU_NCUDA");
  854. conf->nopencl = starpu_get_env_number("STARPU_NOPENCL");
  855. conf->nmic = starpu_get_env_number("STARPU_NMIC");
  856. conf->nscc = starpu_get_env_number("STARPU_NSCC");
  857. conf->nmpi_ms = starpu_get_env_number("STARPU_NMPI_MS");
  858. conf->calibrate = starpu_get_env_number("STARPU_CALIBRATE");
  859. conf->bus_calibrate = starpu_get_env_number("STARPU_BUS_CALIBRATE");
  860. conf->mic_sink_program_path = starpu_getenv("STARPU_MIC_PROGRAM_PATH");
  861. if (conf->calibrate == -1)
  862. conf->calibrate = 0;
  863. if (conf->bus_calibrate == -1)
  864. conf->bus_calibrate = 0;
  865. conf->use_explicit_workers_bindid = 0; /* TODO */
  866. conf->use_explicit_workers_cuda_gpuid = 0; /* TODO */
  867. conf->use_explicit_workers_opencl_gpuid = 0; /* TODO */
  868. conf->use_explicit_workers_mic_deviceid = 0; /* TODO */
  869. conf->use_explicit_workers_scc_deviceid = 0; /* TODO */
  870. conf->use_explicit_workers_mpi_ms_deviceid = 0; /* TODO */
  871. conf->single_combined_worker = starpu_get_env_number("STARPU_SINGLE_COMBINED_WORKER");
  872. if (conf->single_combined_worker == -1)
  873. conf->single_combined_worker = 0;
  874. #if defined(STARPU_DISABLE_ASYNCHRONOUS_COPY)
  875. conf->disable_asynchronous_copy = 1;
  876. #else
  877. conf->disable_asynchronous_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_COPY");
  878. if (conf->disable_asynchronous_copy == -1)
  879. conf->disable_asynchronous_copy = 0;
  880. #endif
  881. #if defined(STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY)
  882. conf->disable_asynchronous_cuda_copy = 1;
  883. #else
  884. conf->disable_asynchronous_cuda_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY");
  885. if (conf->disable_asynchronous_cuda_copy == -1)
  886. conf->disable_asynchronous_cuda_copy = 0;
  887. #endif
  888. #if defined(STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY)
  889. conf->disable_asynchronous_opencl_copy = 1;
  890. #else
  891. conf->disable_asynchronous_opencl_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY");
  892. if (conf->disable_asynchronous_opencl_copy == -1)
  893. conf->disable_asynchronous_opencl_copy = 0;
  894. #endif
  895. #if defined(STARPU_DISABLE_ASYNCHRONOUS_MIC_COPY)
  896. conf->disable_asynchronous_mic_copy = 1;
  897. #else
  898. conf->disable_asynchronous_mic_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_MIC_COPY");
  899. if (conf->disable_asynchronous_mic_copy == -1)
  900. conf->disable_asynchronous_mic_copy = 0;
  901. #endif
  902. #if defined(STARPU_DISABLE_ASYNCHRONOUS_MPI_MS_COPY)
  903. conf->disable_asynchronous_mpi_ms_copy = 1;
  904. #else
  905. conf->disable_asynchronous_mpi_ms_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_MPI_MS_COPY");
  906. if(conf->disable_asynchronous_mpi_ms_copy == -1)
  907. conf->disable_asynchronous_mpi_ms_copy = 0;
  908. #endif
  909. /* 64MiB by default */
  910. conf->trace_buffer_size = starpu_get_env_number_default("STARPU_TRACE_BUFFER_SIZE", 64) << 20;
  911. return 0;
  912. }
  913. static void _starpu_conf_set_value_against_environment(char *name, int *value)
  914. {
  915. int number;
  916. number = starpu_get_env_number(name);
  917. if (number != -1)
  918. {
  919. *value = number;
  920. }
  921. }
  922. void _starpu_conf_check_environment(struct starpu_conf *conf)
  923. {
  924. char *sched = starpu_getenv("STARPU_SCHED");
  925. if (sched)
  926. {
  927. conf->sched_policy_name = sched;
  928. }
  929. _starpu_conf_set_value_against_environment("STARPU_NCPUS", &conf->ncpus);
  930. _starpu_conf_set_value_against_environment("STARPU_NCPU", &conf->ncpus);
  931. _starpu_conf_set_value_against_environment("STARPU_NCUDA", &conf->ncuda);
  932. _starpu_conf_set_value_against_environment("STARPU_NOPENCL", &conf->nopencl);
  933. _starpu_conf_set_value_against_environment("STARPU_CALIBRATE", &conf->calibrate);
  934. _starpu_conf_set_value_against_environment("STARPU_BUS_CALIBRATE", &conf->bus_calibrate);
  935. #ifdef STARPU_SIMGRID
  936. if (conf->calibrate == 2)
  937. {
  938. _STARPU_DISP("Warning: History will be cleared due to calibrate or STARPU_CALIBRATE being set to 2. This will prevent simgrid from having task simulation times!");
  939. }
  940. if (conf->bus_calibrate)
  941. {
  942. _STARPU_DISP("Warning: Bus calibration will be cleared due to bus_calibrate or STARPU_BUS_CALIBRATE being set. This will prevent simgrid from having data transfer simulation times!");
  943. }
  944. #endif
  945. _starpu_conf_set_value_against_environment("STARPU_SINGLE_COMBINED_WORKER", &conf->single_combined_worker);
  946. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_COPY", &conf->disable_asynchronous_copy);
  947. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY", &conf->disable_asynchronous_cuda_copy);
  948. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY", &conf->disable_asynchronous_opencl_copy);
  949. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_MIC_COPY", &conf->disable_asynchronous_mic_copy);
  950. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_MPI_MS_COPY", &conf->disable_asynchronous_mpi_ms_copy);
  951. }
  952. struct starpu_tree* starpu_workers_get_tree(void)
  953. {
  954. return _starpu_config.topology.tree;
  955. }
  956. #ifdef STARPU_HAVE_HWLOC
  957. static void _fill_tree(struct starpu_tree *tree, hwloc_obj_t curr_obj, unsigned depth, hwloc_topology_t topology, struct starpu_tree *father)
  958. {
  959. unsigned i;
  960. if (curr_obj->arity == 1)
  961. {
  962. /* Nothing interestin here, skip level */
  963. _fill_tree(tree, curr_obj->children[0], depth+1, topology, father);
  964. return;
  965. }
  966. starpu_tree_insert(tree, curr_obj->logical_index, depth, curr_obj->type == HWLOC_OBJ_PU, curr_obj->arity, father);
  967. starpu_tree_prepare_children(curr_obj->arity, tree);
  968. for(i = 0; i < curr_obj->arity; i++)
  969. {
  970. /* char string[128]; */
  971. /* hwloc_obj_snprintf(string, sizeof(string), topology, curr_obj->children[i], "#", 0); */
  972. /* printf("%*s%s %d is_pu %d \n", 0, "", string, curr_obj->children[i]->logical_index, curr_obj->children[i]->type == HWLOC_OBJ_PU); */
  973. _fill_tree(&tree->nodes[i], curr_obj->children[i], depth+1, topology, tree);
  974. }
  975. }
  976. #endif
  977. static void _starpu_build_tree(void)
  978. {
  979. #ifdef STARPU_HAVE_HWLOC
  980. hwloc_topology_t cpu_topo;
  981. struct starpu_tree *tree;
  982. _STARPU_MALLOC(tree, sizeof(struct starpu_tree));
  983. _starpu_config.topology.tree = tree;
  984. hwloc_topology_init(&cpu_topo);
  985. #if HWLOC_API_VERSION >= 0x20000
  986. hwloc_topology_set_all_types_filter(cpu_topo, HWLOC_TYPE_FILTER_KEEP_STRUCTURE);
  987. #else
  988. hwloc_topology_ignore_all_keep_structure(cpu_topo);
  989. #endif
  990. hwloc_topology_load(cpu_topo);
  991. hwloc_obj_t root = hwloc_get_root_obj(cpu_topo);
  992. /* char string[128]; */
  993. /* hwloc_obj_snprintf(string, sizeof(string), topology, root, "#", 0); */
  994. /* printf("%*s%s %d is_pu = %d \n", 0, "", string, root->logical_index, root->type == HWLOC_OBJ_PU); */
  995. /* level, is_pu, is in the tree (it will be true only after add) */
  996. _fill_tree(tree, root, 0, cpu_topo, NULL);
  997. hwloc_topology_destroy(cpu_topo);
  998. #endif
  999. }
  1000. int starpu_init(struct starpu_conf *user_conf)
  1001. {
  1002. return starpu_initialize(user_conf, NULL, NULL);
  1003. }
  1004. int starpu_initialize(struct starpu_conf *user_conf, int *argc, char ***argv)
  1005. {
  1006. int is_a_sink = 0; /* Always defined. If the MP infrastructure is not
  1007. * used, we cannot be a sink. */
  1008. unsigned worker;
  1009. (void)argc;
  1010. (void)argv;
  1011. /* This initializes _starpu_silent, thus needs to be early */
  1012. _starpu_util_init();
  1013. #ifdef STARPU_SIMGRID
  1014. /* This initializes the simgrid thread library, thus needs to be early */
  1015. _starpu_simgrid_init_early(argc, argv);
  1016. #endif
  1017. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1018. while (initialized == CHANGING)
  1019. /* Wait for the other one changing it */
  1020. STARPU_PTHREAD_COND_WAIT(&init_cond, &init_mutex);
  1021. init_count++;
  1022. if (initialized == INITIALIZED)
  1023. {
  1024. /* He initialized it, don't do it again, and let the others get the mutex */
  1025. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1026. return 0;
  1027. }
  1028. /* initialized == UNINITIALIZED */
  1029. initialized = CHANGING;
  1030. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1031. #ifdef STARPU_USE_MP
  1032. _starpu_set_argc_argv(argc, argv);
  1033. # ifdef STARPU_USE_SCC
  1034. /* In SCC case we look at the rank to know if we are a sink */
  1035. if (_starpu_scc_common_mp_init() && !_starpu_scc_common_is_src_node())
  1036. setenv("STARPU_SINK", "STARPU_SCC", 1);
  1037. # endif
  1038. # ifdef STARPU_USE_MPI_MASTER_SLAVE
  1039. if (_starpu_mpi_common_mp_init() == -ENODEV)
  1040. {
  1041. initialized = UNINITIALIZED;
  1042. return -ENODEV;
  1043. }
  1044. /* In MPI case we look at the rank to know if we are a sink */
  1045. if (!_starpu_mpi_common_is_src_node())
  1046. setenv("STARPU_SINK", "STARPU_MPI_MS", 1);
  1047. # endif
  1048. /* If StarPU was configured to use MP sinks, we have to control the
  1049. * kind on node we are running on : host or sink ? */
  1050. if (starpu_getenv("STARPU_SINK"))
  1051. is_a_sink = 1;
  1052. #endif /* STARPU_USE_MP */
  1053. int ret;
  1054. #ifdef STARPU_OPENMP
  1055. _starpu_omp_dummy_init();
  1056. #endif
  1057. #ifdef STARPU_SIMGRID
  1058. /* Warn when the lots of stacks malloc()-ated by simgrid for transfer
  1059. * processes will take a long time to get initialized */
  1060. char *perturb = starpu_getenv("MALLOC_PERTURB_");
  1061. if (perturb && perturb[0] && atoi(perturb) != 0)
  1062. _STARPU_DISP("Warning: MALLOC_PERTURB_ is set to non-zero, this makes simgrid run very slow\n");
  1063. #else
  1064. #ifdef __GNUC__
  1065. #ifndef __OPTIMIZE__
  1066. _STARPU_DISP("Warning: StarPU was configured with --enable-debug (-O0), and is thus not optimized\n");
  1067. #endif
  1068. #endif
  1069. #ifdef STARPU_SPINLOCK_CHECK
  1070. _STARPU_DISP("Warning: StarPU was configured with --enable-spinlock-check, which slows down a bit\n");
  1071. #endif
  1072. #if 0
  1073. #ifndef STARPU_NO_ASSERT
  1074. _STARPU_DISP("Warning: StarPU was configured without --enable-fast\n");
  1075. #endif
  1076. #endif
  1077. #ifdef STARPU_MEMORY_STATS
  1078. _STARPU_DISP("Warning: StarPU was configured with --enable-memory-stats, which slows down a bit\n");
  1079. #endif
  1080. #ifdef STARPU_VERBOSE
  1081. _STARPU_DISP("Warning: StarPU was configured with --enable-verbose, which slows down a bit\n");
  1082. #endif
  1083. #ifdef STARPU_USE_FXT
  1084. _STARPU_DISP("Warning: StarPU was configured with --with-fxt, which slows down a bit\n");
  1085. #endif
  1086. #ifdef STARPU_PERF_DEBUG
  1087. _STARPU_DISP("Warning: StarPU was configured with --enable-perf-debug, which slows down a bit\n");
  1088. #endif
  1089. #ifdef STARPU_MODEL_DEBUG
  1090. _STARPU_DISP("Warning: StarPU was configured with --enable-model-debug, which slows down a bit\n");
  1091. #endif
  1092. #ifdef __linux__
  1093. {
  1094. struct utsname buf;
  1095. if (uname(&buf) == 0
  1096. && (!strncmp(buf.release, "4.7.", 4)
  1097. || !strncmp(buf.release, "4.8.", 4)))
  1098. _STARPU_DISP("Warning: This system is running a 4.7 or 4.8 kernel. These have a severe scheduling performance regression issue, please upgrade to at least 4.9.\n");
  1099. }
  1100. #endif
  1101. #endif
  1102. if (starpu_getenv("STARPU_ENABLE_STATS"))
  1103. {
  1104. _STARPU_DISP("Warning: STARPU_ENABLE_STATS is enabled, which slows down a bit\n");
  1105. }
  1106. #if defined(_WIN32) && !defined(__CYGWIN__)
  1107. WSADATA wsadata;
  1108. WSAStartup(MAKEWORD(1,0), &wsadata);
  1109. #endif
  1110. srand(2008);
  1111. STARPU_AYU_PREINIT();
  1112. /* store the pointer to the user explicit configuration during the
  1113. * initialization */
  1114. if (user_conf == NULL)
  1115. starpu_conf_init(&_starpu_config.conf);
  1116. else
  1117. {
  1118. if (user_conf->magic != 42)
  1119. {
  1120. _STARPU_DISP("starpu_conf structure needs to be initialized with starpu_conf_init\n");
  1121. return -EINVAL;
  1122. }
  1123. _starpu_config.conf = *user_conf;
  1124. }
  1125. _starpu_conf_check_environment(&_starpu_config.conf);
  1126. /* Make a copy of arrays */
  1127. if (_starpu_config.conf.sched_policy_name)
  1128. _starpu_config.conf.sched_policy_name = strdup(_starpu_config.conf.sched_policy_name);
  1129. if (_starpu_config.conf.mic_sink_program_path)
  1130. _starpu_config.conf.mic_sink_program_path = strdup(_starpu_config.conf.mic_sink_program_path);
  1131. if (_starpu_config.conf.n_cuda_opengl_interoperability)
  1132. {
  1133. size_t size = _starpu_config.conf.n_cuda_opengl_interoperability * sizeof(*_starpu_config.conf.cuda_opengl_interoperability);
  1134. unsigned *copy;
  1135. _STARPU_MALLOC(copy, size);
  1136. memcpy(copy, _starpu_config.conf.cuda_opengl_interoperability, size);
  1137. _starpu_config.conf.cuda_opengl_interoperability = copy;
  1138. }
  1139. if (_starpu_config.conf.n_not_launched_drivers)
  1140. {
  1141. size_t size = _starpu_config.conf.n_not_launched_drivers * sizeof(*_starpu_config.conf.not_launched_drivers);
  1142. struct starpu_driver *copy;
  1143. _STARPU_MALLOC(copy, size);
  1144. memcpy(copy, _starpu_config.conf.not_launched_drivers, size);
  1145. _starpu_config.conf.not_launched_drivers = copy;
  1146. }
  1147. _starpu_sched_init();
  1148. _starpu_job_init();
  1149. _starpu_graph_init();
  1150. _starpu_init_all_sched_ctxs(&_starpu_config);
  1151. _starpu_init_progression_hooks();
  1152. _starpu_init_tags();
  1153. #ifdef STARPU_USE_FXT
  1154. _starpu_fxt_init_profiling(_starpu_config.conf.trace_buffer_size);
  1155. #endif
  1156. _starpu_open_debug_logfile();
  1157. _starpu_data_interface_init();
  1158. _starpu_timing_init();
  1159. _starpu_profiling_init();
  1160. _starpu_load_bus_performance_files();
  1161. /* Depending on whether we are a MP sink or not, we must build the
  1162. * topology with MP nodes or not. */
  1163. ret = _starpu_build_topology(&_starpu_config, is_a_sink);
  1164. /* sink doesn't exit even if no worker discorvered */
  1165. if (ret && !is_a_sink)
  1166. {
  1167. starpu_perfmodel_free_sampling_directories();
  1168. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1169. init_count--;
  1170. _starpu_destroy_machine_config(&_starpu_config);
  1171. #ifdef STARPU_USE_SCC
  1172. if (_starpu_scc_common_is_mp_initialized())
  1173. _starpu_scc_src_mp_deinit();
  1174. #endif
  1175. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1176. if (_starpu_mpi_common_is_mp_initialized())
  1177. _starpu_mpi_common_mp_deinit();
  1178. #endif
  1179. initialized = UNINITIALIZED;
  1180. /* Let somebody else try to do it */
  1181. STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  1182. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1183. return ret;
  1184. }
  1185. _starpu_task_init();
  1186. for (worker = 0; worker < _starpu_config.topology.nworkers; worker++)
  1187. _starpu_worker_init(&_starpu_config.workers[worker], &_starpu_config);
  1188. check_entire_platform = starpu_get_env_number("STARPU_CHECK_ENTIRE_PLATFORM");
  1189. _starpu_config.disable_kernels = starpu_get_env_number("STARPU_DISABLE_KERNELS");
  1190. STARPU_PTHREAD_KEY_CREATE(&_starpu_worker_key, NULL);
  1191. STARPU_PTHREAD_KEY_CREATE(&_starpu_worker_set_key, NULL);
  1192. _starpu_keys_initialized = 1;
  1193. STARPU_WMB();
  1194. _starpu_build_tree();
  1195. if (!is_a_sink)
  1196. {
  1197. struct starpu_sched_policy *selected_policy = _starpu_select_sched_policy(&_starpu_config, _starpu_config.conf.sched_policy_name);
  1198. _starpu_create_sched_ctx(selected_policy, NULL, -1, 1, "init", (_starpu_config.conf.global_sched_ctx_min_priority != -1), _starpu_config.conf.global_sched_ctx_min_priority, (_starpu_config.conf.global_sched_ctx_min_priority != -1), _starpu_config.conf.global_sched_ctx_max_priority, 1, _starpu_config.conf.sched_policy_init, NULL, 0, NULL, 0);
  1199. }
  1200. _starpu_initialize_registered_performance_models();
  1201. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  1202. _starpu_cuda_init();
  1203. #endif
  1204. #ifdef STARPU_SIMGRID
  1205. _starpu_simgrid_init();
  1206. #endif
  1207. /* Launch "basic" workers (ie. non-combined workers) */
  1208. if (!is_a_sink)
  1209. _starpu_launch_drivers(&_starpu_config);
  1210. /* Allocate swap, if any */
  1211. _starpu_swap_init();
  1212. _starpu_watchdog_init();
  1213. _starpu_profiling_start();
  1214. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1215. initialized = INITIALIZED;
  1216. /* Tell everybody that we initialized */
  1217. STARPU_PTHREAD_COND_BROADCAST(&init_cond);
  1218. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1219. _STARPU_DEBUG("Initialisation finished\n");
  1220. #ifdef STARPU_USE_MP
  1221. /* Finally, if we are a MP sink, we never leave this function. Else,
  1222. * we enter an infinite event loop which listen for MP commands from
  1223. * the source. */
  1224. if (is_a_sink)
  1225. {
  1226. _starpu_sink_common_worker();
  1227. /* We should normally never leave the loop as we don't want to
  1228. * really initialize STARPU */
  1229. STARPU_ASSERT(0);
  1230. }
  1231. #endif
  1232. return 0;
  1233. }
  1234. /*
  1235. * Handle runtime termination
  1236. */
  1237. static void _starpu_terminate_workers(struct _starpu_machine_config *pconfig)
  1238. {
  1239. int status = 0;
  1240. unsigned workerid;
  1241. unsigned n;
  1242. starpu_wake_all_blocked_workers();
  1243. for (workerid = 0; workerid < pconfig->topology.nworkers; workerid++)
  1244. {
  1245. _STARPU_DEBUG("wait for worker %u\n", workerid);
  1246. struct _starpu_worker_set *set = pconfig->workers[workerid].set;
  1247. struct _starpu_worker *worker = &pconfig->workers[workerid];
  1248. /* in case StarPU termination code is called from a callback,
  1249. * we have to check if starpu_pthread_self() is the worker itself */
  1250. if (set && set->nworkers > 0)
  1251. {
  1252. if (set->started)
  1253. {
  1254. if (!starpu_pthread_equal(starpu_pthread_self(), set->worker_thread))
  1255. status = starpu_pthread_join(set->worker_thread, NULL);
  1256. if (status)
  1257. {
  1258. #ifdef STARPU_VERBOSE
  1259. _STARPU_DEBUG("starpu_pthread_join -> %d\n", status);
  1260. #endif
  1261. }
  1262. set->started = 0;
  1263. }
  1264. }
  1265. else
  1266. {
  1267. if (!worker->run_by_starpu)
  1268. goto out;
  1269. if (!starpu_pthread_equal(starpu_pthread_self(), worker->worker_thread))
  1270. status = starpu_pthread_join(worker->worker_thread, NULL);
  1271. if (status)
  1272. {
  1273. #ifdef STARPU_VERBOSE
  1274. _STARPU_DEBUG("starpu_pthread_join -> %d\n", status);
  1275. #endif
  1276. }
  1277. }
  1278. out:
  1279. STARPU_ASSERT(starpu_task_list_empty(&worker->local_tasks));
  1280. for (n = 0; n < worker->local_ordered_tasks_size; n++)
  1281. STARPU_ASSERT(worker->local_ordered_tasks[n] == NULL);
  1282. _starpu_sched_ctx_list_delete(&worker->sched_ctx_list);
  1283. free(worker->local_ordered_tasks);
  1284. STARPU_ASSERT(_starpu_ctx_change_list_empty(&worker->ctx_change_list));
  1285. }
  1286. }
  1287. /* Condition variable and mutex used to pause/resume. */
  1288. static starpu_pthread_cond_t pause_cond = STARPU_PTHREAD_COND_INITIALIZER;
  1289. static starpu_pthread_mutex_t pause_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  1290. void _starpu_may_pause(void)
  1291. {
  1292. /* pause_depth is just protected by a memory barrier */
  1293. STARPU_RMB();
  1294. if (STARPU_UNLIKELY(_starpu_config.pause_depth > 0))
  1295. {
  1296. STARPU_PTHREAD_MUTEX_LOCK(&pause_mutex);
  1297. if (_starpu_config.pause_depth > 0)
  1298. {
  1299. STARPU_PTHREAD_COND_WAIT(&pause_cond, &pause_mutex);
  1300. }
  1301. STARPU_PTHREAD_MUTEX_UNLOCK(&pause_mutex);
  1302. }
  1303. }
  1304. void starpu_pause()
  1305. {
  1306. STARPU_HG_DISABLE_CHECKING(_starpu_config.pause_depth);
  1307. _starpu_config.pause_depth += 1;
  1308. }
  1309. void starpu_resume()
  1310. {
  1311. STARPU_PTHREAD_MUTEX_LOCK(&pause_mutex);
  1312. _starpu_config.pause_depth -= 1;
  1313. if (!_starpu_config.pause_depth)
  1314. {
  1315. STARPU_PTHREAD_COND_BROADCAST(&pause_cond);
  1316. }
  1317. STARPU_PTHREAD_MUTEX_UNLOCK(&pause_mutex);
  1318. }
  1319. unsigned _starpu_worker_can_block(unsigned memnode STARPU_ATTRIBUTE_UNUSED, struct _starpu_worker *worker STARPU_ATTRIBUTE_UNUSED)
  1320. {
  1321. #ifdef STARPU_NON_BLOCKING_DRIVERS
  1322. return 0;
  1323. #else
  1324. unsigned can_block = 1;
  1325. struct starpu_driver driver;
  1326. driver.type = worker->arch;
  1327. switch (driver.type)
  1328. {
  1329. case STARPU_CPU_WORKER:
  1330. driver.id.cpu_id = worker->devid;
  1331. break;
  1332. case STARPU_CUDA_WORKER:
  1333. driver.id.cuda_id = worker->devid;
  1334. break;
  1335. #ifdef STARPU_USE_OPENCL
  1336. case STARPU_OPENCL_WORKER:
  1337. starpu_opencl_get_device(worker->devid, &driver.id.opencl_id);
  1338. break;
  1339. #endif
  1340. default:
  1341. goto always_launch;
  1342. }
  1343. if (!_starpu_may_launch_driver(&_starpu_config.conf, &driver))
  1344. return 0;
  1345. always_launch:
  1346. #ifndef STARPU_SIMGRID
  1347. if (!_starpu_check_that_no_data_request_exists(memnode))
  1348. can_block = 0;
  1349. #endif
  1350. if (!_starpu_machine_is_running())
  1351. can_block = 0;
  1352. if (!_starpu_execute_registered_progression_hooks())
  1353. can_block = 0;
  1354. return can_block;
  1355. #endif
  1356. }
  1357. static void _starpu_kill_all_workers(struct _starpu_machine_config *pconfig)
  1358. {
  1359. /* set the flag which will tell workers to stop */
  1360. ANNOTATE_HAPPENS_AFTER(&_starpu_config.running);
  1361. pconfig->running = 0;
  1362. /* running is just protected by a memory barrier */
  1363. ANNOTATE_HAPPENS_BEFORE(&_starpu_config.running);
  1364. STARPU_WMB();
  1365. starpu_wake_all_blocked_workers();
  1366. }
  1367. void starpu_display_stats()
  1368. {
  1369. starpu_profiling_bus_helper_display_summary();
  1370. starpu_profiling_worker_helper_display_summary();
  1371. }
  1372. void starpu_shutdown(void)
  1373. {
  1374. unsigned worker;
  1375. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1376. init_count--;
  1377. STARPU_ASSERT_MSG(init_count >= 0, "Number of calls to starpu_shutdown() can not be higher than the number of calls to starpu_init()\n");
  1378. if (init_count)
  1379. {
  1380. _STARPU_DEBUG("Still somebody needing StarPU, don't deinitialize\n");
  1381. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1382. return;
  1383. }
  1384. /* We're last */
  1385. initialized = CHANGING;
  1386. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1387. /* If the workers are frozen, no progress can be made. */
  1388. STARPU_ASSERT(_starpu_config.pause_depth <= 0);
  1389. starpu_task_wait_for_no_ready();
  1390. /* tell all workers to shutdown */
  1391. _starpu_kill_all_workers(&_starpu_config);
  1392. unsigned i;
  1393. unsigned nb_numa_nodes = starpu_memory_nodes_get_numa_count();
  1394. for (i=0; i<nb_numa_nodes; i++)
  1395. {
  1396. _starpu_free_all_automatically_allocated_buffers(i);
  1397. }
  1398. {
  1399. int stats = starpu_get_env_number("STARPU_STATS");
  1400. if (stats != 0)
  1401. {
  1402. _starpu_display_msi_stats(stderr);
  1403. _starpu_display_alloc_cache_stats(stderr);
  1404. }
  1405. }
  1406. starpu_profiling_bus_helper_display_summary();
  1407. starpu_profiling_worker_helper_display_summary();
  1408. _starpu_deinitialize_registered_performance_models();
  1409. _starpu_watchdog_shutdown();
  1410. /* wait for their termination */
  1411. _starpu_terminate_workers(&_starpu_config);
  1412. {
  1413. int stats = starpu_get_env_number("STARPU_MEMORY_STATS");
  1414. if (stats != 0)
  1415. {
  1416. // Display statistics on data which have not been unregistered
  1417. starpu_data_display_memory_stats();
  1418. }
  1419. }
  1420. _starpu_delete_all_sched_ctxs();
  1421. _starpu_sched_component_workers_destroy();
  1422. _starpu_top_shutdown();
  1423. for (worker = 0; worker < _starpu_config.topology.nworkers; worker++)
  1424. _starpu_worker_deinit(&_starpu_config.workers[worker]);
  1425. _starpu_profiling_terminate();
  1426. _starpu_disk_unregister();
  1427. #ifdef STARPU_HAVE_HWLOC
  1428. starpu_tree_free(_starpu_config.topology.tree);
  1429. free(_starpu_config.topology.tree);
  1430. #endif
  1431. _starpu_destroy_topology(&_starpu_config);
  1432. #ifdef STARPU_USE_FXT
  1433. _starpu_stop_fxt_profiling();
  1434. #endif
  1435. _starpu_data_interface_shutdown();
  1436. _starpu_job_fini();
  1437. /* Drop all remaining tags */
  1438. _starpu_tag_clear();
  1439. #ifdef STARPU_OPENMP
  1440. _starpu_omp_dummy_shutdown();
  1441. #endif
  1442. _starpu_close_debug_logfile();
  1443. _starpu_keys_initialized = 0;
  1444. STARPU_PTHREAD_KEY_DELETE(_starpu_worker_key);
  1445. STARPU_PTHREAD_KEY_DELETE(_starpu_worker_set_key);
  1446. _starpu_task_deinit();
  1447. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1448. initialized = UNINITIALIZED;
  1449. /* Let someone else that wants to initialize it again do it */
  1450. STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  1451. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1452. /* Clear memory */
  1453. free((char*) _starpu_config.conf.sched_policy_name);
  1454. free(_starpu_config.conf.mic_sink_program_path);
  1455. if (_starpu_config.conf.n_cuda_opengl_interoperability)
  1456. free(_starpu_config.conf.cuda_opengl_interoperability);
  1457. if (_starpu_config.conf.n_not_launched_drivers)
  1458. free(_starpu_config.conf.not_launched_drivers);
  1459. STARPU_AYU_FINISH();
  1460. #ifdef STARPU_USE_SCC
  1461. if (_starpu_scc_common_is_mp_initialized())
  1462. _starpu_scc_src_mp_deinit();
  1463. #endif
  1464. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1465. if (_starpu_mpi_common_is_mp_initialized())
  1466. _starpu_mpi_common_mp_deinit();
  1467. #endif
  1468. _starpu_print_idle_time();
  1469. _STARPU_DEBUG("Shutdown finished\n");
  1470. #ifdef STARPU_SIMGRID
  1471. /* This finalizes the simgrid thread library, thus needs to be late */
  1472. _starpu_simgrid_deinit();
  1473. #endif
  1474. }
  1475. #undef starpu_worker_get_count
  1476. unsigned starpu_worker_get_count(void)
  1477. {
  1478. return _starpu_config.topology.nworkers;
  1479. }
  1480. unsigned starpu_worker_is_blocked_in_parallel(int workerid)
  1481. {
  1482. int relax_own_observation_state = 0;
  1483. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1484. STARPU_ASSERT(worker != NULL);
  1485. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  1486. struct _starpu_worker *cur_worker = NULL;
  1487. int cur_workerid = starpu_worker_get_id();
  1488. if (workerid != cur_workerid)
  1489. {
  1490. /* in order to observe the 'blocked' state of a worker from
  1491. * another worker, we must avoid race conditions between
  1492. * 'blocked' state changes and state observations. This is the
  1493. * purpose of this 'if' block. */
  1494. cur_worker = cur_workerid<starpu_worker_get_count()?_starpu_get_worker_struct(cur_workerid):NULL;
  1495. relax_own_observation_state = (cur_worker != NULL) && (cur_worker->state_relax_refcnt == 0);
  1496. if (relax_own_observation_state && !worker->state_relax_refcnt)
  1497. {
  1498. /* moreover, when a worker (cur_worker != NULL)
  1499. * observes another worker, we need to take special
  1500. * care to avoid live locks, thus the observing worker
  1501. * must enter the relaxed state (if not relaxed
  1502. * already) before doing the observation in mutual
  1503. * exclusion */
  1504. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  1505. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&cur_worker->sched_mutex);
  1506. cur_worker->state_relax_refcnt = 1;
  1507. STARPU_PTHREAD_COND_BROADCAST(&cur_worker->sched_cond);
  1508. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&cur_worker->sched_mutex);
  1509. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  1510. }
  1511. /* the observer waits for a safe window to observe the state,
  1512. * and also waits for any pending blocking state change
  1513. * requests to be processed, in order to not obtain an
  1514. * ephemeral information */
  1515. while (!worker->state_relax_refcnt
  1516. || worker->state_block_in_parallel_req
  1517. || worker->state_unblock_in_parallel_req)
  1518. {
  1519. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  1520. }
  1521. }
  1522. unsigned ret = _starpu_config.workers[workerid].state_blocked_in_parallel;
  1523. /* once a worker state has been observed, the worker is 'tainted' for the next one full sched_op,
  1524. * to avoid changing the observed worker state - on which the observer
  1525. * made a scheduling decision - after the fact. */
  1526. worker->state_blocked_in_parallel_observed = 1;
  1527. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  1528. if (relax_own_observation_state)
  1529. {
  1530. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&cur_worker->sched_mutex);
  1531. cur_worker->state_relax_refcnt = 0;
  1532. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&cur_worker->sched_mutex);
  1533. }
  1534. return ret;
  1535. }
  1536. unsigned starpu_worker_is_slave_somewhere(int workerid)
  1537. {
  1538. _starpu_worker_lock(workerid);
  1539. unsigned ret = _starpu_config.workers[workerid].is_slave_somewhere;
  1540. _starpu_worker_unlock(workerid);
  1541. return ret;
  1542. }
  1543. int starpu_worker_get_count_by_type(enum starpu_worker_archtype type)
  1544. {
  1545. switch (type)
  1546. {
  1547. case STARPU_CPU_WORKER:
  1548. return _starpu_config.topology.ncpus;
  1549. case STARPU_CUDA_WORKER:
  1550. return _starpu_config.topology.ncudagpus * _starpu_config.topology.nworkerpercuda;
  1551. case STARPU_OPENCL_WORKER:
  1552. return _starpu_config.topology.nopenclgpus;
  1553. case STARPU_MIC_WORKER:
  1554. return _starpu_config.topology.nmicdevices;
  1555. case STARPU_SCC_WORKER:
  1556. return _starpu_config.topology.nsccdevices;
  1557. case STARPU_MPI_MS_WORKER:
  1558. return _starpu_config.topology.nmpidevices;
  1559. case STARPU_ANY_WORKER:
  1560. return _starpu_config.topology.ncpus+
  1561. _starpu_config.topology.ncudagpus * _starpu_config.topology.nworkerpercuda+
  1562. _starpu_config.topology.nopenclgpus+
  1563. _starpu_config.topology.nmicdevices+
  1564. _starpu_config.topology.nsccdevices+
  1565. _starpu_config.topology.nmpidevices;
  1566. default:
  1567. return -EINVAL;
  1568. }
  1569. }
  1570. unsigned starpu_combined_worker_get_count(void)
  1571. {
  1572. return _starpu_config.topology.ncombinedworkers;
  1573. }
  1574. unsigned starpu_cpu_worker_get_count(void)
  1575. {
  1576. return _starpu_config.topology.ncpus;
  1577. }
  1578. unsigned starpu_cuda_worker_get_count(void)
  1579. {
  1580. return _starpu_config.topology.ncudagpus * _starpu_config.topology.nworkerpercuda;
  1581. }
  1582. unsigned starpu_opencl_worker_get_count(void)
  1583. {
  1584. return _starpu_config.topology.nopenclgpus;
  1585. }
  1586. int starpu_asynchronous_copy_disabled(void)
  1587. {
  1588. return _starpu_config.conf.disable_asynchronous_copy;
  1589. }
  1590. int starpu_asynchronous_cuda_copy_disabled(void)
  1591. {
  1592. return _starpu_config.conf.disable_asynchronous_cuda_copy;
  1593. }
  1594. int starpu_asynchronous_opencl_copy_disabled(void)
  1595. {
  1596. return _starpu_config.conf.disable_asynchronous_opencl_copy;
  1597. }
  1598. int starpu_asynchronous_mic_copy_disabled(void)
  1599. {
  1600. return _starpu_config.conf.disable_asynchronous_mic_copy;
  1601. }
  1602. int starpu_asynchronous_mpi_ms_copy_disabled(void)
  1603. {
  1604. return _starpu_config.conf.disable_asynchronous_mpi_ms_copy;
  1605. }
  1606. unsigned starpu_mic_worker_get_count(void)
  1607. {
  1608. int i = 0, count = 0;
  1609. for (i = 0; i < STARPU_MAXMICDEVS; i++)
  1610. count += _starpu_config.topology.nmiccores[i];
  1611. return count;
  1612. }
  1613. unsigned starpu_scc_worker_get_count(void)
  1614. {
  1615. return _starpu_config.topology.nsccdevices;
  1616. }
  1617. unsigned starpu_mpi_ms_worker_get_count(void)
  1618. {
  1619. return _starpu_config.topology.nmpidevices;
  1620. }
  1621. /* When analyzing performance, it is useful to see what is the processing unit
  1622. * that actually performed the task. This function returns the id of the
  1623. * processing unit actually executing it, therefore it makes no sense to use it
  1624. * within the callbacks of SPU functions for instance. If called by some thread
  1625. * that is not controlled by StarPU, starpu_worker_get_id returns -1. */
  1626. #undef starpu_worker_get_id
  1627. int starpu_worker_get_id(void)
  1628. {
  1629. struct _starpu_worker * worker;
  1630. worker = _starpu_get_local_worker_key();
  1631. if (worker)
  1632. {
  1633. return worker->workerid;
  1634. }
  1635. else
  1636. {
  1637. /* there is no worker associated to that thread, perhaps it is
  1638. * a thread from the application or this is some SPU worker */
  1639. return -1;
  1640. }
  1641. }
  1642. #define starpu_worker_get_id _starpu_worker_get_id
  1643. #undef _starpu_worker_get_id_check
  1644. unsigned _starpu_worker_get_id_check(const char *f, int l)
  1645. {
  1646. (void) f;
  1647. (void) l;
  1648. int id = _starpu_worker_get_id();
  1649. STARPU_ASSERT_MSG(id>=0, "%s:%d Cannot be called from outside a worker\n", f, l);
  1650. return id;
  1651. }
  1652. int starpu_combined_worker_get_id(void)
  1653. {
  1654. struct _starpu_worker *worker;
  1655. worker = _starpu_get_local_worker_key();
  1656. if (worker)
  1657. {
  1658. return worker->combined_workerid;
  1659. }
  1660. else
  1661. {
  1662. /* there is no worker associated to that thread, perhaps it is
  1663. * a thread from the application or this is some SPU worker */
  1664. return -1;
  1665. }
  1666. }
  1667. int starpu_combined_worker_get_size(void)
  1668. {
  1669. struct _starpu_worker *worker;
  1670. worker = _starpu_get_local_worker_key();
  1671. if (worker)
  1672. {
  1673. return worker->worker_size;
  1674. }
  1675. else
  1676. {
  1677. /* there is no worker associated to that thread, perhaps it is
  1678. * a thread from the application or this is some SPU worker */
  1679. return -1;
  1680. }
  1681. }
  1682. int starpu_combined_worker_get_rank(void)
  1683. {
  1684. struct _starpu_worker *worker;
  1685. worker = _starpu_get_local_worker_key();
  1686. if (worker)
  1687. {
  1688. return worker->current_rank;
  1689. }
  1690. else
  1691. {
  1692. /* there is no worker associated to that thread, perhaps it is
  1693. * a thread from the application or this is some SPU worker */
  1694. return -1;
  1695. }
  1696. }
  1697. int starpu_worker_get_subworkerid(int id)
  1698. {
  1699. return _starpu_config.workers[id].subworkerid;
  1700. }
  1701. int starpu_worker_get_devid(int id)
  1702. {
  1703. return _starpu_config.workers[id].devid;
  1704. }
  1705. unsigned starpu_worker_is_combined_worker(int id)
  1706. {
  1707. return id >= (int)_starpu_config.topology.nworkers;
  1708. }
  1709. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id)
  1710. {
  1711. unsigned basic_worker_count = starpu_worker_get_count();
  1712. //_STARPU_DEBUG("basic_worker_count:%d\n",basic_worker_count);
  1713. STARPU_ASSERT(id >= basic_worker_count);
  1714. return &_starpu_config.combined_workers[id - basic_worker_count];
  1715. }
  1716. enum starpu_worker_archtype starpu_worker_get_type(int id)
  1717. {
  1718. return _starpu_config.workers[id].arch;
  1719. }
  1720. unsigned starpu_worker_get_ids_by_type(enum starpu_worker_archtype type, int *workerids, unsigned maxsize)
  1721. {
  1722. unsigned nworkers = starpu_worker_get_count();
  1723. unsigned cnt = 0;
  1724. unsigned id;
  1725. for (id = 0; id < nworkers; id++)
  1726. {
  1727. if (type == STARPU_ANY_WORKER || starpu_worker_get_type(id) == type)
  1728. {
  1729. /* Perhaps the array is too small ? */
  1730. if (cnt >= maxsize)
  1731. return -ERANGE;
  1732. workerids[cnt++] = id;
  1733. }
  1734. }
  1735. return cnt;
  1736. }
  1737. int starpu_worker_get_by_type(enum starpu_worker_archtype type, int num)
  1738. {
  1739. unsigned nworkers = starpu_worker_get_count();
  1740. int cnt = 0;
  1741. unsigned id;
  1742. for (id = 0; id < nworkers; id++)
  1743. {
  1744. if (type == STARPU_ANY_WORKER || starpu_worker_get_type(id) == type)
  1745. {
  1746. if (num == cnt)
  1747. return id;
  1748. cnt++;
  1749. }
  1750. }
  1751. /* Not found */
  1752. return -1;
  1753. }
  1754. int starpu_worker_get_by_devid(enum starpu_worker_archtype type, int devid)
  1755. {
  1756. unsigned nworkers = starpu_worker_get_count();
  1757. unsigned id;
  1758. for (id = 0; id < nworkers; id++)
  1759. if (starpu_worker_get_type(id) == type && starpu_worker_get_devid(id) == devid)
  1760. return id;
  1761. /* Not found */
  1762. return -1;
  1763. }
  1764. int starpu_worker_get_devids(enum starpu_worker_archtype type, int *devids, int num)
  1765. {
  1766. int cnt = 0;
  1767. unsigned nworkers = starpu_worker_get_count();
  1768. int workerids[nworkers];
  1769. unsigned ndevice_workers = starpu_worker_get_ids_by_type(type, workerids, nworkers);
  1770. unsigned ndevids = 0;
  1771. if(ndevice_workers > 0)
  1772. {
  1773. unsigned id, devid;
  1774. int curr_devid = -1;
  1775. unsigned found = 0;
  1776. for(id = 0; id < ndevice_workers; id++)
  1777. {
  1778. curr_devid = _starpu_config.workers[workerids[id]].devid;
  1779. for(devid = 0; devid < ndevids; devid++)
  1780. {
  1781. if(curr_devid == devids[devid])
  1782. {
  1783. found = 1;
  1784. break;
  1785. }
  1786. }
  1787. if(!found)
  1788. {
  1789. devids[ndevids++] = curr_devid;
  1790. cnt++;
  1791. }
  1792. else
  1793. found = 0;
  1794. if(cnt == num)
  1795. break;
  1796. }
  1797. }
  1798. return ndevids;
  1799. }
  1800. void starpu_worker_get_name(int id, char *dst, size_t maxlen)
  1801. {
  1802. char *name = _starpu_config.workers[id].name;
  1803. snprintf(dst, maxlen, "%s", name);
  1804. }
  1805. int starpu_worker_get_bindid(int workerid)
  1806. {
  1807. return _starpu_config.workers[workerid].bindid;
  1808. }
  1809. int starpu_bindid_get_workerids(int bindid, int **workerids)
  1810. {
  1811. if (bindid >= (int) _starpu_config.nbindid)
  1812. return 0;
  1813. *workerids = _starpu_config.bindid_workers[bindid].workerids;
  1814. return _starpu_config.bindid_workers[bindid].nworkers;
  1815. }
  1816. int starpu_worker_get_stream_workerids(unsigned devid, int *workerids, enum starpu_worker_archtype type)
  1817. {
  1818. unsigned nworkers = starpu_worker_get_count();
  1819. int nw = 0;
  1820. unsigned id;
  1821. for (id = 0; id < nworkers; id++)
  1822. {
  1823. if (_starpu_config.workers[id].devid == devid &&
  1824. (type == STARPU_ANY_WORKER || _starpu_config.workers[id].arch == type))
  1825. workerids[nw++] = id;
  1826. }
  1827. return nw;
  1828. }
  1829. void starpu_worker_get_sched_condition(int workerid, starpu_pthread_mutex_t **sched_mutex, starpu_pthread_cond_t **sched_cond)
  1830. {
  1831. *sched_cond = &_starpu_config.workers[workerid].sched_cond;
  1832. *sched_mutex = &_starpu_config.workers[workerid].sched_mutex;
  1833. }
  1834. static int starpu_wakeup_worker_locked(int workerid, starpu_pthread_cond_t *sched_cond, starpu_pthread_mutex_t *mutex STARPU_ATTRIBUTE_UNUSED)
  1835. {
  1836. #ifdef STARPU_SIMGRID
  1837. starpu_pthread_queue_broadcast(&_starpu_simgrid_task_queue[workerid]);
  1838. #endif
  1839. if (_starpu_config.workers[workerid].status == STATUS_SCHEDULING)
  1840. {
  1841. _starpu_config.workers[workerid].state_keep_awake = 1;
  1842. return 1;
  1843. }
  1844. else if (_starpu_config.workers[workerid].status == STATUS_SLEEPING)
  1845. {
  1846. _starpu_config.workers[workerid].state_keep_awake = 1;
  1847. /* cond_broadcast is required over cond_signal since
  1848. * the condition is share for multiple purpose */
  1849. STARPU_PTHREAD_COND_BROADCAST(sched_cond);
  1850. return 1;
  1851. }
  1852. return 0;
  1853. }
  1854. static int starpu_wakeup_worker_no_relax(int workerid, starpu_pthread_cond_t *sched_cond, starpu_pthread_mutex_t *sched_mutex)
  1855. {
  1856. int success;
  1857. STARPU_PTHREAD_MUTEX_LOCK_SCHED(sched_mutex);
  1858. success = starpu_wakeup_worker_locked(workerid, sched_cond, sched_mutex);
  1859. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(sched_mutex);
  1860. return success;
  1861. }
  1862. int starpu_wake_worker_locked(int workerid)
  1863. {
  1864. starpu_pthread_mutex_t *sched_mutex;
  1865. starpu_pthread_cond_t *sched_cond;
  1866. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  1867. return starpu_wakeup_worker_locked(workerid, sched_cond, sched_mutex);
  1868. }
  1869. int starpu_wake_worker_no_relax(int workerid)
  1870. {
  1871. starpu_pthread_mutex_t *sched_mutex;
  1872. starpu_pthread_cond_t *sched_cond;
  1873. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  1874. return starpu_wakeup_worker_no_relax(workerid, sched_cond, sched_mutex);
  1875. }
  1876. int starpu_worker_get_nids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize)
  1877. {
  1878. unsigned nworkers = starpu_worker_get_count();
  1879. int cnt = 0;
  1880. unsigned id;
  1881. for (id = 0; id < nworkers; id++)
  1882. {
  1883. if (type == STARPU_ANY_WORKER || starpu_worker_get_type(id) == type)
  1884. {
  1885. /* Perhaps the array is too small ? */
  1886. if (cnt >= maxsize)
  1887. return cnt;
  1888. workerids[cnt++] = id;
  1889. }
  1890. }
  1891. return cnt;
  1892. }
  1893. int starpu_worker_get_nids_ctx_free_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize)
  1894. {
  1895. unsigned nworkers = starpu_worker_get_count();
  1896. int cnt = 0;
  1897. unsigned id;
  1898. for (id = 0; id < nworkers; id++)
  1899. {
  1900. if (type == STARPU_ANY_WORKER || starpu_worker_get_type(id) == type)
  1901. {
  1902. /* Perhaps the array is too small ? */
  1903. if (cnt >= maxsize)
  1904. return cnt;
  1905. unsigned found = 0;
  1906. int s;
  1907. for(s = 1; s < STARPU_NMAX_SCHED_CTXS; s++)
  1908. {
  1909. if(_starpu_config.sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  1910. {
  1911. struct starpu_worker_collection *workers = _starpu_config.sched_ctxs[s].workers;
  1912. struct starpu_sched_ctx_iterator it;
  1913. workers->init_iterator(workers, &it);
  1914. while(workers->has_next(workers, &it))
  1915. {
  1916. unsigned worker = workers->get_next(workers, &it);
  1917. if(worker == id)
  1918. {
  1919. found = 1;
  1920. break;
  1921. }
  1922. }
  1923. if(found) break;
  1924. }
  1925. }
  1926. if(!found)
  1927. workerids[cnt++] = id;
  1928. }
  1929. }
  1930. return cnt;
  1931. }
  1932. void starpu_get_version(int *major, int *minor, int *release)
  1933. {
  1934. *major = STARPU_MAJOR_VERSION;
  1935. *minor = STARPU_MINOR_VERSION;
  1936. *release = STARPU_RELEASE_VERSION;
  1937. }
  1938. unsigned starpu_worker_get_sched_ctx_list(int workerid, unsigned **sched_ctxs)
  1939. {
  1940. unsigned s = 0;
  1941. unsigned nsched_ctxs = _starpu_worker_get_nsched_ctxs(workerid);
  1942. _STARPU_MALLOC(*sched_ctxs, nsched_ctxs*sizeof(unsigned));
  1943. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1944. struct _starpu_sched_ctx_elt *e = NULL;
  1945. struct _starpu_sched_ctx_list_iterator list_it;
  1946. _starpu_sched_ctx_list_iterator_init(worker->sched_ctx_list, &list_it);
  1947. while (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  1948. {
  1949. e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  1950. (*sched_ctxs)[s++] = e->sched_ctx;
  1951. }
  1952. return nsched_ctxs;
  1953. }
  1954. char *starpu_worker_get_type_as_string(enum starpu_worker_archtype type)
  1955. {
  1956. if (type == STARPU_CPU_WORKER) return "STARPU_CPU_WORKER";
  1957. if (type == STARPU_CUDA_WORKER) return "STARPU_CUDA_WORKER";
  1958. if (type == STARPU_OPENCL_WORKER) return "STARPU_OPENCL_WORKER";
  1959. if (type == STARPU_MIC_WORKER) return "STARPU_MIC_WORKER";
  1960. if (type == STARPU_MPI_MS_WORKER) return "STARPU_MPI_MS_WORKER";
  1961. if (type == STARPU_SCC_WORKER) return "STARPU_SCC_WORKER";
  1962. if (type == STARPU_ANY_WORKER) return "STARPU_ANY_WORKER";
  1963. return "STARPU_unknown_WORKER";
  1964. }
  1965. void _starpu_worker_set_stream_ctx(unsigned workerid, struct _starpu_sched_ctx *sched_ctx)
  1966. {
  1967. STARPU_ASSERT(workerid < starpu_worker_get_count());
  1968. struct _starpu_worker *w = _starpu_get_worker_struct(workerid);
  1969. w->stream_ctx = sched_ctx;
  1970. }
  1971. struct _starpu_sched_ctx* _starpu_worker_get_ctx_stream(unsigned stream_workerid)
  1972. {
  1973. if (stream_workerid >= starpu_worker_get_count())
  1974. return NULL;
  1975. struct _starpu_worker *w = _starpu_get_worker_struct(stream_workerid);
  1976. return w->stream_ctx;
  1977. }
  1978. unsigned starpu_worker_get_sched_ctx_id_stream(unsigned stream_workerid)
  1979. {
  1980. if (stream_workerid >= starpu_worker_get_count())
  1981. return STARPU_NMAX_SCHED_CTXS;
  1982. struct _starpu_worker *w = _starpu_get_worker_struct(stream_workerid);
  1983. return w->stream_ctx != NULL ? w->stream_ctx->id : STARPU_NMAX_SCHED_CTXS;
  1984. }
  1985. void starpu_worker_display_names(FILE *output, enum starpu_worker_archtype type)
  1986. {
  1987. int nworkers = starpu_worker_get_count_by_type(type);
  1988. if (nworkers <= 0)
  1989. {
  1990. fprintf(output, "No %s worker\n", starpu_worker_get_type_as_string(type));
  1991. }
  1992. else
  1993. {
  1994. int i, ids[nworkers];
  1995. starpu_worker_get_ids_by_type(type, ids, nworkers);
  1996. fprintf(output, "%d %s worker%s:\n", nworkers, starpu_worker_get_type_as_string(type), nworkers==1?"":"s");
  1997. for(i = 0; i < nworkers; i++)
  1998. {
  1999. char name[256];
  2000. starpu_worker_get_name(ids[i], name, 256);
  2001. fprintf(output, "\t%s\n", name);
  2002. }
  2003. }
  2004. }
  2005. void _starpu_worker_refuse_task(struct _starpu_worker *worker, struct starpu_task *task)
  2006. {
  2007. if (worker->pipeline_length || worker->arch == STARPU_OPENCL_WORKER)
  2008. {
  2009. int j;
  2010. for (j = 0; j < worker->ntasks; j++)
  2011. {
  2012. const int j_mod = (j+worker->first_task)%STARPU_MAX_PIPELINE;
  2013. if (task == worker->current_tasks[j_mod])
  2014. {
  2015. worker->current_tasks[j_mod] = NULL;
  2016. if (j == 0)
  2017. {
  2018. worker->first_task = (worker->first_task + 1) % STARPU_MAX_PIPELINE;
  2019. worker->current_task = NULL;
  2020. _starpu_set_current_task(NULL);
  2021. }
  2022. break;
  2023. }
  2024. }
  2025. STARPU_ASSERT(j<worker->ntasks);
  2026. }
  2027. else
  2028. {
  2029. worker->current_task = NULL;
  2030. _starpu_set_current_task(NULL);
  2031. }
  2032. worker->ntasks--;
  2033. int res = _starpu_push_task_to_workers(task);
  2034. STARPU_ASSERT_MSG(res == 0, "_starpu_push_task_to_workers() unexpectedly returned = %d\n", res);
  2035. }
  2036. int starpu_worker_sched_op_pending(void)
  2037. {
  2038. return _starpu_worker_sched_op_pending();
  2039. }
  2040. void starpu_worker_relax_on(void)
  2041. {
  2042. _starpu_worker_relax_on();
  2043. }
  2044. void starpu_worker_relax_off(void)
  2045. {
  2046. starpu_worker_relax_off();
  2047. }
  2048. int starpu_worker_get_relax_state(void)
  2049. {
  2050. return _starpu_worker_get_relax_state();
  2051. }
  2052. void starpu_worker_lock(int workerid)
  2053. {
  2054. _starpu_worker_lock(workerid);
  2055. }
  2056. int starpu_worker_trylock(int workerid)
  2057. {
  2058. return _starpu_worker_trylock(workerid);
  2059. }
  2060. void starpu_worker_unlock(int workerid)
  2061. {
  2062. _starpu_worker_unlock(workerid);
  2063. }
  2064. void starpu_worker_lock_self(void)
  2065. {
  2066. _starpu_worker_lock_self();
  2067. }
  2068. void starpu_worker_unlock_self(void)
  2069. {
  2070. _starpu_worker_unlock_self();
  2071. }
  2072. int starpu_wake_worker_relax(int workerid)
  2073. {
  2074. return _starpu_wake_worker_relax(workerid);
  2075. }