workers.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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. _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_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. /* if some codelet's termination cannot be handled directly :
  338. * for instance in the Gordon driver, Gordon tasks' callbacks
  339. * may be executed by another thread than that of the Gordon
  340. * driver so that we cannot call the push_codelet_output method
  341. * directly */
  342. workerarg->terminated_jobs = _starpu_job_list_new();
  343. starpu_task_list_init(&workerarg->local_tasks);
  344. workerarg->status = STATUS_INITIALIZING;
  345. _STARPU_DEBUG("initialising worker %u\n", worker);
  346. _starpu_init_worker_queue(workerarg);
  347. struct starpu_driver driver;
  348. driver.type = workerarg->arch;
  349. switch (workerarg->arch)
  350. {
  351. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  352. case STARPU_CPU_WORKER:
  353. workerarg->set = NULL;
  354. driver.id.cpu_id = cpu;
  355. if (_starpu_may_launch_driver(pconfig->conf, &driver))
  356. {
  357. _STARPU_PTHREAD_CREATE_ON(
  358. workerarg->name,
  359. &workerarg->worker_thread,
  360. NULL,
  361. _starpu_cpu_worker,
  362. workerarg,
  363. worker+1);
  364. #ifdef STARPU_USE_FXT
  365. _STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  366. while (!workerarg->worker_is_running)
  367. _STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  368. _STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  369. #endif
  370. }
  371. else
  372. {
  373. workerarg->run_by_starpu = 0;
  374. }
  375. cpu++;
  376. break;
  377. #endif
  378. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  379. case STARPU_CUDA_WORKER:
  380. workerarg->set = NULL;
  381. driver.id.cuda_id = cuda;
  382. if (_starpu_may_launch_driver(pconfig->conf, &driver))
  383. {
  384. _STARPU_PTHREAD_CREATE_ON(
  385. workerarg->name,
  386. &workerarg->worker_thread,
  387. NULL,
  388. _starpu_cuda_worker,
  389. workerarg,
  390. worker+1);
  391. #ifdef STARPU_USE_FXT
  392. _STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  393. while (!workerarg->worker_is_running)
  394. _STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  395. _STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  396. #endif
  397. }
  398. else
  399. {
  400. workerarg->run_by_starpu = 0;
  401. }
  402. cuda++;
  403. break;
  404. #endif
  405. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  406. case STARPU_OPENCL_WORKER:
  407. #ifndef STARPU_SIMGRID
  408. starpu_opencl_get_device(workerarg->devid, &driver.id.opencl_id);
  409. if (!_starpu_may_launch_driver(pconfig->conf, &driver))
  410. {
  411. workerarg->run_by_starpu = 0;
  412. break;
  413. }
  414. #endif
  415. workerarg->set = NULL;
  416. _STARPU_PTHREAD_CREATE_ON(
  417. workerarg->name,
  418. &workerarg->worker_thread,
  419. NULL,
  420. _starpu_opencl_worker,
  421. workerarg,
  422. worker+1);
  423. #ifdef STARPU_USE_FXT
  424. _STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  425. while (!workerarg->worker_is_running)
  426. _STARPU_PTHREAD_COND_WAIT(&workerarg->started_cond, &workerarg->mutex);
  427. _STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  428. #endif
  429. break;
  430. #endif
  431. default:
  432. STARPU_ABORT();
  433. }
  434. }
  435. cpu = 0;
  436. cuda = 0;
  437. for (worker = 0; worker < nworkers; worker++)
  438. {
  439. struct _starpu_worker *workerarg = &pconfig->workers[worker];
  440. struct starpu_driver driver;
  441. driver.type = workerarg->arch;
  442. switch (workerarg->arch)
  443. {
  444. case STARPU_CPU_WORKER:
  445. driver.id.cpu_id = cpu;
  446. if (!_starpu_may_launch_driver(pconfig->conf, &driver))
  447. {
  448. cpu++;
  449. break;
  450. }
  451. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  452. _STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  453. while (!workerarg->worker_is_initialized)
  454. _STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  455. _STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  456. cpu++;
  457. break;
  458. case STARPU_CUDA_WORKER:
  459. driver.id.cuda_id = cuda;
  460. if (!_starpu_may_launch_driver(pconfig->conf, &driver))
  461. {
  462. cuda++;
  463. break;
  464. }
  465. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  466. _STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  467. while (!workerarg->worker_is_initialized)
  468. _STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  469. _STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  470. cuda++;
  471. break;
  472. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  473. case STARPU_OPENCL_WORKER:
  474. #ifndef STARPU_SIMGRID
  475. starpu_opencl_get_device(workerarg->devid, &driver.id.opencl_id);
  476. if (!_starpu_may_launch_driver(pconfig->conf, &driver))
  477. break;
  478. #endif
  479. _STARPU_DEBUG("waiting for worker %u initialization\n", worker);
  480. _STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  481. while (!workerarg->worker_is_initialized)
  482. _STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  483. _STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  484. break;
  485. #endif
  486. default:
  487. STARPU_ABORT();
  488. }
  489. }
  490. _STARPU_DEBUG("finished launching drivers\n");
  491. }
  492. void _starpu_set_local_worker_key(struct _starpu_worker *worker)
  493. {
  494. _STARPU_PTHREAD_SETSPECIFIC(worker_key, worker);
  495. }
  496. struct _starpu_worker *_starpu_get_local_worker_key(void)
  497. {
  498. return (struct _starpu_worker *) _STARPU_PTHREAD_GETSPECIFIC(worker_key);
  499. }
  500. /* Initialize the starpu_conf with default values */
  501. int starpu_conf_init(struct starpu_conf *conf)
  502. {
  503. if (!conf)
  504. return -EINVAL;
  505. memset(conf, 0, sizeof(*conf));
  506. conf->magic = 42;
  507. conf->sched_policy_name = getenv("STARPU_SCHED");
  508. conf->sched_policy = NULL;
  509. /* Note that starpu_get_env_number returns -1 in case the variable is
  510. * not defined */
  511. /* Backward compatibility: check the value of STARPU_NCPUS if
  512. * STARPU_NCPU is not set. */
  513. conf->ncpus = starpu_get_env_number("STARPU_NCPU");
  514. if (conf->ncpus == -1)
  515. conf->ncpus = starpu_get_env_number("STARPU_NCPUS");
  516. conf->ncuda = starpu_get_env_number("STARPU_NCUDA");
  517. conf->nopencl = starpu_get_env_number("STARPU_NOPENCL");
  518. conf->calibrate = starpu_get_env_number("STARPU_CALIBRATE");
  519. conf->bus_calibrate = starpu_get_env_number("STARPU_BUS_CALIBRATE");
  520. if (conf->calibrate == -1)
  521. conf->calibrate = 0;
  522. if (conf->bus_calibrate == -1)
  523. conf->bus_calibrate = 0;
  524. conf->use_explicit_workers_bindid = 0; /* TODO */
  525. conf->use_explicit_workers_cuda_gpuid = 0; /* TODO */
  526. conf->use_explicit_workers_opencl_gpuid = 0; /* TODO */
  527. conf->single_combined_worker = starpu_get_env_number("STARPU_SINGLE_COMBINED_WORKER");
  528. if (conf->single_combined_worker == -1)
  529. conf->single_combined_worker = 0;
  530. #if defined(STARPU_DISABLE_ASYNCHRONOUS_COPY)
  531. conf->disable_asynchronous_copy = 1;
  532. #else
  533. conf->disable_asynchronous_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_COPY");
  534. if (conf->disable_asynchronous_copy == -1)
  535. conf->disable_asynchronous_copy = 0;
  536. #endif
  537. #if defined(STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY)
  538. conf->disable_asynchronous_cuda_copy = 1;
  539. #else
  540. conf->disable_asynchronous_cuda_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY");
  541. if (conf->disable_asynchronous_cuda_copy == -1)
  542. conf->disable_asynchronous_cuda_copy = 0;
  543. #endif
  544. #if defined(STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY)
  545. conf->disable_asynchronous_opencl_copy = 1;
  546. #else
  547. conf->disable_asynchronous_opencl_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY");
  548. if (conf->disable_asynchronous_opencl_copy == -1)
  549. conf->disable_asynchronous_opencl_copy = 0;
  550. #endif
  551. /* 64MiB by default */
  552. conf->trace_buffer_size = 64<<20;
  553. return 0;
  554. }
  555. static void _starpu_conf_set_value_against_environment(char *name, int *value)
  556. {
  557. int number;
  558. number = starpu_get_env_number(name);
  559. if (number != -1)
  560. {
  561. *value = number;
  562. }
  563. }
  564. void _starpu_conf_check_environment(struct starpu_conf *conf)
  565. {
  566. char *sched = getenv("STARPU_SCHED");
  567. if (sched)
  568. {
  569. conf->sched_policy_name = sched;
  570. }
  571. _starpu_conf_set_value_against_environment("STARPU_NCPUS", &conf->ncpus);
  572. _starpu_conf_set_value_against_environment("STARPU_NCPU", &conf->ncpus);
  573. _starpu_conf_set_value_against_environment("STARPU_NCUDA", &conf->ncuda);
  574. _starpu_conf_set_value_against_environment("STARPU_NOPENCL", &conf->nopencl);
  575. _starpu_conf_set_value_against_environment("STARPU_CALIBRATE", &conf->calibrate);
  576. _starpu_conf_set_value_against_environment("STARPU_BUS_CALIBRATE", &conf->bus_calibrate);
  577. _starpu_conf_set_value_against_environment("STARPU_SINGLE_COMBINED_WORKER", &conf->single_combined_worker);
  578. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_COPY", &conf->disable_asynchronous_copy);
  579. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_CUDA_COPY", &conf->disable_asynchronous_cuda_copy);
  580. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_OPENCL_COPY", &conf->disable_asynchronous_opencl_copy);
  581. }
  582. int starpu_init(struct starpu_conf *user_conf)
  583. {
  584. int ret;
  585. #ifndef STARPU_SIMGRID
  586. #ifdef __GNUC__
  587. #ifndef __OPTIMIZE__
  588. _STARPU_DISP("Warning: StarPU was configured with --enable-debug (-O0), and is thus not optimized\n");
  589. #endif
  590. #endif
  591. #if 0
  592. #ifndef STARPU_NO_ASSERT
  593. _STARPU_DISP("Warning: StarPU was configured without --enable-fast\n");
  594. #endif
  595. #endif
  596. #ifdef STARPU_MEMORY_STATS
  597. _STARPU_DISP("Warning: StarPU was configured with --enable-memory-stats, which slows down a bit\n");
  598. #endif
  599. #ifdef STARPU_VERBOSE
  600. _STARPU_DISP("Warning: StarPU was configured with --enable-verbose, which slows down a bit\n");
  601. #endif
  602. #ifdef STARPU_USE_FXT
  603. _STARPU_DISP("Warning: StarPU was configured with --with-fxt, which slows down a bit\n");
  604. #endif
  605. #ifdef STARPU_PERF_DEBUG
  606. _STARPU_DISP("Warning: StarPU was configured with --enable-perf-debug, which slows down a bit\n");
  607. #endif
  608. #ifdef STARPU_MODEL_DEBUG
  609. _STARPU_DISP("Warning: StarPU was configured with --enable-model-debug, which slows down a bit\n");
  610. #endif
  611. #ifdef STARPU_ENABLE_STATS
  612. _STARPU_DISP("Warning: StarPU was configured with --enable-stats, which slows down a bit\n");
  613. #endif
  614. #endif
  615. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  616. while (initialized == CHANGING)
  617. /* Wait for the other one changing it */
  618. _STARPU_PTHREAD_COND_WAIT(&init_cond, &init_mutex);
  619. init_count++;
  620. if (initialized == INITIALIZED)
  621. {
  622. /* He initialized it, don't do it again, and let the others get the mutex */
  623. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  624. return 0;
  625. }
  626. /* initialized == UNINITIALIZED */
  627. initialized = CHANGING;
  628. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  629. #ifdef __MINGW32__
  630. WSADATA wsadata;
  631. WSAStartup(MAKEWORD(1,0), &wsadata);
  632. #endif
  633. srand(2008);
  634. #ifdef HAVE_AYUDAME_H
  635. #ifndef AYU_RT_STARPU
  636. /* Dumb value for now */
  637. #define AYU_RT_STARPU 32
  638. #endif
  639. if (AYU_event)
  640. {
  641. enum ayu_runtime_t ayu_rt = AYU_RT_STARPU;
  642. AYU_event(AYU_PREINIT, 0, (void*) &ayu_rt);
  643. }
  644. #endif
  645. /* store the pointer to the user explicit configuration during the
  646. * initialization */
  647. if (user_conf == NULL)
  648. {
  649. struct starpu_conf *conf = malloc(sizeof(struct starpu_conf));
  650. starpu_conf_init(conf);
  651. config.conf = conf;
  652. config.default_conf = 1;
  653. }
  654. else
  655. {
  656. if (user_conf->magic != 42)
  657. {
  658. _STARPU_DISP("starpu_conf structure needs to be initialized with starpu_conf_init\n");
  659. return -EINVAL;
  660. }
  661. config.conf = user_conf;
  662. config.default_conf = 0;
  663. }
  664. _starpu_conf_check_environment(config.conf);
  665. _starpu_init_all_sched_ctxs(&config);
  666. _starpu_init_progression_hooks();
  667. _starpu_init_tags();
  668. #ifdef STARPU_USE_FXT
  669. _starpu_init_fxt_profiling(config.conf->trace_buffer_size);
  670. #endif
  671. _starpu_open_debug_logfile();
  672. _starpu_data_interface_init();
  673. _starpu_timing_init();
  674. _starpu_profiling_init();
  675. _starpu_load_bus_performance_files();
  676. ret = _starpu_build_topology(&config);
  677. if (ret)
  678. {
  679. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  680. init_count--;
  681. initialized = UNINITIALIZED;
  682. /* Let somebody else try to do it */
  683. _STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  684. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  685. return ret;
  686. }
  687. /* We need to store the current task handled by the different
  688. * threads */
  689. _starpu_initialize_current_task_key();
  690. _starpu_create_sched_ctx(config.conf->sched_policy_name, NULL, -1, 1, "init");
  691. _starpu_initialize_registered_performance_models();
  692. /* Launch "basic" workers (ie. non-combined workers) */
  693. _starpu_launch_drivers(&config);
  694. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  695. initialized = INITIALIZED;
  696. /* Tell everybody that we initialized */
  697. _STARPU_PTHREAD_COND_BROADCAST(&init_cond);
  698. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  699. _STARPU_DEBUG("Initialisation finished\n");
  700. return 0;
  701. }
  702. void starpu_profiling_init()
  703. {
  704. _starpu_profiling_init();
  705. }
  706. /*
  707. * Handle runtime termination
  708. */
  709. static void _starpu_terminate_workers(struct _starpu_machine_config *pconfig)
  710. {
  711. int status STARPU_ATTRIBUTE_UNUSED;
  712. unsigned workerid;
  713. for (workerid = 0; workerid < pconfig->topology.nworkers; workerid++)
  714. {
  715. starpu_wake_all_blocked_workers();
  716. _STARPU_DEBUG("wait for worker %u\n", workerid);
  717. struct _starpu_worker_set *set = pconfig->workers[workerid].set;
  718. struct _starpu_worker *worker = &pconfig->workers[workerid];
  719. /* in case StarPU termination code is called from a callback,
  720. * we have to check if pthread_self() is the worker itself */
  721. if (set)
  722. {
  723. if (!set->joined)
  724. {
  725. #ifdef STARPU_SIMGRID
  726. #ifdef STARPU_DEVEL
  727. #warning TODO: use a simgrid_join when it becomes available
  728. #endif
  729. MSG_process_sleep(1);
  730. #else
  731. if (!pthread_equal(pthread_self(), set->worker_thread))
  732. {
  733. status = pthread_join(set->worker_thread, NULL);
  734. #ifdef STARPU_VERBOSE
  735. if (status)
  736. {
  737. _STARPU_DEBUG("pthread_join -> %d\n", status);
  738. }
  739. #endif
  740. }
  741. #endif
  742. set->joined = 1;
  743. }
  744. }
  745. else
  746. {
  747. if (!worker->run_by_starpu)
  748. goto out;
  749. #ifdef STARPU_SIMGRID
  750. MSG_process_sleep(1);
  751. #else
  752. if (!pthread_equal(pthread_self(), worker->worker_thread))
  753. {
  754. status = pthread_join(worker->worker_thread, NULL);
  755. #ifdef STARPU_VERBOSE
  756. if (status)
  757. {
  758. _STARPU_DEBUG("pthread_join -> %d\n", status);
  759. }
  760. #endif
  761. }
  762. #endif
  763. }
  764. out:
  765. STARPU_ASSERT(starpu_task_list_empty(&worker->local_tasks));
  766. _starpu_delete_sched_ctx_for_worker(workerid);
  767. _starpu_job_list_delete(worker->terminated_jobs);
  768. }
  769. }
  770. unsigned _starpu_machine_is_running(void)
  771. {
  772. unsigned ret;
  773. /* running is just protected by a memory barrier */
  774. STARPU_RMB();
  775. ANNOTATE_HAPPENS_AFTER(&config.running);
  776. ret = config.running;
  777. ANNOTATE_HAPPENS_BEFORE(&config.running);
  778. return ret;
  779. }
  780. unsigned _starpu_worker_can_block(unsigned memnode STARPU_ATTRIBUTE_UNUSED)
  781. {
  782. #ifdef STARPU_NON_BLOCKING_DRIVERS
  783. return 0;
  784. #else
  785. unsigned can_block = 1;
  786. #ifndef STARPU_SIMGRID
  787. if (!_starpu_check_that_no_data_request_exists(memnode))
  788. can_block = 0;
  789. #endif
  790. if (!_starpu_machine_is_running())
  791. can_block = 0;
  792. if (!_starpu_execute_registered_progression_hooks())
  793. can_block = 0;
  794. return can_block;
  795. #endif
  796. }
  797. static void _starpu_kill_all_workers(struct _starpu_machine_config *pconfig)
  798. {
  799. /* set the flag which will tell workers to stop */
  800. ANNOTATE_HAPPENS_AFTER(&config.running);
  801. pconfig->running = 0;
  802. /* running is just protected by a memory barrier */
  803. ANNOTATE_HAPPENS_BEFORE(&config.running);
  804. STARPU_WMB();
  805. starpu_wake_all_blocked_workers();
  806. }
  807. void starpu_display_stats()
  808. {
  809. starpu_bus_profiling_helper_display_summary();
  810. starpu_worker_profiling_helper_display_summary();
  811. }
  812. void starpu_shutdown(void)
  813. {
  814. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  815. init_count--;
  816. if (init_count)
  817. {
  818. _STARPU_DEBUG("Still somebody needing StarPU, don't deinitialize\n");
  819. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  820. return;
  821. }
  822. /* We're last */
  823. initialized = CHANGING;
  824. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  825. starpu_task_wait_for_no_ready();
  826. /* tell all workers to shutdown */
  827. _starpu_kill_all_workers(&config);
  828. {
  829. int stats = starpu_get_env_number("STARPU_STATS");
  830. if (stats != 0)
  831. {
  832. _starpu_display_msi_stats();
  833. _starpu_display_alloc_cache_stats();
  834. _starpu_display_comm_amounts();
  835. }
  836. }
  837. starpu_bus_profiling_helper_display_summary();
  838. starpu_worker_profiling_helper_display_summary();
  839. _starpu_deinitialize_registered_performance_models();
  840. /* wait for their termination */
  841. _starpu_terminate_workers(&config);
  842. {
  843. int stats = starpu_get_env_number("STARPU_MEMORY_STATS");
  844. if (stats != 0)
  845. {
  846. // Display statistics on data which have not been unregistered
  847. starpu_memory_display_stats();
  848. }
  849. }
  850. _starpu_delete_all_sched_ctxs();
  851. _starpu_destroy_topology(&config);
  852. #ifdef STARPU_USE_FXT
  853. _starpu_stop_fxt_profiling();
  854. #endif
  855. _starpu_data_interface_shutdown();
  856. /* Drop all remaining tags */
  857. _starpu_tag_clear();
  858. _starpu_close_debug_logfile();
  859. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  860. initialized = UNINITIALIZED;
  861. /* Let someone else that wants to initialize it again do it */
  862. _STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  863. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  864. /* Clear memory if it was allocated by StarPU */
  865. if (config.default_conf)
  866. free(config.conf);
  867. #ifdef HAVE_AYUDAME_H
  868. if (AYU_event) AYU_event(AYU_FINISH, 0, NULL);
  869. #endif
  870. _STARPU_DEBUG("Shutdown finished\n");
  871. }
  872. unsigned starpu_worker_get_count(void)
  873. {
  874. return config.topology.nworkers;
  875. }
  876. int starpu_worker_get_count_by_type(enum starpu_archtype type)
  877. {
  878. switch (type)
  879. {
  880. case STARPU_CPU_WORKER:
  881. return config.topology.ncpus;
  882. case STARPU_CUDA_WORKER:
  883. return config.topology.ncudagpus;
  884. case STARPU_OPENCL_WORKER:
  885. return config.topology.nopenclgpus;
  886. default:
  887. return -EINVAL;
  888. }
  889. }
  890. unsigned starpu_combined_worker_get_count(void)
  891. {
  892. return config.topology.ncombinedworkers;
  893. }
  894. unsigned starpu_cpu_worker_get_count(void)
  895. {
  896. return config.topology.ncpus;
  897. }
  898. unsigned starpu_cuda_worker_get_count(void)
  899. {
  900. return config.topology.ncudagpus;
  901. }
  902. unsigned starpu_opencl_worker_get_count(void)
  903. {
  904. return config.topology.nopenclgpus;
  905. }
  906. int starpu_asynchronous_copy_disabled(void)
  907. {
  908. return config.conf->disable_asynchronous_copy;
  909. }
  910. int starpu_asynchronous_cuda_copy_disabled(void)
  911. {
  912. return config.conf->disable_asynchronous_cuda_copy;
  913. }
  914. int starpu_asynchronous_opencl_copy_disabled(void)
  915. {
  916. return config.conf->disable_asynchronous_opencl_copy;
  917. }
  918. /* When analyzing performance, it is useful to see what is the processing unit
  919. * that actually performed the task. This function returns the id of the
  920. * processing unit actually executing it, therefore it makes no sense to use it
  921. * within the callbacks of SPU functions for instance. If called by some thread
  922. * that is not controlled by StarPU, starpu_worker_get_id returns -1. */
  923. int starpu_worker_get_id(void)
  924. {
  925. struct _starpu_worker * worker;
  926. worker = _starpu_get_local_worker_key();
  927. if (worker)
  928. {
  929. return worker->workerid;
  930. }
  931. else
  932. {
  933. /* there is no worker associated to that thread, perhaps it is
  934. * a thread from the application or this is some SPU worker */
  935. return -1;
  936. }
  937. }
  938. int starpu_combined_worker_get_id(void)
  939. {
  940. struct _starpu_worker *worker;
  941. worker = _starpu_get_local_worker_key();
  942. if (worker)
  943. {
  944. return worker->combined_workerid;
  945. }
  946. else
  947. {
  948. /* there is no worker associated to that thread, perhaps it is
  949. * a thread from the application or this is some SPU worker */
  950. return -1;
  951. }
  952. }
  953. int starpu_combined_worker_get_size(void)
  954. {
  955. struct _starpu_worker *worker;
  956. worker = _starpu_get_local_worker_key();
  957. if (worker)
  958. {
  959. return worker->worker_size;
  960. }
  961. else
  962. {
  963. /* there is no worker associated to that thread, perhaps it is
  964. * a thread from the application or this is some SPU worker */
  965. return -1;
  966. }
  967. }
  968. int starpu_combined_worker_get_rank(void)
  969. {
  970. struct _starpu_worker *worker;
  971. worker = _starpu_get_local_worker_key();
  972. if (worker)
  973. {
  974. return worker->current_rank;
  975. }
  976. else
  977. {
  978. /* there is no worker associated to that thread, perhaps it is
  979. * a thread from the application or this is some SPU worker */
  980. return -1;
  981. }
  982. }
  983. int starpu_worker_get_devid(int id)
  984. {
  985. return config.workers[id].devid;
  986. }
  987. struct _starpu_worker *_starpu_get_worker_struct(unsigned id)
  988. {
  989. return &config.workers[id];
  990. }
  991. unsigned starpu_worker_is_combined_worker(int id)
  992. {
  993. return id >= (int)config.topology.nworkers;
  994. }
  995. struct _starpu_sched_ctx *_starpu_get_sched_ctx_struct(unsigned id)
  996. {
  997. if (id == STARPU_NMAX_SCHED_CTXS) return NULL;
  998. return &config.sched_ctxs[id];
  999. }
  1000. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id)
  1001. {
  1002. unsigned basic_worker_count = starpu_worker_get_count();
  1003. STARPU_ASSERT(id >= basic_worker_count);
  1004. return &config.combined_workers[id - basic_worker_count];
  1005. }
  1006. enum starpu_archtype starpu_worker_get_type(int id)
  1007. {
  1008. return config.workers[id].arch;
  1009. }
  1010. int starpu_worker_get_ids_by_type(enum starpu_archtype type, int *workerids, int maxsize)
  1011. {
  1012. unsigned nworkers = starpu_worker_get_count();
  1013. int cnt = 0;
  1014. unsigned id;
  1015. for (id = 0; id < nworkers; id++)
  1016. {
  1017. if (starpu_worker_get_type(id) == type)
  1018. {
  1019. /* Perhaps the array is too small ? */
  1020. if (cnt >= maxsize)
  1021. return -ERANGE;
  1022. workerids[cnt++] = id;
  1023. }
  1024. }
  1025. return cnt;
  1026. }
  1027. int starpu_worker_get_by_type(enum starpu_archtype type, int num)
  1028. {
  1029. unsigned nworkers = starpu_worker_get_count();
  1030. int cnt = 0;
  1031. unsigned id;
  1032. for (id = 0; id < nworkers; id++)
  1033. {
  1034. if (starpu_worker_get_type(id) == type)
  1035. {
  1036. if (num == cnt)
  1037. return id;
  1038. cnt++;
  1039. }
  1040. }
  1041. /* Not found */
  1042. return -1;
  1043. }
  1044. int starpu_worker_get_by_devid(enum starpu_archtype type, int devid)
  1045. {
  1046. unsigned nworkers = starpu_worker_get_count();
  1047. unsigned id;
  1048. for (id = 0; id < nworkers; id++)
  1049. if (starpu_worker_get_type(id) == type && starpu_worker_get_devid(id) == devid)
  1050. return id;
  1051. /* Not found */
  1052. return -1;
  1053. }
  1054. void starpu_worker_get_name(int id, char *dst, size_t maxlen)
  1055. {
  1056. char *name = config.workers[id].name;
  1057. snprintf(dst, maxlen, "%s", name);
  1058. }
  1059. /* Retrieve the status which indicates what the worker is currently doing. */
  1060. enum _starpu_worker_status _starpu_worker_get_status(int workerid)
  1061. {
  1062. return config.workers[workerid].status;
  1063. }
  1064. /* Change the status of the worker which indicates what the worker is currently
  1065. * doing (eg. executing a callback). */
  1066. void _starpu_worker_set_status(int workerid, enum _starpu_worker_status status)
  1067. {
  1068. config.workers[workerid].status = status;
  1069. }
  1070. void starpu_worker_get_sched_condition(int workerid, _starpu_pthread_mutex_t **sched_mutex, _starpu_pthread_cond_t **sched_cond)
  1071. {
  1072. *sched_cond = &config.workers[workerid].sched_cond;
  1073. *sched_mutex = &config.workers[workerid].sched_mutex;
  1074. }
  1075. int starpu_worker_get_nids_by_type(enum starpu_archtype type, int *workerids, int maxsize)
  1076. {
  1077. unsigned nworkers = starpu_worker_get_count();
  1078. int cnt = 0;
  1079. unsigned id;
  1080. for (id = 0; id < nworkers; id++)
  1081. {
  1082. if (starpu_worker_get_type(id) == type)
  1083. {
  1084. /* Perhaps the array is too small ? */
  1085. if (cnt >= maxsize)
  1086. return cnt;
  1087. workerids[cnt++] = id;
  1088. }
  1089. }
  1090. return cnt;
  1091. }
  1092. int starpu_worker_get_nids_ctx_free_by_type(enum starpu_archtype type, int *workerids, int maxsize)
  1093. {
  1094. unsigned nworkers = starpu_worker_get_count();
  1095. int cnt = 0;
  1096. unsigned id, worker;
  1097. unsigned found = 0;
  1098. for (id = 0; id < nworkers; id++)
  1099. {
  1100. found = 0;
  1101. if (starpu_worker_get_type(id) == type)
  1102. {
  1103. /* Perhaps the array is too small ? */
  1104. if (cnt >= maxsize)
  1105. return cnt;
  1106. int s;
  1107. for(s = 1; s < STARPU_NMAX_SCHED_CTXS; s++)
  1108. {
  1109. if(config.sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  1110. {
  1111. struct starpu_sched_ctx_worker_collection *workers = config.sched_ctxs[s].workers;
  1112. struct starpu_sched_ctx_iterator it;
  1113. if(workers->init_iterator)
  1114. workers->init_iterator(workers, &it);
  1115. while(workers->has_next(workers, &it))
  1116. {
  1117. worker = workers->get_next(workers, &it);
  1118. if(worker == id)
  1119. {
  1120. found = 1;
  1121. break;
  1122. }
  1123. }
  1124. if(found) break;
  1125. }
  1126. }
  1127. if(!found)
  1128. workerids[cnt++] = id;
  1129. }
  1130. }
  1131. return cnt;
  1132. }
  1133. struct _starpu_sched_ctx* _starpu_get_initial_sched_ctx(void)
  1134. {
  1135. return &config.sched_ctxs[0];
  1136. }
  1137. int
  1138. starpu_driver_run(struct starpu_driver *d)
  1139. {
  1140. if (!d)
  1141. {
  1142. _STARPU_DEBUG("Invalid argument\n");
  1143. return -EINVAL;
  1144. }
  1145. switch (d->type)
  1146. {
  1147. #ifdef STARPU_USE_CPU
  1148. case STARPU_CPU_WORKER:
  1149. return _starpu_run_cpu(d);
  1150. #endif
  1151. #ifdef STARPU_USE_CUDA
  1152. case STARPU_CUDA_WORKER:
  1153. return _starpu_run_cuda(d);
  1154. #endif
  1155. #ifdef STARPU_USE_OPENCL
  1156. case STARPU_OPENCL_WORKER:
  1157. return _starpu_run_opencl(d);
  1158. #endif
  1159. default:
  1160. {
  1161. _STARPU_DEBUG("Invalid device type\n");
  1162. return -EINVAL;
  1163. }
  1164. }
  1165. }
  1166. int
  1167. starpu_driver_init(struct starpu_driver *d)
  1168. {
  1169. STARPU_ASSERT(d);
  1170. switch (d->type)
  1171. {
  1172. #ifdef STARPU_USE_CPU
  1173. case STARPU_CPU_WORKER:
  1174. return _starpu_cpu_driver_init(d);
  1175. #endif
  1176. #ifdef STARPU_USE_CUDA
  1177. case STARPU_CUDA_WORKER:
  1178. return _starpu_cuda_driver_init(d);
  1179. #endif
  1180. #ifdef STARPU_USE_OPENCL
  1181. case STARPU_OPENCL_WORKER:
  1182. return _starpu_opencl_driver_init(d);
  1183. #endif
  1184. default:
  1185. return -EINVAL;
  1186. }
  1187. }
  1188. int
  1189. starpu_driver_run_once(struct starpu_driver *d)
  1190. {
  1191. STARPU_ASSERT(d);
  1192. switch (d->type)
  1193. {
  1194. #ifdef STARPU_USE_CPU
  1195. case STARPU_CPU_WORKER:
  1196. return _starpu_cpu_driver_run_once(d);
  1197. #endif
  1198. #ifdef STARPU_USE_CUDA
  1199. case STARPU_CUDA_WORKER:
  1200. return _starpu_cuda_driver_run_once(d);
  1201. #endif
  1202. #ifdef STARPU_USE_OPENCL
  1203. case STARPU_OPENCL_WORKER:
  1204. return _starpu_opencl_driver_run_once(d);
  1205. #endif
  1206. default:
  1207. return -EINVAL;
  1208. }
  1209. }
  1210. int
  1211. starpu_driver_deinit(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_deinit(d);
  1219. #endif
  1220. #ifdef STARPU_USE_CUDA
  1221. case STARPU_CUDA_WORKER:
  1222. return _starpu_cuda_driver_deinit(d);
  1223. #endif
  1224. #ifdef STARPU_USE_OPENCL
  1225. case STARPU_OPENCL_WORKER:
  1226. return _starpu_opencl_driver_deinit(d);
  1227. #endif
  1228. default:
  1229. return -EINVAL;
  1230. }
  1231. }