workers.c 37 KB

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