workers.c 73 KB

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