topology.c 96 KB

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