workers.c 73 KB

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