workers.c 79 KB

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