workers.c 74 KB

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