workers.c 59 KB

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