workers.c 59 KB

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