workers.c 52 KB

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