workers.c 81 KB

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