workers.c 58 KB

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