workers.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  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 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. #ifdef STARPU_SIMGRID
  496. starpu_pthread_queue_unregister(&workerarg->wait, &_starpu_simgrid_task_queue[workerarg->workerid]);
  497. starpu_pthread_wait_destroy(&workerarg->wait);
  498. #endif
  499. }
  500. #ifdef STARPU_USE_FXT
  501. void _starpu_worker_start(struct _starpu_worker *worker, unsigned fut_key, unsigned sync)
  502. {
  503. unsigned devid = worker->devid;
  504. unsigned memnode = worker->memory_node;
  505. _STARPU_TRACE_WORKER_INIT_START(fut_key, worker->workerid, devid, memnode, worker->bindid, sync);
  506. }
  507. #endif
  508. void _starpu_driver_start(struct _starpu_worker *worker, unsigned fut_key, unsigned sync STARPU_ATTRIBUTE_UNUSED)
  509. {
  510. (void) fut_key;
  511. int devid = worker->devid;
  512. (void) devid;
  513. #if defined(STARPU_PERF_DEBUG) && !defined(STARPU_SIMGRID)
  514. setitimer(ITIMER_PROF, &prof_itimer, NULL);
  515. #endif
  516. #ifdef STARPU_USE_FXT
  517. _starpu_fxt_register_thread(worker->bindid);
  518. _starpu_worker_start(worker, fut_key, sync);
  519. #endif
  520. _starpu_bind_thread_on_cpu(worker->config, worker->bindid, worker->workerid);
  521. _STARPU_DEBUG("worker %p %d for dev %d is ready on logical cpu %d\n", worker, worker->workerid, devid, worker->bindid);
  522. #ifdef STARPU_HAVE_HWLOC
  523. _STARPU_DEBUG("worker %p %d cpuset start at %d\n", worker, worker->workerid, hwloc_bitmap_first(worker->hwloc_cpu_set));
  524. #endif
  525. _starpu_memory_node_set_local_key(&worker->memory_node);
  526. _starpu_set_local_worker_key(worker);
  527. STARPU_PTHREAD_MUTEX_LOCK(&worker->mutex);
  528. worker->worker_is_running = 1;
  529. STARPU_PTHREAD_COND_SIGNAL(&worker->started_cond);
  530. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->mutex);
  531. }
  532. static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
  533. {
  534. pconfig->running = 1;
  535. pconfig->pause_depth = 0;
  536. pconfig->submitting = 1;
  537. STARPU_HG_DISABLE_CHECKING(pconfig->watchdog_ok);
  538. unsigned nworkers = pconfig->topology.nworkers;
  539. /* Launch workers asynchronously */
  540. unsigned worker;
  541. #if defined(STARPU_PERF_DEBUG) && !defined(STARPU_SIMGRID)
  542. /* Get itimer of the main thread, to set it for the worker threads */
  543. getitimer(ITIMER_PROF, &prof_itimer);
  544. #endif
  545. #ifdef HAVE_AYUDAME_H
  546. if (AYU_event) AYU_event(AYU_INIT, 0, NULL);
  547. #endif
  548. for (worker = 0; worker < nworkers; worker++)
  549. {
  550. struct _starpu_worker *workerarg = &pconfig->workers[worker];
  551. unsigned devid = workerarg->devid;
  552. #if defined(STARPU_USE_MIC) || defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  553. struct _starpu_worker_set *worker_set = workerarg->set;
  554. #endif
  555. _STARPU_DEBUG("initialising worker %u/%u\n", worker, nworkers);
  556. _starpu_init_worker_queue(workerarg);
  557. struct starpu_driver driver;
  558. driver.type = workerarg->arch;
  559. switch (workerarg->arch)
  560. {
  561. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  562. case STARPU_CPU_WORKER:
  563. driver.id.cpu_id = devid;
  564. if (_starpu_may_launch_driver(&pconfig->conf, &driver))
  565. {
  566. STARPU_PTHREAD_CREATE_ON(
  567. workerarg->name,
  568. &workerarg->worker_thread,
  569. NULL,
  570. _starpu_cpu_worker,
  571. workerarg,
  572. _starpu_simgrid_get_host_by_worker(workerarg));
  573. #ifdef STARPU_USE_FXT
  574. /* In tracing mode, make sure the
  575. * thread is really started before
  576. * starting another one, to make sure
  577. * they appear in order in the trace.
  578. */
  579. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  580. while (!workerarg->worker_is_running)
  581. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  582. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  583. #endif
  584. }
  585. else
  586. {
  587. workerarg->run_by_starpu = 0;
  588. }
  589. break;
  590. #endif
  591. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  592. case STARPU_CUDA_WORKER:
  593. driver.id.cuda_id = devid;
  594. /* We spawn only one thread per CUDA driver,
  595. * which will control all CUDA workers of this
  596. * driver. (by using a worker set). */
  597. if (worker_set->workers != workerarg)
  598. break;
  599. worker_set->nworkers = starpu_get_env_number_default("STARPU_NWORKER_PER_CUDA", 1);
  600. #ifndef STARPU_NON_BLOCKING_DRIVERS
  601. if (worker_set->nworkers > 1)
  602. {
  603. _STARPU_DISP("Warning: reducing STARPU_NWORKER_PER_CUDA to 1 because blocking drivers are enabled\n");
  604. worker_set->nworkers = 1;
  605. }
  606. #endif
  607. worker_set->set_is_initialized = 0;
  608. if (!_starpu_may_launch_driver(&pconfig->conf, &driver))
  609. {
  610. workerarg->run_by_starpu = 0;
  611. break;
  612. }
  613. STARPU_PTHREAD_CREATE_ON(
  614. workerarg->name,
  615. &worker_set->worker_thread,
  616. NULL,
  617. _starpu_cuda_worker,
  618. worker_set,
  619. _starpu_simgrid_get_host_by_worker(workerarg));
  620. #ifdef STARPU_USE_FXT
  621. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  622. while (!workerarg->worker_is_running)
  623. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  624. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  625. #endif
  626. STARPU_PTHREAD_MUTEX_LOCK(&worker_set->mutex);
  627. while (!worker_set->set_is_initialized)
  628. STARPU_PTHREAD_COND_WAIT(&worker_set->ready_cond,
  629. &worker_set->mutex);
  630. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_set->mutex);
  631. worker_set->started = 1;
  632. break;
  633. #endif
  634. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  635. case STARPU_OPENCL_WORKER:
  636. #ifndef STARPU_SIMGRID
  637. starpu_opencl_get_device(devid, &driver.id.opencl_id);
  638. if (!_starpu_may_launch_driver(&pconfig->conf, &driver))
  639. {
  640. workerarg->run_by_starpu = 0;
  641. break;
  642. }
  643. #endif
  644. STARPU_PTHREAD_CREATE_ON(
  645. workerarg->name,
  646. &workerarg->worker_thread,
  647. NULL,
  648. _starpu_opencl_worker,
  649. workerarg,
  650. _starpu_simgrid_get_host_by_worker(workerarg));
  651. #ifdef STARPU_USE_FXT
  652. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  653. while (!workerarg->worker_is_running)
  654. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  655. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  656. #endif
  657. break;
  658. #endif
  659. #ifdef STARPU_USE_MIC
  660. case STARPU_MIC_WORKER:
  661. /* We spawn only one thread
  662. * per MIC device, which will control all MIC
  663. * workers of this device. (by using a worker set). */
  664. if (worker_set->workers != workerarg)
  665. break;
  666. worker_set->nworkers = pconfig->topology.nmiccores[devid];
  667. worker_set->set_is_initialized = 0;
  668. STARPU_PTHREAD_CREATE_ON(
  669. workerarg->name,
  670. &worker_set->worker_thread,
  671. NULL,
  672. _starpu_mic_src_worker,
  673. worker_set,
  674. _starpu_simgrid_get_host_by_worker(workerarg));
  675. #ifdef STARPU_USE_FXT
  676. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  677. while (!workerarg->worker_is_running)
  678. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  679. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  680. #endif
  681. STARPU_PTHREAD_MUTEX_LOCK(&worker_set->mutex);
  682. while (!worker_set->set_is_initialized)
  683. STARPU_PTHREAD_COND_WAIT(&worker_set->ready_cond,
  684. &worker_set->mutex);
  685. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_set->mutex);
  686. worker_set->started = 1;
  687. break;
  688. #endif /* STARPU_USE_MIC */
  689. #ifdef STARPU_USE_SCC
  690. case STARPU_SCC_WORKER:
  691. workerarg->worker_is_initialized = 0;
  692. STARPU_PTHREAD_CREATE_ON(
  693. workerarg->name,
  694. &workerarg->worker_thread,
  695. NULL,
  696. _starpu_scc_src_worker,
  697. workerarg,
  698. _starpu_simgrid_get_host_by_worker(workerarg));
  699. #ifdef STARPU_USE_FXT
  700. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  701. while (!workerarg->worker_is_running)
  702. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  703. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  704. #endif
  705. break;
  706. #endif
  707. default:
  708. STARPU_ABORT();
  709. }
  710. }
  711. for (worker = 0; worker < nworkers; worker++)
  712. {
  713. struct _starpu_worker *workerarg = &pconfig->workers[worker];
  714. struct starpu_driver driver;
  715. unsigned devid = workerarg->devid;
  716. driver.type = workerarg->arch;
  717. switch (workerarg->arch)
  718. {
  719. case STARPU_CPU_WORKER:
  720. driver.id.cpu_id = devid;
  721. if (!_starpu_may_launch_driver(&pconfig->conf, &driver))
  722. break;
  723. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  724. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  725. while (!workerarg->worker_is_initialized)
  726. STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  727. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  728. break;
  729. case STARPU_CUDA_WORKER:
  730. /* Already waited above */
  731. break;
  732. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  733. case STARPU_OPENCL_WORKER:
  734. #ifndef STARPU_SIMGRID
  735. starpu_opencl_get_device(devid, &driver.id.opencl_id);
  736. if (!_starpu_may_launch_driver(&pconfig->conf, &driver))
  737. break;
  738. #endif
  739. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  740. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  741. while (!workerarg->worker_is_initialized)
  742. STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  743. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  744. break;
  745. #endif
  746. case STARPU_MIC_WORKER:
  747. /* Already waited above */
  748. break;
  749. case STARPU_SCC_WORKER:
  750. /* TODO: implement may_launch? */
  751. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  752. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  753. while (!workerarg->worker_is_initialized)
  754. STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  755. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  756. break;
  757. default:
  758. STARPU_ABORT();
  759. }
  760. }
  761. _STARPU_DEBUG("finished launching drivers\n");
  762. }
  763. /* Initialize the starpu_conf with default values */
  764. int starpu_conf_init(struct starpu_conf *conf)
  765. {
  766. if (!conf)
  767. return -EINVAL;
  768. memset(conf, 0, sizeof(*conf));
  769. conf->magic = 42;
  770. conf->sched_policy_name = starpu_getenv("STARPU_SCHED");
  771. conf->sched_policy = NULL;
  772. conf->global_sched_ctx_min_priority = starpu_get_env_number("STARPU_MIN_PRIO");
  773. conf->global_sched_ctx_max_priority = starpu_get_env_number("STARPU_MAX_PRIO");
  774. /* Note that starpu_get_env_number returns -1 in case the variable is
  775. * not defined */
  776. /* Backward compatibility: check the value of STARPU_NCPUS if
  777. * STARPU_NCPU is not set. */
  778. conf->ncpus = starpu_get_env_number("STARPU_NCPU");
  779. if (conf->ncpus == -1)
  780. conf->ncpus = starpu_get_env_number("STARPU_NCPUS");
  781. conf->ncuda = starpu_get_env_number("STARPU_NCUDA");
  782. conf->nopencl = starpu_get_env_number("STARPU_NOPENCL");
  783. conf->nmic = starpu_get_env_number("STARPU_NMIC");
  784. conf->nscc = starpu_get_env_number("STARPU_NSCC");
  785. conf->calibrate = starpu_get_env_number("STARPU_CALIBRATE");
  786. conf->bus_calibrate = starpu_get_env_number("STARPU_BUS_CALIBRATE");
  787. conf->mic_sink_program_path = starpu_getenv("STARPU_MIC_PROGRAM_PATH");
  788. if (conf->calibrate == -1)
  789. conf->calibrate = 0;
  790. if (conf->bus_calibrate == -1)
  791. conf->bus_calibrate = 0;
  792. conf->use_explicit_workers_bindid = 0; /* TODO */
  793. conf->use_explicit_workers_cuda_gpuid = 0; /* TODO */
  794. conf->use_explicit_workers_opencl_gpuid = 0; /* TODO */
  795. conf->use_explicit_workers_mic_deviceid = 0; /* TODO */
  796. conf->use_explicit_workers_scc_deviceid = 0; /* TODO */
  797. conf->single_combined_worker = starpu_get_env_number("STARPU_SINGLE_COMBINED_WORKER");
  798. if (conf->single_combined_worker == -1)
  799. conf->single_combined_worker = 0;
  800. #if defined(STARPU_DISABLE_ASYNCHRONOUS_COPY)
  801. conf->disable_asynchronous_copy = 1;
  802. #else
  803. conf->disable_asynchronous_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_COPY");
  804. if (conf->disable_asynchronous_copy == -1)
  805. conf->disable_asynchronous_copy = 0;
  806. #endif
  807. #if defined(STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY)
  808. conf->disable_asynchronous_cuda_copy = 1;
  809. #else
  810. conf->disable_asynchronous_cuda_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY");
  811. if (conf->disable_asynchronous_cuda_copy == -1)
  812. conf->disable_asynchronous_cuda_copy = 0;
  813. #endif
  814. #if defined(STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY)
  815. conf->disable_asynchronous_opencl_copy = 1;
  816. #else
  817. conf->disable_asynchronous_opencl_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY");
  818. if (conf->disable_asynchronous_opencl_copy == -1)
  819. conf->disable_asynchronous_opencl_copy = 0;
  820. #endif
  821. #if defined(STARPU_DISABLE_ASYNCHRONOUS_MIC_COPY)
  822. conf->disable_asynchronous_mic_copy = 1;
  823. #else
  824. conf->disable_asynchronous_mic_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_MIC_COPY");
  825. if (conf->disable_asynchronous_mic_copy == -1)
  826. conf->disable_asynchronous_mic_copy = 0;
  827. #endif
  828. /* 64MiB by default */
  829. conf->trace_buffer_size = starpu_get_env_number_default("STARPU_TRACE_BUFFER_SIZE", 64) << 20;
  830. return 0;
  831. }
  832. static void _starpu_conf_set_value_against_environment(char *name, int *value)
  833. {
  834. int number;
  835. number = starpu_get_env_number(name);
  836. if (number != -1)
  837. {
  838. *value = number;
  839. }
  840. }
  841. void _starpu_conf_check_environment(struct starpu_conf *conf)
  842. {
  843. char *sched = starpu_getenv("STARPU_SCHED");
  844. if (sched)
  845. {
  846. conf->sched_policy_name = sched;
  847. }
  848. _starpu_conf_set_value_against_environment("STARPU_NCPUS", &conf->ncpus);
  849. _starpu_conf_set_value_against_environment("STARPU_NCPU", &conf->ncpus);
  850. _starpu_conf_set_value_against_environment("STARPU_NCUDA", &conf->ncuda);
  851. _starpu_conf_set_value_against_environment("STARPU_NOPENCL", &conf->nopencl);
  852. _starpu_conf_set_value_against_environment("STARPU_CALIBRATE", &conf->calibrate);
  853. _starpu_conf_set_value_against_environment("STARPU_BUS_CALIBRATE", &conf->bus_calibrate);
  854. #ifdef STARPU_SIMGRID
  855. if (conf->calibrate == 2)
  856. {
  857. _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!");
  858. }
  859. if (conf->bus_calibrate)
  860. {
  861. _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!");
  862. }
  863. #endif
  864. _starpu_conf_set_value_against_environment("STARPU_SINGLE_COMBINED_WORKER", &conf->single_combined_worker);
  865. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_COPY", &conf->disable_asynchronous_copy);
  866. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY", &conf->disable_asynchronous_cuda_copy);
  867. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY", &conf->disable_asynchronous_opencl_copy);
  868. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_MIC_COPY", &conf->disable_asynchronous_mic_copy);
  869. }
  870. struct starpu_tree* starpu_workers_get_tree(void)
  871. {
  872. return _starpu_config.topology.tree;
  873. }
  874. #ifdef STARPU_HAVE_HWLOC
  875. static void _fill_tree(struct starpu_tree *tree, hwloc_obj_t curr_obj, unsigned depth, hwloc_topology_t topology)
  876. {
  877. unsigned i;
  878. for(i = 0; i < curr_obj->arity; i++)
  879. {
  880. 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);
  881. /* char string[128]; */
  882. /* hwloc_obj_snprintf(string, sizeof(string), topology, curr_obj->children[i], "#", 0); */
  883. /* printf("%*s%s %d is_pu %d \n", 0, "", string, curr_obj->children[i]->logical_index, curr_obj->children[i]->type == HWLOC_OBJ_PU); */
  884. _fill_tree(tree->nodes[i], curr_obj->children[i], depth+1, topology);
  885. }
  886. }
  887. #endif
  888. static void _starpu_build_tree(void)
  889. {
  890. #ifdef STARPU_HAVE_HWLOC
  891. struct starpu_tree* tree = (struct starpu_tree*)malloc(sizeof(struct starpu_tree));
  892. _starpu_config.topology.tree = tree;
  893. hwloc_obj_t root = hwloc_get_root_obj(_starpu_config.topology.hwtopology);
  894. /* char string[128]; */
  895. /* hwloc_obj_snprintf(string, sizeof(string), topology, root, "#", 0); */
  896. /* printf("%*s%s %d is_pu = %d \n", 0, "", string, root->logical_index, root->type == HWLOC_OBJ_PU); */
  897. /* level, is_pu, is in the tree (it will be true only after add*/
  898. starpu_tree_insert(tree, root->logical_index, 0,root->type == HWLOC_OBJ_PU, root->arity, NULL);
  899. _fill_tree(tree, root, 1, _starpu_config.topology.hwtopology);
  900. #endif
  901. }
  902. int starpu_init(struct starpu_conf *user_conf)
  903. {
  904. return starpu_initialize(user_conf, NULL, NULL);
  905. }
  906. int starpu_initialize(struct starpu_conf *user_conf, int *argc, char ***argv)
  907. {
  908. int is_a_sink = 0; /* Always defined. If the MP infrastructure is not
  909. * used, we cannot be a sink. */
  910. unsigned worker;
  911. (void)argc;
  912. (void)argv;
  913. /* This initializes _starpu_silent, thus needs to be early */
  914. _starpu_util_init();
  915. #ifdef STARPU_SIMGRID
  916. /* This initializes the simgrid thread library, thus needs to be early */
  917. _starpu_simgrid_init();
  918. #endif
  919. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  920. while (initialized == CHANGING)
  921. /* Wait for the other one changing it */
  922. STARPU_PTHREAD_COND_WAIT(&init_cond, &init_mutex);
  923. init_count++;
  924. if (initialized == INITIALIZED)
  925. {
  926. /* He initialized it, don't do it again, and let the others get the mutex */
  927. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  928. return 0;
  929. }
  930. /* initialized == UNINITIALIZED */
  931. initialized = CHANGING;
  932. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  933. #ifdef STARPU_USE_MP
  934. _starpu_set_argc_argv(argc, argv);
  935. # ifdef STARPU_USE_SCC
  936. /* In SCC case we look at the rank to know if we are a sink */
  937. if (_starpu_scc_common_mp_init() && !_starpu_scc_common_is_src_node())
  938. setenv("STARPU_SINK", "STARPU_SCC", 1);
  939. # endif
  940. /* If StarPU was configured to use MP sinks, we have to control the
  941. * kind on node we are running on : host or sink ? */
  942. if (starpu_getenv("STARPU_SINK"))
  943. is_a_sink = 1;
  944. #endif /* STARPU_USE_MP */
  945. int ret;
  946. #ifdef STARPU_OPENMP
  947. _starpu_omp_dummy_init();
  948. #endif
  949. #ifdef STARPU_SIMGRID
  950. /* Warn when the lots of stacks malloc()-ated by simgrid for transfer
  951. * processes will take a long time to get initialized */
  952. char *perturb = starpu_getenv("MALLOC_PERTURB_");
  953. if (perturb && perturb[0] && atoi(perturb) != 0)
  954. _STARPU_DISP("Warning: MALLOC_PERTURB_ is set to non-zero, this makes simgrid runs very slow\n");
  955. #else
  956. #ifdef __GNUC__
  957. #ifndef __OPTIMIZE__
  958. _STARPU_DISP("Warning: StarPU was configured with --enable-debug (-O0), and is thus not optimized\n");
  959. #endif
  960. #endif
  961. #ifdef STARPU_SPINLOCK_CHECK
  962. _STARPU_DISP("Warning: StarPU was configured with --enable-spinlock-check, which slows down a bit\n");
  963. #endif
  964. #if 0
  965. #ifndef STARPU_NO_ASSERT
  966. _STARPU_DISP("Warning: StarPU was configured without --enable-fast\n");
  967. #endif
  968. #endif
  969. #ifdef STARPU_MEMORY_STATS
  970. _STARPU_DISP("Warning: StarPU was configured with --enable-memory-stats, which slows down a bit\n");
  971. #endif
  972. #ifdef STARPU_VERBOSE
  973. _STARPU_DISP("Warning: StarPU was configured with --enable-verbose, which slows down a bit\n");
  974. #endif
  975. #ifdef STARPU_USE_FXT
  976. _STARPU_DISP("Warning: StarPU was configured with --with-fxt, which slows down a bit\n");
  977. #endif
  978. #ifdef STARPU_PERF_DEBUG
  979. _STARPU_DISP("Warning: StarPU was configured with --enable-perf-debug, which slows down a bit\n");
  980. #endif
  981. #ifdef STARPU_MODEL_DEBUG
  982. _STARPU_DISP("Warning: StarPU was configured with --enable-model-debug, which slows down a bit\n");
  983. #endif
  984. #endif
  985. if (starpu_getenv("STARPU_ENABLE_STATS"))
  986. {
  987. _STARPU_DISP("Warning: STARPU_ENABLE_STATS is enabled, which slows down a bit\n");
  988. }
  989. #if defined(_WIN32) && !defined(__CYGWIN__)
  990. WSADATA wsadata;
  991. WSAStartup(MAKEWORD(1,0), &wsadata);
  992. #endif
  993. srand(2008);
  994. #ifdef HAVE_AYUDAME_H
  995. #ifndef AYU_RT_STARPU
  996. #define AYU_RT_STARPU 4
  997. #endif
  998. if (AYU_event)
  999. {
  1000. enum ayu_runtime_t ayu_rt = AYU_RT_STARPU;
  1001. AYU_event(AYU_PREINIT, 0, (void*) &ayu_rt);
  1002. }
  1003. #endif
  1004. /* store the pointer to the user explicit configuration during the
  1005. * initialization */
  1006. if (user_conf == NULL)
  1007. starpu_conf_init(&_starpu_config.conf);
  1008. else
  1009. {
  1010. if (user_conf->magic != 42)
  1011. {
  1012. _STARPU_DISP("starpu_conf structure needs to be initialized with starpu_conf_init\n");
  1013. return -EINVAL;
  1014. }
  1015. _starpu_config.conf = *user_conf;
  1016. }
  1017. _starpu_conf_check_environment(&_starpu_config.conf);
  1018. /* Make a copy of arrays */
  1019. if (_starpu_config.conf.sched_policy_name)
  1020. _starpu_config.conf.sched_policy_name = strdup(_starpu_config.conf.sched_policy_name);
  1021. if (_starpu_config.conf.mic_sink_program_path)
  1022. _starpu_config.conf.mic_sink_program_path = strdup(_starpu_config.conf.mic_sink_program_path);
  1023. if (_starpu_config.conf.n_cuda_opengl_interoperability)
  1024. {
  1025. size_t size = _starpu_config.conf.n_cuda_opengl_interoperability * sizeof(*_starpu_config.conf.cuda_opengl_interoperability);
  1026. unsigned *copy = malloc(size);
  1027. memcpy(copy, _starpu_config.conf.cuda_opengl_interoperability, size);
  1028. _starpu_config.conf.cuda_opengl_interoperability = copy;
  1029. }
  1030. if (_starpu_config.conf.n_not_launched_drivers)
  1031. {
  1032. size_t size = _starpu_config.conf.n_not_launched_drivers * sizeof(*_starpu_config.conf.not_launched_drivers);
  1033. struct starpu_driver *copy = malloc(size);
  1034. memcpy(copy, _starpu_config.conf.not_launched_drivers, size);
  1035. _starpu_config.conf.not_launched_drivers = copy;
  1036. }
  1037. _starpu_job_init();
  1038. _starpu_graph_init();
  1039. _starpu_init_all_sched_ctxs(&_starpu_config);
  1040. _starpu_init_progression_hooks();
  1041. _starpu_init_tags();
  1042. #ifdef STARPU_USE_FXT
  1043. _starpu_fxt_init_profiling(_starpu_config.conf.trace_buffer_size);
  1044. #endif
  1045. _starpu_open_debug_logfile();
  1046. _starpu_data_interface_init();
  1047. _starpu_timing_init();
  1048. _starpu_profiling_init();
  1049. _starpu_load_bus_performance_files();
  1050. /* Depending on whether we are a MP sink or not, we must build the
  1051. * topology with MP nodes or not. */
  1052. ret = _starpu_build_topology(&_starpu_config, is_a_sink);
  1053. if (ret)
  1054. {
  1055. starpu_perfmodel_free_sampling_directories();
  1056. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1057. init_count--;
  1058. _starpu_destroy_machine_config(&_starpu_config);
  1059. #ifdef STARPU_USE_SCC
  1060. if (_starpu_scc_common_is_mp_initialized())
  1061. _starpu_scc_src_mp_deinit();
  1062. #endif
  1063. initialized = UNINITIALIZED;
  1064. /* Let somebody else try to do it */
  1065. STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  1066. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1067. return ret;
  1068. }
  1069. _starpu_task_init();
  1070. for (worker = 0; worker < _starpu_config.topology.nworkers; worker++)
  1071. _starpu_worker_init(&_starpu_config.workers[worker], &_starpu_config);
  1072. check_entire_platform = starpu_get_env_number("STARPU_CHECK_ENTIRE_PLATFORM");
  1073. _starpu_config.disable_kernels = starpu_get_env_number("STARPU_DISABLE_KERNELS");
  1074. STARPU_PTHREAD_KEY_CREATE(&_starpu_worker_key, NULL);
  1075. STARPU_PTHREAD_KEY_CREATE(&_starpu_worker_set_key, NULL);
  1076. _starpu_keys_initialized = 1;
  1077. _starpu_build_tree();
  1078. if (!is_a_sink)
  1079. {
  1080. struct starpu_sched_policy *selected_policy = _starpu_select_sched_policy(&_starpu_config, _starpu_config.conf.sched_policy_name);
  1081. _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);
  1082. }
  1083. _starpu_initialize_registered_performance_models();
  1084. /* Launch "basic" workers (ie. non-combined workers) */
  1085. if (!is_a_sink)
  1086. _starpu_launch_drivers(&_starpu_config);
  1087. /* Allocate swap, if any */
  1088. _starpu_swap_init();
  1089. _starpu_watchdog_init();
  1090. _starpu_profiling_start();
  1091. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1092. initialized = INITIALIZED;
  1093. /* Tell everybody that we initialized */
  1094. STARPU_PTHREAD_COND_BROADCAST(&init_cond);
  1095. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1096. _STARPU_DEBUG("Initialisation finished\n");
  1097. #ifdef STARPU_USE_MP
  1098. /* Finally, if we are a MP sink, we never leave this function. Else,
  1099. * we enter an infinite event loop which listen for MP commands from
  1100. * the source. */
  1101. if (is_a_sink)
  1102. {
  1103. _starpu_sink_common_worker();
  1104. /* We should normally never leave the loop as we don't want to
  1105. * really initialize STARPU */
  1106. STARPU_ASSERT(0);
  1107. }
  1108. #endif
  1109. return 0;
  1110. }
  1111. /*
  1112. * Handle runtime termination
  1113. */
  1114. static void _starpu_terminate_workers(struct _starpu_machine_config *pconfig)
  1115. {
  1116. int status = 0;
  1117. unsigned workerid;
  1118. unsigned n;
  1119. for (workerid = 0; workerid < pconfig->topology.nworkers; workerid++)
  1120. {
  1121. starpu_wake_all_blocked_workers();
  1122. _STARPU_DEBUG("wait for worker %u\n", workerid);
  1123. struct _starpu_worker_set *set = pconfig->workers[workerid].set;
  1124. struct _starpu_worker *worker = &pconfig->workers[workerid];
  1125. /* in case StarPU termination code is called from a callback,
  1126. * we have to check if pthread_self() is the worker itself */
  1127. if (set)
  1128. {
  1129. if (set->started)
  1130. {
  1131. #ifdef STARPU_SIMGRID
  1132. status = starpu_pthread_join(set->worker_thread, NULL);
  1133. #else
  1134. if (!pthread_equal(pthread_self(), set->worker_thread))
  1135. status = starpu_pthread_join(set->worker_thread, NULL);
  1136. #endif
  1137. if (status)
  1138. {
  1139. #ifdef STARPU_VERBOSE
  1140. _STARPU_DEBUG("starpu_pthread_join -> %d\n", status);
  1141. #endif
  1142. }
  1143. set->started = 0;
  1144. }
  1145. }
  1146. else
  1147. {
  1148. if (!worker->run_by_starpu)
  1149. goto out;
  1150. #ifdef STARPU_SIMGRID
  1151. status = starpu_pthread_join(worker->worker_thread, NULL);
  1152. #else
  1153. if (!pthread_equal(pthread_self(), worker->worker_thread))
  1154. status = starpu_pthread_join(worker->worker_thread, NULL);
  1155. #endif
  1156. if (status)
  1157. {
  1158. #ifdef STARPU_VERBOSE
  1159. _STARPU_DEBUG("starpu_pthread_join -> %d\n", status);
  1160. #endif
  1161. }
  1162. }
  1163. out:
  1164. STARPU_ASSERT(starpu_task_list_empty(&worker->local_tasks));
  1165. for (n = 0; n < worker->local_ordered_tasks_size; n++)
  1166. STARPU_ASSERT(worker->local_ordered_tasks[n] == NULL);
  1167. _starpu_sched_ctx_list_delete(&worker->sched_ctx_list);
  1168. free(worker->local_ordered_tasks);
  1169. }
  1170. }
  1171. /* Condition variable and mutex used to pause/resume. */
  1172. static starpu_pthread_cond_t pause_cond = STARPU_PTHREAD_COND_INITIALIZER;
  1173. static starpu_pthread_mutex_t pause_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  1174. void _starpu_may_pause(void)
  1175. {
  1176. /* pause_depth is just protected by a memory barrier */
  1177. STARPU_RMB();
  1178. if (STARPU_UNLIKELY(_starpu_config.pause_depth > 0))
  1179. {
  1180. STARPU_PTHREAD_MUTEX_LOCK(&pause_mutex);
  1181. if (_starpu_config.pause_depth > 0)
  1182. {
  1183. STARPU_PTHREAD_COND_WAIT(&pause_cond, &pause_mutex);
  1184. }
  1185. STARPU_PTHREAD_MUTEX_UNLOCK(&pause_mutex);
  1186. }
  1187. }
  1188. void starpu_pause()
  1189. {
  1190. STARPU_HG_DISABLE_CHECKING(_starpu_config.pause_depth);
  1191. _starpu_config.pause_depth += 1;
  1192. }
  1193. void starpu_resume()
  1194. {
  1195. STARPU_PTHREAD_MUTEX_LOCK(&pause_mutex);
  1196. _starpu_config.pause_depth -= 1;
  1197. if (!_starpu_config.pause_depth)
  1198. {
  1199. STARPU_PTHREAD_COND_BROADCAST(&pause_cond);
  1200. }
  1201. STARPU_PTHREAD_MUTEX_UNLOCK(&pause_mutex);
  1202. }
  1203. unsigned _starpu_worker_can_block(unsigned memnode STARPU_ATTRIBUTE_UNUSED, struct _starpu_worker *worker STARPU_ATTRIBUTE_UNUSED)
  1204. {
  1205. #ifdef STARPU_NON_BLOCKING_DRIVERS
  1206. return 0;
  1207. #else
  1208. unsigned can_block = 1;
  1209. struct starpu_driver driver;
  1210. driver.type = worker->arch;
  1211. switch (driver.type)
  1212. {
  1213. case STARPU_CPU_WORKER:
  1214. driver.id.cpu_id = worker->devid;
  1215. break;
  1216. case STARPU_CUDA_WORKER:
  1217. driver.id.cuda_id = worker->devid;
  1218. break;
  1219. #ifdef STARPU_USE_OPENCL
  1220. case STARPU_OPENCL_WORKER:
  1221. starpu_opencl_get_device(worker->devid, &driver.id.opencl_id);
  1222. break;
  1223. #endif
  1224. default:
  1225. goto always_launch;
  1226. }
  1227. if (!_starpu_may_launch_driver(&_starpu_config.conf, &driver))
  1228. return 0;
  1229. always_launch:
  1230. #ifndef STARPU_SIMGRID
  1231. if (!_starpu_check_that_no_data_request_exists(memnode))
  1232. can_block = 0;
  1233. #endif
  1234. if (!_starpu_machine_is_running())
  1235. can_block = 0;
  1236. if (!_starpu_execute_registered_progression_hooks())
  1237. can_block = 0;
  1238. return can_block;
  1239. #endif
  1240. }
  1241. static void _starpu_kill_all_workers(struct _starpu_machine_config *pconfig)
  1242. {
  1243. /* set the flag which will tell workers to stop */
  1244. ANNOTATE_HAPPENS_AFTER(&_starpu_config.running);
  1245. pconfig->running = 0;
  1246. /* running is just protected by a memory barrier */
  1247. ANNOTATE_HAPPENS_BEFORE(&_starpu_config.running);
  1248. STARPU_WMB();
  1249. starpu_wake_all_blocked_workers();
  1250. }
  1251. void starpu_display_stats()
  1252. {
  1253. starpu_profiling_bus_helper_display_summary();
  1254. starpu_profiling_worker_helper_display_summary();
  1255. }
  1256. void starpu_shutdown(void)
  1257. {
  1258. unsigned worker;
  1259. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1260. init_count--;
  1261. 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");
  1262. if (init_count)
  1263. {
  1264. _STARPU_DEBUG("Still somebody needing StarPU, don't deinitialize\n");
  1265. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1266. return;
  1267. }
  1268. /* We're last */
  1269. initialized = CHANGING;
  1270. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1271. /* If the workers are frozen, no progress can be made. */
  1272. STARPU_ASSERT(_starpu_config.pause_depth <= 0);
  1273. starpu_task_wait_for_no_ready();
  1274. /* tell all workers to shutdown */
  1275. _starpu_kill_all_workers(&_starpu_config);
  1276. _starpu_free_all_automatically_allocated_buffers(STARPU_MAIN_RAM);
  1277. {
  1278. int stats = starpu_get_env_number("STARPU_STATS");
  1279. if (stats != 0)
  1280. {
  1281. _starpu_display_msi_stats();
  1282. _starpu_display_alloc_cache_stats();
  1283. }
  1284. }
  1285. starpu_profiling_bus_helper_display_summary();
  1286. starpu_profiling_worker_helper_display_summary();
  1287. _starpu_deinitialize_registered_performance_models();
  1288. _starpu_watchdog_shutdown();
  1289. /* wait for their termination */
  1290. _starpu_terminate_workers(&_starpu_config);
  1291. {
  1292. int stats = starpu_get_env_number("STARPU_MEMORY_STATS");
  1293. if (stats != 0)
  1294. {
  1295. // Display statistics on data which have not been unregistered
  1296. starpu_data_display_memory_stats();
  1297. }
  1298. }
  1299. _starpu_delete_all_sched_ctxs();
  1300. _starpu_sched_component_workers_destroy();
  1301. _starpu_top_shutdown();
  1302. for (worker = 0; worker < _starpu_config.topology.nworkers; worker++)
  1303. _starpu_worker_deinit(&_starpu_config.workers[worker]);
  1304. _starpu_profiling_terminate();
  1305. _starpu_disk_unregister();
  1306. #ifdef STARPU_HAVE_HWLOC
  1307. starpu_tree_free(_starpu_config.topology.tree);
  1308. free(_starpu_config.topology.tree);
  1309. #endif
  1310. _starpu_destroy_topology(&_starpu_config);
  1311. #ifdef STARPU_USE_FXT
  1312. _starpu_stop_fxt_profiling();
  1313. #endif
  1314. _starpu_data_interface_shutdown();
  1315. /* Drop all remaining tags */
  1316. _starpu_tag_clear();
  1317. #ifdef STARPU_OPENMP
  1318. _starpu_omp_dummy_shutdown();
  1319. #endif
  1320. _starpu_close_debug_logfile();
  1321. _starpu_keys_initialized = 0;
  1322. STARPU_PTHREAD_KEY_DELETE(_starpu_worker_key);
  1323. STARPU_PTHREAD_KEY_DELETE(_starpu_worker_set_key);
  1324. _starpu_task_deinit();
  1325. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  1326. initialized = UNINITIALIZED;
  1327. /* Let someone else that wants to initialize it again do it */
  1328. STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  1329. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  1330. /* Clear memory */
  1331. free((char*) _starpu_config.conf.sched_policy_name);
  1332. free(_starpu_config.conf.mic_sink_program_path);
  1333. if (_starpu_config.conf.n_cuda_opengl_interoperability)
  1334. free(_starpu_config.conf.cuda_opengl_interoperability);
  1335. if (_starpu_config.conf.n_not_launched_drivers)
  1336. free(_starpu_config.conf.not_launched_drivers);
  1337. #ifdef HAVE_AYUDAME_H
  1338. if (AYU_event) AYU_event(AYU_FINISH, 0, NULL);
  1339. #endif
  1340. #ifdef STARPU_USE_SCC
  1341. if (_starpu_scc_common_is_mp_initialized())
  1342. _starpu_scc_src_mp_deinit();
  1343. #endif
  1344. _starpu_print_idle_time();
  1345. _STARPU_DEBUG("Shutdown finished\n");
  1346. }
  1347. unsigned starpu_worker_get_count(void)
  1348. {
  1349. return _starpu_config.topology.nworkers;
  1350. }
  1351. unsigned starpu_worker_is_blocked(int workerid)
  1352. {
  1353. return _starpu_config.workers[workerid].blocked;
  1354. }
  1355. unsigned starpu_worker_is_slave_somewhere(int workerid)
  1356. {
  1357. return _starpu_config.workers[workerid].is_slave_somewhere;
  1358. }
  1359. int starpu_worker_get_count_by_type(enum starpu_worker_archtype type)
  1360. {
  1361. switch (type)
  1362. {
  1363. case STARPU_CPU_WORKER:
  1364. return _starpu_config.topology.ncpus;
  1365. case STARPU_CUDA_WORKER:
  1366. return _starpu_config.topology.ncudagpus;
  1367. case STARPU_OPENCL_WORKER:
  1368. return _starpu_config.topology.nopenclgpus;
  1369. case STARPU_MIC_WORKER:
  1370. return _starpu_config.topology.nmicdevices;
  1371. case STARPU_SCC_WORKER:
  1372. return _starpu_config.topology.nsccdevices;
  1373. default:
  1374. return -EINVAL;
  1375. }
  1376. }
  1377. unsigned starpu_combined_worker_get_count(void)
  1378. {
  1379. return _starpu_config.topology.ncombinedworkers;
  1380. }
  1381. unsigned starpu_cpu_worker_get_count(void)
  1382. {
  1383. return _starpu_config.topology.ncpus;
  1384. }
  1385. unsigned starpu_cuda_worker_get_count(void)
  1386. {
  1387. return _starpu_config.topology.ncudagpus;
  1388. }
  1389. unsigned starpu_opencl_worker_get_count(void)
  1390. {
  1391. return _starpu_config.topology.nopenclgpus;
  1392. }
  1393. int starpu_asynchronous_copy_disabled(void)
  1394. {
  1395. return _starpu_config.conf.disable_asynchronous_copy;
  1396. }
  1397. int starpu_asynchronous_cuda_copy_disabled(void)
  1398. {
  1399. return _starpu_config.conf.disable_asynchronous_cuda_copy;
  1400. }
  1401. int starpu_asynchronous_opencl_copy_disabled(void)
  1402. {
  1403. return _starpu_config.conf.disable_asynchronous_opencl_copy;
  1404. }
  1405. int starpu_asynchronous_mic_copy_disabled(void)
  1406. {
  1407. return _starpu_config.conf.disable_asynchronous_mic_copy;
  1408. }
  1409. unsigned starpu_mic_worker_get_count(void)
  1410. {
  1411. int i = 0, count = 0;
  1412. for (i = 0; i < STARPU_MAXMICDEVS; i++)
  1413. count += _starpu_config.topology.nmiccores[i];
  1414. return count;
  1415. }
  1416. unsigned starpu_scc_worker_get_count(void)
  1417. {
  1418. return _starpu_config.topology.nsccdevices;
  1419. }
  1420. /* When analyzing performance, it is useful to see what is the processing unit
  1421. * that actually performed the task. This function returns the id of the
  1422. * processing unit actually executing it, therefore it makes no sense to use it
  1423. * within the callbacks of SPU functions for instance. If called by some thread
  1424. * that is not controlled by StarPU, starpu_worker_get_id returns -1. */
  1425. #undef starpu_worker_get_id
  1426. int starpu_worker_get_id(void)
  1427. {
  1428. struct _starpu_worker * worker;
  1429. worker = _starpu_get_local_worker_key();
  1430. if (worker)
  1431. {
  1432. return worker->workerid;
  1433. }
  1434. else
  1435. {
  1436. /* there is no worker associated to that thread, perhaps it is
  1437. * a thread from the application or this is some SPU worker */
  1438. return -1;
  1439. }
  1440. }
  1441. #define starpu_worker_get_id _starpu_worker_get_id
  1442. int starpu_combined_worker_get_id(void)
  1443. {
  1444. struct _starpu_worker *worker;
  1445. worker = _starpu_get_local_worker_key();
  1446. if (worker)
  1447. {
  1448. return worker->combined_workerid;
  1449. }
  1450. else
  1451. {
  1452. /* there is no worker associated to that thread, perhaps it is
  1453. * a thread from the application or this is some SPU worker */
  1454. return -1;
  1455. }
  1456. }
  1457. int starpu_combined_worker_get_size(void)
  1458. {
  1459. struct _starpu_worker *worker;
  1460. worker = _starpu_get_local_worker_key();
  1461. if (worker)
  1462. {
  1463. return worker->worker_size;
  1464. }
  1465. else
  1466. {
  1467. /* there is no worker associated to that thread, perhaps it is
  1468. * a thread from the application or this is some SPU worker */
  1469. return -1;
  1470. }
  1471. }
  1472. int starpu_combined_worker_get_rank(void)
  1473. {
  1474. struct _starpu_worker *worker;
  1475. worker = _starpu_get_local_worker_key();
  1476. if (worker)
  1477. {
  1478. return worker->current_rank;
  1479. }
  1480. else
  1481. {
  1482. /* there is no worker associated to that thread, perhaps it is
  1483. * a thread from the application or this is some SPU worker */
  1484. return -1;
  1485. }
  1486. }
  1487. int starpu_worker_get_subworkerid(int id)
  1488. {
  1489. return _starpu_config.workers[id].subworkerid;
  1490. }
  1491. int starpu_worker_get_devid(int id)
  1492. {
  1493. return _starpu_config.workers[id].devid;
  1494. }
  1495. unsigned starpu_worker_is_combined_worker(int id)
  1496. {
  1497. return id >= (int)_starpu_config.topology.nworkers;
  1498. }
  1499. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id)
  1500. {
  1501. unsigned basic_worker_count = starpu_worker_get_count();
  1502. //_STARPU_DEBUG("basic_worker_count:%d\n",basic_worker_count);
  1503. STARPU_ASSERT(id >= basic_worker_count);
  1504. return &_starpu_config.combined_workers[id - basic_worker_count];
  1505. }
  1506. enum starpu_worker_archtype starpu_worker_get_type(int id)
  1507. {
  1508. return _starpu_config.workers[id].arch;
  1509. }
  1510. int starpu_worker_get_ids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize)
  1511. {
  1512. unsigned nworkers = starpu_worker_get_count();
  1513. int cnt = 0;
  1514. unsigned id;
  1515. for (id = 0; id < nworkers; id++)
  1516. {
  1517. if (starpu_worker_get_type(id) == type)
  1518. {
  1519. /* Perhaps the array is too small ? */
  1520. if (cnt >= maxsize)
  1521. return -ERANGE;
  1522. workerids[cnt++] = id;
  1523. }
  1524. }
  1525. return cnt;
  1526. }
  1527. int starpu_worker_get_by_type(enum starpu_worker_archtype type, int num)
  1528. {
  1529. unsigned nworkers = starpu_worker_get_count();
  1530. int cnt = 0;
  1531. unsigned id;
  1532. for (id = 0; id < nworkers; id++)
  1533. {
  1534. if (starpu_worker_get_type(id) == type)
  1535. {
  1536. if (num == cnt)
  1537. return id;
  1538. cnt++;
  1539. }
  1540. }
  1541. /* Not found */
  1542. return -1;
  1543. }
  1544. int starpu_worker_get_by_devid(enum starpu_worker_archtype type, int devid)
  1545. {
  1546. unsigned nworkers = starpu_worker_get_count();
  1547. unsigned id;
  1548. for (id = 0; id < nworkers; id++)
  1549. if (starpu_worker_get_type(id) == type && starpu_worker_get_devid(id) == devid)
  1550. return id;
  1551. /* Not found */
  1552. return -1;
  1553. }
  1554. void starpu_worker_get_name(int id, char *dst, size_t maxlen)
  1555. {
  1556. char *name = _starpu_config.workers[id].name;
  1557. snprintf(dst, maxlen, "%s", name);
  1558. }
  1559. int starpu_worker_get_bindid(int workerid)
  1560. {
  1561. return _starpu_config.workers[workerid].bindid;
  1562. }
  1563. int starpu_bindid_get_workerids(int bindid, int **workerids)
  1564. {
  1565. if (bindid >= (int) _starpu_config.nbindid)
  1566. return 0;
  1567. *workerids = _starpu_config.bindid_workers[bindid].workerids;
  1568. return _starpu_config.bindid_workers[bindid].nworkers;
  1569. }
  1570. void starpu_worker_get_sched_condition(int workerid, starpu_pthread_mutex_t **sched_mutex, starpu_pthread_cond_t **sched_cond)
  1571. {
  1572. *sched_cond = &_starpu_config.workers[workerid].sched_cond;
  1573. *sched_mutex = &_starpu_config.workers[workerid].sched_mutex;
  1574. }
  1575. int starpu_wakeup_worker_locked(int workerid, starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex STARPU_ATTRIBUTE_UNUSED)
  1576. {
  1577. #ifdef STARPU_SIMGRID
  1578. starpu_pthread_queue_broadcast(&_starpu_simgrid_task_queue[workerid]);
  1579. #endif
  1580. if (_starpu_config.workers[workerid].status == STATUS_SLEEPING)
  1581. {
  1582. _starpu_config.workers[workerid].status = STATUS_WAKING_UP;
  1583. STARPU_PTHREAD_COND_SIGNAL(cond);
  1584. return 1;
  1585. }
  1586. return 0;
  1587. }
  1588. int starpu_wakeup_worker(int workerid, starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex)
  1589. {
  1590. int success;
  1591. STARPU_PTHREAD_MUTEX_LOCK_SCHED(mutex);
  1592. success = starpu_wakeup_worker_locked(workerid, cond, mutex);
  1593. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(mutex);
  1594. return success;
  1595. }
  1596. int starpu_wake_worker_locked(int workerid)
  1597. {
  1598. starpu_pthread_mutex_t *sched_mutex;
  1599. starpu_pthread_cond_t *sched_cond;
  1600. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  1601. return starpu_wakeup_worker_locked(workerid, sched_cond, sched_mutex);
  1602. }
  1603. int starpu_wake_worker(int workerid)
  1604. {
  1605. starpu_pthread_mutex_t *sched_mutex;
  1606. starpu_pthread_cond_t *sched_cond;
  1607. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  1608. return starpu_wakeup_worker(workerid, sched_cond, sched_mutex);
  1609. }
  1610. int starpu_worker_get_nids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize)
  1611. {
  1612. unsigned nworkers = starpu_worker_get_count();
  1613. int cnt = 0;
  1614. unsigned id;
  1615. for (id = 0; id < nworkers; id++)
  1616. {
  1617. if (starpu_worker_get_type(id) == type)
  1618. {
  1619. /* Perhaps the array is too small ? */
  1620. if (cnt >= maxsize)
  1621. return cnt;
  1622. workerids[cnt++] = id;
  1623. }
  1624. }
  1625. return cnt;
  1626. }
  1627. int starpu_worker_get_nids_ctx_free_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize)
  1628. {
  1629. unsigned nworkers = starpu_worker_get_count();
  1630. int cnt = 0;
  1631. unsigned id, worker;
  1632. unsigned found = 0;
  1633. for (id = 0; id < nworkers; id++)
  1634. {
  1635. found = 0;
  1636. if (starpu_worker_get_type(id) == type)
  1637. {
  1638. /* Perhaps the array is too small ? */
  1639. if (cnt >= maxsize)
  1640. return cnt;
  1641. int s;
  1642. for(s = 1; s < STARPU_NMAX_SCHED_CTXS; s++)
  1643. {
  1644. if(_starpu_config.sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  1645. {
  1646. struct starpu_worker_collection *workers = _starpu_config.sched_ctxs[s].workers;
  1647. struct starpu_sched_ctx_iterator it;
  1648. workers->init_iterator(workers, &it);
  1649. while(workers->has_next(workers, &it))
  1650. {
  1651. worker = workers->get_next(workers, &it);
  1652. if(worker == id)
  1653. {
  1654. found = 1;
  1655. break;
  1656. }
  1657. }
  1658. if(found) break;
  1659. }
  1660. }
  1661. if(!found)
  1662. workerids[cnt++] = id;
  1663. }
  1664. }
  1665. return cnt;
  1666. }
  1667. int
  1668. starpu_driver_run(struct starpu_driver *d)
  1669. {
  1670. if (!d)
  1671. {
  1672. _STARPU_DEBUG("Invalid argument\n");
  1673. return -EINVAL;
  1674. }
  1675. void *worker = _starpu_get_worker_from_driver(d);
  1676. switch (d->type)
  1677. {
  1678. #ifdef STARPU_USE_CPU
  1679. case STARPU_CPU_WORKER:
  1680. return _starpu_run_cpu(worker);
  1681. #endif
  1682. #ifdef STARPU_USE_CUDA
  1683. case STARPU_CUDA_WORKER:
  1684. return _starpu_run_cuda(worker);
  1685. #endif
  1686. #ifdef STARPU_USE_OPENCL
  1687. case STARPU_OPENCL_WORKER:
  1688. return _starpu_run_opencl(worker);
  1689. #endif
  1690. default:
  1691. _STARPU_DEBUG("Invalid device type\n");
  1692. return -EINVAL;
  1693. }
  1694. }
  1695. int
  1696. starpu_driver_init(struct starpu_driver *d)
  1697. {
  1698. STARPU_ASSERT(d);
  1699. void *worker = _starpu_get_worker_from_driver(d);
  1700. switch (d->type)
  1701. {
  1702. #ifdef STARPU_USE_CPU
  1703. case STARPU_CPU_WORKER:
  1704. return _starpu_cpu_driver_init(worker);
  1705. #endif
  1706. #ifdef STARPU_USE_CUDA
  1707. case STARPU_CUDA_WORKER:
  1708. return _starpu_cuda_driver_init(worker);
  1709. #endif
  1710. #ifdef STARPU_USE_OPENCL
  1711. case STARPU_OPENCL_WORKER:
  1712. return _starpu_opencl_driver_init(worker);
  1713. #endif
  1714. default:
  1715. return -EINVAL;
  1716. }
  1717. }
  1718. int
  1719. starpu_driver_run_once(struct starpu_driver *d)
  1720. {
  1721. STARPU_ASSERT(d);
  1722. void *worker = _starpu_get_worker_from_driver(d);
  1723. switch (d->type)
  1724. {
  1725. #ifdef STARPU_USE_CPU
  1726. case STARPU_CPU_WORKER:
  1727. return _starpu_cpu_driver_run_once(worker);
  1728. #endif
  1729. #ifdef STARPU_USE_CUDA
  1730. case STARPU_CUDA_WORKER:
  1731. return _starpu_cuda_driver_run_once(worker);
  1732. #endif
  1733. #ifdef STARPU_USE_OPENCL
  1734. case STARPU_OPENCL_WORKER:
  1735. return _starpu_opencl_driver_run_once(worker);
  1736. #endif
  1737. default:
  1738. return -EINVAL;
  1739. }
  1740. }
  1741. int
  1742. starpu_driver_deinit(struct starpu_driver *d)
  1743. {
  1744. STARPU_ASSERT(d);
  1745. void *worker = _starpu_get_worker_from_driver(d);
  1746. switch (d->type)
  1747. {
  1748. #ifdef STARPU_USE_CPU
  1749. case STARPU_CPU_WORKER:
  1750. return _starpu_cpu_driver_deinit(worker);
  1751. #endif
  1752. #ifdef STARPU_USE_CUDA
  1753. case STARPU_CUDA_WORKER:
  1754. return _starpu_cuda_driver_deinit(worker);
  1755. #endif
  1756. #ifdef STARPU_USE_OPENCL
  1757. case STARPU_OPENCL_WORKER:
  1758. return _starpu_opencl_driver_deinit(worker);
  1759. #endif
  1760. default:
  1761. return -EINVAL;
  1762. }
  1763. }
  1764. void starpu_get_version(int *major, int *minor, int *release)
  1765. {
  1766. *major = STARPU_MAJOR_VERSION;
  1767. *minor = STARPU_MINOR_VERSION;
  1768. *release = STARPU_RELEASE_VERSION;
  1769. }
  1770. void _starpu_unlock_mutex_if_prev_locked()
  1771. {
  1772. int workerid = starpu_worker_get_id();
  1773. if(workerid != -1)
  1774. {
  1775. struct _starpu_worker *w = _starpu_get_worker_struct(workerid);
  1776. if(w->sched_mutex_locked)
  1777. {
  1778. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&w->sched_mutex);
  1779. _starpu_worker_set_flag_sched_mutex_locked(workerid, 1);
  1780. }
  1781. }
  1782. return;
  1783. }
  1784. void _starpu_relock_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. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&w->sched_mutex);
  1792. }
  1793. return;
  1794. }
  1795. unsigned starpu_worker_get_sched_ctx_list(int workerid, unsigned **sched_ctxs)
  1796. {
  1797. unsigned s = 0;
  1798. unsigned nsched_ctxs = _starpu_worker_get_nsched_ctxs(workerid);
  1799. *sched_ctxs = (unsigned*)malloc(nsched_ctxs*sizeof(unsigned));
  1800. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1801. struct _starpu_sched_ctx_elt *e = NULL;
  1802. struct _starpu_sched_ctx_list_iterator list_it;
  1803. _starpu_sched_ctx_list_iterator_init(worker->sched_ctx_list, &list_it);
  1804. while (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  1805. {
  1806. e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  1807. (*sched_ctxs)[s++] = e->sched_ctx;
  1808. }
  1809. return nsched_ctxs;
  1810. }
  1811. char *starpu_worker_get_type_as_string(enum starpu_worker_archtype type)
  1812. {
  1813. if (type == STARPU_CPU_WORKER) return "STARPU_CPU_WORKER";
  1814. if (type == STARPU_CUDA_WORKER) return "STARPU_CUDA_WORKER";
  1815. if (type == STARPU_OPENCL_WORKER) return "STARPU_OPENCL_WORKER";
  1816. if (type == STARPU_MIC_WORKER) return "STARPU_MIC_WORKER";
  1817. if (type == STARPU_SCC_WORKER) return "STARPU_SCC_WORKER";
  1818. if (type == STARPU_ANY_WORKER) return "STARPU_ANY_WORKER";
  1819. return "STARPU_unknown_WORKER";
  1820. }