workers.c 52 KB

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