topology.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Thibaut Lambert
  5. * Copyright (C) 2016 Uppsala University
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <common/config.h>
  21. #ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #include <core/workers.h>
  25. #include <core/debug.h>
  26. #include <core/topology.h>
  27. #include <drivers/cuda/driver_cuda.h>
  28. #include <drivers/cpu/driver_cpu.h>
  29. #include <drivers/mpi/driver_mpi_source.h>
  30. #include <drivers/mpi/driver_mpi_common.h>
  31. #include <drivers/mp_common/source_common.h>
  32. #include <drivers/opencl/driver_opencl.h>
  33. #include <drivers/opencl/driver_opencl_utils.h>
  34. #include <profiling/profiling.h>
  35. #include <datawizard/datastats.h>
  36. #include <datawizard/memory_nodes.h>
  37. #include <datawizard/memory_manager.h>
  38. #include <common/uthash.h>
  39. #ifdef STARPU_HAVE_HWLOC
  40. #include <hwloc.h>
  41. #ifndef HWLOC_API_VERSION
  42. #define HWLOC_OBJ_PU HWLOC_OBJ_PROC
  43. #endif
  44. #if HWLOC_API_VERSION < 0x00010b00
  45. #define HWLOC_OBJ_NUMANODE HWLOC_OBJ_NODE
  46. #endif
  47. #endif
  48. #ifdef STARPU_HAVE_WINDOWS
  49. #include <windows.h>
  50. #endif
  51. #ifdef STARPU_SIMGRID
  52. #include <core/simgrid.h>
  53. #endif
  54. #if defined(HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX) && HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX
  55. #include <hwloc/cuda.h>
  56. #endif
  57. #if defined(STARPU_HAVE_HWLOC) && defined(STARPU_USE_OPENCL)
  58. #include <hwloc/opencl.h>
  59. #endif
  60. static unsigned topology_is_initialized = 0;
  61. static int nobind;
  62. static int numa_enabled = -1;
  63. /* For checking whether two workers share the same PU, indexed by PU number */
  64. static int cpu_worker[STARPU_MAXCPUS];
  65. static char * cpu_name[STARPU_MAXCPUS];
  66. static unsigned nb_numa_nodes = 0;
  67. static int numa_memory_nodes_to_hwloclogid[STARPU_MAXNUMANODES]; /* indexed by StarPU numa node to convert in hwloc logid */
  68. static int numa_memory_nodes_to_physicalid[STARPU_MAXNUMANODES]; /* indexed by StarPU numa node to convert in physical id */
  69. static unsigned numa_bus_id[STARPU_MAXNUMANODES*STARPU_MAXNUMANODES];
  70. static int _starpu_get_logical_numa_node_worker(unsigned workerid);
  71. #define STARPU_NUMA_UNINITIALIZED (-2)
  72. #define STARPU_NUMA_MAIN_RAM (-1)
  73. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID) || defined(STARPU_USE_MPI_MASTER_SLAVE)
  74. struct handle_entry
  75. {
  76. UT_hash_handle hh;
  77. unsigned gpuid;
  78. };
  79. # if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  80. /* Entry in the `devices_using_cuda' hash table. */
  81. static struct handle_entry *devices_using_cuda;
  82. # endif
  83. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  84. static unsigned may_bind_automatically[STARPU_NARCH] = { 0 };
  85. #endif
  86. #endif // defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  87. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  88. static struct _starpu_worker_set cuda_worker_set[STARPU_MAXCUDADEVS];
  89. #endif
  90. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  91. struct _starpu_worker_set mpi_worker_set[STARPU_MAXMPIDEVS];
  92. #endif
  93. int starpu_memory_nodes_get_numa_count(void)
  94. {
  95. return nb_numa_nodes;
  96. }
  97. #if defined(STARPU_HAVE_HWLOC)
  98. static hwloc_obj_t numa_get_obj(hwloc_obj_t obj)
  99. {
  100. #if HWLOC_API_VERSION >= 0x00020000
  101. while (obj->memory_first_child == NULL)
  102. {
  103. obj = obj->parent;
  104. if (!obj)
  105. return NULL;
  106. }
  107. return obj->memory_first_child;
  108. #else
  109. while (obj->type != HWLOC_OBJ_NUMANODE)
  110. {
  111. obj = obj->parent;
  112. /* If we don't find a "node" obj before the root, this means
  113. * hwloc does not know whether there are numa nodes or not, so
  114. * we should not use a per-node sampling in that case. */
  115. if (!obj)
  116. return NULL;
  117. }
  118. return obj;
  119. #endif
  120. }
  121. static int numa_get_logical_id(hwloc_obj_t obj)
  122. {
  123. STARPU_ASSERT(obj);
  124. obj = numa_get_obj(obj);
  125. if (!obj)
  126. return 0;
  127. return obj->logical_index;
  128. }
  129. static int numa_get_physical_id(hwloc_obj_t obj)
  130. {
  131. STARPU_ASSERT(obj);
  132. obj = numa_get_obj(obj);
  133. if (!obj)
  134. return 0;
  135. return obj->os_index;
  136. }
  137. #endif
  138. /* This returns the exact NUMA node next to a worker */
  139. static int _starpu_get_logical_numa_node_worker(unsigned workerid)
  140. {
  141. #if defined(STARPU_HAVE_HWLOC)
  142. STARPU_ASSERT(numa_enabled != -1);
  143. if (numa_enabled)
  144. {
  145. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  146. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config() ;
  147. struct _starpu_machine_topology *topology = &config->topology ;
  148. hwloc_obj_t obj;
  149. switch(worker->arch)
  150. {
  151. case STARPU_CPU_WORKER:
  152. obj = hwloc_get_obj_by_type(topology->hwtopology, HWLOC_OBJ_PU, worker->bindid) ;
  153. break;
  154. default:
  155. STARPU_ABORT();
  156. }
  157. return numa_get_logical_id(obj);
  158. }
  159. else
  160. #endif
  161. {
  162. (void) workerid; /* unused */
  163. return STARPU_NUMA_MAIN_RAM;
  164. }
  165. }
  166. /* This returns the exact NUMA node next to a worker */
  167. static int _starpu_get_physical_numa_node_worker(unsigned workerid)
  168. {
  169. #if defined(STARPU_HAVE_HWLOC)
  170. STARPU_ASSERT(numa_enabled != -1);
  171. if (numa_enabled)
  172. {
  173. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  174. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config() ;
  175. struct _starpu_machine_topology *topology = &config->topology ;
  176. hwloc_obj_t obj;
  177. switch(worker->arch)
  178. {
  179. case STARPU_CPU_WORKER:
  180. obj = hwloc_get_obj_by_type(topology->hwtopology, HWLOC_OBJ_PU, worker->bindid) ;
  181. break;
  182. default:
  183. STARPU_ABORT();
  184. }
  185. return numa_get_physical_id(obj);
  186. }
  187. else
  188. #endif
  189. {
  190. (void) workerid; /* unused */
  191. return STARPU_NUMA_MAIN_RAM;
  192. }
  193. }
  194. /* This returns the CPU NUMA memory close to a worker */
  195. static int _starpu_get_logical_close_numa_node_worker(unsigned workerid)
  196. {
  197. #if defined(STARPU_HAVE_HWLOC)
  198. STARPU_ASSERT(numa_enabled != -1);
  199. if (numa_enabled)
  200. {
  201. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  202. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config() ;
  203. struct _starpu_machine_topology *topology = &config->topology ;
  204. hwloc_obj_t obj;
  205. switch(worker->arch)
  206. {
  207. default:
  208. obj = hwloc_get_obj_by_type(topology->hwtopology, HWLOC_OBJ_PU, worker->bindid) ;
  209. break;
  210. #ifndef STARPU_SIMGRID
  211. #if defined(HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX) && HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX
  212. case STARPU_CUDA_WORKER:
  213. obj = hwloc_cuda_get_device_osdev_by_index(topology->hwtopology, worker->devid);
  214. if (!obj)
  215. obj = hwloc_get_obj_by_type(topology->hwtopology, HWLOC_OBJ_PU, worker->bindid) ;
  216. break;
  217. #endif
  218. #endif
  219. }
  220. return numa_get_logical_id(obj);
  221. }
  222. else
  223. #endif
  224. {
  225. (void) workerid; /* unused */
  226. return STARPU_NUMA_MAIN_RAM;
  227. }
  228. }
  229. //TODO change this in an array
  230. int starpu_memory_nodes_numa_hwloclogid_to_id(int logid)
  231. {
  232. unsigned n;
  233. for (n = 0; n < nb_numa_nodes; n++)
  234. if (numa_memory_nodes_to_hwloclogid[n] == logid)
  235. return n;
  236. return -1;
  237. }
  238. int starpu_memory_nodes_numa_id_to_hwloclogid(unsigned id)
  239. {
  240. STARPU_ASSERT(id < STARPU_MAXNUMANODES);
  241. return numa_memory_nodes_to_hwloclogid[id];
  242. }
  243. int starpu_memory_nodes_numa_devid_to_id(unsigned id)
  244. {
  245. STARPU_ASSERT(id < STARPU_MAXNUMANODES);
  246. return numa_memory_nodes_to_physicalid[id];
  247. }
  248. //TODO change this in an array
  249. int starpu_memory_nodes_numa_id_to_devid(int osid)
  250. {
  251. unsigned n;
  252. for (n = 0; n < nb_numa_nodes; n++)
  253. if (numa_memory_nodes_to_physicalid[n] == osid)
  254. return n;
  255. return -1;
  256. }
  257. // TODO: cache the values instead of looking in hwloc each time
  258. /* Avoid using this one, prefer _starpu_task_data_get_node_on_worker */
  259. int _starpu_task_data_get_node_on_node(struct starpu_task *task, unsigned index, unsigned local_node)
  260. {
  261. int node = STARPU_SPECIFIC_NODE_LOCAL;
  262. if (task->cl->specific_nodes)
  263. node = STARPU_CODELET_GET_NODE(task->cl, index);
  264. switch (node)
  265. {
  266. case STARPU_SPECIFIC_NODE_LOCAL:
  267. // TODO: rather find MCDRAM
  268. node = local_node;
  269. break;
  270. case STARPU_SPECIFIC_NODE_CPU:
  271. switch (starpu_node_get_kind(local_node))
  272. {
  273. case STARPU_CPU_RAM:
  274. node = local_node;
  275. break;
  276. default:
  277. // TODO: rather take close NUMA node
  278. node = STARPU_MAIN_RAM;
  279. break;
  280. }
  281. break;
  282. case STARPU_SPECIFIC_NODE_SLOW:
  283. // TODO: rather leave in DDR
  284. node = local_node;
  285. break;
  286. case STARPU_SPECIFIC_NODE_LOCAL_OR_CPU:
  287. {
  288. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(task, index);
  289. if (mode & STARPU_R)
  290. {
  291. if (mode & STARPU_R && task->handles[index]->per_node[local_node].state != STARPU_INVALID)
  292. {
  293. /* It is here already, rather access it from here */
  294. node = local_node;
  295. }
  296. else
  297. {
  298. /* It is not here already, do not bother moving it */
  299. node = STARPU_MAIN_RAM;
  300. }
  301. }
  302. else
  303. {
  304. /* Nothing to read, consider where to write */
  305. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, index);
  306. if (handle->wt_mask & (1 << STARPU_MAIN_RAM))
  307. /* Write through, better simply write to the main memory */
  308. node = STARPU_MAIN_RAM;
  309. else
  310. /* Better keep temporary data on the accelerator to save PCI bandwidth */
  311. node = local_node;
  312. }
  313. break;
  314. }
  315. }
  316. return node;
  317. }
  318. int _starpu_task_data_get_node_on_worker(struct starpu_task *task, unsigned index, unsigned worker)
  319. {
  320. unsigned local_node = starpu_worker_get_memory_node(worker);
  321. int node = STARPU_SPECIFIC_NODE_LOCAL;
  322. if (task->cl->specific_nodes)
  323. node = STARPU_CODELET_GET_NODE(task->cl, index);
  324. switch (node)
  325. {
  326. case STARPU_SPECIFIC_NODE_LOCAL:
  327. // TODO: rather find MCDRAM
  328. node = local_node;
  329. break;
  330. case STARPU_SPECIFIC_NODE_CPU:
  331. node = starpu_memory_nodes_numa_hwloclogid_to_id(_starpu_get_logical_close_numa_node_worker(worker));
  332. if (node == -1)
  333. node = STARPU_MAIN_RAM;
  334. break;
  335. case STARPU_SPECIFIC_NODE_SLOW:
  336. // TODO: rather leave in DDR
  337. node = local_node;
  338. break;
  339. case STARPU_SPECIFIC_NODE_LOCAL_OR_CPU:
  340. {
  341. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(task, index);
  342. if (mode & STARPU_R)
  343. {
  344. if (task->handles[index]->per_node[local_node].state != STARPU_INVALID)
  345. {
  346. /* It is here already, rather access it from here */
  347. node = local_node;
  348. }
  349. else
  350. {
  351. /* It is not here already, do not bother moving it */
  352. node = STARPU_MAIN_RAM;
  353. }
  354. }
  355. else
  356. {
  357. /* Nothing to read, consider where to write */
  358. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, index);
  359. if (handle->wt_mask & (1 << STARPU_MAIN_RAM))
  360. /* Write through, better simply write to the main memory */
  361. node = STARPU_MAIN_RAM;
  362. else
  363. /* Better keep temporary data on the accelerator to save PCI bandwidth */
  364. node = local_node;
  365. }
  366. break;
  367. }
  368. }
  369. return node;
  370. }
  371. struct _starpu_worker *_starpu_get_worker_from_driver(struct starpu_driver *d)
  372. {
  373. unsigned nworkers = starpu_worker_get_count();
  374. unsigned workerid;
  375. for (workerid = 0; workerid < nworkers; workerid++)
  376. {
  377. if (starpu_worker_get_type(workerid) == d->type)
  378. {
  379. struct _starpu_worker *worker;
  380. worker = _starpu_get_worker_struct(workerid);
  381. switch (d->type)
  382. {
  383. #ifdef STARPU_USE_CPU
  384. case STARPU_CPU_WORKER:
  385. if (worker->devid == d->id.cpu_id)
  386. return worker;
  387. break;
  388. #endif
  389. #ifdef STARPU_USE_OPENCL
  390. case STARPU_OPENCL_WORKER:
  391. {
  392. cl_device_id device;
  393. starpu_opencl_get_device(worker->devid, &device);
  394. if (device == d->id.opencl_id)
  395. return worker;
  396. break;
  397. }
  398. #endif
  399. #ifdef STARPU_USE_CUDA
  400. case STARPU_CUDA_WORKER:
  401. {
  402. if (worker->devid == d->id.cuda_id)
  403. return worker;
  404. break;
  405. }
  406. #endif
  407. default:
  408. (void) worker;
  409. _STARPU_DEBUG("Invalid device type\n");
  410. return NULL;
  411. }
  412. }
  413. }
  414. return NULL;
  415. }
  416. /*
  417. * Discover the topology of the machine
  418. */
  419. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  420. static void _starpu_initialize_workers_deviceid(int *explicit_workers_gpuid,
  421. int *current, int *workers_gpuid,
  422. const char *varname, unsigned nhwgpus,
  423. enum starpu_worker_archtype type)
  424. {
  425. char *strval;
  426. unsigned i;
  427. *current = 0;
  428. /* conf->workers_gpuid indicates the successive GPU identifier that
  429. * should be used to bind the workers. It should be either filled
  430. * according to the user's explicit parameters (from starpu_conf) or
  431. * according to the STARPU_WORKERS_CUDAID env. variable. Otherwise, a
  432. * round-robin policy is used to distributed the workers over the
  433. * cores. */
  434. /* what do we use, explicit value, env. variable, or round-robin ? */
  435. strval = starpu_getenv(varname);
  436. if (strval)
  437. {
  438. /* STARPU_WORKERS_CUDAID certainly contains less entries than
  439. * STARPU_NMAXWORKERS, so we reuse its entries in a round
  440. * robin fashion: "1 2" is equivalent to "1 2 1 2 1 2 .... 1
  441. * 2". */
  442. unsigned wrap = 0;
  443. unsigned number_of_entries = 0;
  444. char *endptr;
  445. /* we use the content of the STARPU_WORKERS_CUDAID
  446. * env. variable */
  447. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  448. {
  449. if (!wrap)
  450. {
  451. long int val;
  452. val = strtol(strval, &endptr, 10);
  453. if (endptr != strval)
  454. {
  455. workers_gpuid[i] = (unsigned)val;
  456. strval = endptr;
  457. }
  458. else
  459. {
  460. /* there must be at least one entry */
  461. STARPU_ASSERT(i != 0);
  462. number_of_entries = i;
  463. /* there is no more values in the
  464. * string */
  465. wrap = 1;
  466. workers_gpuid[i] = workers_gpuid[0];
  467. }
  468. }
  469. else
  470. {
  471. workers_gpuid[i] =
  472. workers_gpuid[i % number_of_entries];
  473. }
  474. }
  475. }
  476. else if (explicit_workers_gpuid)
  477. {
  478. /* we use the explicit value from the user */
  479. memcpy(workers_gpuid,
  480. explicit_workers_gpuid,
  481. STARPU_NMAXWORKERS*sizeof(unsigned));
  482. }
  483. else
  484. {
  485. /* by default, we take a round robin policy */
  486. if (nhwgpus > 0)
  487. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  488. workers_gpuid[i] = (unsigned)(i % nhwgpus);
  489. /* StarPU can use sampling techniques to bind threads
  490. * correctly */
  491. may_bind_automatically[type] = 1;
  492. }
  493. }
  494. #endif
  495. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  496. static void _starpu_initialize_workers_cuda_gpuid(struct _starpu_machine_config *config)
  497. {
  498. struct _starpu_machine_topology *topology = &config->topology;
  499. struct starpu_conf *uconf = &config->conf;
  500. _starpu_initialize_workers_deviceid(uconf->use_explicit_workers_cuda_gpuid == 0
  501. ? NULL
  502. : (int *)uconf->workers_cuda_gpuid,
  503. &(config->current_cuda_gpuid),
  504. (int *)topology->workers_cuda_gpuid,
  505. "STARPU_WORKERS_CUDAID",
  506. topology->nhwdevices[STARPU_CUDA_WORKER],
  507. STARPU_CUDA_WORKER);
  508. }
  509. static inline int _starpu_get_next_cuda_gpuid(struct _starpu_machine_config *config)
  510. {
  511. unsigned i = ((config->current_cuda_gpuid++) % config->topology.ndevices[STARPU_CUDA_WORKER]);
  512. return (int)config->topology.workers_cuda_gpuid[i];
  513. }
  514. #endif
  515. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  516. static void _starpu_initialize_workers_opencl_gpuid(struct _starpu_machine_config*config)
  517. {
  518. struct _starpu_machine_topology *topology = &config->topology;
  519. struct starpu_conf *uconf = &config->conf;
  520. _starpu_initialize_workers_deviceid(uconf->use_explicit_workers_opencl_gpuid == 0
  521. ? NULL
  522. : (int *)uconf->workers_opencl_gpuid,
  523. &(config->current_opencl_gpuid),
  524. (int *)topology->workers_opencl_gpuid,
  525. "STARPU_WORKERS_OPENCLID",
  526. topology->nhwdevices[STARPU_OPENCL_WORKER],
  527. STARPU_OPENCL_WORKER);
  528. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  529. // Detect devices which are already used with CUDA
  530. {
  531. unsigned tmp[STARPU_NMAXWORKERS];
  532. unsigned nb=0;
  533. int i;
  534. for(i=0 ; i<STARPU_NMAXWORKERS ; i++)
  535. {
  536. struct handle_entry *entry;
  537. int devid = config->topology.workers_opencl_gpuid[i];
  538. HASH_FIND_INT(devices_using_cuda, &devid, entry);
  539. if (entry == NULL)
  540. {
  541. tmp[nb] = topology->workers_opencl_gpuid[i];
  542. nb++;
  543. }
  544. }
  545. for (i=nb ; i<STARPU_NMAXWORKERS ; i++)
  546. tmp[i] = -1;
  547. memcpy(topology->workers_opencl_gpuid, tmp, sizeof(unsigned)*STARPU_NMAXWORKERS);
  548. }
  549. #endif /* STARPU_USE_CUDA */
  550. {
  551. // Detect identical devices
  552. struct handle_entry *devices_already_used = NULL;
  553. unsigned tmp[STARPU_NMAXWORKERS];
  554. unsigned nb=0;
  555. int i;
  556. for(i=0 ; i<STARPU_NMAXWORKERS ; i++)
  557. {
  558. int devid = topology->workers_opencl_gpuid[i];
  559. struct handle_entry *entry;
  560. HASH_FIND_INT(devices_already_used, &devid, entry);
  561. if (entry == NULL)
  562. {
  563. struct handle_entry *entry2;
  564. _STARPU_MALLOC(entry2, sizeof(*entry2));
  565. entry2->gpuid = devid;
  566. HASH_ADD_INT(devices_already_used, gpuid,
  567. entry2);
  568. tmp[nb] = devid;
  569. nb ++;
  570. }
  571. }
  572. struct handle_entry *entry=NULL, *tempo=NULL;
  573. HASH_ITER(hh, devices_already_used, entry, tempo)
  574. {
  575. HASH_DEL(devices_already_used, entry);
  576. free(entry);
  577. }
  578. for (i=nb ; i<STARPU_NMAXWORKERS ; i++)
  579. tmp[i] = -1;
  580. memcpy(topology->workers_opencl_gpuid, tmp, sizeof(unsigned)*STARPU_NMAXWORKERS);
  581. }
  582. }
  583. static inline int _starpu_get_next_opencl_gpuid(struct _starpu_machine_config *config)
  584. {
  585. unsigned i = ((config->current_opencl_gpuid++) % config->topology.ndevices[STARPU_OPENCL_WORKER]);
  586. return (int)config->topology.workers_opencl_gpuid[i];
  587. }
  588. #endif
  589. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  590. static inline int _starpu_get_next_mpi_deviceid(struct _starpu_machine_config *config)
  591. {
  592. unsigned i = ((config->current_mpi_deviceid++) % config->topology.ndevices[STARPU_MPI_MS_WORKER]);
  593. return (int)config->topology.workers_mpi_ms_deviceid[i];
  594. }
  595. static void _starpu_init_mpi_topology(struct _starpu_machine_config *config, long mpi_idx)
  596. {
  597. /* Discover the topology of the mpi node identifier by MPI_IDX. That
  598. * means, make this StarPU instance aware of the number of cores available
  599. * on this MPI device. Update the `nhwworker[STARPU_MPI_MS_WORKER]' topology field
  600. * accordingly. */
  601. struct _starpu_machine_topology *topology = &config->topology;
  602. int nbcores;
  603. _starpu_src_common_sink_nbcores(_starpu_mpi_ms_nodes[mpi_idx], &nbcores);
  604. topology->nhwworker[STARPU_MPI_MS_WORKER][mpi_idx] = nbcores;
  605. }
  606. #endif /* STARPU_USE_MPI_MASTER_SLAVE */
  607. #ifndef STARPU_SIMGRID
  608. #ifdef STARPU_HAVE_HWLOC
  609. static void _starpu_allocate_topology_userdata(hwloc_obj_t obj)
  610. {
  611. unsigned i;
  612. _STARPU_CALLOC(obj->userdata, 1, sizeof(struct _starpu_hwloc_userdata));
  613. for (i = 0; i < obj->arity; i++)
  614. _starpu_allocate_topology_userdata(obj->children[i]);
  615. #if HWLOC_API_VERSION >= 0x00020000
  616. hwloc_obj_t child;
  617. for (child = obj->io_first_child; child; child = child->next_sibling)
  618. _starpu_allocate_topology_userdata(child);
  619. #endif
  620. }
  621. static void _starpu_deallocate_topology_userdata(hwloc_obj_t obj)
  622. {
  623. unsigned i;
  624. struct _starpu_hwloc_userdata *data = obj->userdata;
  625. STARPU_ASSERT(!data->worker_list || data->worker_list == (void*)-1);
  626. free(data);
  627. for (i = 0; i < obj->arity; i++)
  628. _starpu_deallocate_topology_userdata(obj->children[i]);
  629. #if HWLOC_API_VERSION >= 0x00020000
  630. hwloc_obj_t child;
  631. for (child = obj->io_first_child; child; child = child->next_sibling)
  632. _starpu_deallocate_topology_userdata(child);
  633. #endif
  634. }
  635. #endif
  636. #endif
  637. static void _starpu_init_topology(struct _starpu_machine_config *config)
  638. {
  639. /* Discover the topology, meaning finding all the available PUs for
  640. the compiled drivers. These drivers MUST have been initialized
  641. before calling this function. The discovered topology is filled in
  642. CONFIG. */
  643. struct _starpu_machine_topology *topology = &config->topology;
  644. if (topology_is_initialized)
  645. return;
  646. nobind = starpu_get_env_number("STARPU_WORKERS_NOBIND");
  647. topology->nhwdevices[STARPU_CPU_WORKER] = 1;
  648. topology->nhwworker[STARPU_CPU_WORKER][0] = 0;
  649. topology->nhwpus = 0;
  650. #ifndef STARPU_SIMGRID
  651. #ifdef STARPU_HAVE_HWLOC
  652. hwloc_topology_init(&topology->hwtopology);
  653. char *hwloc_input = starpu_getenv("STARPU_HWLOC_INPUT");
  654. if (hwloc_input && hwloc_input[0])
  655. {
  656. int err = hwloc_topology_set_xml(topology->hwtopology, hwloc_input);
  657. if (err < 0) _STARPU_DISP("Could not load hwloc input %s\n", hwloc_input);
  658. }
  659. _starpu_topology_filter(topology->hwtopology);
  660. hwloc_topology_load(topology->hwtopology);
  661. #ifdef HAVE_HWLOC_CPUKINDS_GET_NR
  662. int nr_kinds = hwloc_cpukinds_get_nr(topology->hwtopology, 0);
  663. if (nr_kinds > 1)
  664. _STARPU_DISP("Warning: there are several kinds of CPU on this system. For now StarPU assumes all CPU are equal\n");
  665. #endif
  666. if (starpu_get_env_number_default("STARPU_WORKERS_GETBIND", 0))
  667. {
  668. /* Respect the existing binding */
  669. hwloc_bitmap_t cpuset = hwloc_bitmap_alloc();
  670. int ret = hwloc_get_cpubind(topology->hwtopology, cpuset, HWLOC_CPUBIND_THREAD);
  671. if (ret)
  672. _STARPU_DISP("Warning: could not get current CPU binding: %s\n", strerror(errno));
  673. else
  674. {
  675. ret = hwloc_topology_restrict(topology->hwtopology, cpuset, 0);
  676. if (ret)
  677. _STARPU_DISP("Warning: could not restrict hwloc to cpuset: %s\n", strerror(errno));
  678. }
  679. hwloc_bitmap_free(cpuset);
  680. }
  681. _starpu_allocate_topology_userdata(hwloc_get_root_obj(topology->hwtopology));
  682. #endif
  683. #endif
  684. #ifdef STARPU_SIMGRID
  685. config->topology.nhwworker[STARPU_CPU_WORKER][0] = config->topology.nhwpus = _starpu_simgrid_get_nbhosts("CPU");
  686. #elif defined(STARPU_HAVE_HWLOC)
  687. /* Discover the CPUs relying on the hwloc interface and fills CONFIG
  688. * accordingly. */
  689. config->cpu_depth = hwloc_get_type_depth(topology->hwtopology, HWLOC_OBJ_CORE);
  690. config->pu_depth = hwloc_get_type_depth(topology->hwtopology, HWLOC_OBJ_PU);
  691. /* Would be very odd */
  692. STARPU_ASSERT(config->cpu_depth != HWLOC_TYPE_DEPTH_MULTIPLE);
  693. if (config->cpu_depth == HWLOC_TYPE_DEPTH_UNKNOWN)
  694. {
  695. /* unknown, using logical procesors as fallback */
  696. _STARPU_DISP("Warning: The OS did not report CPU cores. Assuming there is only one hardware thread per core.\n");
  697. config->cpu_depth = hwloc_get_type_depth(topology->hwtopology,
  698. HWLOC_OBJ_PU);
  699. }
  700. topology->nhwworker[STARPU_CPU_WORKER][0] = hwloc_get_nbobjs_by_depth(topology->hwtopology, config->cpu_depth);
  701. topology->nhwpus = hwloc_get_nbobjs_by_depth(topology->hwtopology, config->pu_depth);
  702. #elif defined(HAVE_SYSCONF)
  703. /* Discover the CPUs relying on the sysconf(3) function and fills
  704. * CONFIG accordingly. */
  705. config->topology.nhwworker[STARPU_CPU_WORKER][0] = config->topology.nhwpus = sysconf(_SC_NPROCESSORS_ONLN);
  706. #elif defined(_WIN32)
  707. /* Discover the CPUs on Cygwin and MinGW systems. */
  708. SYSTEM_INFO sysinfo;
  709. GetSystemInfo(&sysinfo);
  710. config->topology.nhwworker[STARPU_CPU_WORKER][0] = config->topology.nhwpus = sysinfo.dwNumberOfProcessors;
  711. #else
  712. #warning no way to know number of cores, assuming 1
  713. config->topology.nhwworker[STARPU_CPU_WORKER][0] = config->topology.nhwpus = 1;
  714. #endif
  715. if (config->conf.ncuda != 0)
  716. _starpu_cuda_discover_devices(config);
  717. if (config->conf.nopencl != 0)
  718. _starpu_opencl_discover_devices(config);
  719. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  720. config->topology.nhwdevices[STARPU_MPI_MS_WORKER] = _starpu_mpi_src_get_device_count();
  721. #endif
  722. topology_is_initialized = 1;
  723. }
  724. /*
  725. * Bind workers on the different processors
  726. */
  727. static void _starpu_initialize_workers_bindid(struct _starpu_machine_config *config)
  728. {
  729. char *strval;
  730. unsigned i;
  731. struct _starpu_machine_topology *topology = &config->topology;
  732. int nhyperthreads = topology->nhwpus / topology->nhwworker[STARPU_CPU_WORKER][0];
  733. unsigned bind_on_core = 0;
  734. int scale = 1;
  735. config->current_bindid = 0;
  736. if (starpu_getenv("STARPU_WORKERS_CPUID") && starpu_getenv("STARPU_WORKERS_COREID"))
  737. {
  738. _STARPU_DISP("Warning: STARPU_WORKERS_CPUID and STARPU_WORKERS_COREID cannot be set at the same time. STARPU_WORKERS_CPUID will be used.\n");
  739. }
  740. /* conf->workers_bindid indicates the successive logical PU identifier that
  741. * should be used to bind the workers. It should be either filled
  742. * according to the user's explicit parameters (from starpu_conf) or
  743. * according to the STARPU_WORKERS_CPUID env. variable. Otherwise, a
  744. * round-robin policy is used to distributed the workers over the
  745. * cores. */
  746. /* what do we use, explicit value, env. variable, or round-robin ? */
  747. strval = starpu_getenv("STARPU_WORKERS_CPUID");
  748. if (strval == NULL)
  749. {
  750. strval = starpu_getenv("STARPU_WORKERS_COREID");
  751. if (strval)
  752. {
  753. bind_on_core = 1;
  754. scale = nhyperthreads;
  755. }
  756. }
  757. if (strval)
  758. {
  759. /* STARPU_WORKERS_CPUID certainly contains less entries than
  760. * STARPU_NMAXWORKERS, so we reuse its entries in a round
  761. * robin fashion: "1 2" is equivalent to "1 2 1 2 1 2 .... 1
  762. * 2". */
  763. unsigned wrap = 0;
  764. unsigned number_of_entries = 0;
  765. char *endptr;
  766. /* we use the content of the STARPU_WORKERS_CPUID
  767. * env. variable */
  768. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  769. {
  770. if (!wrap)
  771. {
  772. long int val;
  773. val = strtol(strval, &endptr, 10);
  774. if (endptr != strval)
  775. {
  776. topology->workers_bindid[i] = (unsigned)((val * scale) % topology->nhwpus);
  777. strval = endptr;
  778. if (*strval == '-')
  779. {
  780. /* range of values */
  781. long int endval;
  782. strval++;
  783. if (*strval && *strval != ' ' && *strval != ',')
  784. {
  785. endval = strtol(strval, &endptr, 10);
  786. strval = endptr;
  787. }
  788. else
  789. {
  790. endval = (bind_on_core ? topology->nhwworker[STARPU_CPU_WORKER][0] : topology->nhwpus) - 1;
  791. if (*strval)
  792. strval++;
  793. }
  794. for (val++; val <= endval && i < STARPU_NMAXWORKERS-1; val++)
  795. {
  796. i++;
  797. topology->workers_bindid[i] = (unsigned)((val * scale) % topology->nhwpus);
  798. }
  799. }
  800. if (*strval == ',')
  801. strval++;
  802. }
  803. else
  804. {
  805. /* there must be at least one entry */
  806. STARPU_ASSERT(i != 0);
  807. number_of_entries = i;
  808. /* there is no more values in the
  809. * string */
  810. wrap = 1;
  811. topology->workers_bindid[i] =
  812. topology->workers_bindid[0];
  813. }
  814. }
  815. else
  816. {
  817. topology->workers_bindid[i] =
  818. topology->workers_bindid[i % number_of_entries];
  819. }
  820. }
  821. }
  822. else if (config->conf.use_explicit_workers_bindid)
  823. {
  824. /* we use the explicit value from the user */
  825. memcpy(topology->workers_bindid,
  826. config->conf.workers_bindid,
  827. STARPU_NMAXWORKERS*sizeof(unsigned));
  828. }
  829. else
  830. {
  831. int nth_per_core = starpu_get_env_number_default("STARPU_NTHREADS_PER_CORE", 1);
  832. int k;
  833. int nbindids=0;
  834. STARPU_ASSERT_MSG(nth_per_core > 0 && nth_per_core <= nhyperthreads , "Incorrect number of hyperthreads");
  835. i = 0; /* PU number currently assigned */
  836. k = 0; /* Number of threads already put on the current core */
  837. while(nbindids < STARPU_NMAXWORKERS)
  838. {
  839. if (k >= nth_per_core)
  840. {
  841. /* We have already put enough workers on this
  842. * core, skip remaining PUs from this core, and
  843. * proceed with next core */
  844. i += nhyperthreads-nth_per_core;
  845. k = 0;
  846. continue;
  847. }
  848. /* Add a worker to this core, by using this logical PU */
  849. topology->workers_bindid[nbindids++] = (unsigned)(i % topology->nhwpus);
  850. k++;
  851. i++;
  852. }
  853. }
  854. for (i = 0; i < STARPU_MAXCPUS;i++)
  855. cpu_worker[i] = STARPU_NOWORKERID;
  856. /* no binding yet */
  857. memset(&config->currently_bound, 0, sizeof(config->currently_bound));
  858. memset(&config->currently_shared, 0, sizeof(config->currently_shared));
  859. }
  860. static void _starpu_deinitialize_workers_bindid(struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED)
  861. {
  862. unsigned i;
  863. for (i = 0; i < STARPU_MAXCPUS;i++)
  864. {
  865. if (cpu_name[i])
  866. {
  867. free(cpu_name[i]);
  868. cpu_name[i] = NULL;
  869. }
  870. }
  871. }
  872. /* This function gets the identifier of the next core on which to bind a
  873. * worker. In case a list of preferred cores was specified (logical indexes),
  874. * we look for a an available core among the list if possible, otherwise a
  875. * round-robin policy is used. */
  876. static inline unsigned _starpu_get_next_bindid(struct _starpu_machine_config *config, unsigned flags,
  877. unsigned *preferred_binding, unsigned npreferred)
  878. {
  879. struct _starpu_machine_topology *topology = &config->topology;
  880. STARPU_ASSERT_MSG(topology_is_initialized, "The StarPU core is not initialized yet, have you called starpu_init?");
  881. unsigned current_preferred;
  882. unsigned nhyperthreads = topology->nhwpus / topology->nhwworker[STARPU_CPU_WORKER][0];
  883. unsigned ncores = topology->nhwpus / nhyperthreads;
  884. unsigned i;
  885. if (npreferred)
  886. {
  887. STARPU_ASSERT_MSG(preferred_binding, "Passing NULL pointer for parameter preferred_binding with a non-0 value of parameter npreferred");
  888. }
  889. /* loop over the preference list */
  890. for (current_preferred = 0;
  891. current_preferred < npreferred;
  892. current_preferred++)
  893. {
  894. /* can we bind the worker on the preferred core ? */
  895. unsigned requested_core = preferred_binding[current_preferred];
  896. unsigned requested_bindid = requested_core * nhyperthreads;
  897. /* Look at the remaining cores to be bound to */
  898. for (i = 0; i < ncores; i++)
  899. {
  900. if (topology->workers_bindid[i] == requested_bindid &&
  901. (!config->currently_bound[i] ||
  902. (config->currently_shared[i] && !(flags & STARPU_THREAD_ACTIVE)))
  903. )
  904. {
  905. /* the cpu is available, or shareable with us, we use it ! */
  906. config->currently_bound[i] = 1;
  907. if (!(flags & STARPU_THREAD_ACTIVE))
  908. config->currently_shared[i] = 1;
  909. return requested_bindid;
  910. }
  911. }
  912. }
  913. if (!(flags & STARPU_THREAD_ACTIVE))
  914. {
  915. /* Try to find a shareable PU */
  916. for (i = 0; i < ncores; i++)
  917. if (config->currently_shared[i])
  918. return topology->workers_bindid[i];
  919. }
  920. /* Try to find an available PU from last used PU */
  921. for (i = config->current_bindid; i < ncores; i++)
  922. if (!config->currently_bound[i])
  923. /* Found a cpu ready for use, use it! */
  924. break;
  925. if (i == ncores)
  926. {
  927. /* Finished binding on all cpus, restart from start in
  928. * case the user really wants overloading */
  929. memset(&config->currently_bound, 0, sizeof(config->currently_bound));
  930. i = 0;
  931. }
  932. STARPU_ASSERT(i < ncores);
  933. unsigned bindid = topology->workers_bindid[i];
  934. config->currently_bound[i] = 1;
  935. if (!(flags & STARPU_THREAD_ACTIVE))
  936. config->currently_shared[i] = 1;
  937. config->current_bindid = i;
  938. return bindid;
  939. }
  940. unsigned starpu_get_next_bindid(unsigned flags, unsigned *preferred, unsigned npreferred)
  941. {
  942. return _starpu_get_next_bindid(_starpu_get_machine_config(), flags, preferred, npreferred);
  943. }
  944. unsigned _starpu_topology_get_nhwcpu(struct _starpu_machine_config *config)
  945. {
  946. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  947. if (config->conf.nopencl != 0)
  948. _starpu_opencl_init();
  949. #endif
  950. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  951. if (config->conf.ncuda != 0)
  952. _starpu_init_cuda();
  953. #endif
  954. _starpu_init_topology(config);
  955. return config->topology.nhwworker[STARPU_CPU_WORKER][0];
  956. }
  957. unsigned _starpu_topology_get_nhwpu(struct _starpu_machine_config *config)
  958. {
  959. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  960. if (config->conf.nopencl != 0)
  961. _starpu_opencl_init();
  962. #endif
  963. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  964. if (config->conf.ncuda != 0)
  965. _starpu_init_cuda();
  966. #endif
  967. _starpu_init_topology(config);
  968. return config->topology.nhwpus;
  969. }
  970. unsigned _starpu_topology_get_nnumanodes(struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED)
  971. {
  972. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  973. if (config->conf.nopencl != 0)
  974. _starpu_opencl_init();
  975. #endif
  976. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  977. if (config->conf.ncuda != 0)
  978. _starpu_init_cuda();
  979. #endif
  980. _starpu_init_topology(config);
  981. int res;
  982. #if defined(STARPU_HAVE_HWLOC)
  983. if (numa_enabled == -1)
  984. numa_enabled = starpu_get_env_number_default("STARPU_USE_NUMA", 0);
  985. if (numa_enabled)
  986. {
  987. struct _starpu_machine_topology *topology = &config->topology ;
  988. int nnumanodes = hwloc_get_nbobjs_by_type(topology->hwtopology, HWLOC_OBJ_NUMANODE) ;
  989. res = nnumanodes > 0 ? nnumanodes : 1 ;
  990. }
  991. else
  992. #endif
  993. {
  994. res = 1;
  995. }
  996. STARPU_ASSERT_MSG(res <= STARPU_MAXNUMANODES, "Number of NUMA nodes discovered %d is higher than maximum accepted %d ! Use configure option --enable-maxnumanodes=xxx to increase the maximum value of supported NUMA nodes.\n", res, STARPU_MAXNUMANODES);
  997. return res;
  998. }
  999. #ifdef STARPU_HAVE_HWLOC
  1000. void _starpu_topology_filter(hwloc_topology_t topology)
  1001. {
  1002. #if HWLOC_API_VERSION >= 0x20000
  1003. hwloc_topology_set_io_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_ALL);
  1004. hwloc_topology_set_flags(topology, HWLOC_TOPOLOGY_FLAG_IS_THISSYSTEM);
  1005. #else
  1006. hwloc_topology_set_flags(topology, HWLOC_TOPOLOGY_FLAG_IS_THISSYSTEM | HWLOC_TOPOLOGY_FLAG_WHOLE_IO);
  1007. #endif
  1008. #ifdef HAVE_HWLOC_TOPOLOGY_SET_COMPONENTS
  1009. # ifndef STARPU_USE_CUDA
  1010. hwloc_topology_set_components(topology, HWLOC_TOPOLOGY_COMPONENTS_FLAG_BLACKLIST, "cuda");
  1011. hwloc_topology_set_components(topology, HWLOC_TOPOLOGY_COMPONENTS_FLAG_BLACKLIST, "nvml");
  1012. # endif
  1013. # ifndef STARPU_USE_OPENCL
  1014. hwloc_topology_set_components(topology, HWLOC_TOPOLOGY_COMPONENTS_FLAG_BLACKLIST, "opencl");
  1015. # endif
  1016. #endif
  1017. }
  1018. #endif
  1019. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1020. static void _starpu_init_mpi_config(struct _starpu_machine_config *config,
  1021. struct starpu_conf *user_conf,
  1022. unsigned mpi_idx)
  1023. {
  1024. struct _starpu_machine_topology *topology = &config->topology;
  1025. topology->nhwworker[STARPU_MPI_MS_WORKER][mpi_idx] = 0;
  1026. _starpu_init_mpi_topology(config, mpi_idx);
  1027. int nmpicores;
  1028. nmpicores = starpu_get_env_number("STARPU_NMPIMSTHREADS");
  1029. if (nmpicores == -1)
  1030. {
  1031. /* Nothing was specified, so let's use the number of
  1032. * detected mpi cores. ! */
  1033. nmpicores = topology->nhwworker[STARPU_MPI_MS_WORKER][mpi_idx];
  1034. }
  1035. else
  1036. {
  1037. if ((unsigned) nmpicores > topology->nhwworker[STARPU_MPI_MS_WORKER][mpi_idx])
  1038. {
  1039. /* The user requires more MPI cores than there is available */
  1040. _STARPU_MSG("# Warning: %d MPI cores requested. Only %u available.\n",
  1041. nmpicores, topology->nhwworker[STARPU_MPI_MS_WORKER][mpi_idx]);
  1042. nmpicores = topology->nhwworker[STARPU_MPI_MS_WORKER][mpi_idx];
  1043. }
  1044. }
  1045. topology->nworker[STARPU_MPI_MS_WORKER][mpi_idx] = nmpicores;
  1046. STARPU_ASSERT_MSG(topology->nworker[STARPU_MPI_MS_WORKER][mpi_idx] + topology->nworkers <= STARPU_NMAXWORKERS,
  1047. "topology->nworker[STARPU_MPI_MS_WORKER][mpi_idx(%u)] (%u) + topology->nworkers (%u) <= STARPU_NMAXWORKERS (%d)",
  1048. mpi_idx, topology->nworker[STARPU_MPI_MS_WORKER][mpi_idx], topology->nworkers, STARPU_NMAXWORKERS);
  1049. mpi_worker_set[mpi_idx].workers = &config->workers[topology->nworkers];
  1050. mpi_worker_set[mpi_idx].nworkers = topology->nworker[STARPU_MPI_MS_WORKER][mpi_idx];
  1051. unsigned mpicore_id;
  1052. for (mpicore_id = 0; mpicore_id < topology->nworker[STARPU_MPI_MS_WORKER][mpi_idx]; mpicore_id++)
  1053. {
  1054. int worker_idx = topology->nworkers + mpicore_id;
  1055. config->workers[worker_idx].set = &mpi_worker_set[mpi_idx];
  1056. config->workers[worker_idx].arch = STARPU_MPI_MS_WORKER;
  1057. _STARPU_MALLOC(config->workers[worker_idx].perf_arch.devices, sizeof(struct starpu_perfmodel_device));
  1058. config->workers[worker_idx].perf_arch.ndevices = 1;
  1059. config->workers[worker_idx].perf_arch.devices[0].type = STARPU_MPI_MS_WORKER;
  1060. config->workers[worker_idx].perf_arch.devices[0].devid = mpi_idx;
  1061. config->workers[worker_idx].perf_arch.devices[0].ncores = 1;
  1062. config->workers[worker_idx].devid = mpi_idx;
  1063. config->workers[worker_idx].subworkerid = mpicore_id;
  1064. config->workers[worker_idx].worker_mask = STARPU_MPI_MS;
  1065. config->worker_mask |= STARPU_MPI_MS;
  1066. }
  1067. _starpu_mpi_ms_nodes[mpi_idx]->baseworkerid = topology->nworkers;
  1068. topology->nworkers += topology->nworker[STARPU_MPI_MS_WORKER][mpi_idx];
  1069. }
  1070. #endif
  1071. #if defined(STARPU_USE_MPI_MASTER_SLAVE)
  1072. static void _starpu_init_mp_config(struct _starpu_machine_config *config,
  1073. struct starpu_conf *user_conf, int no_mp_config)
  1074. {
  1075. /* Discover and configure the mp topology. That means:
  1076. * - discover the number of mp nodes;
  1077. * - initialize each discovered node;
  1078. * - discover the local topology (number of PUs/devices) of each node;
  1079. * - configure the workers accordingly.
  1080. */
  1081. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1082. {
  1083. struct _starpu_machine_topology *topology = &config->topology;
  1084. /* Discover and initialize the number of MPI nodes through the mp
  1085. * infrastructure. */
  1086. unsigned nhwmpidevices = _starpu_mpi_src_get_device_count();
  1087. int reqmpidevices = starpu_get_env_number("STARPU_NMPI_MS");
  1088. if (reqmpidevices == -1 && user_conf)
  1089. reqmpidevices = user_conf->nmpi_ms;
  1090. if (reqmpidevices == -1)
  1091. /* Nothing was specified, so let's use the number of
  1092. * detected mpi devices. ! */
  1093. reqmpidevices = nhwmpidevices;
  1094. if (reqmpidevices != -1)
  1095. {
  1096. if ((unsigned) reqmpidevices > nhwmpidevices)
  1097. {
  1098. /* The user requires more MPI devices than there is available */
  1099. _STARPU_MSG("# Warning: %d MPI Master-Slave devices requested. Only %u available.\n",
  1100. reqmpidevices, nhwmpidevices);
  1101. reqmpidevices = nhwmpidevices;
  1102. }
  1103. }
  1104. topology->ndevices[STARPU_MPI_MS_WORKER] = reqmpidevices;
  1105. /* if user don't want to use MPI slaves, we close the slave processes */
  1106. if (no_mp_config && topology->ndevices[STARPU_MPI_MS_WORKER] == 0)
  1107. {
  1108. _starpu_mpi_common_mp_deinit();
  1109. exit(0);
  1110. }
  1111. if (!no_mp_config)
  1112. {
  1113. unsigned i;
  1114. for (i = 0; i < topology->ndevices[STARPU_MPI_MS_WORKER]; i++)
  1115. _starpu_mpi_ms_nodes[i] = _starpu_mp_common_node_create(STARPU_NODE_MPI_SOURCE, i);
  1116. for (i = 0; i < topology->ndevices[STARPU_MPI_MS_WORKER]; i++)
  1117. _starpu_init_mpi_config(config, user_conf, i);
  1118. }
  1119. }
  1120. #endif
  1121. }
  1122. #endif
  1123. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1124. static void _starpu_deinit_mpi_node(int devid)
  1125. {
  1126. _starpu_mp_common_send_command(_starpu_mpi_ms_nodes[devid], STARPU_MP_COMMAND_EXIT, NULL, 0);
  1127. _starpu_mp_common_node_destroy(_starpu_mpi_ms_nodes[devid]);
  1128. }
  1129. #endif
  1130. #if defined(STARPU_USE_MPI_MASTER_SLAVE)
  1131. static void _starpu_deinit_mp_config(struct _starpu_machine_config *config)
  1132. {
  1133. struct _starpu_machine_topology *topology = &config->topology;
  1134. unsigned i;
  1135. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1136. for (i = 0; i < topology->ndevices[STARPU_MPI_MS_WORKER]; i++)
  1137. _starpu_deinit_mpi_node(i);
  1138. #endif
  1139. }
  1140. #endif
  1141. #ifdef STARPU_HAVE_HWLOC
  1142. static unsigned _starpu_topology_count_ngpus(hwloc_obj_t obj)
  1143. {
  1144. struct _starpu_hwloc_userdata *data = obj->userdata;
  1145. unsigned n = data->ngpus;
  1146. unsigned i;
  1147. for (i = 0; i < obj->arity; i++)
  1148. n += _starpu_topology_count_ngpus(obj->children[i]);
  1149. data->ngpus = n;
  1150. //#ifdef STARPU_VERBOSE
  1151. // {
  1152. // char name[64];
  1153. // hwloc_obj_type_snprintf(name, sizeof(name), obj, 0);
  1154. // _STARPU_DEBUG("hwloc obj %s has %u GPUs below\n", name, n);
  1155. // }
  1156. //#endif
  1157. return n;
  1158. }
  1159. #endif
  1160. static int _starpu_init_machine_config(struct _starpu_machine_config *config, int no_mp_config STARPU_ATTRIBUTE_UNUSED)
  1161. {
  1162. int i;
  1163. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  1164. {
  1165. config->workers[i].workerid = i;
  1166. config->workers[i].set = NULL;
  1167. }
  1168. struct _starpu_machine_topology *topology = &config->topology;
  1169. topology->nworkers = 0;
  1170. topology->ncombinedworkers = 0;
  1171. topology->nsched_ctxs = 0;
  1172. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  1173. if (config->conf.nopencl != 0)
  1174. _starpu_opencl_init();
  1175. #endif
  1176. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  1177. if (config->conf.ncuda != 0)
  1178. _starpu_init_cuda();
  1179. #endif
  1180. _starpu_init_topology(config);
  1181. _starpu_initialize_workers_bindid(config);
  1182. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  1183. for (i = 0; i < (int) (sizeof(cuda_worker_set)/sizeof(cuda_worker_set[0])); i++)
  1184. cuda_worker_set[i].workers = NULL;
  1185. #endif
  1186. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1187. for (i = 0; i < (int) (sizeof(mpi_worker_set)/sizeof(mpi_worker_set[0])); i++)
  1188. mpi_worker_set[i].workers = NULL;
  1189. #endif
  1190. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  1191. int ncuda = config->conf.ncuda;
  1192. int nworker_per_cuda = starpu_get_env_number_default("STARPU_NWORKER_PER_CUDA", 1);
  1193. STARPU_ASSERT_MSG(nworker_per_cuda > 0, "STARPU_NWORKER_PER_CUDA has to be > 0");
  1194. STARPU_ASSERT_MSG(nworker_per_cuda < STARPU_NMAXWORKERS, "STARPU_NWORKER_PER_CUDA (%d) cannot be higher than STARPU_NMAXWORKERS (%d)\n", nworker_per_cuda, STARPU_NMAXWORKERS);
  1195. #ifndef STARPU_NON_BLOCKING_DRIVERS
  1196. if (nworker_per_cuda > 1)
  1197. {
  1198. _STARPU_DISP("Warning: reducing STARPU_NWORKER_PER_CUDA to 1 because blocking drivers are enabled\n");
  1199. nworker_per_cuda = 1;
  1200. }
  1201. #endif
  1202. if (ncuda != 0)
  1203. {
  1204. /* The user did not disable CUDA. We need to initialize CUDA
  1205. * early to count the number of devices */
  1206. _starpu_init_cuda();
  1207. int nb_devices = _starpu_get_cuda_device_count();
  1208. STARPU_ASSERT_MSG(ncuda >= -1, "ncuda can not be negative and different from -1 (is is %d)", ncuda);
  1209. if (ncuda == -1)
  1210. {
  1211. /* Nothing was specified, so let's choose ! */
  1212. ncuda = nb_devices;
  1213. }
  1214. else
  1215. {
  1216. if (ncuda > nb_devices)
  1217. {
  1218. /* The user requires more CUDA devices than
  1219. * there is available */
  1220. _STARPU_DISP("Warning: %d CUDA devices requested. Only %d available.\n", ncuda, nb_devices);
  1221. ncuda = nb_devices;
  1222. }
  1223. }
  1224. }
  1225. /* Now we know how many CUDA devices will be used */
  1226. topology->ndevices[STARPU_CUDA_WORKER] = ncuda;
  1227. for (i = 0; i < ncuda; i++)
  1228. topology->nworker[STARPU_CUDA_WORKER][i] = nworker_per_cuda;
  1229. STARPU_ASSERT(topology->ndevices[STARPU_CUDA_WORKER] <= STARPU_MAXCUDADEVS);
  1230. _starpu_initialize_workers_cuda_gpuid(config);
  1231. /* allow having one worker per stream */
  1232. topology->cuda_th_per_stream = starpu_get_env_number_default("STARPU_CUDA_THREAD_PER_WORKER", -1);
  1233. topology->cuda_th_per_dev = starpu_get_env_number_default("STARPU_CUDA_THREAD_PER_DEV", -1);
  1234. STARPU_ASSERT_MSG(!(topology->cuda_th_per_stream == 1 && topology->cuda_th_per_dev != -1), "It does not make sense to set both STARPU_CUDA_THREAD_PER_WORKER to 1 and to set STARPU_CUDA_THREAD_PER_DEV, please choose either per worker or per device or none");
  1235. /* per device by default */
  1236. if (topology->cuda_th_per_dev == -1)
  1237. {
  1238. if (topology->cuda_th_per_stream == 1)
  1239. topology->cuda_th_per_dev = 0;
  1240. else
  1241. topology->cuda_th_per_dev = 1;
  1242. }
  1243. /* Not per stream by default */
  1244. if (topology->cuda_th_per_stream == -1)
  1245. {
  1246. topology->cuda_th_per_stream = 0;
  1247. }
  1248. if (!topology->cuda_th_per_dev)
  1249. {
  1250. cuda_worker_set[0].workers = &config->workers[topology->nworkers];
  1251. cuda_worker_set[0].nworkers = topology->ndevices[STARPU_CUDA_WORKER] * nworker_per_cuda;
  1252. }
  1253. unsigned cudagpu;
  1254. for (cudagpu = 0; cudagpu < topology->ndevices[STARPU_CUDA_WORKER]; cudagpu++)
  1255. {
  1256. int devid = _starpu_get_next_cuda_gpuid(config);
  1257. int worker_idx0 = topology->nworkers + cudagpu * nworker_per_cuda;
  1258. struct _starpu_worker_set *worker_set;
  1259. if (topology->cuda_th_per_dev)
  1260. {
  1261. worker_set = &cuda_worker_set[devid];
  1262. worker_set->workers = &config->workers[worker_idx0];
  1263. worker_set->nworkers = nworker_per_cuda;
  1264. }
  1265. else
  1266. {
  1267. /* Same worker set for all devices */
  1268. worker_set = &cuda_worker_set[0];
  1269. }
  1270. for (i = 0; i < nworker_per_cuda; i++)
  1271. {
  1272. int worker_idx = worker_idx0 + i;
  1273. if(topology->cuda_th_per_stream)
  1274. {
  1275. /* Just one worker in the set */
  1276. _STARPU_CALLOC(config->workers[worker_idx].set, 1, sizeof(struct _starpu_worker_set));
  1277. config->workers[worker_idx].set->workers = &config->workers[worker_idx];
  1278. config->workers[worker_idx].set->nworkers = 1;
  1279. }
  1280. else
  1281. config->workers[worker_idx].set = worker_set;
  1282. config->workers[worker_idx].arch = STARPU_CUDA_WORKER;
  1283. _STARPU_MALLOC(config->workers[worker_idx].perf_arch.devices, sizeof(struct starpu_perfmodel_device));
  1284. config->workers[worker_idx].perf_arch.ndevices = 1;
  1285. config->workers[worker_idx].perf_arch.devices[0].type = STARPU_CUDA_WORKER;
  1286. config->workers[worker_idx].perf_arch.devices[0].devid = devid;
  1287. // TODO: fix perfmodels etc.
  1288. //config->workers[worker_idx].perf_arch.ncore = nworker_per_cuda - 1;
  1289. config->workers[worker_idx].perf_arch.devices[0].ncores = 1;
  1290. config->workers[worker_idx].devid = devid;
  1291. config->workers[worker_idx].subworkerid = i;
  1292. config->workers[worker_idx].worker_mask = STARPU_CUDA;
  1293. config->worker_mask |= STARPU_CUDA;
  1294. struct handle_entry *entry;
  1295. HASH_FIND_INT(devices_using_cuda, &devid, entry);
  1296. if (!entry)
  1297. {
  1298. _STARPU_MALLOC(entry, sizeof(*entry));
  1299. entry->gpuid = devid;
  1300. HASH_ADD_INT(devices_using_cuda, gpuid, entry);
  1301. }
  1302. }
  1303. #ifndef STARPU_SIMGRID
  1304. #if defined(HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX) && HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX
  1305. {
  1306. hwloc_obj_t obj = hwloc_cuda_get_device_osdev_by_index(topology->hwtopology, devid);
  1307. if (obj)
  1308. {
  1309. struct _starpu_hwloc_userdata *data = obj->userdata;
  1310. data->ngpus++;
  1311. }
  1312. else
  1313. {
  1314. _STARPU_DEBUG("Warning: could not find location of CUDA%u, do you have the hwloc CUDA plugin installed?\n", devid);
  1315. }
  1316. }
  1317. #endif
  1318. #endif
  1319. }
  1320. topology->nworkers += topology->ndevices[STARPU_CUDA_WORKER] * nworker_per_cuda;
  1321. #endif
  1322. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  1323. int nopencl = config->conf.nopencl;
  1324. if (nopencl != 0)
  1325. {
  1326. /* The user did not disable OPENCL. We need to initialize
  1327. * OpenCL early to count the number of devices */
  1328. _starpu_opencl_init();
  1329. int nb_devices;
  1330. nb_devices = _starpu_opencl_get_device_count();
  1331. STARPU_ASSERT_MSG(nopencl >= -1, "nopencl can not be negative and different from -1 (is is %d)", nopencl);
  1332. if (nopencl == -1)
  1333. {
  1334. /* Nothing was specified, so let's choose ! */
  1335. nopencl = nb_devices;
  1336. if (nopencl > STARPU_MAXOPENCLDEVS)
  1337. {
  1338. _STARPU_DISP("Warning: %d OpenCL devices available. Only %d enabled. Use configure option --enable-maxopencldadev=xxx to update the maximum value of supported OpenCL devices.\n", nb_devices, STARPU_MAXOPENCLDEVS);
  1339. nopencl = STARPU_MAXOPENCLDEVS;
  1340. }
  1341. }
  1342. else
  1343. {
  1344. /* Let's make sure this value is OK. */
  1345. if (nopencl > nb_devices)
  1346. {
  1347. /* The user requires more OpenCL devices than
  1348. * there is available */
  1349. _STARPU_DISP("Warning: %d OpenCL devices requested. Only %d available.\n", nopencl, nb_devices);
  1350. nopencl = nb_devices;
  1351. }
  1352. /* Let's make sure this value is OK. */
  1353. if (nopencl > STARPU_MAXOPENCLDEVS)
  1354. {
  1355. _STARPU_DISP("Warning: %d OpenCL devices requested. Only %d enabled. Use configure option --enable-maxopencldev=xxx to update the maximum value of supported OpenCL devices.\n", nopencl, STARPU_MAXOPENCLDEVS);
  1356. nopencl = STARPU_MAXOPENCLDEVS;
  1357. }
  1358. }
  1359. }
  1360. topology->ndevices[STARPU_OPENCL_WORKER] = nopencl;
  1361. for (i = 0; i < nopencl; i++)
  1362. topology->nworker[STARPU_OPENCL_WORKER][i] = 1;
  1363. STARPU_ASSERT(topology->ndevices[STARPU_OPENCL_WORKER] + topology->nworkers <= STARPU_NMAXWORKERS);
  1364. _starpu_initialize_workers_opencl_gpuid(config);
  1365. unsigned openclgpu;
  1366. for (openclgpu = 0; openclgpu < topology->ndevices[STARPU_OPENCL_WORKER]; openclgpu++)
  1367. {
  1368. int worker_idx = topology->nworkers + openclgpu;
  1369. int devid = _starpu_get_next_opencl_gpuid(config);
  1370. if (devid == -1)
  1371. {
  1372. // There is no more devices left
  1373. topology->ndevices[STARPU_OPENCL_WORKER] = openclgpu;
  1374. break;
  1375. }
  1376. config->workers[worker_idx].arch = STARPU_OPENCL_WORKER;
  1377. _STARPU_MALLOC(config->workers[worker_idx].perf_arch.devices, sizeof(struct starpu_perfmodel_device));
  1378. config->workers[worker_idx].perf_arch.ndevices = 1;
  1379. config->workers[worker_idx].perf_arch.devices[0].type = STARPU_OPENCL_WORKER;
  1380. config->workers[worker_idx].perf_arch.devices[0].devid = devid;
  1381. config->workers[worker_idx].perf_arch.devices[0].ncores = 1;
  1382. config->workers[worker_idx].subworkerid = 0;
  1383. config->workers[worker_idx].devid = devid;
  1384. config->workers[worker_idx].worker_mask = STARPU_OPENCL;
  1385. config->worker_mask |= STARPU_OPENCL;
  1386. }
  1387. topology->nworkers += topology->ndevices[STARPU_OPENCL_WORKER];
  1388. #endif
  1389. #if defined(STARPU_USE_MPI_MASTER_SLAVE)
  1390. _starpu_init_mp_config(config, &config->conf, no_mp_config);
  1391. #endif
  1392. /* we put the CPU section after the accelerator : in case there was an
  1393. * accelerator found, we devote one cpu */
  1394. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  1395. int ncpu = config->conf.ncpus;
  1396. if (ncpu != 0)
  1397. {
  1398. STARPU_ASSERT_MSG(ncpu >= -1, "ncpus can not be negative and different from -1 (is is %d)", ncpu);
  1399. if (ncpu == -1)
  1400. {
  1401. unsigned mpi_ms_busy_cpus = 0;
  1402. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1403. #ifdef STARPU_MPI_MASTER_SLAVE_MULTIPLE_THREAD
  1404. for (j = 0; j < STARPU_MAXMPIDEVS; j++)
  1405. mpi_ms_busy_cpus += (topology->nworker[STARPU_MPI_MS_WORKER][j] ? 1 : 0);
  1406. #else
  1407. mpi_ms_busy_cpus = 1; /* we launch one thread to control all slaves */
  1408. #endif
  1409. #endif /* STARPU_USE_MPI_MASTER_SLAVE */
  1410. unsigned cuda_busy_cpus = 0;
  1411. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  1412. cuda_busy_cpus =
  1413. topology->cuda_th_per_dev == 0 && topology->cuda_th_per_stream == 0 ? (topology->ndevices[STARPU_CUDA_WORKER] ? 1 : 0) :
  1414. topology->cuda_th_per_stream ? (nworker_per_cuda * topology->ndevices[STARPU_CUDA_WORKER]) : topology->ndevices[STARPU_CUDA_WORKER];
  1415. #endif
  1416. unsigned already_busy_cpus = mpi_ms_busy_cpus
  1417. + cuda_busy_cpus
  1418. + topology->ndevices[STARPU_OPENCL_WORKER];
  1419. long avail_cpus = (long) topology->nhwworker[STARPU_CPU_WORKER][0] - (long) already_busy_cpus;
  1420. if (avail_cpus < 0)
  1421. avail_cpus = 0;
  1422. int nth_per_core = starpu_get_env_number_default("STARPU_NTHREADS_PER_CORE", 1);
  1423. avail_cpus *= nth_per_core;
  1424. ncpu = avail_cpus;
  1425. }
  1426. if (ncpu > STARPU_MAXCPUS)
  1427. {
  1428. _STARPU_DISP("Warning: %d CPU cores requested. Only %d enabled. Use configure option --enable-maxcpus=xxx to update the maximum value of supported CPU devices.\n", ncpu, STARPU_MAXCPUS);
  1429. ncpu = STARPU_MAXCPUS;
  1430. }
  1431. if (config->conf.reserve_ncpus > 0)
  1432. {
  1433. if (ncpu < config->conf.reserve_ncpus)
  1434. {
  1435. _STARPU_DISP("Warning: %d CPU cores were requested to be reserved, but only %d were available,\n", config->conf.reserve_ncpus, ncpu);
  1436. ncpu = 0;
  1437. }
  1438. else
  1439. {
  1440. ncpu -= config->conf.reserve_ncpus;
  1441. }
  1442. }
  1443. }
  1444. topology->ndevices[STARPU_CPU_WORKER] = 1;
  1445. topology->nworker[STARPU_CPU_WORKER][0] = ncpu;
  1446. STARPU_ASSERT(topology->nworker[STARPU_CPU_WORKER][0] + topology->nworkers <= STARPU_NMAXWORKERS);
  1447. unsigned cpu;
  1448. unsigned homogeneous = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_CPU", 1);
  1449. for (cpu = 0; cpu < topology->nworker[STARPU_CPU_WORKER][0]; cpu++)
  1450. {
  1451. int worker_idx = topology->nworkers + cpu;
  1452. config->workers[worker_idx].arch = STARPU_CPU_WORKER;
  1453. _STARPU_MALLOC(config->workers[worker_idx].perf_arch.devices, sizeof(struct starpu_perfmodel_device));
  1454. config->workers[worker_idx].perf_arch.ndevices = 1;
  1455. config->workers[worker_idx].perf_arch.devices[0].type = STARPU_CPU_WORKER;
  1456. config->workers[worker_idx].perf_arch.devices[0].devid = homogeneous ? 0 : cpu;
  1457. config->workers[worker_idx].perf_arch.devices[0].ncores = 1;
  1458. config->workers[worker_idx].subworkerid = 0;
  1459. config->workers[worker_idx].devid = cpu;
  1460. config->workers[worker_idx].worker_mask = STARPU_CPU;
  1461. config->worker_mask |= STARPU_CPU;
  1462. }
  1463. topology->nworkers += topology->nworker[STARPU_CPU_WORKER][0];
  1464. #endif
  1465. if (topology->nworkers == 0)
  1466. {
  1467. _STARPU_DEBUG("No worker found, aborting ...\n");
  1468. return -ENODEV;
  1469. }
  1470. return 0;
  1471. }
  1472. void _starpu_destroy_machine_config(struct _starpu_machine_config *config)
  1473. {
  1474. _starpu_close_debug_logfile();
  1475. unsigned worker;
  1476. for (worker = 0; worker < config->topology.nworkers; worker++)
  1477. {
  1478. struct _starpu_worker *workerarg = &config->workers[worker];
  1479. int bindid = workerarg->bindid;
  1480. free(workerarg->perf_arch.devices);
  1481. #ifdef STARPU_HAVE_HWLOC
  1482. hwloc_bitmap_free(workerarg->hwloc_cpu_set);
  1483. if (bindid != -1)
  1484. {
  1485. hwloc_obj_t worker_obj = hwloc_get_obj_by_depth(config->topology.hwtopology,
  1486. config->pu_depth,
  1487. bindid);
  1488. struct _starpu_hwloc_userdata *data = worker_obj->userdata;
  1489. if (data->worker_list)
  1490. {
  1491. _starpu_worker_list_delete(data->worker_list);
  1492. data->worker_list = NULL;
  1493. }
  1494. }
  1495. #endif
  1496. if (bindid != -1)
  1497. {
  1498. free(config->bindid_workers[bindid].workerids);
  1499. config->bindid_workers[bindid].workerids = NULL;
  1500. }
  1501. }
  1502. free(config->bindid_workers);
  1503. config->bindid_workers = NULL;
  1504. config->nbindid = 0;
  1505. unsigned combined_worker_id;
  1506. for(combined_worker_id=0 ; combined_worker_id < config->topology.ncombinedworkers ; combined_worker_id++)
  1507. {
  1508. struct _starpu_combined_worker *combined_worker = &config->combined_workers[combined_worker_id];
  1509. #ifdef STARPU_HAVE_HWLOC
  1510. hwloc_bitmap_free(combined_worker->hwloc_cpu_set);
  1511. #endif
  1512. free(combined_worker->perf_arch.devices);
  1513. }
  1514. #ifdef STARPU_HAVE_HWLOC
  1515. _starpu_deallocate_topology_userdata(hwloc_get_root_obj(config->topology.hwtopology));
  1516. hwloc_topology_destroy(config->topology.hwtopology);
  1517. #endif
  1518. topology_is_initialized = 0;
  1519. #ifdef STARPU_USE_CUDA
  1520. struct handle_entry *entry=NULL, *tmp=NULL;
  1521. HASH_ITER(hh, devices_using_cuda, entry, tmp)
  1522. {
  1523. HASH_DEL(devices_using_cuda, entry);
  1524. free(entry);
  1525. }
  1526. devices_using_cuda = NULL;
  1527. #endif
  1528. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  1529. int i;
  1530. for (i=0; i<STARPU_NARCH; i++)
  1531. may_bind_automatically[i] = 0;
  1532. #endif
  1533. }
  1534. int _starpu_bind_thread_on_cpu(int cpuid STARPU_ATTRIBUTE_UNUSED, int workerid STARPU_ATTRIBUTE_UNUSED, const char *name STARPU_ATTRIBUTE_UNUSED)
  1535. {
  1536. int ret = 0;
  1537. #ifdef STARPU_SIMGRID
  1538. return ret;
  1539. #else
  1540. if (nobind > 0)
  1541. return ret;
  1542. if (cpuid < 0)
  1543. return ret;
  1544. #ifdef STARPU_HAVE_HWLOC
  1545. const struct hwloc_topology_support *support;
  1546. struct _starpu_machine_config *config = _starpu_get_machine_config();
  1547. #ifdef STARPU_USE_OPENCL
  1548. if (config->conf.nopencl != 0)
  1549. _starpu_opencl_init();
  1550. #endif
  1551. #ifdef STARPU_USE_CUDA
  1552. if (config->conf.ncuda != 0)
  1553. _starpu_init_cuda();
  1554. #endif
  1555. _starpu_init_topology(config);
  1556. if (workerid != STARPU_NOWORKERID && cpuid < STARPU_MAXCPUS)
  1557. {
  1558. /* TODO: mutex... */
  1559. int previous = cpu_worker[cpuid];
  1560. /* We would like the PU to be available, or we are perhaps fine to share it */
  1561. if ( !( previous == STARPU_NOWORKERID ||
  1562. (previous == STARPU_NONACTIVETHREAD && workerid == STARPU_NONACTIVETHREAD) ||
  1563. (previous >= 0 && previous == workerid) ||
  1564. (name && cpu_name[cpuid] && !strcmp(name, cpu_name[cpuid])) ) )
  1565. {
  1566. char hostname[65];
  1567. gethostname(hostname, sizeof(hostname));
  1568. if (previous == STARPU_ACTIVETHREAD)
  1569. _STARPU_DISP("[%s] Warning: active thread %s was already bound to PU %d\n", hostname, cpu_name[cpuid], cpuid);
  1570. else if (previous == STARPU_NONACTIVETHREAD)
  1571. _STARPU_DISP("[%s] Warning: non-active thread %s was already bound to PU %d\n", hostname, cpu_name[cpuid], cpuid);
  1572. else
  1573. _STARPU_DISP("[%s] Warning: worker %d was already bound to PU %d\n", hostname, previous, cpuid);
  1574. if (workerid == STARPU_ACTIVETHREAD)
  1575. _STARPU_DISP("and we were told to also bind active thread %s to it.\n", name);
  1576. else if (previous == STARPU_NONACTIVETHREAD)
  1577. _STARPU_DISP("and we were told to also bind non-active thread %s to it.\n", name);
  1578. else
  1579. _STARPU_DISP("and we were told to also bind worker %d to it.\n", workerid);
  1580. _STARPU_DISP("This will strongly degrade performance.\n");
  1581. if (workerid >= 0)
  1582. /* This shouldn't happen for workers */
  1583. _STARPU_DISP("[%s] Maybe check starpu_machine_display's output to determine what wrong binding happened. Hwloc reported %d cores and %d threads, perhaps there is misdetection between hwloc, the kernel and the BIOS, or an administrative allocation issue from e.g. the job scheduler?\n", hostname, config->topology.nhwworker[STARPU_CPU_WORKER][0], config->topology.nhwpus);
  1584. ret = -1;
  1585. }
  1586. else
  1587. {
  1588. cpu_worker[cpuid] = workerid;
  1589. if (name)
  1590. {
  1591. if (cpu_name[cpuid])
  1592. free(cpu_name[cpuid]);
  1593. cpu_name[cpuid] = strdup(name);
  1594. }
  1595. }
  1596. }
  1597. support = hwloc_topology_get_support(config->topology.hwtopology);
  1598. if (support->cpubind->set_thisthread_cpubind)
  1599. {
  1600. hwloc_obj_t obj = hwloc_get_obj_by_depth(config->topology.hwtopology, config->pu_depth, cpuid);
  1601. hwloc_bitmap_t set = obj->cpuset;
  1602. int res;
  1603. hwloc_bitmap_singlify(set);
  1604. res = hwloc_set_cpubind(config->topology.hwtopology, set, HWLOC_CPUBIND_THREAD);
  1605. if (res)
  1606. {
  1607. perror("hwloc_set_cpubind");
  1608. STARPU_ABORT();
  1609. }
  1610. }
  1611. #elif defined(HAVE_PTHREAD_SETAFFINITY_NP) && defined(__linux__)
  1612. int res;
  1613. /* fix the thread on the correct cpu */
  1614. cpu_set_t aff_mask;
  1615. CPU_ZERO(&aff_mask);
  1616. CPU_SET(cpuid, &aff_mask);
  1617. starpu_pthread_t self = starpu_pthread_self();
  1618. res = pthread_setaffinity_np(self, sizeof(aff_mask), &aff_mask);
  1619. if (res)
  1620. {
  1621. const char *msg = strerror(res);
  1622. _STARPU_MSG("pthread_setaffinity_np: %s\n", msg);
  1623. STARPU_ABORT();
  1624. }
  1625. #elif defined(_WIN32)
  1626. DWORD mask = 1 << cpuid;
  1627. if (!SetThreadAffinityMask(GetCurrentThread(), mask))
  1628. {
  1629. _STARPU_ERROR("SetThreadMaskAffinity(%lx) failed\n", mask);
  1630. }
  1631. #else
  1632. #warning no CPU binding support
  1633. #endif
  1634. #endif
  1635. return ret;
  1636. }
  1637. int
  1638. starpu_bind_thread_on(int cpuid, unsigned flags, const char *name)
  1639. {
  1640. int workerid;
  1641. STARPU_ASSERT_MSG(name, "starpu_bind_thread_on must be provided with a name");
  1642. starpu_pthread_setname(name);
  1643. if (flags & STARPU_THREAD_ACTIVE)
  1644. workerid = STARPU_ACTIVETHREAD;
  1645. else
  1646. workerid = STARPU_NONACTIVETHREAD;
  1647. return _starpu_bind_thread_on_cpu(cpuid, workerid, name);
  1648. }
  1649. void _starpu_bind_thread_on_cpus(struct _starpu_combined_worker *combined_worker STARPU_ATTRIBUTE_UNUSED)
  1650. {
  1651. #ifdef STARPU_SIMGRID
  1652. return;
  1653. #endif
  1654. #ifdef STARPU_HAVE_HWLOC
  1655. const struct hwloc_topology_support *support;
  1656. struct _starpu_machine_config *config = _starpu_get_machine_config();
  1657. #ifdef STARPU_USE_OPENC
  1658. if (config->conf.nopencl != 0)
  1659. _starpu_opencl_init();
  1660. #endif
  1661. #ifdef STARPU_USE_CUDA
  1662. if (config->conf.ncuda != 0)
  1663. _starpu_init_cuda();
  1664. #endif
  1665. _starpu_init_topology(config);
  1666. support = hwloc_topology_get_support(config->topology.hwtopology);
  1667. if (support->cpubind->set_thisthread_cpubind)
  1668. {
  1669. hwloc_bitmap_t set = combined_worker->hwloc_cpu_set;
  1670. int ret;
  1671. ret = hwloc_set_cpubind(config->topology.hwtopology, set, HWLOC_CPUBIND_THREAD);
  1672. if (ret)
  1673. {
  1674. perror("binding thread");
  1675. STARPU_ABORT();
  1676. }
  1677. }
  1678. #else
  1679. #ifdef __GLIBC__
  1680. sched_setaffinity(0,sizeof(combined_worker->cpu_set),&combined_worker->cpu_set);
  1681. #else
  1682. # warning no parallel worker CPU binding support
  1683. #endif
  1684. #endif
  1685. }
  1686. static void _starpu_init_binding_cpu(struct _starpu_machine_config *config)
  1687. {
  1688. unsigned worker;
  1689. for (worker = 0; worker < config->topology.nworkers; worker++)
  1690. {
  1691. struct _starpu_worker *workerarg = &config->workers[worker];
  1692. switch (workerarg->arch)
  1693. {
  1694. case STARPU_CPU_WORKER:
  1695. {
  1696. /* Dedicate a cpu core to that worker */
  1697. workerarg->bindid = _starpu_get_next_bindid(config, STARPU_THREAD_ACTIVE, NULL, 0);
  1698. break;
  1699. }
  1700. default:
  1701. /* Do nothing */
  1702. break;
  1703. }
  1704. }
  1705. }
  1706. static size_t _starpu_cpu_get_global_mem_size(int nodeid, struct _starpu_machine_config *config)
  1707. {
  1708. size_t global_mem;
  1709. starpu_ssize_t limit = -1;
  1710. #if defined(STARPU_HAVE_HWLOC)
  1711. struct _starpu_machine_topology *topology = &config->topology;
  1712. STARPU_ASSERT(numa_enabled != -1);
  1713. if (numa_enabled)
  1714. {
  1715. int depth_node = hwloc_get_type_depth(topology->hwtopology, HWLOC_OBJ_NUMANODE);
  1716. if (depth_node == HWLOC_TYPE_DEPTH_UNKNOWN)
  1717. {
  1718. #if HWLOC_API_VERSION >= 0x00020000
  1719. global_mem = hwloc_get_root_obj(topology->hwtopology)->total_memory;
  1720. #else
  1721. global_mem = hwloc_get_root_obj(topology->hwtopology)->memory.total_memory;
  1722. #endif
  1723. }
  1724. else
  1725. {
  1726. char name[32];
  1727. hwloc_obj_t obj = hwloc_get_obj_by_depth(topology->hwtopology, depth_node, nodeid);
  1728. #if HWLOC_API_VERSION >= 0x00020000
  1729. global_mem = obj->attr->numanode.local_memory;
  1730. #else
  1731. global_mem = obj->memory.local_memory;
  1732. #endif
  1733. snprintf(name, sizeof(name), "STARPU_LIMIT_CPU_NUMA_%d_MEM", obj->os_index);
  1734. limit = starpu_get_env_number(name);
  1735. }
  1736. }
  1737. else
  1738. {
  1739. /* Do not limit ourself to a single NUMA node */
  1740. #if HWLOC_API_VERSION >= 0x00020000
  1741. global_mem = hwloc_get_root_obj(topology->hwtopology)->total_memory;
  1742. #else
  1743. global_mem = hwloc_get_root_obj(topology->hwtopology)->memory.total_memory;
  1744. #endif
  1745. }
  1746. #else /* STARPU_HAVE_HWLOC */
  1747. #ifdef STARPU_DEVEL
  1748. # warning TODO: use sysinfo when available to get global size
  1749. #endif
  1750. global_mem = 0;
  1751. #endif
  1752. if (limit == -1)
  1753. limit = starpu_get_env_number("STARPU_LIMIT_CPU_NUMA_MEM");
  1754. if (limit == -1)
  1755. {
  1756. limit = starpu_get_env_number("STARPU_LIMIT_CPU_MEM");
  1757. if (limit != -1 && numa_enabled)
  1758. {
  1759. _STARPU_DISP("NUMA is enabled and STARPU_LIMIT_CPU_MEM is set to %luMB. Assuming that it should be distributed over the %d NUMA node(s). You probably want to use STARPU_LIMIT_CPU_NUMA_MEM instead.\n", (long) limit, _starpu_topology_get_nnumanodes(config));
  1760. limit /= _starpu_topology_get_nnumanodes(config);
  1761. }
  1762. }
  1763. /* Don't eat all memory for ourself */
  1764. global_mem *= 0.9;
  1765. if (limit < 0)
  1766. // No limit is defined, we return the global memory size
  1767. return global_mem;
  1768. else if (global_mem && (size_t)limit * 1024*1024 > global_mem)
  1769. {
  1770. if (numa_enabled)
  1771. _STARPU_DISP("The requested limit %ldMB for NUMA node %d is higher that available memory %luMB, using the latter\n", (unsigned long) limit, nodeid, (unsigned long) global_mem / (1024*1024));
  1772. else
  1773. _STARPU_DISP("The requested limit %ldMB is higher that available memory %luMB, using the latter\n", (long) limit, (unsigned long) global_mem / (1024*1024));
  1774. return global_mem;
  1775. }
  1776. else
  1777. // We limit the memory
  1778. return limit*1024*1024;
  1779. }
  1780. //TODO : Check SIMGRID
  1781. static void _starpu_init_numa_node(struct _starpu_machine_config *config)
  1782. {
  1783. nb_numa_nodes = 0;
  1784. unsigned i;
  1785. for (i = 0; i < STARPU_MAXNUMANODES; i++)
  1786. {
  1787. numa_memory_nodes_to_hwloclogid[i] = STARPU_NUMA_UNINITIALIZED;
  1788. numa_memory_nodes_to_physicalid[i] = STARPU_NUMA_UNINITIALIZED;
  1789. }
  1790. #ifdef STARPU_SIMGRID
  1791. char name[16];
  1792. starpu_sg_host_t host;
  1793. #endif
  1794. numa_enabled = starpu_get_env_number_default("STARPU_USE_NUMA", 0);
  1795. /* NUMA mode activated */
  1796. if (numa_enabled)
  1797. {
  1798. /* Take all NUMA nodes used by CPU workers */
  1799. unsigned worker;
  1800. for (worker = 0; worker < config->topology.nworkers; worker++)
  1801. {
  1802. struct _starpu_worker *workerarg = &config->workers[worker];
  1803. if (workerarg->arch == STARPU_CPU_WORKER)
  1804. {
  1805. int numa_logical_id = _starpu_get_logical_numa_node_worker(worker);
  1806. /* Convert logical id to StarPU id to check if this NUMA node is already saved or not */
  1807. int numa_starpu_id = starpu_memory_nodes_numa_hwloclogid_to_id(numa_logical_id);
  1808. /* This shouldn't happen */
  1809. if (numa_starpu_id == -1 && nb_numa_nodes == STARPU_MAXNUMANODES)
  1810. {
  1811. _STARPU_MSG("Warning: %u NUMA nodes available. Only %u enabled. Use configure option --enable-maxnumanodes=xxx to update the maximum value of supported NUMA nodes.\n", _starpu_topology_get_nnumanodes(config), STARPU_MAXNUMANODES);
  1812. STARPU_ABORT();
  1813. }
  1814. if (numa_starpu_id == -1)
  1815. {
  1816. int devid = numa_logical_id == STARPU_NUMA_MAIN_RAM ? 0 : numa_logical_id;
  1817. int memnode = _starpu_memory_node_register(STARPU_CPU_RAM, devid, &_starpu_driver_cpu_node_ops);
  1818. _starpu_memory_manager_set_global_memory_size(memnode, _starpu_cpu_get_global_mem_size(devid, config));
  1819. STARPU_ASSERT_MSG(memnode < STARPU_MAXNUMANODES, "Wrong Memory Node : %d (only %d available)", memnode, STARPU_MAXNUMANODES);
  1820. numa_memory_nodes_to_hwloclogid[memnode] = numa_logical_id;
  1821. int numa_physical_id = _starpu_get_physical_numa_node_worker(worker);
  1822. numa_memory_nodes_to_physicalid[memnode] = numa_physical_id;
  1823. nb_numa_nodes++;
  1824. #ifdef STARPU_SIMGRID
  1825. snprintf(name, sizeof(name), "RAM%d", memnode);
  1826. host = _starpu_simgrid_get_host_by_name(name);
  1827. STARPU_ASSERT(host);
  1828. _starpu_simgrid_memory_node_set_host(memnode, host);
  1829. #endif
  1830. }
  1831. }
  1832. }
  1833. /* If we found NUMA nodes from CPU workers, it's good */
  1834. if (nb_numa_nodes != 0)
  1835. return;
  1836. _STARPU_DISP("No NUMA nodes found when checking CPU workers...\n");
  1837. #if (defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)) && defined(STARPU_HAVE_HWLOC)
  1838. _STARPU_DISP("Take NUMA nodes attached to CUDA and OpenCL devices...\n");
  1839. #endif
  1840. #if defined(STARPU_USE_CUDA) && defined(STARPU_HAVE_HWLOC)
  1841. for (i = 0; i < config->topology.ndevices[STARPU_CUDA_WORKER]; i++)
  1842. {
  1843. hwloc_obj_t obj = hwloc_cuda_get_device_osdev_by_index(config->topology.hwtopology, i);
  1844. if (obj)
  1845. obj = numa_get_obj(obj);
  1846. /* Hwloc cannot recognize some devices */
  1847. if (!obj)
  1848. continue;
  1849. int numa_starpu_id = starpu_memory_nodes_numa_hwloclogid_to_id(obj->logical_index);
  1850. /* This shouldn't happen */
  1851. if (numa_starpu_id == -1 && nb_numa_nodes == STARPU_MAXNUMANODES)
  1852. {
  1853. _STARPU_MSG("Warning: %u NUMA nodes available. Only %u enabled. Use configure option --enable-maxnumanodes=xxx to update the maximum value of supported NUMA nodes.\n", _starpu_topology_get_nnumanodes(config), STARPU_MAXNUMANODES);
  1854. STARPU_ABORT();
  1855. }
  1856. if (numa_starpu_id == -1)
  1857. {
  1858. int memnode = _starpu_memory_node_register(STARPU_CPU_RAM, obj->logical_index, &_starpu_driver_cpu_node_ops);
  1859. _starpu_memory_manager_set_global_memory_size(memnode, _starpu_cpu_get_global_mem_size(obj->logical_index, config));
  1860. STARPU_ASSERT_MSG(memnode < STARPU_MAXNUMANODES, "Wrong Memory Node : %d (only %d available)", memnode, STARPU_MAXNUMANODES);
  1861. numa_memory_nodes_to_hwloclogid[memnode] = obj->logical_index;
  1862. numa_memory_nodes_to_physicalid[memnode] = obj->os_index;
  1863. nb_numa_nodes++;
  1864. #ifdef STARPU_SIMGRID
  1865. snprintf(name, sizeof(name), "RAM%d", memnode);
  1866. host = _starpu_simgrid_get_host_by_name(name);
  1867. STARPU_ASSERT(host);
  1868. _starpu_simgrid_memory_node_set_host(memnode, host);
  1869. #endif
  1870. }
  1871. }
  1872. #endif
  1873. #if defined(STARPU_USE_OPENCL) && defined(STARPU_HAVE_HWLOC)
  1874. if (config->topology.ndevices[STARPU_OPENCL_WORKER] > 0)
  1875. {
  1876. cl_int err;
  1877. cl_platform_id platform_id[_STARPU_OPENCL_PLATFORM_MAX];
  1878. cl_uint nb_platforms;
  1879. unsigned platform;
  1880. unsigned nb_opencl_devices = 0, num = 0;
  1881. err = clGetPlatformIDs(_STARPU_OPENCL_PLATFORM_MAX, platform_id, &nb_platforms);
  1882. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  1883. nb_platforms=0;
  1884. cl_device_type device_type = CL_DEVICE_TYPE_GPU|CL_DEVICE_TYPE_ACCELERATOR;
  1885. if (starpu_get_env_number("STARPU_OPENCL_ON_CPUS") > 0)
  1886. device_type |= CL_DEVICE_TYPE_CPU;
  1887. if (starpu_get_env_number("STARPU_OPENCL_ONLY_ON_CPUS") > 0)
  1888. device_type = CL_DEVICE_TYPE_CPU;
  1889. for (platform = 0; platform < nb_platforms ; platform++)
  1890. {
  1891. err = clGetDeviceIDs(platform_id[platform], device_type, 0, NULL, &num);
  1892. if (err != CL_SUCCESS)
  1893. num = 0;
  1894. nb_opencl_devices += num;
  1895. for (i = 0; i < num; i++)
  1896. {
  1897. hwloc_obj_t obj = hwloc_opencl_get_device_osdev_by_index(config->topology.hwtopology, platform, i);
  1898. if (obj)
  1899. obj = numa_get_obj(obj);
  1900. /* Hwloc cannot recognize some devices */
  1901. if (!obj)
  1902. continue;
  1903. int numa_starpu_id = starpu_memory_nodes_numa_hwloclogid_to_id(obj->logical_index);
  1904. /* This shouldn't happen */
  1905. if (numa_starpu_id == -1 && nb_numa_nodes == STARPU_MAXNUMANODES)
  1906. {
  1907. _STARPU_MSG("Warning: %u NUMA nodes available. Only %u enabled. Use configure option --enable-maxnumanodes=xxx to update the maximum value of supported NUMA nodes.\n", _starpu_topology_get_nnumanodes(config), STARPU_MAXNUMANODES);
  1908. STARPU_ABORT();
  1909. }
  1910. if (numa_starpu_id == -1)
  1911. {
  1912. int memnode = _starpu_memory_node_register(STARPU_CPU_RAM, obj->logical_index, &_starpu_driver_cpu_node_ops);
  1913. _starpu_memory_manager_set_global_memory_size(memnode, _starpu_cpu_get_global_mem_size(obj->logical_index, config));
  1914. STARPU_ASSERT_MSG(memnode < STARPU_MAXNUMANODES, "Wrong Memory Node : %d (only %d available)", memnode, STARPU_MAXNUMANODES);
  1915. numa_memory_nodes_to_hwloclogid[memnode] = obj->logical_index;
  1916. numa_memory_nodes_to_physicalid[memnode] = obj->os_index;
  1917. nb_numa_nodes++;
  1918. #ifdef STARPU_SIMGRID
  1919. snprintf(name, sizeof(name), "RAM%d", memnode);
  1920. host = _starpu_simgrid_get_host_by_name(name);
  1921. STARPU_ASSERT(host);
  1922. _starpu_simgrid_memory_node_set_host(memnode, host);
  1923. #endif
  1924. }
  1925. }
  1926. }
  1927. }
  1928. #endif
  1929. }
  1930. #if (defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)) && defined(STARPU_HAVE_HWLOC)
  1931. //Found NUMA nodes from CUDA nodes
  1932. if (nb_numa_nodes != 0)
  1933. return;
  1934. /* In case, we do not find any NUMA nodes when checking NUMA nodes attached to GPUs, we take all of them */
  1935. if (numa_enabled)
  1936. _STARPU_DISP("No NUMA nodes found when checking GPUs devices...\n");
  1937. #endif
  1938. if (numa_enabled)
  1939. _STARPU_DISP("Finally, take all NUMA nodes available... \n");
  1940. unsigned nnuma = _starpu_topology_get_nnumanodes(config);
  1941. if (nnuma > STARPU_MAXNUMANODES)
  1942. {
  1943. _STARPU_MSG("Warning: %u NUMA nodes available. Only %u enabled. Use configure option --enable-maxnumanodes=xxx to update the maximum value of supported NUMA nodes.\n", _starpu_topology_get_nnumanodes(config), STARPU_MAXNUMANODES);
  1944. nnuma = STARPU_MAXNUMANODES;
  1945. }
  1946. unsigned numa;
  1947. for (numa = 0; numa < nnuma; numa++)
  1948. {
  1949. unsigned numa_logical_id;
  1950. unsigned numa_physical_id;
  1951. #if defined(STARPU_HAVE_HWLOC)
  1952. hwloc_obj_t obj = hwloc_get_obj_by_type(config->topology.hwtopology, HWLOC_OBJ_NUMANODE, numa);
  1953. if (obj)
  1954. {
  1955. numa_logical_id = obj->logical_index;
  1956. numa_physical_id = obj->os_index;
  1957. }
  1958. else
  1959. #endif
  1960. {
  1961. numa_logical_id = 0;
  1962. numa_physical_id = 0;
  1963. }
  1964. int memnode = _starpu_memory_node_register(STARPU_CPU_RAM, numa_logical_id, &_starpu_driver_cpu_node_ops);
  1965. _starpu_memory_manager_set_global_memory_size(memnode, _starpu_cpu_get_global_mem_size(numa_logical_id, config));
  1966. numa_memory_nodes_to_hwloclogid[memnode] = numa_logical_id;
  1967. numa_memory_nodes_to_physicalid[memnode] = numa_physical_id;
  1968. nb_numa_nodes++;
  1969. if (numa == 0)
  1970. STARPU_ASSERT_MSG(memnode == STARPU_MAIN_RAM, "Wrong Memory Node : %d (expected %d) \n", memnode, STARPU_MAIN_RAM);
  1971. STARPU_ASSERT_MSG(memnode < STARPU_MAXNUMANODES, "Wrong Memory Node : %d (only %d available) \n", memnode, STARPU_MAXNUMANODES);
  1972. #ifdef STARPU_SIMGRID
  1973. if (nnuma > 1)
  1974. {
  1975. snprintf(name, sizeof(name), "RAM%d", memnode);
  1976. host = _starpu_simgrid_get_host_by_name(name);
  1977. }
  1978. else
  1979. {
  1980. /* In this case, nnuma has only one node */
  1981. host = _starpu_simgrid_get_host_by_name("RAM");
  1982. }
  1983. STARPU_ASSERT(host);
  1984. _starpu_simgrid_memory_node_set_host(memnode, host);
  1985. #endif
  1986. }
  1987. STARPU_ASSERT_MSG(nb_numa_nodes > 0, "No NUMA node found... We need at least one memory node !\n");
  1988. }
  1989. static void _starpu_init_numa_bus()
  1990. {
  1991. unsigned i, j;
  1992. for (i = 0; i < nb_numa_nodes; i++)
  1993. for (j = 0; j < nb_numa_nodes; j++)
  1994. if (i != j)
  1995. numa_bus_id[i*nb_numa_nodes+j] = _starpu_register_bus(i, j);
  1996. }
  1997. #if defined(STARPU_HAVE_HWLOC) && !defined(STARPU_SIMGRID)
  1998. static int _starpu_find_pu_driving_numa_from(hwloc_obj_t root, unsigned node)
  1999. {
  2000. unsigned i;
  2001. int found = 0;
  2002. if (!root->arity)
  2003. {
  2004. if (root->type == HWLOC_OBJ_PU)
  2005. {
  2006. struct _starpu_hwloc_userdata *userdata = root->userdata;
  2007. if (userdata->pu_worker)
  2008. {
  2009. /* Cool, found a worker! */
  2010. _STARPU_DEBUG("found PU %d to drive memory node %d\n", userdata->pu_worker->bindid, node);
  2011. _starpu_worker_drives_memory_node(userdata->pu_worker, node);
  2012. found = 1;
  2013. }
  2014. }
  2015. }
  2016. for (i = 0; i < root->arity; i++)
  2017. {
  2018. if (_starpu_find_pu_driving_numa_from(root->children[i], node))
  2019. found = 1;
  2020. }
  2021. return found;
  2022. }
  2023. /* Look upward to find a level containing the given NUMA node and workers to drive it */
  2024. static int _starpu_find_pu_driving_numa_up(hwloc_obj_t root, unsigned node)
  2025. {
  2026. if (_starpu_find_pu_driving_numa_from(root, node))
  2027. /* Ok, we already managed to find drivers */
  2028. return 1;
  2029. if (!root->parent)
  2030. /* And no parent!? nobody can drive this... */
  2031. return 0;
  2032. /* Try from parent */
  2033. return _starpu_find_pu_driving_numa_up(root->parent, node);
  2034. }
  2035. #endif
  2036. static void _starpu_init_workers_binding_and_memory(struct _starpu_machine_config *config, int no_mp_config STARPU_ATTRIBUTE_UNUSED)
  2037. {
  2038. /* We will store all the busid of the different (src, dst)
  2039. * combinations in a matrix which we initialize here. */
  2040. _starpu_initialize_busid_matrix();
  2041. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  2042. unsigned cuda_init[STARPU_MAXCUDADEVS] = { };
  2043. unsigned cuda_memory_nodes[STARPU_MAXCUDADEVS];
  2044. unsigned cuda_bindid[STARPU_MAXCUDADEVS];
  2045. int cuda_globalbindid = -1;
  2046. #endif
  2047. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  2048. unsigned opencl_init[STARPU_MAXOPENCLDEVS] = { };
  2049. unsigned opencl_memory_nodes[STARPU_MAXOPENCLDEVS];
  2050. unsigned opencl_bindid[STARPU_MAXOPENCLDEVS];
  2051. #endif
  2052. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  2053. unsigned mpi_init[STARPU_MAXMPIDEVS] = { };
  2054. unsigned mpi_memory_nodes[STARPU_MAXMPIDEVS];
  2055. unsigned mpi_bindid[STARPU_MAXMPIDEVS];
  2056. #endif
  2057. unsigned bindid;
  2058. for (bindid = 0; bindid < config->nbindid; bindid++)
  2059. {
  2060. free(config->bindid_workers[bindid].workerids);
  2061. config->bindid_workers[bindid].workerids = NULL;
  2062. config->bindid_workers[bindid].nworkers = 0;
  2063. }
  2064. /* Init CPU binding before NUMA nodes, because we use it to discover NUMA nodes */
  2065. _starpu_init_binding_cpu(config);
  2066. /* Initialize NUMA nodes */
  2067. _starpu_init_numa_node(config);
  2068. _starpu_init_numa_bus();
  2069. unsigned worker;
  2070. for (worker = 0; worker < config->topology.nworkers; worker++)
  2071. {
  2072. unsigned memory_node = -1;
  2073. struct _starpu_worker *workerarg = &config->workers[worker];
  2074. unsigned devid STARPU_ATTRIBUTE_UNUSED = workerarg->devid;
  2075. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID) || defined(STARPU_USE_MPI_MASTER_SLAVE)
  2076. /* Perhaps the worker has some "favourite" bindings */
  2077. unsigned *preferred_binding = NULL;
  2078. unsigned npreferred = 0;
  2079. #endif
  2080. /* select the memory node that contains worker's memory */
  2081. switch (workerarg->arch)
  2082. {
  2083. case STARPU_CPU_WORKER:
  2084. {
  2085. int numa_logical_id = _starpu_get_logical_numa_node_worker(worker);
  2086. int numa_starpu_id = starpu_memory_nodes_numa_hwloclogid_to_id(numa_logical_id);
  2087. if (numa_starpu_id < 0 || numa_starpu_id >= STARPU_MAXNUMANODES)
  2088. numa_starpu_id = STARPU_MAIN_RAM;
  2089. #if defined(STARPU_HAVE_HWLOC) && !defined(STARPU_SIMGRID)
  2090. hwloc_obj_t pu_obj = hwloc_get_obj_by_type(config->topology.hwtopology, HWLOC_OBJ_PU, workerarg->bindid);
  2091. struct _starpu_hwloc_userdata *userdata = pu_obj->userdata;
  2092. userdata->pu_worker = workerarg;
  2093. #endif
  2094. workerarg->numa_memory_node = memory_node = numa_starpu_id;
  2095. _starpu_memory_node_add_nworkers(memory_node);
  2096. _starpu_worker_drives_memory_node(workerarg, numa_starpu_id);
  2097. break;
  2098. }
  2099. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  2100. case STARPU_CUDA_WORKER:
  2101. {
  2102. unsigned numa;
  2103. #ifndef STARPU_SIMGRID
  2104. if (may_bind_automatically[STARPU_CUDA_WORKER])
  2105. {
  2106. /* StarPU is allowed to bind threads automatically */
  2107. preferred_binding = _starpu_get_cuda_affinity_vector(devid);
  2108. npreferred = config->topology.nhwpus;
  2109. }
  2110. #endif /* SIMGRID */
  2111. if (cuda_init[devid])
  2112. {
  2113. memory_node = cuda_memory_nodes[devid];
  2114. if (config->topology.cuda_th_per_stream == 0)
  2115. workerarg->bindid = cuda_bindid[devid];
  2116. else
  2117. workerarg->bindid = _starpu_get_next_bindid(config, STARPU_THREAD_ACTIVE, preferred_binding, npreferred);
  2118. }
  2119. else
  2120. {
  2121. cuda_init[devid] = 1;
  2122. if (config->topology.cuda_th_per_dev == 0 && config->topology.cuda_th_per_stream == 0)
  2123. {
  2124. if (cuda_globalbindid == -1)
  2125. cuda_globalbindid = _starpu_get_next_bindid(config, STARPU_THREAD_ACTIVE, preferred_binding, npreferred);
  2126. workerarg->bindid = cuda_bindid[devid] = cuda_globalbindid;
  2127. }
  2128. else
  2129. workerarg->bindid = cuda_bindid[devid] = _starpu_get_next_bindid(config, STARPU_THREAD_ACTIVE, preferred_binding, npreferred);
  2130. memory_node = cuda_memory_nodes[devid] = _starpu_memory_node_register(STARPU_CUDA_RAM, devid, &_starpu_driver_cuda_node_ops);
  2131. for (numa = 0; numa < nb_numa_nodes; numa++)
  2132. {
  2133. _starpu_cuda_bus_ids[numa][devid+STARPU_MAXNUMANODES] = _starpu_register_bus(numa, memory_node);
  2134. _starpu_cuda_bus_ids[devid+STARPU_MAXNUMANODES][numa] = _starpu_register_bus(memory_node, numa);
  2135. }
  2136. #ifdef STARPU_SIMGRID
  2137. const char* cuda_memcpy_peer;
  2138. char name[16];
  2139. snprintf(name, sizeof(name), "CUDA%u", devid);
  2140. starpu_sg_host_t host = _starpu_simgrid_get_host_by_name(name);
  2141. STARPU_ASSERT(host);
  2142. _starpu_simgrid_memory_node_set_host(memory_node, host);
  2143. # ifdef STARPU_HAVE_SIMGRID_ACTOR_H
  2144. cuda_memcpy_peer = sg_host_get_property_value(host, "memcpy_peer");
  2145. # else
  2146. cuda_memcpy_peer = MSG_host_get_property_value(host, "memcpy_peer");
  2147. # endif
  2148. #endif /* SIMGRID */
  2149. if (
  2150. #ifdef STARPU_SIMGRID
  2151. cuda_memcpy_peer && atoll(cuda_memcpy_peer)
  2152. #elif defined(STARPU_HAVE_CUDA_MEMCPY_PEER)
  2153. 1
  2154. #else /* MEMCPY_PEER */
  2155. 0
  2156. #endif /* MEMCPY_PEER */
  2157. )
  2158. {
  2159. unsigned worker2;
  2160. for (worker2 = 0; worker2 < worker; worker2++)
  2161. {
  2162. struct _starpu_worker *workerarg2 = &config->workers[worker2];
  2163. int devid2 = workerarg2->devid;
  2164. if (workerarg2->arch == STARPU_CUDA_WORKER)
  2165. {
  2166. unsigned memory_node2 = starpu_worker_get_memory_node(worker2);
  2167. _starpu_cuda_bus_ids[devid2+STARPU_MAXNUMANODES][devid+STARPU_MAXNUMANODES] = _starpu_register_bus(memory_node2, memory_node);
  2168. _starpu_cuda_bus_ids[devid+STARPU_MAXNUMANODES][devid2+STARPU_MAXNUMANODES] = _starpu_register_bus(memory_node, memory_node2);
  2169. #ifndef STARPU_SIMGRID
  2170. #if defined(HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX) && HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX
  2171. {
  2172. hwloc_obj_t obj, obj2, ancestor;
  2173. obj = hwloc_cuda_get_device_osdev_by_index(config->topology.hwtopology, devid);
  2174. obj2 = hwloc_cuda_get_device_osdev_by_index(config->topology.hwtopology, devid2);
  2175. ancestor = hwloc_get_common_ancestor_obj(config->topology.hwtopology, obj, obj2);
  2176. if (ancestor)
  2177. {
  2178. struct _starpu_hwloc_userdata *data = ancestor->userdata;
  2179. #ifdef STARPU_VERBOSE
  2180. {
  2181. char name[64];
  2182. hwloc_obj_type_snprintf(name, sizeof(name), ancestor, 0);
  2183. _STARPU_DEBUG("CUDA%u and CUDA%u are linked through %s, along %u GPUs\n", devid, devid2, name, data->ngpus);
  2184. }
  2185. #endif
  2186. starpu_bus_set_ngpus(_starpu_cuda_bus_ids[devid2+STARPU_MAXNUMANODES][devid+STARPU_MAXNUMANODES], data->ngpus);
  2187. starpu_bus_set_ngpus(_starpu_cuda_bus_ids[devid+STARPU_MAXNUMANODES][devid2+STARPU_MAXNUMANODES], data->ngpus);
  2188. }
  2189. }
  2190. #endif
  2191. #endif
  2192. }
  2193. }
  2194. }
  2195. }
  2196. _starpu_memory_node_add_nworkers(memory_node);
  2197. //This worker can manage transfers on NUMA nodes
  2198. for (numa = 0; numa < nb_numa_nodes; numa++)
  2199. _starpu_worker_drives_memory_node(&workerarg->set->workers[0], numa);
  2200. _starpu_worker_drives_memory_node(&workerarg->set->workers[0], memory_node);
  2201. break;
  2202. }
  2203. #endif
  2204. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  2205. case STARPU_OPENCL_WORKER:
  2206. {
  2207. unsigned numa;
  2208. #ifndef STARPU_SIMGRID
  2209. if (may_bind_automatically[STARPU_OPENCL_WORKER])
  2210. {
  2211. /* StarPU is allowed to bind threads automatically */
  2212. preferred_binding = _starpu_get_opencl_affinity_vector(devid);
  2213. npreferred = config->topology.nhwpus;
  2214. }
  2215. #endif /* SIMGRID */
  2216. if (opencl_init[devid])
  2217. {
  2218. memory_node = opencl_memory_nodes[devid];
  2219. #ifndef STARPU_SIMGRID
  2220. workerarg->bindid = opencl_bindid[devid];
  2221. #endif /* SIMGRID */
  2222. }
  2223. else
  2224. {
  2225. opencl_init[devid] = 1;
  2226. workerarg->bindid = opencl_bindid[devid] = _starpu_get_next_bindid(config, STARPU_THREAD_ACTIVE, preferred_binding, npreferred);
  2227. memory_node = opencl_memory_nodes[devid] = _starpu_memory_node_register(STARPU_OPENCL_RAM, devid, &_starpu_driver_opencl_node_ops);
  2228. for (numa = 0; numa < nb_numa_nodes; numa++)
  2229. {
  2230. _starpu_register_bus(numa, memory_node);
  2231. _starpu_register_bus(memory_node, numa);
  2232. }
  2233. #ifdef STARPU_SIMGRID
  2234. char name[16];
  2235. snprintf(name, sizeof(name), "OpenCL%u", devid);
  2236. starpu_sg_host_t host = _starpu_simgrid_get_host_by_name(name);
  2237. STARPU_ASSERT(host);
  2238. _starpu_simgrid_memory_node_set_host(memory_node, host);
  2239. #endif /* SIMGRID */
  2240. }
  2241. _starpu_memory_node_add_nworkers(memory_node);
  2242. //This worker can manage transfers on NUMA nodes
  2243. for (numa = 0; numa < nb_numa_nodes; numa++)
  2244. _starpu_worker_drives_memory_node(workerarg, numa);
  2245. _starpu_worker_drives_memory_node(workerarg, memory_node);
  2246. break;
  2247. }
  2248. #endif
  2249. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  2250. case STARPU_MPI_MS_WORKER:
  2251. {
  2252. unsigned numa;
  2253. if (mpi_init[devid])
  2254. {
  2255. memory_node = mpi_memory_nodes[devid];
  2256. }
  2257. else
  2258. {
  2259. mpi_init[devid] = 1;
  2260. mpi_bindid[devid] = _starpu_get_next_bindid(config, STARPU_THREAD_ACTIVE, preferred_binding, npreferred);
  2261. memory_node = mpi_memory_nodes[devid] = _starpu_memory_node_register(STARPU_MPI_MS_RAM, devid, &_starpu_driver_mpi_node_ops);
  2262. for (numa = 0; numa < nb_numa_nodes; numa++)
  2263. {
  2264. _starpu_register_bus(numa, memory_node);
  2265. _starpu_register_bus(memory_node, numa);
  2266. }
  2267. }
  2268. //This worker can manage transfers on NUMA nodes
  2269. for (numa = 0; numa < nb_numa_nodes; numa++)
  2270. _starpu_worker_drives_memory_node(&workerarg->set->workers[0], numa);
  2271. _starpu_worker_drives_memory_node(&workerarg->set->workers[0], memory_node);
  2272. #ifndef STARPU_MPI_MASTER_SLAVE_MULTIPLE_THREAD
  2273. /* MPI driver thread can manage all slave memories if we disable the MPI multiple thread */
  2274. unsigned findworker;
  2275. for (findworker = 0; findworker < worker; findworker++)
  2276. {
  2277. struct _starpu_worker *findworkerarg = &config->workers[findworker];
  2278. if (findworkerarg->arch == STARPU_MPI_MS_WORKER)
  2279. {
  2280. _starpu_worker_drives_memory_node(workerarg, findworkerarg->memory_node);
  2281. _starpu_worker_drives_memory_node(findworkerarg, memory_node);
  2282. }
  2283. }
  2284. #endif
  2285. workerarg->bindid = mpi_bindid[devid];
  2286. _starpu_memory_node_add_nworkers(memory_node);
  2287. break;
  2288. }
  2289. #endif /* STARPU_USE_MPI_MASTER_SLAVE */
  2290. default:
  2291. STARPU_ABORT();
  2292. }
  2293. workerarg->memory_node = memory_node;
  2294. _STARPU_DEBUG("worker %u type %d devid %u bound to cpu %d, STARPU memory node %u\n", worker, workerarg->arch, devid, workerarg->bindid, memory_node);
  2295. #ifdef __GLIBC__
  2296. if (workerarg->bindid != -1)
  2297. {
  2298. /* Save the initial cpuset */
  2299. CPU_ZERO(&workerarg->cpu_set);
  2300. CPU_SET(workerarg->bindid, &workerarg->cpu_set);
  2301. }
  2302. #endif /* __GLIBC__ */
  2303. #ifdef STARPU_HAVE_HWLOC
  2304. if (workerarg->bindid == -1)
  2305. {
  2306. workerarg->hwloc_cpu_set = hwloc_bitmap_alloc();
  2307. workerarg->hwloc_obj = NULL;
  2308. }
  2309. else
  2310. {
  2311. /* Put the worker descriptor in the userdata field of the
  2312. * hwloc object describing the CPU */
  2313. hwloc_obj_t worker_obj = hwloc_get_obj_by_depth(config->topology.hwtopology,
  2314. config->pu_depth,
  2315. workerarg->bindid);
  2316. struct _starpu_hwloc_userdata *data = worker_obj->userdata;
  2317. if (data->worker_list == NULL)
  2318. data->worker_list = _starpu_worker_list_new();
  2319. _starpu_worker_list_push_front(data->worker_list, workerarg);
  2320. /* Clear the cpu set and set the cpu */
  2321. workerarg->hwloc_cpu_set = hwloc_bitmap_dup(worker_obj->cpuset);
  2322. workerarg->hwloc_obj = worker_obj;
  2323. }
  2324. #endif
  2325. if (workerarg->bindid != -1)
  2326. {
  2327. bindid = workerarg->bindid;
  2328. unsigned old_nbindid = config->nbindid;
  2329. if (bindid >= old_nbindid)
  2330. {
  2331. /* More room needed */
  2332. if (!old_nbindid)
  2333. config->nbindid = STARPU_NMAXWORKERS;
  2334. else
  2335. config->nbindid = 2 * old_nbindid;
  2336. if (bindid >= config->nbindid)
  2337. {
  2338. config->nbindid = bindid+1;
  2339. }
  2340. _STARPU_REALLOC(config->bindid_workers, config->nbindid * sizeof(config->bindid_workers[0]));
  2341. memset(&config->bindid_workers[old_nbindid], 0, (config->nbindid - old_nbindid) * sizeof(config->bindid_workers[0]));
  2342. }
  2343. /* Add slot for this worker */
  2344. /* Don't care about amortizing the cost, there are usually very few workers sharing the same bindid */
  2345. config->bindid_workers[bindid].nworkers++;
  2346. _STARPU_REALLOC(config->bindid_workers[bindid].workerids, config->bindid_workers[bindid].nworkers * sizeof(config->bindid_workers[bindid].workerids[0]));
  2347. config->bindid_workers[bindid].workerids[config->bindid_workers[bindid].nworkers-1] = worker;
  2348. }
  2349. }
  2350. #if defined(STARPU_HAVE_HWLOC) && !defined(STARPU_SIMGRID)
  2351. /* If some NUMA nodes don't have drivers, attribute some */
  2352. unsigned node, nnodes = starpu_memory_nodes_get_count();;
  2353. for (node = 0; node < nnodes; node++)
  2354. {
  2355. if (starpu_node_get_kind(node) != STARPU_CPU_RAM)
  2356. /* Only RAM nodes can be processed by any CPU */
  2357. continue;
  2358. for (worker = 0; worker < config->topology.nworkers; worker++)
  2359. {
  2360. if (_starpu_worker_drives_memory[worker][node])
  2361. break;
  2362. }
  2363. if (worker < config->topology.nworkers)
  2364. /* Already somebody driving it */
  2365. continue;
  2366. /* Nobody driving this node! Attribute some */
  2367. _STARPU_DEBUG("nobody drives memory node %d\n", node);
  2368. hwloc_obj_t numa_node_obj = hwloc_get_obj_by_type(config->topology.hwtopology, HWLOC_OBJ_NUMANODE, starpu_memory_nodes_numa_id_to_hwloclogid(node));
  2369. int ret = _starpu_find_pu_driving_numa_up(numa_node_obj, node);
  2370. STARPU_ASSERT_MSG(ret, "oops, didn't find any worker to drive memory node %d!?", node);
  2371. }
  2372. #endif
  2373. #ifdef STARPU_SIMGRID
  2374. _starpu_simgrid_count_ngpus();
  2375. #else
  2376. #ifdef STARPU_HAVE_HWLOC
  2377. _starpu_topology_count_ngpus(hwloc_get_root_obj(config->topology.hwtopology));
  2378. #endif
  2379. #endif
  2380. }
  2381. int _starpu_build_topology(struct _starpu_machine_config *config, int no_mp_config)
  2382. {
  2383. int ret;
  2384. unsigned i;
  2385. enum starpu_worker_archtype type;
  2386. ret = _starpu_init_machine_config(config, no_mp_config);
  2387. if (ret)
  2388. return ret;
  2389. /* for the data management library */
  2390. _starpu_memory_nodes_init();
  2391. _starpu_datastats_init();
  2392. _starpu_init_workers_binding_and_memory(config, no_mp_config);
  2393. _starpu_mem_chunk_init_last();
  2394. for (type = 0; type < STARPU_NARCH; type++)
  2395. config->arch_nodeid[type] = -1;
  2396. for (i = 0; i < starpu_worker_get_count(); i++)
  2397. {
  2398. type = starpu_worker_get_type(i);
  2399. if (config->arch_nodeid[type] == -1)
  2400. config->arch_nodeid[type] = starpu_worker_get_memory_node(i);
  2401. else if (config->arch_nodeid[type] != (int) starpu_worker_get_memory_node(i))
  2402. config->arch_nodeid[type] = -2;
  2403. }
  2404. return 0;
  2405. }
  2406. void _starpu_destroy_topology(struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED)
  2407. {
  2408. #if defined(STARPU_USE_MPI_MASTER_SLAVE)
  2409. _starpu_deinit_mp_config(config);
  2410. #endif
  2411. /* cleanup StarPU internal data structures */
  2412. _starpu_memory_nodes_deinit();
  2413. _starpu_destroy_machine_config(config);
  2414. _starpu_deinitialize_workers_bindid(config);
  2415. }
  2416. void starpu_topology_print(FILE *output)
  2417. {
  2418. struct _starpu_machine_config *config = _starpu_get_machine_config();
  2419. struct _starpu_machine_topology *topology = &config->topology;
  2420. unsigned pu;
  2421. unsigned worker;
  2422. unsigned nworkers = starpu_worker_get_count();
  2423. unsigned ncombinedworkers = topology->ncombinedworkers;
  2424. unsigned nthreads_per_core = topology->nhwpus / topology->nhwworker[STARPU_CPU_WORKER][0];
  2425. #ifdef STARPU_HAVE_HWLOC
  2426. hwloc_topology_t topo = topology->hwtopology;
  2427. hwloc_obj_t pu_obj;
  2428. hwloc_obj_t last_numa_obj = NULL, numa_obj;
  2429. hwloc_obj_t last_package_obj = NULL, package_obj;
  2430. #endif
  2431. for (pu = 0; pu < topology->nhwpus; pu++)
  2432. {
  2433. #ifdef STARPU_HAVE_HWLOC
  2434. pu_obj = hwloc_get_obj_by_type(topo, HWLOC_OBJ_PU, pu);
  2435. numa_obj = numa_get_obj(pu_obj);
  2436. if (numa_obj != last_numa_obj)
  2437. {
  2438. fprintf(output, "numa %u", numa_obj->logical_index);
  2439. last_numa_obj = numa_obj;
  2440. }
  2441. fprintf(output, "\t");
  2442. package_obj = hwloc_get_ancestor_obj_by_type(topo, HWLOC_OBJ_SOCKET, pu_obj);
  2443. if (package_obj != last_package_obj)
  2444. {
  2445. fprintf(output, "pack %u", package_obj->logical_index);
  2446. last_package_obj = package_obj;
  2447. }
  2448. fprintf(output, "\t");
  2449. #endif
  2450. if ((pu % nthreads_per_core) == 0)
  2451. fprintf(output, "core %u", pu / nthreads_per_core);
  2452. fprintf(output, "\tPU %u\t", pu);
  2453. for (worker = 0;
  2454. worker < nworkers + ncombinedworkers;
  2455. worker++)
  2456. {
  2457. if (worker < nworkers)
  2458. {
  2459. struct _starpu_worker *workerarg = &config->workers[worker];
  2460. if (workerarg->bindid == (int) pu)
  2461. {
  2462. char name[256];
  2463. starpu_worker_get_name(worker, name, sizeof(name));
  2464. fprintf(output, "%s\t", name);
  2465. }
  2466. }
  2467. else
  2468. {
  2469. int worker_size, i;
  2470. int *combined_workerid;
  2471. starpu_combined_worker_get_description(worker, &worker_size, &combined_workerid);
  2472. for (i = 0; i < worker_size; i++)
  2473. {
  2474. if (topology->workers_bindid[combined_workerid[i]] == pu)
  2475. fprintf(output, "comb %u\t", worker-nworkers);
  2476. }
  2477. }
  2478. }
  2479. fprintf(output, "\n");
  2480. }
  2481. }
  2482. int starpu_get_pu_os_index(unsigned logical_index)
  2483. {
  2484. #ifdef STARPU_HAVE_HWLOC
  2485. struct _starpu_machine_config *config = _starpu_get_machine_config();
  2486. struct _starpu_machine_topology *topology = &config->topology;
  2487. hwloc_topology_t topo = topology->hwtopology;
  2488. return hwloc_get_obj_by_type(topo, HWLOC_OBJ_PU, logical_index)->os_index;
  2489. #else
  2490. return logical_index;
  2491. #endif
  2492. }
  2493. #ifdef STARPU_HAVE_HWLOC
  2494. hwloc_topology_t starpu_get_hwloc_topology(void)
  2495. {
  2496. struct _starpu_machine_config *config = _starpu_get_machine_config();
  2497. return config->topology.hwtopology;
  2498. }
  2499. #endif
  2500. unsigned _starpu_get_nhyperthreads()
  2501. {
  2502. struct _starpu_machine_config *config = _starpu_get_machine_config();
  2503. return config->topology.nhwpus / config->topology.nhwworker[STARPU_CPU_WORKER][0];
  2504. }