workers.c 44 KB

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