workers.c 68 KB

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