workers.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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/task.h>
  28. #include <profiling/profiling.h>
  29. #include <starpu_task_list.h>
  30. #include <drivers/cpu/driver_cpu.h>
  31. #include <drivers/cuda/driver_cuda.h>
  32. #include <drivers/opencl/driver_opencl.h>
  33. #ifdef STARPU_SIMGRID
  34. #include <msg/msg.h>
  35. #endif
  36. #ifdef __MINGW32__
  37. #include <windows.h>
  38. #endif
  39. /* acquire/release semantic for concurrent initialization/de-initialization */
  40. static starpu_pthread_mutex_t init_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  41. static starpu_pthread_cond_t init_cond = STARPU_PTHREAD_COND_INITIALIZER;
  42. static int init_count = 0;
  43. static enum { UNINITIALIZED, CHANGING, INITIALIZED } initialized = UNINITIALIZED;
  44. static starpu_pthread_key_t worker_key;
  45. static struct _starpu_machine_config config;
  46. int _starpu_is_initialized(void)
  47. {
  48. return initialized == INITIALIZED;
  49. }
  50. struct _starpu_machine_config *_starpu_get_machine_config(void)
  51. {
  52. return &config;
  53. }
  54. /* Makes sure that at least one of the workers of type <arch> can execute
  55. * <task>, for at least one of its implementations. */
  56. static uint32_t _starpu_worker_exists_and_can_execute(struct starpu_task *task,
  57. enum starpu_worker_archtype arch)
  58. {
  59. int i;
  60. int nworkers = starpu_worker_get_count();
  61. _starpu_codelet_check_deprecated_fields(task->cl);
  62. for (i = 0; i < nworkers; i++)
  63. {
  64. if (starpu_worker_get_type(i) != arch)
  65. continue;
  66. unsigned impl;
  67. for (impl = 0; impl < STARPU_MAXIMPLEMENTATIONS; impl++)
  68. {
  69. /* We could call task->cl->can_execute(i, task, impl)
  70. here, it would definitely work. It is probably
  71. cheaper to check whether it is necessary in order to
  72. avoid a useless function call, though. */
  73. unsigned test_implementation = 0;
  74. switch (arch)
  75. {
  76. case STARPU_CPU_WORKER:
  77. if (task->cl->cpu_funcs[impl] != NULL)
  78. test_implementation = 1;
  79. break;
  80. case STARPU_CUDA_WORKER:
  81. if (task->cl->cuda_funcs[impl] != NULL)
  82. test_implementation = 1;
  83. break;
  84. case STARPU_OPENCL_WORKER:
  85. if (task->cl->opencl_funcs[impl] != NULL)
  86. test_implementation = 1;
  87. break;
  88. default:
  89. STARPU_ABORT();
  90. }
  91. if (!test_implementation)
  92. break;
  93. if (task->cl->can_execute(i, task, impl))
  94. return 1;
  95. }
  96. }
  97. return 0;
  98. }
  99. /* in case a task is submitted, we may check whether there exists a worker
  100. that may execute the task or not */
  101. uint32_t _starpu_worker_exists(struct starpu_task *task)
  102. {
  103. _starpu_codelet_check_deprecated_fields(task->cl);
  104. if (!(task->cl->where & config.worker_mask))
  105. return 0;
  106. if (!task->cl->can_execute)
  107. return 1;
  108. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  109. if ((task->cl->where & STARPU_CPU) &&
  110. _starpu_worker_exists_and_can_execute(task, STARPU_CPU_WORKER))
  111. return 1;
  112. #endif
  113. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  114. if ((task->cl->where & STARPU_CUDA) &&
  115. _starpu_worker_exists_and_can_execute(task, STARPU_CUDA_WORKER))
  116. return 1;
  117. #endif
  118. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  119. if ((task->cl->where & STARPU_OPENCL) &&
  120. _starpu_worker_exists_and_can_execute(task, STARPU_OPENCL_WORKER))
  121. return 1;
  122. #endif
  123. return 0;
  124. }
  125. uint32_t _starpu_can_submit_cuda_task(void)
  126. {
  127. return (STARPU_CUDA & config.worker_mask);
  128. }
  129. uint32_t _starpu_can_submit_cpu_task(void)
  130. {
  131. return (STARPU_CPU & config.worker_mask);
  132. }
  133. uint32_t _starpu_can_submit_opencl_task(void)
  134. {
  135. return (STARPU_OPENCL & config.worker_mask);
  136. }
  137. static int _starpu_can_use_nth_implementation(enum starpu_worker_archtype arch, struct starpu_codelet *cl, unsigned nimpl)
  138. {
  139. switch(arch)
  140. {
  141. case STARPU_ANY_WORKER:
  142. {
  143. int cpu_func_enabled=1, cuda_func_enabled=1, opencl_func_enabled=1;
  144. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  145. starpu_cpu_func_t cpu_func = _starpu_task_get_cpu_nth_implementation(cl, nimpl);
  146. cpu_func_enabled = cpu_func != NULL && starpu_cpu_worker_get_count();
  147. #endif
  148. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  149. starpu_cuda_func_t cuda_func = _starpu_task_get_cuda_nth_implementation(cl, nimpl);
  150. cuda_func_enabled = cuda_func != NULL && starpu_cuda_worker_get_count();
  151. #endif
  152. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  153. starpu_opencl_func_t opencl_func = _starpu_task_get_opencl_nth_implementation(cl, nimpl);
  154. opencl_func_enabled = opencl_func != NULL && starpu_opencl_worker_get_count();
  155. #endif
  156. return (cpu_func_enabled && cuda_func_enabled && opencl_func_enabled);
  157. }
  158. case STARPU_CPU_WORKER:
  159. {
  160. starpu_cpu_func_t func = _starpu_task_get_cpu_nth_implementation(cl, nimpl);
  161. return func != NULL;
  162. }
  163. case STARPU_CUDA_WORKER:
  164. {
  165. starpu_cuda_func_t func = _starpu_task_get_cuda_nth_implementation(cl, nimpl);
  166. return func != NULL;
  167. }
  168. case STARPU_OPENCL_WORKER:
  169. {
  170. starpu_opencl_func_t func = _starpu_task_get_opencl_nth_implementation(cl, nimpl);
  171. return func != NULL;
  172. }
  173. default:
  174. STARPU_ASSERT_MSG(0, "Unknown arch type %d", arch);
  175. }
  176. return 0;
  177. }
  178. int starpu_worker_can_execute_task(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  179. {
  180. /* TODO: check that the task operand sizes will fit on that device */
  181. return (task->cl->where & config.workers[workerid].worker_mask) &&
  182. _starpu_can_use_nth_implementation(config.workers[workerid].arch, task->cl, nimpl) &&
  183. (!task->cl->can_execute || task->cl->can_execute(workerid, task, nimpl));
  184. }
  185. int starpu_combined_worker_can_execute_task(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  186. {
  187. /* TODO: check that the task operand sizes will fit on that device */
  188. /* TODO: call application-provided function for various cases like
  189. * double support, shared memory size limit, etc. */
  190. struct starpu_codelet *cl = task->cl;
  191. unsigned nworkers = config.topology.nworkers;
  192. /* Is this a parallel worker ? */
  193. if (workerid < nworkers)
  194. {
  195. return !!((task->cl->where & config.workers[workerid].worker_mask) &&
  196. _starpu_can_use_nth_implementation(config.workers[workerid].arch, task->cl, nimpl));
  197. }
  198. else
  199. {
  200. if ((cl->type == STARPU_SPMD)
  201. #ifdef STARPU_HAVE_HWLOC
  202. || (cl->type == STARPU_FORKJOIN)
  203. #endif
  204. )
  205. {
  206. /* TODO we should add other types of constraints */
  207. /* Is the worker larger than requested ? */
  208. int worker_size = (int)config.combined_workers[workerid - nworkers].worker_size;
  209. return !!((worker_size <= task->cl->max_parallelism) &&
  210. _starpu_can_use_nth_implementation(config.workers[workerid].arch, task->cl, nimpl));
  211. }
  212. else
  213. {
  214. /* We have a sequential task but a parallel worker */
  215. return 0;
  216. }
  217. }
  218. }
  219. /*
  220. * Runtime initialization methods
  221. */
  222. static void _starpu_init_worker_queue(struct _starpu_worker *workerarg)
  223. {
  224. starpu_pthread_cond_t *cond = &workerarg->sched_cond;
  225. starpu_pthread_mutex_t *mutex = &workerarg->sched_mutex;
  226. unsigned memory_node = workerarg->memory_node;
  227. _starpu_memory_node_register_condition(cond, mutex, memory_node);
  228. }
  229. /*
  230. * Returns 0 if the given driver is one of the drivers that must be launched by
  231. * the application itself, and not by StarPU, 1 otherwise.
  232. */
  233. static unsigned _starpu_may_launch_driver(struct starpu_conf *conf,
  234. struct starpu_driver *d)
  235. {
  236. if (conf->n_not_launched_drivers == 0 ||
  237. conf->not_launched_drivers == NULL)
  238. return 1;
  239. /* Is <d> in conf->not_launched_drivers ? */
  240. unsigned i;
  241. for (i = 0; i < conf->n_not_launched_drivers; i++)
  242. {
  243. if (d->type != conf->not_launched_drivers[i].type)
  244. continue;
  245. switch (d->type)
  246. {
  247. case STARPU_CPU_WORKER:
  248. if (d->id.cpu_id == conf->not_launched_drivers[i].id.cpu_id)
  249. return 0;
  250. case STARPU_CUDA_WORKER:
  251. if (d->id.cuda_id == conf->not_launched_drivers[i].id.cuda_id)
  252. return 0;
  253. break;
  254. #ifdef STARPU_USE_OPENCL
  255. case STARPU_OPENCL_WORKER:
  256. if (d->id.opencl_id == conf->not_launched_drivers[i].id.opencl_id)
  257. return 0;
  258. break;
  259. #endif
  260. default:
  261. STARPU_ABORT();
  262. }
  263. }
  264. return 1;
  265. }
  266. #ifdef STARPU_PERF_DEBUG
  267. struct itimerval prof_itimer;
  268. #endif
  269. void _starpu_worker_init(struct _starpu_worker *worker, unsigned fut_key)
  270. {
  271. (void) fut_key;
  272. int devid = worker->devid;
  273. (void) devid;
  274. #if defined(STARPU_PERF_DEBUG) && !defined(STARPU_SIMGRID)
  275. setitimer(ITIMER_PROF, &prof_itimer, NULL);
  276. #endif
  277. #ifdef STARPU_USE_FXT
  278. _starpu_fxt_register_thread(worker->bindid);
  279. unsigned memnode = worker->memory_node;
  280. _STARPU_TRACE_WORKER_INIT_START(fut_key, worker->workerid, devid, memnode);
  281. #endif
  282. _starpu_bind_thread_on_cpu(worker->config, worker->bindid);
  283. _STARPU_DEBUG("worker %d is ready on logical cpu %d\n", devid, worker->bindid);
  284. #ifdef STARPU_HAVE_HWLOC
  285. _STARPU_DEBUG("worker %d cpuset start at %d\n", devid, hwloc_bitmap_first(worker->initial_hwloc_cpu_set));
  286. #endif
  287. _starpu_memory_node_set_local_key(&worker->memory_node);
  288. _starpu_set_local_worker_key(worker);
  289. STARPU_PTHREAD_MUTEX_LOCK(&worker->mutex);
  290. worker->worker_is_running = 1;
  291. STARPU_PTHREAD_COND_SIGNAL(&worker->started_cond);
  292. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->mutex);
  293. }
  294. static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
  295. {
  296. pconfig->running = 1;
  297. pconfig->submitting = 1;
  298. STARPU_PTHREAD_KEY_CREATE(&worker_key, NULL);
  299. unsigned nworkers = pconfig->topology.nworkers;
  300. /* Launch workers asynchronously */
  301. unsigned cpu = 0, cuda = 0;
  302. unsigned worker;
  303. #if defined(STARPU_PERF_DEBUG) && !defined(STARPU_SIMGRID)
  304. /* Get itimer of the main thread, to set it for the worker threads */
  305. getitimer(ITIMER_PROF, &prof_itimer);
  306. #endif
  307. #ifdef HAVE_AYUDAME_H
  308. if (AYU_event)
  309. {
  310. unsigned long n = nworkers;
  311. AYU_event(AYU_INIT, 0, (void*) &n);
  312. }
  313. #endif
  314. for (worker = 0; worker < nworkers; worker++)
  315. {
  316. struct _starpu_worker *workerarg = &pconfig->workers[worker];
  317. workerarg->config = pconfig;
  318. _starpu_barrier_counter_init(&workerarg->tasks_barrier, 0);
  319. STARPU_PTHREAD_MUTEX_INIT(&workerarg->mutex, NULL);
  320. STARPU_PTHREAD_COND_INIT(&workerarg->started_cond, NULL);
  321. STARPU_PTHREAD_COND_INIT(&workerarg->ready_cond, NULL);
  322. workerarg->worker_size = 1;
  323. workerarg->combined_workerid = workerarg->workerid;
  324. workerarg->current_rank = 0;
  325. workerarg->has_prev_init = 0;
  326. /* mutex + cond only for the local list */
  327. /* we have a single local list */
  328. /* afterwards there would be a mutex + cond for the list of each strategy */
  329. workerarg->run_by_starpu = 1;
  330. workerarg->worker_is_running = 0;
  331. workerarg->worker_is_initialized = 0;
  332. int ctx;
  333. for(ctx = 0; ctx < STARPU_NMAX_SCHED_CTXS; ctx++)
  334. workerarg->removed_from_ctx[ctx] = 0;
  335. STARPU_PTHREAD_MUTEX_INIT(&workerarg->sched_mutex, NULL);
  336. STARPU_PTHREAD_COND_INIT(&workerarg->sched_cond, NULL);
  337. STARPU_PTHREAD_MUTEX_INIT(&workerarg->parallel_sect_mutex, NULL);
  338. STARPU_PTHREAD_COND_INIT(&workerarg->parallel_sect_cond, NULL);
  339. workerarg->parallel_sect = 0;
  340. /* if some codelet's termination cannot be handled directly :
  341. * for instance in the Gordon driver, Gordon tasks' callbacks
  342. * may be executed by another thread than that of the Gordon
  343. * driver so that we cannot call the push_codelet_output method
  344. * directly */
  345. workerarg->terminated_jobs = _starpu_job_list_new();
  346. starpu_task_list_init(&workerarg->local_tasks);
  347. workerarg->status = STATUS_INITIALIZING;
  348. _STARPU_DEBUG("initialising worker %u\n", worker);
  349. _starpu_init_worker_queue(workerarg);
  350. struct starpu_driver driver;
  351. driver.type = workerarg->arch;
  352. switch (workerarg->arch)
  353. {
  354. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  355. case STARPU_CPU_WORKER:
  356. workerarg->set = NULL;
  357. driver.id.cpu_id = cpu;
  358. if (_starpu_may_launch_driver(pconfig->conf, &driver))
  359. {
  360. STARPU_PTHREAD_CREATE_ON(
  361. workerarg->name,
  362. &workerarg->worker_thread,
  363. NULL,
  364. _starpu_cpu_worker,
  365. workerarg,
  366. worker+1);
  367. #ifdef STARPU_USE_FXT
  368. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  369. while (!workerarg->worker_is_running)
  370. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  371. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  372. #endif
  373. }
  374. else
  375. {
  376. workerarg->run_by_starpu = 0;
  377. }
  378. cpu++;
  379. break;
  380. #endif
  381. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  382. case STARPU_CUDA_WORKER:
  383. workerarg->set = NULL;
  384. driver.id.cuda_id = cuda;
  385. if (_starpu_may_launch_driver(pconfig->conf, &driver))
  386. {
  387. STARPU_PTHREAD_CREATE_ON(
  388. workerarg->name,
  389. &workerarg->worker_thread,
  390. NULL,
  391. _starpu_cuda_worker,
  392. workerarg,
  393. worker+1);
  394. #ifdef STARPU_USE_FXT
  395. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  396. while (!workerarg->worker_is_running)
  397. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  398. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  399. #endif
  400. }
  401. else
  402. {
  403. workerarg->run_by_starpu = 0;
  404. }
  405. cuda++;
  406. break;
  407. #endif
  408. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  409. case STARPU_OPENCL_WORKER:
  410. #ifndef STARPU_SIMGRID
  411. starpu_opencl_get_device(workerarg->devid, &driver.id.opencl_id);
  412. if (!_starpu_may_launch_driver(pconfig->conf, &driver))
  413. {
  414. workerarg->run_by_starpu = 0;
  415. break;
  416. }
  417. #endif
  418. workerarg->set = NULL;
  419. STARPU_PTHREAD_CREATE_ON(
  420. workerarg->name,
  421. &workerarg->worker_thread,
  422. NULL,
  423. _starpu_opencl_worker,
  424. workerarg,
  425. worker+1);
  426. #ifdef STARPU_USE_FXT
  427. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  428. while (!workerarg->worker_is_running)
  429. STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  430. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  431. #endif
  432. break;
  433. #endif
  434. default:
  435. STARPU_ABORT();
  436. }
  437. }
  438. cpu = 0;
  439. cuda = 0;
  440. for (worker = 0; worker < nworkers; worker++)
  441. {
  442. struct _starpu_worker *workerarg = &pconfig->workers[worker];
  443. struct starpu_driver driver;
  444. driver.type = workerarg->arch;
  445. switch (workerarg->arch)
  446. {
  447. case STARPU_CPU_WORKER:
  448. driver.id.cpu_id = cpu;
  449. if (!_starpu_may_launch_driver(pconfig->conf, &driver))
  450. {
  451. cpu++;
  452. break;
  453. }
  454. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  455. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  456. while (!workerarg->worker_is_initialized)
  457. STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  458. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  459. cpu++;
  460. break;
  461. case STARPU_CUDA_WORKER:
  462. driver.id.cuda_id = cuda;
  463. if (!_starpu_may_launch_driver(pconfig->conf, &driver))
  464. {
  465. cuda++;
  466. break;
  467. }
  468. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  469. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  470. while (!workerarg->worker_is_initialized)
  471. STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  472. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  473. cuda++;
  474. break;
  475. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  476. case STARPU_OPENCL_WORKER:
  477. #ifndef STARPU_SIMGRID
  478. starpu_opencl_get_device(workerarg->devid, &driver.id.opencl_id);
  479. if (!_starpu_may_launch_driver(pconfig->conf, &driver))
  480. break;
  481. #endif
  482. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  483. STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  484. while (!workerarg->worker_is_initialized)
  485. STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  486. STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  487. break;
  488. #endif
  489. default:
  490. STARPU_ABORT();
  491. }
  492. }
  493. _STARPU_DEBUG("finished launching drivers\n");
  494. }
  495. void _starpu_set_local_worker_key(struct _starpu_worker *worker)
  496. {
  497. STARPU_PTHREAD_SETSPECIFIC(worker_key, worker);
  498. }
  499. struct _starpu_worker *_starpu_get_local_worker_key(void)
  500. {
  501. return (struct _starpu_worker *) STARPU_PTHREAD_GETSPECIFIC(worker_key);
  502. }
  503. /* Initialize the starpu_conf with default values */
  504. int starpu_conf_init(struct starpu_conf *conf)
  505. {
  506. if (!conf)
  507. return -EINVAL;
  508. memset(conf, 0, sizeof(*conf));
  509. conf->magic = 42;
  510. conf->sched_policy_name = getenv("STARPU_SCHED");
  511. conf->sched_policy = NULL;
  512. /* Note that starpu_get_env_number returns -1 in case the variable is
  513. * not defined */
  514. /* Backward compatibility: check the value of STARPU_NCPUS if
  515. * STARPU_NCPU is not set. */
  516. conf->ncpus = starpu_get_env_number("STARPU_NCPU");
  517. if (conf->ncpus == -1)
  518. conf->ncpus = starpu_get_env_number("STARPU_NCPUS");
  519. conf->ncuda = starpu_get_env_number("STARPU_NCUDA");
  520. conf->nopencl = starpu_get_env_number("STARPU_NOPENCL");
  521. conf->calibrate = starpu_get_env_number("STARPU_CALIBRATE");
  522. conf->bus_calibrate = starpu_get_env_number("STARPU_BUS_CALIBRATE");
  523. if (conf->calibrate == -1)
  524. conf->calibrate = 0;
  525. if (conf->bus_calibrate == -1)
  526. conf->bus_calibrate = 0;
  527. conf->use_explicit_workers_bindid = 0; /* TODO */
  528. conf->use_explicit_workers_cuda_gpuid = 0; /* TODO */
  529. conf->use_explicit_workers_opencl_gpuid = 0; /* TODO */
  530. conf->single_combined_worker = starpu_get_env_number("STARPU_SINGLE_COMBINED_WORKER");
  531. if (conf->single_combined_worker == -1)
  532. conf->single_combined_worker = 0;
  533. #if defined(STARPU_DISABLE_ASYNCHRONOUS_COPY)
  534. conf->disable_asynchronous_copy = 1;
  535. #else
  536. conf->disable_asynchronous_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_COPY");
  537. if (conf->disable_asynchronous_copy == -1)
  538. conf->disable_asynchronous_copy = 0;
  539. #endif
  540. #if defined(STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY)
  541. conf->disable_asynchronous_cuda_copy = 1;
  542. #else
  543. conf->disable_asynchronous_cuda_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY");
  544. if (conf->disable_asynchronous_cuda_copy == -1)
  545. conf->disable_asynchronous_cuda_copy = 0;
  546. #endif
  547. #if defined(STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY)
  548. conf->disable_asynchronous_opencl_copy = 1;
  549. #else
  550. conf->disable_asynchronous_opencl_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY");
  551. if (conf->disable_asynchronous_opencl_copy == -1)
  552. conf->disable_asynchronous_opencl_copy = 0;
  553. #endif
  554. /* 64MiB by default */
  555. conf->trace_buffer_size = 64<<20;
  556. return 0;
  557. }
  558. static void _starpu_conf_set_value_against_environment(char *name, int *value)
  559. {
  560. int number;
  561. number = starpu_get_env_number(name);
  562. if (number != -1)
  563. {
  564. *value = number;
  565. }
  566. }
  567. void _starpu_conf_check_environment(struct starpu_conf *conf)
  568. {
  569. char *sched = getenv("STARPU_SCHED");
  570. if (sched)
  571. {
  572. conf->sched_policy_name = sched;
  573. }
  574. _starpu_conf_set_value_against_environment("STARPU_NCPUS", &conf->ncpus);
  575. _starpu_conf_set_value_against_environment("STARPU_NCPU", &conf->ncpus);
  576. _starpu_conf_set_value_against_environment("STARPU_NCUDA", &conf->ncuda);
  577. _starpu_conf_set_value_against_environment("STARPU_NOPENCL", &conf->nopencl);
  578. _starpu_conf_set_value_against_environment("STARPU_CALIBRATE", &conf->calibrate);
  579. _starpu_conf_set_value_against_environment("STARPU_BUS_CALIBRATE", &conf->bus_calibrate);
  580. _starpu_conf_set_value_against_environment("STARPU_SINGLE_COMBINED_WORKER", &conf->single_combined_worker);
  581. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_COPY", &conf->disable_asynchronous_copy);
  582. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY", &conf->disable_asynchronous_cuda_copy);
  583. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY", &conf->disable_asynchronous_opencl_copy);
  584. }
  585. int starpu_init(struct starpu_conf *user_conf)
  586. {
  587. int ret;
  588. #ifndef STARPU_SIMGRID
  589. #ifdef __GNUC__
  590. #ifndef __OPTIMIZE__
  591. _STARPU_DISP("Warning: StarPU was configured with --enable-debug (-O0), and is thus not optimized\n");
  592. #endif
  593. #endif
  594. #if 0
  595. #ifndef STARPU_NO_ASSERT
  596. _STARPU_DISP("Warning: StarPU was configured without --enable-fast\n");
  597. #endif
  598. #endif
  599. #ifdef STARPU_MEMORY_STATS
  600. _STARPU_DISP("Warning: StarPU was configured with --enable-memory-stats, which slows down a bit\n");
  601. #endif
  602. #ifdef STARPU_VERBOSE
  603. _STARPU_DISP("Warning: StarPU was configured with --enable-verbose, which slows down a bit\n");
  604. #endif
  605. #ifdef STARPU_USE_FXT
  606. _STARPU_DISP("Warning: StarPU was configured with --with-fxt, which slows down a bit\n");
  607. #endif
  608. #ifdef STARPU_PERF_DEBUG
  609. _STARPU_DISP("Warning: StarPU was configured with --enable-perf-debug, which slows down a bit\n");
  610. #endif
  611. #ifdef STARPU_MODEL_DEBUG
  612. _STARPU_DISP("Warning: StarPU was configured with --enable-model-debug, which slows down a bit\n");
  613. #endif
  614. #ifdef STARPU_ENABLE_STATS
  615. _STARPU_DISP("Warning: StarPU was configured with --enable-stats, which slows down a bit\n");
  616. #endif
  617. #endif
  618. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  619. while (initialized == CHANGING)
  620. /* Wait for the other one changing it */
  621. STARPU_PTHREAD_COND_WAIT(&init_cond, &init_mutex);
  622. init_count++;
  623. if (initialized == INITIALIZED)
  624. {
  625. /* He initialized it, don't do it again, and let the others get the mutex */
  626. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  627. return 0;
  628. }
  629. /* initialized == UNINITIALIZED */
  630. initialized = CHANGING;
  631. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  632. #ifdef __MINGW32__
  633. WSADATA wsadata;
  634. WSAStartup(MAKEWORD(1,0), &wsadata);
  635. #endif
  636. srand(2008);
  637. #ifdef HAVE_AYUDAME_H
  638. #ifndef AYU_RT_STARPU
  639. /* Dumb value for now */
  640. #define AYU_RT_STARPU 32
  641. #endif
  642. if (AYU_event)
  643. {
  644. enum ayu_runtime_t ayu_rt = AYU_RT_STARPU;
  645. AYU_event(AYU_PREINIT, 0, (void*) &ayu_rt);
  646. }
  647. #endif
  648. /* store the pointer to the user explicit configuration during the
  649. * initialization */
  650. if (user_conf == NULL)
  651. {
  652. struct starpu_conf *conf = malloc(sizeof(struct starpu_conf));
  653. starpu_conf_init(conf);
  654. config.conf = conf;
  655. config.default_conf = 1;
  656. }
  657. else
  658. {
  659. if (user_conf->magic != 42)
  660. {
  661. _STARPU_DISP("starpu_conf structure needs to be initialized with starpu_conf_init\n");
  662. return -EINVAL;
  663. }
  664. config.conf = user_conf;
  665. config.default_conf = 0;
  666. }
  667. _starpu_conf_check_environment(config.conf);
  668. _starpu_init_all_sched_ctxs(&config);
  669. _starpu_init_progression_hooks();
  670. _starpu_init_tags();
  671. #ifdef STARPU_USE_FXT
  672. _starpu_init_fxt_profiling(config.conf->trace_buffer_size);
  673. #endif
  674. _starpu_open_debug_logfile();
  675. _starpu_data_interface_init();
  676. _starpu_timing_init();
  677. _starpu_profiling_init();
  678. _starpu_load_bus_performance_files();
  679. ret = _starpu_build_topology(&config);
  680. if (ret)
  681. {
  682. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  683. init_count--;
  684. initialized = UNINITIALIZED;
  685. /* Let somebody else try to do it */
  686. STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  687. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  688. return ret;
  689. }
  690. /* We need to store the current task handled by the different
  691. * threads */
  692. _starpu_initialize_current_task_key();
  693. _starpu_create_sched_ctx(config.conf->sched_policy_name, NULL, -1, 1, "init");
  694. _starpu_initialize_registered_performance_models();
  695. /* Launch "basic" workers (ie. non-combined workers) */
  696. _starpu_launch_drivers(&config);
  697. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  698. initialized = INITIALIZED;
  699. /* Tell everybody that we initialized */
  700. STARPU_PTHREAD_COND_BROADCAST(&init_cond);
  701. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  702. _STARPU_DEBUG("Initialisation finished\n");
  703. return 0;
  704. }
  705. void starpu_profiling_init()
  706. {
  707. _starpu_profiling_init();
  708. }
  709. /*
  710. * Handle runtime termination
  711. */
  712. static void _starpu_terminate_workers(struct _starpu_machine_config *pconfig)
  713. {
  714. int status = 0;
  715. unsigned workerid;
  716. for (workerid = 0; workerid < pconfig->topology.nworkers; workerid++)
  717. {
  718. starpu_wake_all_blocked_workers();
  719. _STARPU_DEBUG("wait for worker %u\n", workerid);
  720. struct _starpu_worker_set *set = pconfig->workers[workerid].set;
  721. struct _starpu_worker *worker = &pconfig->workers[workerid];
  722. /* in case StarPU termination code is called from a callback,
  723. * we have to check if pthread_self() is the worker itself */
  724. if (set)
  725. {
  726. if (!set->joined)
  727. {
  728. #ifdef STARPU_SIMGRID
  729. status = starpu_pthread_join(set->worker_thread, NULL);
  730. #else
  731. if (!pthread_equal(pthread_self(), set->worker_thread))
  732. status = starpu_pthread_join(set->worker_thread, NULL);
  733. #endif
  734. #ifdef STARPU_VERBOSE
  735. if (status)
  736. {
  737. _STARPU_DEBUG("starpu_pthread_join -> %d\n", status);
  738. }
  739. #endif
  740. set->joined = 1;
  741. }
  742. }
  743. else
  744. {
  745. if (!worker->run_by_starpu)
  746. goto out;
  747. #ifdef STARPU_SIMGRID
  748. status = starpu_pthread_join(worker->worker_thread, NULL);
  749. #else
  750. if (!pthread_equal(pthread_self(), worker->worker_thread))
  751. status = starpu_pthread_join(worker->worker_thread, NULL);
  752. #endif
  753. #ifdef STARPU_VERBOSE
  754. if (status)
  755. {
  756. _STARPU_DEBUG("starpu_pthread_join -> %d\n", status);
  757. }
  758. #endif
  759. }
  760. out:
  761. STARPU_ASSERT(starpu_task_list_empty(&worker->local_tasks));
  762. _starpu_delete_sched_ctx_for_worker(workerid);
  763. _starpu_job_list_delete(worker->terminated_jobs);
  764. }
  765. }
  766. unsigned _starpu_machine_is_running(void)
  767. {
  768. unsigned ret;
  769. /* running is just protected by a memory barrier */
  770. STARPU_RMB();
  771. ANNOTATE_HAPPENS_AFTER(&config.running);
  772. ret = config.running;
  773. ANNOTATE_HAPPENS_BEFORE(&config.running);
  774. return ret;
  775. }
  776. unsigned _starpu_worker_can_block(unsigned memnode STARPU_ATTRIBUTE_UNUSED)
  777. {
  778. #ifdef STARPU_NON_BLOCKING_DRIVERS
  779. return 0;
  780. #else
  781. unsigned can_block = 1;
  782. #ifndef STARPU_SIMGRID
  783. if (!_starpu_check_that_no_data_request_exists(memnode))
  784. can_block = 0;
  785. #endif
  786. if (!_starpu_machine_is_running())
  787. can_block = 0;
  788. if (!_starpu_execute_registered_progression_hooks())
  789. can_block = 0;
  790. return can_block;
  791. #endif
  792. }
  793. static void _starpu_kill_all_workers(struct _starpu_machine_config *pconfig)
  794. {
  795. /* set the flag which will tell workers to stop */
  796. ANNOTATE_HAPPENS_AFTER(&config.running);
  797. pconfig->running = 0;
  798. /* running is just protected by a memory barrier */
  799. ANNOTATE_HAPPENS_BEFORE(&config.running);
  800. STARPU_WMB();
  801. starpu_wake_all_blocked_workers();
  802. }
  803. void starpu_display_stats()
  804. {
  805. starpu_profiling_bus_helper_display_summary();
  806. starpu_profiling_worker_helper_display_summary();
  807. }
  808. void starpu_shutdown(void)
  809. {
  810. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  811. init_count--;
  812. if (init_count)
  813. {
  814. _STARPU_DEBUG("Still somebody needing StarPU, don't deinitialize\n");
  815. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  816. return;
  817. }
  818. /* We're last */
  819. initialized = CHANGING;
  820. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  821. starpu_task_wait_for_no_ready();
  822. /* tell all workers to shutdown */
  823. _starpu_kill_all_workers(&config);
  824. {
  825. int stats = starpu_get_env_number("STARPU_STATS");
  826. if (stats != 0)
  827. {
  828. _starpu_display_msi_stats();
  829. _starpu_display_alloc_cache_stats();
  830. _starpu_display_comm_amounts();
  831. }
  832. }
  833. starpu_profiling_bus_helper_display_summary();
  834. starpu_profiling_worker_helper_display_summary();
  835. _starpu_deinitialize_registered_performance_models();
  836. /* wait for their termination */
  837. _starpu_terminate_workers(&config);
  838. {
  839. int stats = starpu_get_env_number("STARPU_MEMORY_STATS");
  840. if (stats != 0)
  841. {
  842. // Display statistics on data which have not been unregistered
  843. starpu_data_display_memory_stats();
  844. }
  845. }
  846. _starpu_delete_all_sched_ctxs();
  847. _starpu_destroy_topology(&config);
  848. #ifdef STARPU_USE_FXT
  849. _starpu_stop_fxt_profiling();
  850. #endif
  851. _starpu_data_interface_shutdown();
  852. /* Drop all remaining tags */
  853. _starpu_tag_clear();
  854. _starpu_close_debug_logfile();
  855. STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  856. initialized = UNINITIALIZED;
  857. /* Let someone else that wants to initialize it again do it */
  858. STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  859. STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  860. /* Clear memory if it was allocated by StarPU */
  861. if (config.default_conf)
  862. free(config.conf);
  863. #ifdef HAVE_AYUDAME_H
  864. if (AYU_event) AYU_event(AYU_FINISH, 0, NULL);
  865. #endif
  866. _STARPU_DEBUG("Shutdown finished\n");
  867. }
  868. unsigned starpu_worker_get_count(void)
  869. {
  870. return config.topology.nworkers;
  871. }
  872. int starpu_worker_get_count_by_type(enum starpu_worker_archtype type)
  873. {
  874. switch (type)
  875. {
  876. case STARPU_CPU_WORKER:
  877. return config.topology.ncpus;
  878. case STARPU_CUDA_WORKER:
  879. return config.topology.ncudagpus;
  880. case STARPU_OPENCL_WORKER:
  881. return config.topology.nopenclgpus;
  882. default:
  883. return -EINVAL;
  884. }
  885. }
  886. unsigned starpu_combined_worker_get_count(void)
  887. {
  888. return config.topology.ncombinedworkers;
  889. }
  890. unsigned starpu_cpu_worker_get_count(void)
  891. {
  892. return config.topology.ncpus;
  893. }
  894. unsigned starpu_cuda_worker_get_count(void)
  895. {
  896. return config.topology.ncudagpus;
  897. }
  898. unsigned starpu_opencl_worker_get_count(void)
  899. {
  900. return config.topology.nopenclgpus;
  901. }
  902. int starpu_asynchronous_copy_disabled(void)
  903. {
  904. return config.conf->disable_asynchronous_copy;
  905. }
  906. int starpu_asynchronous_cuda_copy_disabled(void)
  907. {
  908. return config.conf->disable_asynchronous_cuda_copy;
  909. }
  910. int starpu_asynchronous_opencl_copy_disabled(void)
  911. {
  912. return config.conf->disable_asynchronous_opencl_copy;
  913. }
  914. /* When analyzing performance, it is useful to see what is the processing unit
  915. * that actually performed the task. This function returns the id of the
  916. * processing unit actually executing it, therefore it makes no sense to use it
  917. * within the callbacks of SPU functions for instance. If called by some thread
  918. * that is not controlled by StarPU, starpu_worker_get_id returns -1. */
  919. int starpu_worker_get_id(void)
  920. {
  921. struct _starpu_worker * worker;
  922. worker = _starpu_get_local_worker_key();
  923. if (worker)
  924. {
  925. return worker->workerid;
  926. }
  927. else
  928. {
  929. /* there is no worker associated to that thread, perhaps it is
  930. * a thread from the application or this is some SPU worker */
  931. return -1;
  932. }
  933. }
  934. int starpu_combined_worker_get_id(void)
  935. {
  936. struct _starpu_worker *worker;
  937. worker = _starpu_get_local_worker_key();
  938. if (worker)
  939. {
  940. return worker->combined_workerid;
  941. }
  942. else
  943. {
  944. /* there is no worker associated to that thread, perhaps it is
  945. * a thread from the application or this is some SPU worker */
  946. return -1;
  947. }
  948. }
  949. int starpu_combined_worker_get_size(void)
  950. {
  951. struct _starpu_worker *worker;
  952. worker = _starpu_get_local_worker_key();
  953. if (worker)
  954. {
  955. return worker->worker_size;
  956. }
  957. else
  958. {
  959. /* there is no worker associated to that thread, perhaps it is
  960. * a thread from the application or this is some SPU worker */
  961. return -1;
  962. }
  963. }
  964. int starpu_combined_worker_get_rank(void)
  965. {
  966. struct _starpu_worker *worker;
  967. worker = _starpu_get_local_worker_key();
  968. if (worker)
  969. {
  970. return worker->current_rank;
  971. }
  972. else
  973. {
  974. /* there is no worker associated to that thread, perhaps it is
  975. * a thread from the application or this is some SPU worker */
  976. return -1;
  977. }
  978. }
  979. int starpu_worker_get_devid(int id)
  980. {
  981. return config.workers[id].devid;
  982. }
  983. struct _starpu_worker *_starpu_get_worker_struct(unsigned id)
  984. {
  985. return &config.workers[id];
  986. }
  987. unsigned starpu_worker_is_combined_worker(int id)
  988. {
  989. return id >= (int)config.topology.nworkers;
  990. }
  991. struct _starpu_sched_ctx *_starpu_get_sched_ctx_struct(unsigned id)
  992. {
  993. if (id == STARPU_NMAX_SCHED_CTXS) return NULL;
  994. return &config.sched_ctxs[id];
  995. }
  996. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id)
  997. {
  998. unsigned basic_worker_count = starpu_worker_get_count();
  999. STARPU_ASSERT(id >= basic_worker_count);
  1000. return &config.combined_workers[id - basic_worker_count];
  1001. }
  1002. enum starpu_worker_archtype starpu_worker_get_type(int id)
  1003. {
  1004. return config.workers[id].arch;
  1005. }
  1006. int starpu_worker_get_ids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize)
  1007. {
  1008. unsigned nworkers = starpu_worker_get_count();
  1009. int cnt = 0;
  1010. unsigned id;
  1011. for (id = 0; id < nworkers; id++)
  1012. {
  1013. if (starpu_worker_get_type(id) == type)
  1014. {
  1015. /* Perhaps the array is too small ? */
  1016. if (cnt >= maxsize)
  1017. return -ERANGE;
  1018. workerids[cnt++] = id;
  1019. }
  1020. }
  1021. return cnt;
  1022. }
  1023. int starpu_worker_get_by_type(enum starpu_worker_archtype type, int num)
  1024. {
  1025. unsigned nworkers = starpu_worker_get_count();
  1026. int cnt = 0;
  1027. unsigned id;
  1028. for (id = 0; id < nworkers; id++)
  1029. {
  1030. if (starpu_worker_get_type(id) == type)
  1031. {
  1032. if (num == cnt)
  1033. return id;
  1034. cnt++;
  1035. }
  1036. }
  1037. /* Not found */
  1038. return -1;
  1039. }
  1040. int starpu_worker_get_by_devid(enum starpu_worker_archtype type, int devid)
  1041. {
  1042. unsigned nworkers = starpu_worker_get_count();
  1043. unsigned id;
  1044. for (id = 0; id < nworkers; id++)
  1045. if (starpu_worker_get_type(id) == type && starpu_worker_get_devid(id) == devid)
  1046. return id;
  1047. /* Not found */
  1048. return -1;
  1049. }
  1050. void starpu_worker_get_name(int id, char *dst, size_t maxlen)
  1051. {
  1052. char *name = config.workers[id].name;
  1053. snprintf(dst, maxlen, "%s", name);
  1054. }
  1055. /* Retrieve the status which indicates what the worker is currently doing. */
  1056. enum _starpu_worker_status _starpu_worker_get_status(int workerid)
  1057. {
  1058. return config.workers[workerid].status;
  1059. }
  1060. /* Change the status of the worker which indicates what the worker is currently
  1061. * doing (eg. executing a callback). */
  1062. void _starpu_worker_set_status(int workerid, enum _starpu_worker_status status)
  1063. {
  1064. config.workers[workerid].status = status;
  1065. }
  1066. void starpu_worker_get_sched_condition(int workerid, starpu_pthread_mutex_t **sched_mutex, starpu_pthread_cond_t **sched_cond)
  1067. {
  1068. *sched_cond = &config.workers[workerid].sched_cond;
  1069. *sched_mutex = &config.workers[workerid].sched_mutex;
  1070. }
  1071. int starpu_worker_get_nids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize)
  1072. {
  1073. unsigned nworkers = starpu_worker_get_count();
  1074. int cnt = 0;
  1075. unsigned id;
  1076. for (id = 0; id < nworkers; id++)
  1077. {
  1078. if (starpu_worker_get_type(id) == type)
  1079. {
  1080. /* Perhaps the array is too small ? */
  1081. if (cnt >= maxsize)
  1082. return cnt;
  1083. workerids[cnt++] = id;
  1084. }
  1085. }
  1086. return cnt;
  1087. }
  1088. int starpu_worker_get_nids_ctx_free_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize)
  1089. {
  1090. unsigned nworkers = starpu_worker_get_count();
  1091. int cnt = 0;
  1092. unsigned id, worker;
  1093. unsigned found = 0;
  1094. for (id = 0; id < nworkers; id++)
  1095. {
  1096. found = 0;
  1097. if (starpu_worker_get_type(id) == type)
  1098. {
  1099. /* Perhaps the array is too small ? */
  1100. if (cnt >= maxsize)
  1101. return cnt;
  1102. int s;
  1103. for(s = 1; s < STARPU_NMAX_SCHED_CTXS; s++)
  1104. {
  1105. if(config.sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  1106. {
  1107. struct starpu_worker_collection *workers = config.sched_ctxs[s].workers;
  1108. struct starpu_sched_ctx_iterator it;
  1109. if(workers->init_iterator)
  1110. workers->init_iterator(workers, &it);
  1111. while(workers->has_next(workers, &it))
  1112. {
  1113. worker = workers->get_next(workers, &it);
  1114. if(worker == id)
  1115. {
  1116. found = 1;
  1117. break;
  1118. }
  1119. }
  1120. if(found) break;
  1121. }
  1122. }
  1123. if(!found)
  1124. workerids[cnt++] = id;
  1125. }
  1126. }
  1127. return cnt;
  1128. }
  1129. struct _starpu_sched_ctx* _starpu_get_initial_sched_ctx(void)
  1130. {
  1131. return &config.sched_ctxs[STARPU_GLOBAL_SCHED_CTX];
  1132. }
  1133. int
  1134. starpu_driver_run(struct starpu_driver *d)
  1135. {
  1136. if (!d)
  1137. {
  1138. _STARPU_DEBUG("Invalid argument\n");
  1139. return -EINVAL;
  1140. }
  1141. switch (d->type)
  1142. {
  1143. #ifdef STARPU_USE_CPU
  1144. case STARPU_CPU_WORKER:
  1145. return _starpu_run_cpu(d);
  1146. #endif
  1147. #ifdef STARPU_USE_CUDA
  1148. case STARPU_CUDA_WORKER:
  1149. return _starpu_run_cuda(d);
  1150. #endif
  1151. #ifdef STARPU_USE_OPENCL
  1152. case STARPU_OPENCL_WORKER:
  1153. return _starpu_run_opencl(d);
  1154. #endif
  1155. default:
  1156. {
  1157. _STARPU_DEBUG("Invalid device type\n");
  1158. return -EINVAL;
  1159. }
  1160. }
  1161. }
  1162. int
  1163. starpu_driver_init(struct starpu_driver *d)
  1164. {
  1165. STARPU_ASSERT(d);
  1166. switch (d->type)
  1167. {
  1168. #ifdef STARPU_USE_CPU
  1169. case STARPU_CPU_WORKER:
  1170. return _starpu_cpu_driver_init(d);
  1171. #endif
  1172. #ifdef STARPU_USE_CUDA
  1173. case STARPU_CUDA_WORKER:
  1174. return _starpu_cuda_driver_init(d);
  1175. #endif
  1176. #ifdef STARPU_USE_OPENCL
  1177. case STARPU_OPENCL_WORKER:
  1178. return _starpu_opencl_driver_init(d);
  1179. #endif
  1180. default:
  1181. return -EINVAL;
  1182. }
  1183. }
  1184. int
  1185. starpu_driver_run_once(struct starpu_driver *d)
  1186. {
  1187. STARPU_ASSERT(d);
  1188. switch (d->type)
  1189. {
  1190. #ifdef STARPU_USE_CPU
  1191. case STARPU_CPU_WORKER:
  1192. return _starpu_cpu_driver_run_once(d);
  1193. #endif
  1194. #ifdef STARPU_USE_CUDA
  1195. case STARPU_CUDA_WORKER:
  1196. return _starpu_cuda_driver_run_once(d);
  1197. #endif
  1198. #ifdef STARPU_USE_OPENCL
  1199. case STARPU_OPENCL_WORKER:
  1200. return _starpu_opencl_driver_run_once(d);
  1201. #endif
  1202. default:
  1203. return -EINVAL;
  1204. }
  1205. }
  1206. int
  1207. starpu_driver_deinit(struct starpu_driver *d)
  1208. {
  1209. STARPU_ASSERT(d);
  1210. switch (d->type)
  1211. {
  1212. #ifdef STARPU_USE_CPU
  1213. case STARPU_CPU_WORKER:
  1214. return _starpu_cpu_driver_deinit(d);
  1215. #endif
  1216. #ifdef STARPU_USE_CUDA
  1217. case STARPU_CUDA_WORKER:
  1218. return _starpu_cuda_driver_deinit(d);
  1219. #endif
  1220. #ifdef STARPU_USE_OPENCL
  1221. case STARPU_OPENCL_WORKER:
  1222. return _starpu_opencl_driver_deinit(d);
  1223. #endif
  1224. default:
  1225. return -EINVAL;
  1226. }
  1227. }
  1228. void starpu_get_version(int *major, int *minor, int *release)
  1229. {
  1230. *major = STARPU_MAJOR_VERSION;
  1231. *minor = STARPU_MINOR_VERSION;
  1232. *release = STARPU_RELEASE_VERSION;
  1233. }