workers.c 83 KB

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