workers.c 75 KB

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