workers.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2010, 2011 Institut National de Recherche en Informatique et Automatique
  6. * Copyright (C) 2011 Télécom-SudParis
  7. * Copyright (C) 2011-2012 INRIA
  8. *
  9. * StarPU is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * StarPU is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  19. */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <common/config.h>
  23. #include <common/utils.h>
  24. #include <core/progress_hook.h>
  25. #include <core/workers.h>
  26. #include <core/debug.h>
  27. #include <core/disk.h>
  28. #include <core/task.h>
  29. #include <datawizard/malloc.h>
  30. #include <profiling/profiling.h>
  31. #include <starpu_task_list.h>
  32. #include <sched_policies/sched_component.h>
  33. #include <drivers/mp_common/sink_common.h>
  34. #include <drivers/scc/driver_scc_common.h>
  35. #include <drivers/cpu/driver_cpu.h>
  36. #include <drivers/cuda/driver_cuda.h>
  37. #include <drivers/opencl/driver_opencl.h>
  38. #ifdef STARPU_SIMGRID
  39. #include <msg/msg.h>
  40. #include <core/simgrid.h>
  41. #endif
  42. #if defined(_WIN32) && !defined(__CYGWIN__)
  43. #include <windows.h>
  44. #endif
  45. /* acquire/release semantic for concurrent initialization/de-initialization */
  46. static starpu_pthread_mutex_t init_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  47. static starpu_pthread_cond_t init_cond = STARPU_PTHREAD_COND_INITIALIZER;
  48. static int init_count = 0;
  49. static enum { UNINITIALIZED, CHANGING, INITIALIZED } initialized = UNINITIALIZED;
  50. static starpu_pthread_key_t worker_key;
  51. static struct _starpu_machine_config config;
  52. /* Pointers to argc and argv
  53. */
  54. static int *my_argc = 0;
  55. static char ***my_argv = NULL;
  56. /* Initialize value of static argc and argv, called when the process begins
  57. */
  58. void _starpu_set_argc_argv(int *argc_param, char ***argv_param)
  59. {
  60. my_argc = argc_param;
  61. my_argv = argv_param;
  62. }
  63. int *_starpu_get_argc()
  64. {
  65. return my_argc;
  66. }
  67. char ***_starpu_get_argv()
  68. {
  69. return my_argv;
  70. }
  71. int _starpu_is_initialized(void)
  72. {
  73. return initialized == INITIALIZED;
  74. }
  75. struct _starpu_machine_config *_starpu_get_machine_config(void)
  76. {
  77. return &config;
  78. }
  79. /* Makes sure that at least one of the workers of type <arch> can execute
  80. * <task>, for at least one of its implementations. */
  81. static uint32_t _starpu_worker_exists_and_can_execute(struct starpu_task *task,
  82. enum starpu_worker_archtype arch)
  83. {
  84. int i;
  85. _starpu_codelet_check_deprecated_fields(task->cl);
  86. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  87. struct starpu_worker_collection *workers = sched_ctx->workers;
  88. struct starpu_sched_ctx_iterator it;
  89. workers->init_iterator(workers, &it);
  90. while(workers->has_next(workers, &it))
  91. {
  92. i = workers->get_next(workers, &it);
  93. if (starpu_worker_get_type(i) != arch)
  94. continue;
  95. unsigned impl;
  96. for (impl = 0; impl < STARPU_MAXIMPLEMENTATIONS; impl++)
  97. {
  98. /* We could call task->cl->can_execute(i, task, impl)
  99. here, it would definitely work. It is probably
  100. cheaper to check whether it is necessary in order to
  101. avoid a useless function call, though. */
  102. unsigned test_implementation = 0;
  103. switch (arch)
  104. {
  105. case STARPU_CPU_WORKER:
  106. if (task->cl->cpu_funcs[impl] != NULL)
  107. test_implementation = 1;
  108. break;
  109. case STARPU_CUDA_WORKER:
  110. if (task->cl->cuda_funcs[impl] != NULL)
  111. test_implementation = 1;
  112. break;
  113. case STARPU_OPENCL_WORKER:
  114. if (task->cl->opencl_funcs[impl] != NULL)
  115. test_implementation = 1;
  116. break;
  117. case STARPU_MIC_WORKER:
  118. if (task->cl->cpu_funcs_name[impl] != NULL || task->cl->mic_funcs[impl] != NULL)
  119. test_implementation = 1;
  120. break;
  121. case STARPU_SCC_WORKER:
  122. if (task->cl->cpu_funcs_name[impl] != NULL || task->cl->scc_funcs[impl] != NULL)
  123. test_implementation = 1;
  124. break;
  125. default:
  126. STARPU_ABORT();
  127. }
  128. if (!test_implementation)
  129. continue;
  130. if (task->cl->can_execute)
  131. return task->cl->can_execute(i, task, impl);
  132. if(test_implementation)
  133. return 1;
  134. }
  135. }
  136. return 0;
  137. }
  138. /* in case a task is submitted, we may check whether there exists a worker
  139. that may execute the task or not */
  140. uint32_t _starpu_worker_exists(struct starpu_task *task)
  141. {
  142. _starpu_codelet_check_deprecated_fields(task->cl);
  143. /* if the task belongs to the init context we can
  144. check out all the worker mask of the machine
  145. if not we should iterate on the workers of the ctx
  146. and verify if it exists a worker able to exec the task */
  147. if(task->sched_ctx == 0)
  148. {
  149. if (!(task->cl->where & config.worker_mask))
  150. return 0;
  151. if (!task->cl->can_execute)
  152. return 1;
  153. }
  154. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  155. if ((task->cl->where & STARPU_CPU) &&
  156. _starpu_worker_exists_and_can_execute(task, STARPU_CPU_WORKER))
  157. return 1;
  158. #endif
  159. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  160. if ((task->cl->where & STARPU_CUDA) &&
  161. _starpu_worker_exists_and_can_execute(task, STARPU_CUDA_WORKER))
  162. return 1;
  163. #endif
  164. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  165. if ((task->cl->where & STARPU_OPENCL) &&
  166. _starpu_worker_exists_and_can_execute(task, STARPU_OPENCL_WORKER))
  167. return 1;
  168. #endif
  169. #ifdef STARPU_USE_MIC
  170. if ((task->cl->where & STARPU_MIC) &&
  171. _starpu_worker_exists_and_can_execute(task, STARPU_MIC_WORKER))
  172. return 1;
  173. #endif
  174. #ifdef STARPU_USE_SCC
  175. if ((task->cl->where & STARPU_SCC) &&
  176. _starpu_worker_exists_and_can_execute(task, STARPU_SCC_WORKER))
  177. return 1;
  178. #endif
  179. return 0;
  180. }
  181. uint32_t _starpu_can_submit_cuda_task(void)
  182. {
  183. return (STARPU_CUDA & config.worker_mask);
  184. }
  185. uint32_t _starpu_can_submit_cpu_task(void)
  186. {
  187. return (STARPU_CPU & config.worker_mask);
  188. }
  189. uint32_t _starpu_can_submit_opencl_task(void)
  190. {
  191. return (STARPU_OPENCL & config.worker_mask);
  192. }
  193. uint32_t _starpu_can_submit_scc_task(void)
  194. {
  195. return (STARPU_SCC & config.worker_mask);
  196. }
  197. static inline int _starpu_can_use_nth_implementation(enum starpu_worker_archtype arch, struct starpu_codelet *cl, unsigned nimpl)
  198. {
  199. switch(arch)
  200. {
  201. case STARPU_ANY_WORKER:
  202. {
  203. int cpu_func_enabled=1, cuda_func_enabled=1, opencl_func_enabled=1;
  204. /* TODO: MIC/SCC */
  205. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  206. starpu_cpu_func_t cpu_func = _starpu_task_get_cpu_nth_implementation(cl, nimpl);
  207. cpu_func_enabled = cpu_func != NULL && starpu_cpu_worker_get_count();
  208. #endif
  209. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  210. starpu_cuda_func_t cuda_func = _starpu_task_get_cuda_nth_implementation(cl, nimpl);
  211. cuda_func_enabled = cuda_func != NULL && starpu_cuda_worker_get_count();
  212. #endif
  213. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  214. starpu_opencl_func_t opencl_func = _starpu_task_get_opencl_nth_implementation(cl, nimpl);
  215. opencl_func_enabled = opencl_func != NULL && starpu_opencl_worker_get_count();
  216. #endif
  217. return (cpu_func_enabled && cuda_func_enabled && opencl_func_enabled);
  218. }
  219. case STARPU_CPU_WORKER:
  220. {
  221. starpu_cpu_func_t func = _starpu_task_get_cpu_nth_implementation(cl, nimpl);
  222. return func != NULL;
  223. }
  224. case STARPU_CUDA_WORKER:
  225. {
  226. starpu_cuda_func_t func = _starpu_task_get_cuda_nth_implementation(cl, nimpl);
  227. return func != NULL;
  228. }
  229. case STARPU_OPENCL_WORKER:
  230. {
  231. starpu_opencl_func_t func = _starpu_task_get_opencl_nth_implementation(cl, nimpl);
  232. return func != NULL;
  233. }
  234. case STARPU_MIC_WORKER:
  235. {
  236. starpu_mic_func_t func = _starpu_task_get_mic_nth_implementation(cl, nimpl);
  237. char *func_name = _starpu_task_get_cpu_name_nth_implementation(cl, nimpl);
  238. return func != NULL || func_name != NULL;
  239. }
  240. case STARPU_SCC_WORKER:
  241. {
  242. starpu_scc_func_t func = _starpu_task_get_scc_nth_implementation(cl, nimpl);
  243. char *func_name = _starpu_task_get_cpu_name_nth_implementation(cl, nimpl);
  244. return func != NULL || func_name != NULL;
  245. }
  246. default:
  247. STARPU_ASSERT_MSG(0, "Unknown arch type %d", arch);
  248. }
  249. return 0;
  250. }
  251. int starpu_worker_can_execute_task(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  252. {
  253. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  254. /* if the task can't be parallel don't submit it to a ctx */
  255. unsigned child_sched_ctx = starpu_sched_ctx_worker_is_master_for_child_ctx(workerid, sched_ctx->id);
  256. if(child_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  257. if(!task->possibly_parallel) return 0;
  258. /* if the worker is blocked in a parallel ctx don't submit tasks on it */
  259. if(sched_ctx->parallel_sect[workerid] ) return 0;
  260. /* TODO: check that the task operand sizes will fit on that device */
  261. return (task->cl->where & config.workers[workerid].worker_mask) &&
  262. _starpu_can_use_nth_implementation(config.workers[workerid].arch, task->cl, nimpl) &&
  263. (!task->cl->can_execute || task->cl->can_execute(workerid, task, nimpl));
  264. }
  265. int starpu_worker_can_execute_task_impl(unsigned workerid, struct starpu_task *task, unsigned *impl_mask)
  266. {
  267. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  268. unsigned mask;
  269. int i;
  270. enum starpu_worker_archtype arch;
  271. struct starpu_codelet *cl;
  272. if(sched_ctx->parallel_sect[workerid]) return 0;
  273. /* TODO: check that the task operand sizes will fit on that device */
  274. cl = task->cl;
  275. if (!(cl->where & config.workers[workerid].worker_mask)) return 0;
  276. mask = 0;
  277. arch = config.workers[workerid].arch;
  278. if (!task->cl->can_execute)
  279. {
  280. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  281. if (_starpu_can_use_nth_implementation(arch, cl, i)) {
  282. mask |= 1U << i;
  283. if (!impl_mask)
  284. break;
  285. }
  286. } else {
  287. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  288. if (_starpu_can_use_nth_implementation(arch, cl, i)
  289. && (!task->cl->can_execute || task->cl->can_execute(workerid, task, i))) {
  290. mask |= 1U << i;
  291. if (!impl_mask)
  292. break;
  293. }
  294. }
  295. if (impl_mask)
  296. *impl_mask = mask;
  297. return mask != 0;
  298. }
  299. int starpu_worker_can_execute_task_first_impl(unsigned workerid, struct starpu_task *task, unsigned *nimpl)
  300. {
  301. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  302. int i;
  303. enum starpu_worker_archtype arch;
  304. struct starpu_codelet *cl;
  305. if(sched_ctx->parallel_sect[workerid]) return 0;
  306. /* TODO: check that the task operand sizes will fit on that device */
  307. cl = task->cl;
  308. if (!(cl->where & config.workers[workerid].worker_mask)) return 0;
  309. arch = config.workers[workerid].arch;
  310. if (!task->cl->can_execute)
  311. {
  312. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  313. if (_starpu_can_use_nth_implementation(arch, cl, i)) {
  314. if (nimpl)
  315. *nimpl = i;
  316. return 1;
  317. }
  318. } else {
  319. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  320. if (_starpu_can_use_nth_implementation(arch, cl, i)
  321. && (!task->cl->can_execute || task->cl->can_execute(workerid, task, i))) {
  322. if (nimpl)
  323. *nimpl = i;
  324. return 1;
  325. }
  326. }
  327. return 0;
  328. }
  329. int starpu_combined_worker_can_execute_task(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  330. {
  331. /* TODO: check that the task operand sizes will fit on that device */
  332. struct starpu_codelet *cl = task->cl;
  333. unsigned nworkers = config.topology.nworkers;
  334. /* Is this a parallel worker ? */
  335. if (workerid < nworkers)
  336. {
  337. return !!((task->cl->where & config.workers[workerid].worker_mask) &&
  338. _starpu_can_use_nth_implementation(config.workers[workerid].arch, task->cl, nimpl) &&
  339. (!task->cl->can_execute || task->cl->can_execute(workerid, task, nimpl)));
  340. }
  341. else
  342. {
  343. if ((cl->type == STARPU_SPMD)
  344. #ifdef STARPU_HAVE_HWLOC
  345. || (cl->type == STARPU_FORKJOIN)
  346. #else
  347. #ifdef __GLIBC__
  348. || (cl->type == STARPU_FORKJOIN)
  349. #endif
  350. #endif
  351. )
  352. {
  353. /* TODO we should add other types of constraints */
  354. /* Is the worker larger than requested ? */
  355. int worker_size = (int)config.combined_workers[workerid - nworkers].worker_size;
  356. int worker0 = config.combined_workers[workerid - nworkers].combined_workerid[0];
  357. return !!((worker_size <= task->cl->max_parallelism) &&
  358. _starpu_can_use_nth_implementation(config.workers[worker0].arch, task->cl, nimpl) &&
  359. (!task->cl->can_execute || task->cl->can_execute(workerid, task, nimpl)));
  360. }
  361. else
  362. {
  363. /* We have a sequential task but a parallel worker */
  364. return 0;
  365. }
  366. }
  367. }
  368. /*
  369. * Runtime initialization methods
  370. */
  371. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  372. static struct _starpu_worker_set cuda_worker_set[STARPU_MAXCUDADEVS];
  373. #endif
  374. #ifdef STARPU_USE_MIC
  375. static struct _starpu_worker_set mic_worker_set[STARPU_MAXMICDEVS];
  376. #endif
  377. static void _starpu_init_worker_queue(struct _starpu_worker *workerarg)
  378. {
  379. starpu_pthread_cond_t *cond = &workerarg->sched_cond;
  380. starpu_pthread_mutex_t *mutex = &workerarg->sched_mutex;
  381. unsigned memory_node = workerarg->memory_node;
  382. _starpu_memory_node_register_condition(cond, mutex, memory_node);
  383. }
  384. /*
  385. * Returns 0 if the given driver is one of the drivers that must be launched by
  386. * the application itself, and not by StarPU, 1 otherwise.
  387. */
  388. static unsigned _starpu_may_launch_driver(struct starpu_conf *conf,
  389. struct starpu_driver *d)
  390. {
  391. if (conf->n_not_launched_drivers == 0 ||
  392. conf->not_launched_drivers == NULL)
  393. return 1;
  394. /* Is <d> in conf->not_launched_drivers ? */
  395. unsigned i;
  396. for (i = 0; i < conf->n_not_launched_drivers; i++)
  397. {
  398. if (d->type != conf->not_launched_drivers[i].type)
  399. continue;
  400. switch (d->type)
  401. {
  402. case STARPU_CPU_WORKER:
  403. if (d->id.cpu_id == conf->not_launched_drivers[i].id.cpu_id)
  404. return 0;
  405. case STARPU_CUDA_WORKER:
  406. if (d->id.cuda_id == conf->not_launched_drivers[i].id.cuda_id)
  407. return 0;
  408. break;
  409. #ifdef STARPU_USE_OPENCL
  410. case STARPU_OPENCL_WORKER:
  411. if (d->id.opencl_id == conf->not_launched_drivers[i].id.opencl_id)
  412. return 0;
  413. break;
  414. #endif
  415. default:
  416. STARPU_ABORT();
  417. }
  418. }
  419. return 1;
  420. }
  421. #ifdef STARPU_PERF_DEBUG
  422. struct itimerval prof_itimer;
  423. #endif
  424. static void _starpu_worker_init(struct _starpu_worker *workerarg, struct _starpu_machine_config *pconfig)
  425. {
  426. workerarg->config = pconfig;
  427. STARPU_PTHREAD_MUTEX_INIT(&workerarg->mutex, NULL);
  428. /* arch initialized by topology.c */
  429. /* worker_mask initialized by topology.c */
  430. /* perf_arch initialized by topology.c */
  431. /* worker_thread initialized by _starpu_launch_drivers */
  432. /* devid initialized by topology.c */
  433. /* subworkerid initialized by topology.c */
  434. /* bindid initialized by topology.c */
  435. /* workerid initialized by topology.c */
  436. workerarg->combined_workerid = workerarg->workerid;
  437. workerarg->current_rank = 0;
  438. workerarg->worker_size = 1;
  439. STARPU_PTHREAD_COND_INIT(&workerarg->started_cond, NULL);
  440. STARPU_PTHREAD_COND_INIT(&workerarg->ready_cond, NULL);
  441. /* memory_node initialized by topology.c */
  442. STARPU_PTHREAD_COND_INIT(&workerarg->sched_cond, NULL);
  443. STARPU_PTHREAD_MUTEX_INIT(&workerarg->sched_mutex, NULL);
  444. starpu_task_list_init(&workerarg->local_tasks);
  445. workerarg->local_ordered_tasks = NULL;
  446. workerarg->local_ordered_tasks_size = 0;
  447. workerarg->current_ordered_task = 0;
  448. workerarg->current_ordered_task_order = 1;
  449. workerarg->current_task = NULL;
  450. workerarg->first_task = 0;
  451. workerarg->ntasks = 0;
  452. workerarg->pipeline_length = 0;
  453. workerarg->pipeline_stuck = 0;
  454. workerarg->set = NULL;
  455. /* if some codelet's termination cannot be handled directly :
  456. * for instance in the Gordon driver, Gordon tasks' callbacks
  457. * may be executed by another thread than that of the Gordon
  458. * driver so that we cannot call the push_codelet_output method
  459. * directly */
  460. workerarg->terminated_jobs = _starpu_job_list_new();
  461. workerarg->worker_is_running = 0;
  462. workerarg->worker_is_initialized = 0;
  463. workerarg->status = STATUS_INITIALIZING;
  464. /* name initialized by driver */
  465. /* short_name initialized by driver */
  466. workerarg->run_by_starpu = 1;
  467. workerarg->sched_ctx_list = NULL;
  468. workerarg->tmp_sched_ctx = -1;
  469. workerarg->nsched_ctxs = 0;
  470. _starpu_barrier_counter_init(&workerarg->tasks_barrier, 0);
  471. workerarg->has_prev_init = 0;
  472. int ctx;
  473. for(ctx = 0; ctx < STARPU_NMAX_SCHED_CTXS; ctx++)
  474. workerarg->removed_from_ctx[ctx] = 0;
  475. workerarg->spinning_backoff = 1;
  476. for(ctx = 0; ctx < STARPU_NMAX_SCHED_CTXS; ctx++)
  477. {
  478. workerarg->shares_tasks_lists[ctx] = 0;
  479. workerarg->poped_in_ctx[ctx] = 0;
  480. }
  481. workerarg->reverse_phase[0] = 0;
  482. workerarg->reverse_phase[1] = 0;
  483. workerarg->pop_ctx_priority = 1;
  484. workerarg->sched_mutex_locked = 0;
  485. workerarg->slave = 0;
  486. /* cpu_set/hwloc_cpu_set initialized in topology.c */
  487. }
  488. void _starpu_worker_start(struct _starpu_worker *worker, unsigned fut_key)
  489. {
  490. (void) fut_key;
  491. int devid = worker->devid;
  492. (void) devid;
  493. #if defined(STARPU_PERF_DEBUG) && !defined(STARPU_SIMGRID)
  494. setitimer(ITIMER_PROF, &prof_itimer, NULL);
  495. #endif
  496. #ifdef STARPU_USE_FXT
  497. _starpu_fxt_register_thread(worker->bindid);
  498. unsigned memnode = worker->memory_node;
  499. _STARPU_TRACE_WORKER_INIT_START(fut_key, worker->workerid, devid, memnode);
  500. #endif
  501. _starpu_bind_thread_on_cpu(worker->config, worker->bindid);
  502. _STARPU_DEBUG("worker %p %d for dev %d is ready on logical cpu %d\n", worker, worker->workerid, devid, worker->bindid);
  503. #ifdef STARPU_HAVE_HWLOC
  504. _STARPU_DEBUG("worker %p %d cpuset start at %d\n", worker, worker->workerid, hwloc_bitmap_first(worker->hwloc_cpu_set));
  505. #endif
  506. _starpu_memory_node_set_local_key(&worker->memory_node);
  507. _starpu_set_local_worker_key(worker);
  508. STARPU_PTHREAD_MUTEX_LOCK(&worker->mutex);
  509. worker->worker_is_running = 1;
  510. STARPU_PTHREAD_COND_SIGNAL(&worker->started_cond);
  511. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->mutex);
  512. }
  513. static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
  514. {
  515. pconfig->running = 1;
  516. pconfig->pause_depth = 0;
  517. pconfig->submitting = 1;
  518. STARPU_HG_DISABLE_CHECKING(pconfig->watchdog_ok);
  519. unsigned nworkers = pconfig->topology.nworkers;
  520. /* Launch workers asynchronously */
  521. unsigned worker, i;
  522. #if defined(STARPU_PERF_DEBUG) && !defined(STARPU_SIMGRID)
  523. /* Get itimer of the main thread, to set it for the worker threads */
  524. getitimer(ITIMER_PROF, &prof_itimer);
  525. #endif
  526. #ifdef HAVE_AYUDAME_H
  527. if (AYU_event) AYU_event(AYU_INIT, 0, NULL);
  528. #endif
  529. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  530. for (i = 0; i < sizeof(cuda_worker_set)/sizeof(cuda_worker_set[0]); i++)
  531. cuda_worker_set[i].workers = NULL;
  532. #endif
  533. #ifdef STARPU_USE_MIC
  534. for (i = 0; i < sizeof(mic_worker_set)/sizeof(mic_worker_set[0]); i++)
  535. mic_worker_set[i].workers = NULL;
  536. #endif
  537. for (worker = 0; worker < nworkers; worker++)
  538. {
  539. struct _starpu_worker *workerarg = &pconfig->workers[worker];
  540. #if defined(STARPU_USE_MIC) || defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  541. unsigned devid = workerarg->devid;
  542. #endif
  543. _STARPU_DEBUG("initialising worker %u/%u\n", worker, nworkers);
  544. _starpu_init_worker_queue(workerarg);
  545. struct starpu_driver driver;
  546. driver.type = workerarg->arch;
  547. switch (workerarg->arch)
  548. {
  549. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  550. case STARPU_CPU_WORKER:
  551. driver.id.cpu_id = workerarg->devid;
  552. if (_starpu_may_launch_driver(pconfig->conf, &driver))
  553. {
  554. STARPU_PTHREAD_CREATE_ON(
  555. workerarg->name,
  556. &workerarg->worker_thread,
  557. NULL,
  558. _starpu_cpu_worker,
  559. workerarg,
  560. _starpu_simgrid_get_host_by_worker(workerarg));
  561. #ifdef STARPU_USE_FXT
  562. /* In tracing mode, make sure the
  563. * thread is really started before
  564. * starting another one, to make sure
  565. * they appear in order in the trace.
  566. */
  567. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  568. while (!workerarg->worker_is_running)
  569. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  570. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  571. #endif
  572. }
  573. else
  574. {
  575. workerarg->run_by_starpu = 0;
  576. }
  577. break;
  578. #endif
  579. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  580. case STARPU_CUDA_WORKER:
  581. driver.id.cuda_id = workerarg->devid;
  582. workerarg->set = &cuda_worker_set[devid];
  583. /* We spawn only one thread per CUDA device,
  584. * which will control all CUDA workers of this
  585. * device. (by using a worker set). */
  586. if (cuda_worker_set[devid].workers)
  587. break;
  588. cuda_worker_set[devid].nworkers = starpu_get_env_number_default("STARPU_NWORKER_PER_CUDA", 1);
  589. #ifndef STARPU_NON_BLOCKING_DRIVERS
  590. if (cuda_worker_set[devid].nworkers > 1)
  591. {
  592. _STARPU_DISP("Warning: reducing STARPU_NWORKER_PER_CUDA to 1 because blocking drivers are enabled\n");
  593. cuda_worker_set[devid].nworkers = 1;
  594. }
  595. #endif
  596. cuda_worker_set[devid].workers = workerarg;
  597. cuda_worker_set[devid].set_is_initialized = 0;
  598. if (!_starpu_may_launch_driver(pconfig->conf, &driver))
  599. {
  600. workerarg->run_by_starpu = 0;
  601. break;
  602. }
  603. STARPU_PTHREAD_CREATE_ON(
  604. workerarg->name,
  605. &cuda_worker_set[devid].worker_thread,
  606. NULL,
  607. _starpu_cuda_worker,
  608. &cuda_worker_set[devid],
  609. _starpu_simgrid_get_host_by_worker(workerarg));
  610. #ifdef STARPU_USE_FXT
  611. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  612. while (!workerarg->worker_is_running)
  613. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  614. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  615. #endif
  616. STARPU_PTHREAD_MUTEX_LOCK(&cuda_worker_set[devid].mutex);
  617. while (!cuda_worker_set[devid].set_is_initialized)
  618. STARPU_PTHREAD_COND_WAIT(&cuda_worker_set[devid].ready_cond,
  619. &cuda_worker_set[devid].mutex);
  620. STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_worker_set[devid].mutex);
  621. cuda_worker_set[devid].started = 1;
  622. break;
  623. #endif
  624. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  625. case STARPU_OPENCL_WORKER:
  626. #ifndef STARPU_SIMGRID
  627. starpu_opencl_get_device(workerarg->devid, &driver.id.opencl_id);
  628. if (!_starpu_may_launch_driver(pconfig->conf, &driver))
  629. {
  630. workerarg->run_by_starpu = 0;
  631. break;
  632. }
  633. #endif
  634. STARPU_PTHREAD_CREATE_ON(
  635. workerarg->name,
  636. &workerarg->worker_thread,
  637. NULL,
  638. _starpu_opencl_worker,
  639. workerarg,
  640. _starpu_simgrid_get_host_by_worker(workerarg));
  641. #ifdef STARPU_USE_FXT
  642. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  643. while (!workerarg->worker_is_running)
  644. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  645. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  646. #endif
  647. break;
  648. #endif
  649. #ifdef STARPU_USE_MIC
  650. case STARPU_MIC_WORKER:
  651. workerarg->set = &mic_worker_set[devid];
  652. /* We spawn only one thread
  653. * per MIC device, which will control all MIC
  654. * workers of this device. (by using a worker set). */
  655. if (mic_worker_set[devid].workers)
  656. break;
  657. mic_worker_set[devid].nworkers = pconfig->topology.nmiccores[devid];
  658. /* We assume all MIC workers of a given MIC
  659. * device are contiguous so that we can
  660. * address them with the first one only. */
  661. mic_worker_set[devid].workers = workerarg;
  662. mic_worker_set[devid].set_is_initialized = 0;
  663. STARPU_PTHREAD_CREATE_ON(
  664. workerarg->name,
  665. &mic_worker_set[devid].worker_thread,
  666. NULL,
  667. _starpu_mic_src_worker,
  668. &mic_worker_set[devid],
  669. _starpu_simgrid_get_host_by_worker(workerarg));
  670. #ifdef STARPU_USE_FXT
  671. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  672. while (!workerarg->worker_is_running)
  673. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  674. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  675. #endif
  676. STARPU_PTHREAD_MUTEX_LOCK(&mic_worker_set[devid].mutex);
  677. while (!mic_worker_set[devid].set_is_initialized)
  678. STARPU_PTHREAD_COND_WAIT(&mic_worker_set[devid].ready_cond,
  679. &mic_worker_set[devid].mutex);
  680. STARPU_PTHREAD_MUTEX_UNLOCK(&mic_worker_set[devid].mutex);
  681. mic_worker_set[devid].started = 1;
  682. break;
  683. #endif /* STARPU_USE_MIC */
  684. #ifdef STARPU_USE_SCC
  685. case STARPU_SCC_WORKER:
  686. workerarg->worker_is_initialized = 0;
  687. STARPU_PTHREAD_CREATE_ON(
  688. workerarg->name,
  689. &workerarg->worker_thread,
  690. NULL,
  691. _starpu_scc_src_worker,
  692. workerarg,
  693. _starpu_simgrid_get_host_by_worker(workerarg));
  694. #ifdef STARPU_USE_FXT
  695. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  696. while (!workerarg->worker_is_running)
  697. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  698. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  699. #endif
  700. break;
  701. #endif
  702. default:
  703. STARPU_ABORT();
  704. }
  705. }
  706. for (worker = 0; worker < nworkers; worker++)
  707. {
  708. struct _starpu_worker *workerarg = &pconfig->workers[worker];
  709. struct starpu_driver driver;
  710. driver.type = workerarg->arch;
  711. switch (workerarg->arch)
  712. {
  713. case STARPU_CPU_WORKER:
  714. driver.id.cpu_id = workerarg->devid;
  715. if (!_starpu_may_launch_driver(pconfig->conf, &driver))
  716. break;
  717. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  718. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  719. while (!workerarg->worker_is_initialized)
  720. STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  721. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  722. break;
  723. case STARPU_CUDA_WORKER:
  724. /* Already waited above */
  725. break;
  726. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  727. case STARPU_OPENCL_WORKER:
  728. #ifndef STARPU_SIMGRID
  729. starpu_opencl_get_device(workerarg->devid, &driver.id.opencl_id);
  730. if (!_starpu_may_launch_driver(pconfig->conf, &driver))
  731. break;
  732. #endif
  733. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  734. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  735. while (!workerarg->worker_is_initialized)
  736. STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  737. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  738. break;
  739. #endif
  740. case STARPU_MIC_WORKER:
  741. /* Already waited above */
  742. break;
  743. case STARPU_SCC_WORKER:
  744. /* TODO: implement may_launch? */
  745. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  746. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  747. while (!workerarg->worker_is_initialized)
  748. STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  749. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  750. break;
  751. default:
  752. STARPU_ABORT();
  753. }
  754. }
  755. _STARPU_DEBUG("finished launching drivers\n");
  756. }
  757. void _starpu_set_local_worker_key(struct _starpu_worker *worker)
  758. {
  759. STARPU_PTHREAD_SETSPECIFIC(worker_key, worker);
  760. }
  761. struct _starpu_worker *_starpu_get_local_worker_key(void)
  762. {
  763. return (struct _starpu_worker *) STARPU_PTHREAD_GETSPECIFIC(worker_key);
  764. }
  765. /* Initialize the starpu_conf with default values */
  766. int starpu_conf_init(struct starpu_conf *conf)
  767. {
  768. if (!conf)
  769. return -EINVAL;
  770. memset(conf, 0, sizeof(*conf));
  771. conf->magic = 42;
  772. conf->sched_policy_name = getenv("STARPU_SCHED");
  773. conf->sched_policy = NULL;
  774. /* Note that starpu_get_env_number returns -1 in case the variable is
  775. * not defined */
  776. /* Backward compatibility: check the value of STARPU_NCPUS if
  777. * STARPU_NCPU is not set. */
  778. conf->ncpus = starpu_get_env_number("STARPU_NCPU");
  779. if (conf->ncpus == -1)
  780. conf->ncpus = starpu_get_env_number("STARPU_NCPUS");
  781. conf->ncuda = starpu_get_env_number("STARPU_NCUDA");
  782. conf->nopencl = starpu_get_env_number("STARPU_NOPENCL");
  783. conf->nmic = starpu_get_env_number("STARPU_NMIC");
  784. conf->nscc = starpu_get_env_number("STARPU_NSCC");
  785. conf->calibrate = starpu_get_env_number("STARPU_CALIBRATE");
  786. conf->bus_calibrate = starpu_get_env_number("STARPU_BUS_CALIBRATE");
  787. conf->mic_sink_program_path = getenv("STARPU_MIC_PROGRAM_PATH");
  788. if (conf->calibrate == -1)
  789. conf->calibrate = 0;
  790. if (conf->bus_calibrate == -1)
  791. conf->bus_calibrate = 0;
  792. conf->use_explicit_workers_bindid = 0; /* TODO */
  793. conf->use_explicit_workers_cuda_gpuid = 0; /* TODO */
  794. conf->use_explicit_workers_opencl_gpuid = 0; /* TODO */
  795. conf->use_explicit_workers_mic_deviceid = 0; /* TODO */
  796. conf->use_explicit_workers_scc_deviceid = 0; /* TODO */
  797. conf->single_combined_worker = starpu_get_env_number("STARPU_SINGLE_COMBINED_WORKER");
  798. if (conf->single_combined_worker == -1)
  799. conf->single_combined_worker = 0;
  800. #if defined(STARPU_DISABLE_ASYNCHRONOUS_COPY)
  801. conf->disable_asynchronous_copy = 1;
  802. #else
  803. conf->disable_asynchronous_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_COPY");
  804. if (conf->disable_asynchronous_copy == -1)
  805. conf->disable_asynchronous_copy = 0;
  806. #endif
  807. #if defined(STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY)
  808. conf->disable_asynchronous_cuda_copy = 1;
  809. #else
  810. conf->disable_asynchronous_cuda_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY");
  811. if (conf->disable_asynchronous_cuda_copy == -1)
  812. conf->disable_asynchronous_cuda_copy = 0;
  813. #endif
  814. #if defined(STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY)
  815. conf->disable_asynchronous_opencl_copy = 1;
  816. #else
  817. conf->disable_asynchronous_opencl_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY");
  818. if (conf->disable_asynchronous_opencl_copy == -1)
  819. conf->disable_asynchronous_opencl_copy = 0;
  820. #endif
  821. #if defined(STARPU_DISABLE_ASYNCHRONOUS_MIC_COPY)
  822. conf->disable_asynchronous_mic_copy = 1;
  823. #else
  824. conf->disable_asynchronous_mic_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_MIC_COPY");
  825. if (conf->disable_asynchronous_mic_copy == -1)
  826. conf->disable_asynchronous_mic_copy = 0;
  827. #endif
  828. /* 64MiB by default */
  829. conf->trace_buffer_size = starpu_get_env_number_default("STARPU_TRACE_BUFFER_SIZE", 64) << 20;
  830. return 0;
  831. }
  832. static void _starpu_conf_set_value_against_environment(char *name, int *value)
  833. {
  834. int number;
  835. number = starpu_get_env_number(name);
  836. if (number != -1)
  837. {
  838. *value = number;
  839. }
  840. }
  841. void _starpu_conf_check_environment(struct starpu_conf *conf)
  842. {
  843. char *sched = getenv("STARPU_SCHED");
  844. if (sched)
  845. {
  846. conf->sched_policy_name = sched;
  847. }
  848. _starpu_conf_set_value_against_environment("STARPU_NCPUS", &conf->ncpus);
  849. _starpu_conf_set_value_against_environment("STARPU_NCPU", &conf->ncpus);
  850. _starpu_conf_set_value_against_environment("STARPU_NCUDA", &conf->ncuda);
  851. _starpu_conf_set_value_against_environment("STARPU_NOPENCL", &conf->nopencl);
  852. _starpu_conf_set_value_against_environment("STARPU_CALIBRATE", &conf->calibrate);
  853. _starpu_conf_set_value_against_environment("STARPU_BUS_CALIBRATE", &conf->bus_calibrate);
  854. _starpu_conf_set_value_against_environment("STARPU_SINGLE_COMBINED_WORKER", &conf->single_combined_worker);
  855. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_COPY", &conf->disable_asynchronous_copy);
  856. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY", &conf->disable_asynchronous_cuda_copy);
  857. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY", &conf->disable_asynchronous_opencl_copy);
  858. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_MIC_COPY", &conf->disable_asynchronous_mic_copy);
  859. }
  860. struct starpu_tree* starpu_workers_get_tree(void)
  861. {
  862. return config.topology.tree;
  863. }
  864. #ifdef STARPU_HAVE_HWLOC
  865. static void _fill_tree(struct starpu_tree *tree, hwloc_obj_t curr_obj, unsigned depth, hwloc_topology_t topology)
  866. {
  867. unsigned i;
  868. for(i = 0; i < curr_obj->arity; i++)
  869. {
  870. starpu_tree_insert(tree->nodes[i], curr_obj->children[i]->logical_index, depth, curr_obj->children[i]->type == HWLOC_OBJ_PU, curr_obj->children[i]->arity, tree);
  871. /* char string[128]; */
  872. /* hwloc_obj_snprintf(string, sizeof(string), topology, curr_obj->children[i], "#", 0); */
  873. /* printf("%*s%s %d is_pu %d \n", 0, "", string, curr_obj->children[i]->logical_index, curr_obj->children[i]->type == HWLOC_OBJ_PU); */
  874. _fill_tree(tree->nodes[i], curr_obj->children[i], depth+1, topology);
  875. }
  876. }
  877. #endif
  878. static void _starpu_build_tree(void)
  879. {
  880. #ifdef STARPU_HAVE_HWLOC
  881. struct starpu_tree* tree = (struct starpu_tree*)malloc(sizeof(struct starpu_tree));
  882. config.topology.tree = tree;
  883. hwloc_obj_t root = hwloc_get_root_obj(config.topology.hwtopology);
  884. /* char string[128]; */
  885. /* hwloc_obj_snprintf(string, sizeof(string), topology, root, "#", 0); */
  886. /* printf("%*s%s %d is_pu = %d \n", 0, "", string, root->logical_index, root->type == HWLOC_OBJ_PU); */
  887. /* level, is_pu, is in the tree (it will be true only after add*/
  888. starpu_tree_insert(tree, root->logical_index, 0,root->type == HWLOC_OBJ_PU, root->arity, NULL);
  889. _fill_tree(tree, root, 1, config.topology.hwtopology);
  890. #endif
  891. }
  892. int starpu_init(struct starpu_conf *user_conf)
  893. {
  894. return starpu_initialize(user_conf, NULL, NULL);
  895. }
  896. int starpu_initialize(struct starpu_conf *user_conf, int *argc, char ***argv)
  897. {
  898. int is_a_sink = 0; /* Always defined. If the MP infrastructure is not
  899. * used, we cannot be a sink. */
  900. unsigned worker;
  901. #ifdef STARPU_USE_MP
  902. _starpu_set_argc_argv(argc, argv);
  903. # ifdef STARPU_USE_SCC
  904. /* In SCC case we look at the rank to know if we are a sink */
  905. if (_starpu_scc_common_mp_init() && !_starpu_scc_common_is_src_node())
  906. setenv("STARPU_SINK", "STARPU_SCC", 1);
  907. # endif
  908. /* If StarPU was configured to use MP sinks, we have to control the
  909. * kind on node we are running on : host or sink ? */
  910. if (getenv("STARPU_SINK"))
  911. is_a_sink = 1;
  912. #else
  913. (void)argc;
  914. (void)argv;
  915. #endif /* STARPU_USE_MP */
  916. int ret;
  917. #ifdef STARPU_OPENMP
  918. _starpu_omp_dummy_init();
  919. #endif
  920. #ifdef STARPU_SIMGRID
  921. _starpu_simgrid_init();
  922. /* Warn when the lots of stacks malloc()-ated by simgrid for transfer
  923. * processes will take a long time to get initialized */
  924. if (getenv("MALLOC_PERTURB_"))
  925. _STARPU_DISP("Warning: MALLOC_PERTURB_ is set, this makes simgrid runs very slow\n");
  926. #else
  927. #ifdef __GNUC__
  928. #ifndef __OPTIMIZE__
  929. _STARPU_DISP("Warning: StarPU was configured with --enable-debug (-O0), and is thus not optimized\n");
  930. #endif
  931. #endif
  932. #ifdef STARPU_SPINLOCK_CHECK
  933. _STARPU_DISP("Warning: StarPU was configured with --enable-spinlock-check, which slows down a bit\n");
  934. #endif
  935. #if 0
  936. #ifndef STARPU_NO_ASSERT
  937. _STARPU_DISP("Warning: StarPU was configured without --enable-fast\n");
  938. #endif
  939. #endif
  940. #ifdef STARPU_MEMORY_STATS
  941. _STARPU_DISP("Warning: StarPU was configured with --enable-memory-stats, which slows down a bit\n");
  942. #endif
  943. #ifdef STARPU_VERBOSE
  944. _STARPU_DISP("Warning: StarPU was configured with --enable-verbose, which slows down a bit\n");
  945. #endif
  946. #ifdef STARPU_USE_FXT
  947. _STARPU_DISP("Warning: StarPU was configured with --with-fxt, which slows down a bit\n");
  948. #endif
  949. #ifdef STARPU_PERF_DEBUG
  950. _STARPU_DISP("Warning: StarPU was configured with --enable-perf-debug, which slows down a bit\n");
  951. #endif
  952. #ifdef STARPU_MODEL_DEBUG
  953. _STARPU_DISP("Warning: StarPU was configured with --enable-model-debug, which slows down a bit\n");
  954. #endif
  955. #ifdef STARPU_ENABLE_STATS
  956. _STARPU_DISP("Warning: StarPU was configured with --enable-stats, which slows down a bit\n");
  957. #endif
  958. #endif
  959. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  960. while (initialized == CHANGING)
  961. /* Wait for the other one changing it */
  962. STARPU_PTHREAD_COND_WAIT(&init_cond, &init_mutex);
  963. init_count++;
  964. if (initialized == INITIALIZED)
  965. {
  966. /* He initialized it, don't do it again, and let the others get the mutex */
  967. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  968. return 0;
  969. }
  970. /* initialized == UNINITIALIZED */
  971. initialized = CHANGING;
  972. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  973. #if defined(_WIN32) && !defined(__CYGWIN__)
  974. WSADATA wsadata;
  975. WSAStartup(MAKEWORD(1,0), &wsadata);
  976. #endif
  977. srand(2008);
  978. #ifdef HAVE_AYUDAME_H
  979. #ifndef AYU_RT_STARPU
  980. /* Dumb value for now */
  981. #define AYU_RT_STARPU 32
  982. #endif
  983. if (AYU_event)
  984. {
  985. enum ayu_runtime_t ayu_rt = AYU_RT_STARPU;
  986. AYU_event(AYU_PREINIT, 0, (void*) &ayu_rt);
  987. }
  988. #endif
  989. /* store the pointer to the user explicit configuration during the
  990. * initialization */
  991. if (user_conf == NULL)
  992. {
  993. struct starpu_conf *conf = malloc(sizeof(struct starpu_conf));
  994. starpu_conf_init(conf);
  995. config.conf = conf;
  996. config.default_conf = 1;
  997. }
  998. else
  999. {
  1000. if (user_conf->magic != 42)
  1001. {
  1002. _STARPU_DISP("starpu_conf structure needs to be initialized with starpu_conf_init\n");
  1003. return -EINVAL;
  1004. }
  1005. config.conf = user_conf;
  1006. config.default_conf = 0;
  1007. }
  1008. _starpu_conf_check_environment(config.conf);
  1009. _starpu_init_all_sched_ctxs(&config);
  1010. _starpu_init_progression_hooks();
  1011. _starpu_init_tags();
  1012. #ifdef STARPU_USE_FXT
  1013. _starpu_init_fxt_profiling(config.conf->trace_buffer_size);
  1014. #endif
  1015. _starpu_open_debug_logfile();
  1016. _starpu_data_interface_init();
  1017. _starpu_timing_init();
  1018. _starpu_profiling_init();
  1019. _starpu_load_bus_performance_files();
  1020. /* Depending on whether we are a MP sink or not, we must build the
  1021. * topology with MP nodes or not. */
  1022. ret = _starpu_build_topology(&config, is_a_sink);
  1023. if (ret)
  1024. {
  1025. starpu_perfmodel_free_sampling_directories();
  1026. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1027. init_count--;
  1028. #ifdef STARPU_USE_SCC
  1029. if (_starpu_scc_common_is_mp_initialized())
  1030. _starpu_scc_src_mp_deinit();
  1031. #endif
  1032. initialized = UNINITIALIZED;
  1033. /* Let somebody else try to do it */
  1034. STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  1035. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1036. return ret;
  1037. }
  1038. /* Allocate swap, if any */
  1039. _starpu_swap_init();
  1040. /* We need to store the current task handled by the different
  1041. * threads */
  1042. _starpu_initialize_current_task_key();
  1043. for (worker = 0; worker < config.topology.nworkers; worker++)
  1044. _starpu_worker_init(&config.workers[worker], &config);
  1045. STARPU_PTHREAD_KEY_CREATE(&worker_key, NULL);
  1046. _starpu_build_tree();
  1047. if (!is_a_sink)
  1048. {
  1049. struct starpu_sched_policy *selected_policy = _starpu_select_sched_policy(&config, config.conf->sched_policy_name);
  1050. _starpu_create_sched_ctx(selected_policy, NULL, -1, 1, "init", 0, 0, 0, 0, 1);
  1051. }
  1052. _starpu_initialize_registered_performance_models();
  1053. /* Launch "basic" workers (ie. non-combined workers) */
  1054. if (!is_a_sink)
  1055. _starpu_launch_drivers(&config);
  1056. _starpu_watchdog_init();
  1057. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1058. initialized = INITIALIZED;
  1059. /* Tell everybody that we initialized */
  1060. STARPU_PTHREAD_COND_BROADCAST(&init_cond);
  1061. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1062. _STARPU_DEBUG("Initialisation finished\n");
  1063. #ifdef STARPU_USE_MP
  1064. /* Finally, if we are a MP sink, we never leave this function. Else,
  1065. * we enter an infinite event loop which listen for MP commands from
  1066. * the source. */
  1067. if (is_a_sink) {
  1068. _starpu_sink_common_worker();
  1069. /* We should normally never leave the loop as we don't want to
  1070. * really initialize STARPU */
  1071. STARPU_ASSERT(0);
  1072. }
  1073. #endif
  1074. return 0;
  1075. }
  1076. /*
  1077. * Handle runtime termination
  1078. */
  1079. static void _starpu_terminate_workers(struct _starpu_machine_config *pconfig)
  1080. {
  1081. int status = 0;
  1082. unsigned workerid;
  1083. unsigned n;
  1084. for (workerid = 0; workerid < pconfig->topology.nworkers; workerid++)
  1085. {
  1086. starpu_wake_all_blocked_workers();
  1087. _STARPU_DEBUG("wait for worker %u\n", workerid);
  1088. struct _starpu_worker_set *set = pconfig->workers[workerid].set;
  1089. struct _starpu_worker *worker = &pconfig->workers[workerid];
  1090. /* in case StarPU termination code is called from a callback,
  1091. * we have to check if pthread_self() is the worker itself */
  1092. if (set)
  1093. {
  1094. if (set->started)
  1095. {
  1096. #ifdef STARPU_SIMGRID
  1097. status = starpu_pthread_join(set->worker_thread, NULL);
  1098. #else
  1099. if (!pthread_equal(pthread_self(), set->worker_thread))
  1100. status = starpu_pthread_join(set->worker_thread, NULL);
  1101. #endif
  1102. if (status)
  1103. {
  1104. #ifdef STARPU_VERBOSE
  1105. _STARPU_DEBUG("starpu_pthread_join -> %d\n", status);
  1106. #endif
  1107. }
  1108. set->started = 0;
  1109. }
  1110. }
  1111. else
  1112. {
  1113. if (!worker->run_by_starpu)
  1114. goto out;
  1115. #ifdef STARPU_SIMGRID
  1116. status = starpu_pthread_join(worker->worker_thread, NULL);
  1117. #else
  1118. if (!pthread_equal(pthread_self(), worker->worker_thread))
  1119. status = starpu_pthread_join(worker->worker_thread, NULL);
  1120. #endif
  1121. if (status)
  1122. {
  1123. #ifdef STARPU_VERBOSE
  1124. _STARPU_DEBUG("starpu_pthread_join -> %d\n", status);
  1125. #endif
  1126. }
  1127. }
  1128. out:
  1129. STARPU_ASSERT(starpu_task_list_empty(&worker->local_tasks));
  1130. for (n = 0; n < worker->local_ordered_tasks_size; n++)
  1131. STARPU_ASSERT(worker->local_ordered_tasks[n] == NULL);
  1132. _starpu_sched_ctx_list_delete(&worker->sched_ctx_list);
  1133. _starpu_job_list_delete(worker->terminated_jobs);
  1134. free(worker->local_ordered_tasks);
  1135. }
  1136. }
  1137. /* Condition variable and mutex used to pause/resume. */
  1138. static starpu_pthread_cond_t pause_cond = STARPU_PTHREAD_COND_INITIALIZER;
  1139. static starpu_pthread_mutex_t pause_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  1140. void _starpu_may_pause(void)
  1141. {
  1142. /* pause_depth is just protected by a memory barrier */
  1143. STARPU_RMB();
  1144. if (STARPU_UNLIKELY(config.pause_depth > 0)) {
  1145. STARPU_PTHREAD_MUTEX_LOCK(&pause_mutex);
  1146. if (config.pause_depth > 0) {
  1147. STARPU_PTHREAD_COND_WAIT(&pause_cond, &pause_mutex);
  1148. }
  1149. STARPU_PTHREAD_MUTEX_UNLOCK(&pause_mutex);
  1150. }
  1151. }
  1152. unsigned _starpu_machine_is_running(void)
  1153. {
  1154. unsigned ret;
  1155. /* running is just protected by a memory barrier */
  1156. STARPU_RMB();
  1157. ANNOTATE_HAPPENS_AFTER(&config.running);
  1158. ret = config.running;
  1159. ANNOTATE_HAPPENS_BEFORE(&config.running);
  1160. return ret;
  1161. }
  1162. void starpu_pause()
  1163. {
  1164. STARPU_HG_DISABLE_CHECKING(config.pause_depth);
  1165. config.pause_depth += 1;
  1166. }
  1167. void starpu_resume()
  1168. {
  1169. STARPU_PTHREAD_MUTEX_LOCK(&pause_mutex);
  1170. config.pause_depth -= 1;
  1171. if (!config.pause_depth) {
  1172. STARPU_PTHREAD_COND_BROADCAST(&pause_cond);
  1173. }
  1174. STARPU_PTHREAD_MUTEX_UNLOCK(&pause_mutex);
  1175. }
  1176. unsigned _starpu_worker_can_block(unsigned memnode STARPU_ATTRIBUTE_UNUSED, struct _starpu_worker *worker STARPU_ATTRIBUTE_UNUSED)
  1177. {
  1178. #ifdef STARPU_NON_BLOCKING_DRIVERS
  1179. return 0;
  1180. #else
  1181. unsigned can_block = 1;
  1182. struct starpu_driver driver;
  1183. driver.type = worker->arch;
  1184. switch (driver.type)
  1185. {
  1186. case STARPU_CPU_WORKER:
  1187. driver.id.cpu_id = worker->devid;
  1188. break;
  1189. case STARPU_CUDA_WORKER:
  1190. driver.id.cuda_id = worker->devid;
  1191. break;
  1192. #ifdef STARPU_USE_OPENCL
  1193. case STARPU_OPENCL_WORKER:
  1194. starpu_opencl_get_device(worker->devid, &driver.id.opencl_id);
  1195. break;
  1196. #endif
  1197. default:
  1198. goto always_launch;
  1199. }
  1200. if (!_starpu_may_launch_driver(config.conf, &driver))
  1201. return 0;
  1202. always_launch:
  1203. #ifndef STARPU_SIMGRID
  1204. if (!_starpu_check_that_no_data_request_exists(memnode))
  1205. can_block = 0;
  1206. #endif
  1207. if (!_starpu_machine_is_running())
  1208. can_block = 0;
  1209. if (!_starpu_execute_registered_progression_hooks())
  1210. can_block = 0;
  1211. return can_block;
  1212. #endif
  1213. }
  1214. static void _starpu_kill_all_workers(struct _starpu_machine_config *pconfig)
  1215. {
  1216. /* set the flag which will tell workers to stop */
  1217. ANNOTATE_HAPPENS_AFTER(&config.running);
  1218. pconfig->running = 0;
  1219. /* running is just protected by a memory barrier */
  1220. ANNOTATE_HAPPENS_BEFORE(&config.running);
  1221. STARPU_WMB();
  1222. starpu_wake_all_blocked_workers();
  1223. }
  1224. void starpu_display_stats()
  1225. {
  1226. starpu_profiling_bus_helper_display_summary();
  1227. starpu_profiling_worker_helper_display_summary();
  1228. }
  1229. void starpu_shutdown(void)
  1230. {
  1231. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1232. init_count--;
  1233. 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");
  1234. if (init_count)
  1235. {
  1236. _STARPU_DEBUG("Still somebody needing StarPU, don't deinitialize\n");
  1237. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1238. return;
  1239. }
  1240. /* We're last */
  1241. initialized = CHANGING;
  1242. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1243. /* If the workers are frozen, no progress can be made. */
  1244. STARPU_ASSERT(config.pause_depth <= 0);
  1245. starpu_task_wait_for_no_ready();
  1246. /* tell all workers to shutdown */
  1247. _starpu_kill_all_workers(&config);
  1248. {
  1249. int stats = starpu_get_env_number("STARPU_STATS");
  1250. if (stats != 0)
  1251. {
  1252. _starpu_display_msi_stats();
  1253. _starpu_display_alloc_cache_stats();
  1254. _starpu_display_comm_amounts();
  1255. }
  1256. }
  1257. starpu_profiling_bus_helper_display_summary();
  1258. starpu_profiling_worker_helper_display_summary();
  1259. _starpu_deinitialize_registered_performance_models();
  1260. _starpu_watchdog_shutdown();
  1261. /* wait for their termination */
  1262. _starpu_terminate_workers(&config);
  1263. {
  1264. int stats = starpu_get_env_number("STARPU_MEMORY_STATS");
  1265. if (stats != 0)
  1266. {
  1267. // Display statistics on data which have not been unregistered
  1268. starpu_data_display_memory_stats();
  1269. }
  1270. }
  1271. _starpu_delete_all_sched_ctxs();
  1272. _starpu_sched_component_workers_destroy();
  1273. _starpu_disk_unregister();
  1274. #ifdef STARPU_HAVE_HWLOC
  1275. starpu_tree_free(config.topology.tree);
  1276. #endif
  1277. _starpu_destroy_topology(&config);
  1278. #ifdef STARPU_USE_FXT
  1279. _starpu_stop_fxt_profiling();
  1280. #endif
  1281. _starpu_data_interface_shutdown();
  1282. /* Drop all remaining tags */
  1283. _starpu_tag_clear();
  1284. #ifdef STARPU_OPENMP
  1285. _starpu_omp_dummy_shutdown();
  1286. #endif
  1287. _starpu_close_debug_logfile();
  1288. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1289. initialized = UNINITIALIZED;
  1290. /* Let someone else that wants to initialize it again do it */
  1291. STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  1292. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1293. /* Clear memory if it was allocated by StarPU */
  1294. if (config.default_conf)
  1295. free(config.conf);
  1296. #ifdef HAVE_AYUDAME_H
  1297. if (AYU_event) AYU_event(AYU_FINISH, 0, NULL);
  1298. #endif
  1299. #ifdef STARPU_USE_SCC
  1300. if (_starpu_scc_common_is_mp_initialized())
  1301. _starpu_scc_src_mp_deinit();
  1302. #endif
  1303. _starpu_print_idle_time();
  1304. _STARPU_DEBUG("Shutdown finished\n");
  1305. }
  1306. unsigned starpu_worker_get_count(void)
  1307. {
  1308. return config.topology.nworkers;
  1309. }
  1310. unsigned starpu_worker_is_slave(int workerid)
  1311. {
  1312. return config.workers[workerid].slave;
  1313. }
  1314. int starpu_worker_get_count_by_type(enum starpu_worker_archtype type)
  1315. {
  1316. switch (type)
  1317. {
  1318. case STARPU_CPU_WORKER:
  1319. return config.topology.ncpus;
  1320. case STARPU_CUDA_WORKER:
  1321. return config.topology.ncudagpus;
  1322. case STARPU_OPENCL_WORKER:
  1323. return config.topology.nopenclgpus;
  1324. case STARPU_MIC_WORKER:
  1325. return config.topology.nmicdevices;
  1326. case STARPU_SCC_WORKER:
  1327. return config.topology.nsccdevices;
  1328. default:
  1329. return -EINVAL;
  1330. }
  1331. }
  1332. unsigned starpu_combined_worker_get_count(void)
  1333. {
  1334. return config.topology.ncombinedworkers;
  1335. }
  1336. unsigned starpu_cpu_worker_get_count(void)
  1337. {
  1338. return config.topology.ncpus;
  1339. }
  1340. unsigned starpu_cuda_worker_get_count(void)
  1341. {
  1342. return config.topology.ncudagpus;
  1343. }
  1344. unsigned starpu_opencl_worker_get_count(void)
  1345. {
  1346. return config.topology.nopenclgpus;
  1347. }
  1348. int starpu_asynchronous_copy_disabled(void)
  1349. {
  1350. return config.conf->disable_asynchronous_copy;
  1351. }
  1352. int starpu_asynchronous_cuda_copy_disabled(void)
  1353. {
  1354. return config.conf->disable_asynchronous_cuda_copy;
  1355. }
  1356. int starpu_asynchronous_opencl_copy_disabled(void)
  1357. {
  1358. return config.conf->disable_asynchronous_opencl_copy;
  1359. }
  1360. int starpu_asynchronous_mic_copy_disabled(void)
  1361. {
  1362. return config.conf->disable_asynchronous_mic_copy;
  1363. }
  1364. unsigned starpu_mic_worker_get_count(void)
  1365. {
  1366. int i = 0, count = 0;
  1367. for (i = 0; i < STARPU_MAXMICDEVS; i++)
  1368. count += config.topology.nmiccores[i];
  1369. return count;
  1370. }
  1371. unsigned starpu_scc_worker_get_count(void)
  1372. {
  1373. return config.topology.nsccdevices;
  1374. }
  1375. /* When analyzing performance, it is useful to see what is the processing unit
  1376. * that actually performed the task. This function returns the id of the
  1377. * processing unit actually executing it, therefore it makes no sense to use it
  1378. * within the callbacks of SPU functions for instance. If called by some thread
  1379. * that is not controlled by StarPU, starpu_worker_get_id returns -1. */
  1380. int starpu_worker_get_id(void)
  1381. {
  1382. struct _starpu_worker * worker;
  1383. worker = _starpu_get_local_worker_key();
  1384. if (worker)
  1385. {
  1386. return worker->workerid;
  1387. }
  1388. else
  1389. {
  1390. /* there is no worker associated to that thread, perhaps it is
  1391. * a thread from the application or this is some SPU worker */
  1392. return -1;
  1393. }
  1394. }
  1395. int starpu_combined_worker_get_id(void)
  1396. {
  1397. struct _starpu_worker *worker;
  1398. worker = _starpu_get_local_worker_key();
  1399. if (worker)
  1400. {
  1401. return worker->combined_workerid;
  1402. }
  1403. else
  1404. {
  1405. /* there is no worker associated to that thread, perhaps it is
  1406. * a thread from the application or this is some SPU worker */
  1407. return -1;
  1408. }
  1409. }
  1410. int starpu_combined_worker_get_size(void)
  1411. {
  1412. struct _starpu_worker *worker;
  1413. worker = _starpu_get_local_worker_key();
  1414. if (worker)
  1415. {
  1416. return worker->worker_size;
  1417. }
  1418. else
  1419. {
  1420. /* there is no worker associated to that thread, perhaps it is
  1421. * a thread from the application or this is some SPU worker */
  1422. return -1;
  1423. }
  1424. }
  1425. int starpu_combined_worker_get_rank(void)
  1426. {
  1427. struct _starpu_worker *worker;
  1428. worker = _starpu_get_local_worker_key();
  1429. if (worker)
  1430. {
  1431. return worker->current_rank;
  1432. }
  1433. else
  1434. {
  1435. /* there is no worker associated to that thread, perhaps it is
  1436. * a thread from the application or this is some SPU worker */
  1437. return -1;
  1438. }
  1439. }
  1440. int starpu_worker_get_subworkerid(int id)
  1441. {
  1442. return config.workers[id].subworkerid;
  1443. }
  1444. int starpu_worker_get_devid(int id)
  1445. {
  1446. return config.workers[id].devid;
  1447. }
  1448. struct _starpu_worker *_starpu_get_worker_struct(unsigned id)
  1449. {
  1450. return &config.workers[id];
  1451. }
  1452. unsigned starpu_worker_is_combined_worker(int id)
  1453. {
  1454. return id >= (int)config.topology.nworkers;
  1455. }
  1456. struct _starpu_sched_ctx *_starpu_get_sched_ctx_struct(unsigned id)
  1457. {
  1458. if(id == STARPU_NMAX_SCHED_CTXS) return NULL;
  1459. return &config.sched_ctxs[id];
  1460. }
  1461. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id)
  1462. {
  1463. unsigned basic_worker_count = starpu_worker_get_count();
  1464. //_STARPU_DEBUG("basic_worker_count:%d\n",basic_worker_count);
  1465. STARPU_ASSERT(id >= basic_worker_count);
  1466. return &config.combined_workers[id - basic_worker_count];
  1467. }
  1468. enum starpu_worker_archtype starpu_worker_get_type(int id)
  1469. {
  1470. return config.workers[id].arch;
  1471. }
  1472. int starpu_worker_get_ids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize)
  1473. {
  1474. unsigned nworkers = starpu_worker_get_count();
  1475. int cnt = 0;
  1476. unsigned id;
  1477. for (id = 0; id < nworkers; id++)
  1478. {
  1479. if (starpu_worker_get_type(id) == type)
  1480. {
  1481. /* Perhaps the array is too small ? */
  1482. if (cnt >= maxsize)
  1483. return -ERANGE;
  1484. workerids[cnt++] = id;
  1485. }
  1486. }
  1487. return cnt;
  1488. }
  1489. int starpu_worker_get_by_type(enum starpu_worker_archtype type, int num)
  1490. {
  1491. unsigned nworkers = starpu_worker_get_count();
  1492. int cnt = 0;
  1493. unsigned id;
  1494. for (id = 0; id < nworkers; id++)
  1495. {
  1496. if (starpu_worker_get_type(id) == type)
  1497. {
  1498. if (num == cnt)
  1499. return id;
  1500. cnt++;
  1501. }
  1502. }
  1503. /* Not found */
  1504. return -1;
  1505. }
  1506. int starpu_worker_get_by_devid(enum starpu_worker_archtype type, int devid)
  1507. {
  1508. unsigned nworkers = starpu_worker_get_count();
  1509. unsigned id;
  1510. for (id = 0; id < nworkers; id++)
  1511. if (starpu_worker_get_type(id) == type && starpu_worker_get_devid(id) == devid)
  1512. return id;
  1513. /* Not found */
  1514. return -1;
  1515. }
  1516. void starpu_worker_get_name(int id, char *dst, size_t maxlen)
  1517. {
  1518. char *name = config.workers[id].name;
  1519. snprintf(dst, maxlen, "%s", name);
  1520. }
  1521. int starpu_worker_get_bindid(int workerid)
  1522. {
  1523. return config.workers[workerid].bindid;
  1524. }
  1525. int _starpu_worker_get_workerids(int bindid, int *workerids)
  1526. {
  1527. unsigned nworkers = starpu_worker_get_count();
  1528. int nw = 0;
  1529. unsigned id;
  1530. for (id = 0; id < nworkers; id++)
  1531. if (config.workers[id].bindid == bindid)
  1532. workerids[nw++] = id;
  1533. return nw;
  1534. }
  1535. /* Retrieve the status which indicates what the worker is currently doing. */
  1536. enum _starpu_worker_status _starpu_worker_get_status(int workerid)
  1537. {
  1538. return config.workers[workerid].status;
  1539. }
  1540. /* Change the status of the worker which indicates what the worker is currently
  1541. * doing (eg. executing a callback). */
  1542. void _starpu_worker_set_status(int workerid, enum _starpu_worker_status status)
  1543. {
  1544. config.workers[workerid].status = status;
  1545. }
  1546. void starpu_worker_get_sched_condition(int workerid, starpu_pthread_mutex_t **sched_mutex, starpu_pthread_cond_t **sched_cond)
  1547. {
  1548. *sched_cond = &config.workers[workerid].sched_cond;
  1549. *sched_mutex = &config.workers[workerid].sched_mutex;
  1550. }
  1551. int starpu_wakeup_worker(int workerid, starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex)
  1552. {
  1553. int success = 0;
  1554. STARPU_PTHREAD_MUTEX_LOCK(mutex);
  1555. if (config.workers[workerid].status == STATUS_SLEEPING)
  1556. {
  1557. config.workers[workerid].status = STATUS_WAKING_UP;
  1558. STARPU_PTHREAD_COND_SIGNAL(cond);
  1559. success = 1;
  1560. }
  1561. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  1562. return success;
  1563. }
  1564. int starpu_wake_worker(int workerid)
  1565. {
  1566. starpu_pthread_mutex_t *sched_mutex;
  1567. starpu_pthread_cond_t *sched_cond;
  1568. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  1569. return starpu_wakeup_worker(workerid, sched_cond, sched_mutex);
  1570. }
  1571. int starpu_worker_get_nids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize)
  1572. {
  1573. unsigned nworkers = starpu_worker_get_count();
  1574. int cnt = 0;
  1575. unsigned id;
  1576. for (id = 0; id < nworkers; id++)
  1577. {
  1578. if (starpu_worker_get_type(id) == type)
  1579. {
  1580. /* Perhaps the array is too small ? */
  1581. if (cnt >= maxsize)
  1582. return cnt;
  1583. workerids[cnt++] = id;
  1584. }
  1585. }
  1586. return cnt;
  1587. }
  1588. int starpu_worker_get_nids_ctx_free_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize)
  1589. {
  1590. unsigned nworkers = starpu_worker_get_count();
  1591. int cnt = 0;
  1592. unsigned id, worker;
  1593. unsigned found = 0;
  1594. for (id = 0; id < nworkers; id++)
  1595. {
  1596. found = 0;
  1597. if (starpu_worker_get_type(id) == type)
  1598. {
  1599. /* Perhaps the array is too small ? */
  1600. if (cnt >= maxsize)
  1601. return cnt;
  1602. int s;
  1603. for(s = 1; s < STARPU_NMAX_SCHED_CTXS; s++)
  1604. {
  1605. if(config.sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  1606. {
  1607. struct starpu_worker_collection *workers = config.sched_ctxs[s].workers;
  1608. struct starpu_sched_ctx_iterator it;
  1609. workers->init_iterator(workers, &it);
  1610. while(workers->has_next(workers, &it))
  1611. {
  1612. worker = workers->get_next(workers, &it);
  1613. if(worker == id)
  1614. {
  1615. found = 1;
  1616. break;
  1617. }
  1618. }
  1619. if(found) break;
  1620. }
  1621. }
  1622. if(!found)
  1623. workerids[cnt++] = id;
  1624. }
  1625. }
  1626. return cnt;
  1627. }
  1628. struct _starpu_sched_ctx* _starpu_get_initial_sched_ctx(void)
  1629. {
  1630. return &config.sched_ctxs[STARPU_GLOBAL_SCHED_CTX];
  1631. }
  1632. int _starpu_worker_get_nsched_ctxs(int workerid)
  1633. {
  1634. return config.workers[workerid].nsched_ctxs;
  1635. }
  1636. static void *
  1637. _starpu_get_worker_from_driver(struct starpu_driver *d)
  1638. {
  1639. unsigned nworkers = starpu_worker_get_count();
  1640. unsigned workerid;
  1641. #ifdef STARPU_USE_CUDA
  1642. if (d->type == STARPU_CUDA_WORKER)
  1643. return &cuda_worker_set[d->id.cuda_id];
  1644. #endif
  1645. for (workerid = 0; workerid < nworkers; workerid++)
  1646. {
  1647. if (starpu_worker_get_type(workerid) == d->type)
  1648. {
  1649. struct _starpu_worker *worker;
  1650. worker = _starpu_get_worker_struct(workerid);
  1651. switch (d->type)
  1652. {
  1653. #ifdef STARPU_USE_CPU
  1654. case STARPU_CPU_WORKER:
  1655. if (worker->devid == d->id.cpu_id)
  1656. return worker;
  1657. break;
  1658. #endif
  1659. #ifdef STARPU_USE_OPENCL
  1660. case STARPU_OPENCL_WORKER:
  1661. {
  1662. cl_device_id device;
  1663. starpu_opencl_get_device(worker->devid, &device);
  1664. if (device == d->id.opencl_id)
  1665. return worker;
  1666. break;
  1667. }
  1668. #endif
  1669. default:
  1670. _STARPU_DEBUG("Invalid device type\n");
  1671. return NULL;
  1672. }
  1673. }
  1674. }
  1675. return NULL;
  1676. }
  1677. int
  1678. starpu_driver_run(struct starpu_driver *d)
  1679. {
  1680. if (!d)
  1681. {
  1682. _STARPU_DEBUG("Invalid argument\n");
  1683. return -EINVAL;
  1684. }
  1685. void *worker = _starpu_get_worker_from_driver(d);
  1686. switch (d->type)
  1687. {
  1688. #ifdef STARPU_USE_CPU
  1689. case STARPU_CPU_WORKER:
  1690. return _starpu_run_cpu(worker);
  1691. #endif
  1692. #ifdef STARPU_USE_CUDA
  1693. case STARPU_CUDA_WORKER:
  1694. return _starpu_run_cuda(worker);
  1695. #endif
  1696. #ifdef STARPU_USE_OPENCL
  1697. case STARPU_OPENCL_WORKER:
  1698. return _starpu_run_opencl(worker);
  1699. #endif
  1700. default:
  1701. _STARPU_DEBUG("Invalid device type\n");
  1702. return -EINVAL;
  1703. }
  1704. }
  1705. int
  1706. starpu_driver_init(struct starpu_driver *d)
  1707. {
  1708. STARPU_ASSERT(d);
  1709. void *worker = _starpu_get_worker_from_driver(d);
  1710. switch (d->type)
  1711. {
  1712. #ifdef STARPU_USE_CPU
  1713. case STARPU_CPU_WORKER:
  1714. return _starpu_cpu_driver_init(worker);
  1715. #endif
  1716. #ifdef STARPU_USE_CUDA
  1717. case STARPU_CUDA_WORKER:
  1718. return _starpu_cuda_driver_init(worker);
  1719. #endif
  1720. #ifdef STARPU_USE_OPENCL
  1721. case STARPU_OPENCL_WORKER:
  1722. return _starpu_opencl_driver_init(worker);
  1723. #endif
  1724. default:
  1725. return -EINVAL;
  1726. }
  1727. }
  1728. int
  1729. starpu_driver_run_once(struct starpu_driver *d)
  1730. {
  1731. STARPU_ASSERT(d);
  1732. void *worker = _starpu_get_worker_from_driver(d);
  1733. switch (d->type)
  1734. {
  1735. #ifdef STARPU_USE_CPU
  1736. case STARPU_CPU_WORKER:
  1737. return _starpu_cpu_driver_run_once(worker);
  1738. #endif
  1739. #ifdef STARPU_USE_CUDA
  1740. case STARPU_CUDA_WORKER:
  1741. return _starpu_cuda_driver_run_once(worker);
  1742. #endif
  1743. #ifdef STARPU_USE_OPENCL
  1744. case STARPU_OPENCL_WORKER:
  1745. return _starpu_opencl_driver_run_once(worker);
  1746. #endif
  1747. default:
  1748. return -EINVAL;
  1749. }
  1750. }
  1751. int
  1752. starpu_driver_deinit(struct starpu_driver *d)
  1753. {
  1754. STARPU_ASSERT(d);
  1755. void *worker = _starpu_get_worker_from_driver(d);
  1756. switch (d->type)
  1757. {
  1758. #ifdef STARPU_USE_CPU
  1759. case STARPU_CPU_WORKER:
  1760. return _starpu_cpu_driver_deinit(worker);
  1761. #endif
  1762. #ifdef STARPU_USE_CUDA
  1763. case STARPU_CUDA_WORKER:
  1764. return _starpu_cuda_driver_deinit(worker);
  1765. #endif
  1766. #ifdef STARPU_USE_OPENCL
  1767. case STARPU_OPENCL_WORKER:
  1768. return _starpu_opencl_driver_deinit(worker);
  1769. #endif
  1770. default:
  1771. return -EINVAL;
  1772. }
  1773. }
  1774. void starpu_get_version(int *major, int *minor, int *release)
  1775. {
  1776. *major = STARPU_MAJOR_VERSION;
  1777. *minor = STARPU_MINOR_VERSION;
  1778. *release = STARPU_RELEASE_VERSION;
  1779. }
  1780. void _starpu_unlock_mutex_if_prev_locked()
  1781. {
  1782. int workerid = starpu_worker_get_id();
  1783. if(workerid != -1)
  1784. {
  1785. struct _starpu_worker *w = _starpu_get_worker_struct(workerid);
  1786. if(w->sched_mutex_locked)
  1787. {
  1788. STARPU_PTHREAD_MUTEX_UNLOCK(&w->sched_mutex);
  1789. _starpu_worker_set_flag_sched_mutex_locked(workerid, 1);
  1790. }
  1791. }
  1792. return;
  1793. }
  1794. void _starpu_relock_mutex_if_prev_locked()
  1795. {
  1796. int workerid = starpu_worker_get_id();
  1797. if(workerid != -1)
  1798. {
  1799. struct _starpu_worker *w = _starpu_get_worker_struct(workerid);
  1800. if(w->sched_mutex_locked)
  1801. STARPU_PTHREAD_MUTEX_LOCK(&w->sched_mutex);
  1802. }
  1803. return;
  1804. }
  1805. void _starpu_worker_set_flag_sched_mutex_locked(int workerid, unsigned flag)
  1806. {
  1807. struct _starpu_worker *w = _starpu_get_worker_struct(workerid);
  1808. w->sched_mutex_locked = flag;
  1809. }
  1810. unsigned _starpu_worker_mutex_is_sched_mutex(int workerid, starpu_pthread_mutex_t *mutex)
  1811. {
  1812. struct _starpu_worker *w = _starpu_get_worker_struct(workerid);
  1813. return &w->sched_mutex == mutex;
  1814. }
  1815. unsigned starpu_worker_get_sched_ctx_list(int workerid, unsigned **sched_ctxs)
  1816. {
  1817. unsigned s = 0;
  1818. unsigned nsched_ctxs = _starpu_worker_get_nsched_ctxs(workerid);
  1819. *sched_ctxs = (unsigned*)malloc(nsched_ctxs*sizeof(unsigned));
  1820. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1821. struct _starpu_sched_ctx_list *l = NULL;
  1822. for (l = worker->sched_ctx_list; l; l = l->next)
  1823. {
  1824. (*sched_ctxs)[s++] = l->sched_ctx;
  1825. }
  1826. return nsched_ctxs;
  1827. }
  1828. char *starpu_worker_get_type_as_string(enum starpu_worker_archtype type)
  1829. {
  1830. if (type == STARPU_CPU_WORKER) return "STARPU_CPU_WORKER";
  1831. if (type == STARPU_CUDA_WORKER) return "STARPU_CUDA_WORKER";
  1832. if (type == STARPU_OPENCL_WORKER) return "STARPU_OPENCL_WORKER";
  1833. if (type == STARPU_MIC_WORKER) return "STARPU_MIC_WORKER";
  1834. if (type == STARPU_SCC_WORKER) return "STARPU_SCC_WORKER";
  1835. if (type == STARPU_ANY_WORKER) return "STARPU_ANY_WORKER";
  1836. return "STARPU_unknown_WORKER";
  1837. }