perfmodel_bus.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 CNRS
  5. * Copyright (C) 2013 Corentin Salingue
  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. #ifdef STARPU_USE_CUDA
  19. #ifndef _GNU_SOURCE
  20. #define _GNU_SOURCE
  21. #endif
  22. #include <sched.h>
  23. #endif
  24. #include <stdlib.h>
  25. #include <math.h>
  26. #include <starpu.h>
  27. #include <starpu_cuda.h>
  28. #include <starpu_opencl.h>
  29. #include <common/config.h>
  30. #ifdef HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif
  33. #include <core/workers.h>
  34. #include <core/perfmodel/perfmodel.h>
  35. #include <core/simgrid.h>
  36. #include <common/utils.h>
  37. #include <drivers/mpi/driver_mpi_common.h>
  38. #ifdef STARPU_USE_OPENCL
  39. #include <starpu_opencl.h>
  40. #endif
  41. #ifdef STARPU_HAVE_WINDOWS
  42. #include <windows.h>
  43. #endif
  44. #if defined(HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX) && HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX
  45. #include <hwloc/cuda.h>
  46. #endif
  47. #define SIZE (32*1024*1024*sizeof(char))
  48. #define NITER 32
  49. #ifndef STARPU_SIMGRID
  50. static void _starpu_bus_force_sampling(void);
  51. #endif
  52. /* timing is in µs per byte (i.e. slowness, inverse of bandwidth) */
  53. struct dev_timing
  54. {
  55. int cpu_id;
  56. double timing_htod;
  57. double latency_htod;
  58. double timing_dtoh;
  59. double latency_dtoh;
  60. };
  61. /* TODO: measure latency */
  62. static double bandwidth_matrix[STARPU_MAXNODES][STARPU_MAXNODES];
  63. static double latency_matrix[STARPU_MAXNODES][STARPU_MAXNODES];
  64. static unsigned was_benchmarked = 0;
  65. #ifndef STARPU_SIMGRID
  66. static unsigned ncpus = 0;
  67. #endif
  68. static unsigned ncuda = 0;
  69. static unsigned nopencl = 0;
  70. static unsigned nmic = 0;
  71. static unsigned nmpi_ms = 0;
  72. /* Benchmarking the performance of the bus */
  73. #ifndef STARPU_SIMGRID
  74. static uint64_t cuda_size[STARPU_MAXCUDADEVS];
  75. #endif
  76. #ifdef STARPU_USE_CUDA
  77. /* preference order of cores (logical indexes) */
  78. static int cuda_affinity_matrix[STARPU_MAXCUDADEVS][STARPU_MAXCPUS];
  79. #ifndef STARPU_SIMGRID
  80. static double cudadev_timing_htod[STARPU_MAXNODES] = {0.0};
  81. static double cudadev_latency_htod[STARPU_MAXNODES] = {0.0};
  82. static double cudadev_timing_dtoh[STARPU_MAXNODES] = {0.0};
  83. static double cudadev_latency_dtoh[STARPU_MAXNODES] = {0.0};
  84. #ifdef HAVE_CUDA_MEMCPY_PEER
  85. static double cudadev_timing_dtod[STARPU_MAXNODES][STARPU_MAXNODES] = {{0.0}};
  86. static double cudadev_latency_dtod[STARPU_MAXNODES][STARPU_MAXNODES] = {{0.0}};
  87. #endif
  88. #endif
  89. static struct dev_timing cudadev_timing_per_cpu[STARPU_MAXNODES*STARPU_MAXCPUS];
  90. #endif
  91. #ifndef STARPU_SIMGRID
  92. static uint64_t opencl_size[STARPU_MAXCUDADEVS];
  93. #endif
  94. #ifdef STARPU_USE_OPENCL
  95. /* preference order of cores (logical indexes) */
  96. static int opencl_affinity_matrix[STARPU_MAXOPENCLDEVS][STARPU_MAXCPUS];
  97. #ifndef STARPU_SIMGRID
  98. static double opencldev_timing_htod[STARPU_MAXNODES] = {0.0};
  99. static double opencldev_latency_htod[STARPU_MAXNODES] = {0.0};
  100. static double opencldev_timing_dtoh[STARPU_MAXNODES] = {0.0};
  101. static double opencldev_latency_dtoh[STARPU_MAXNODES] = {0.0};
  102. #endif
  103. static struct dev_timing opencldev_timing_per_cpu[STARPU_MAXNODES*STARPU_MAXCPUS];
  104. #endif
  105. #ifdef STARPU_USE_MIC
  106. static double mic_time_host_to_device[STARPU_MAXNODES] = {0.0};
  107. static double mic_time_device_to_host[STARPU_MAXNODES] = {0.0};
  108. #endif /* STARPU_USE_MIC */
  109. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  110. static double mpi_time_host_to_device[STARPU_MAXNODES] = {0.0};
  111. static double mpi_time_device_to_host[STARPU_MAXNODES] = {0.0};
  112. static double mpi_latency_host_to_device[STARPU_MAXNODES] = {0.0};
  113. static double mpi_latency_device_to_host[STARPU_MAXNODES] = {0.0};
  114. #endif
  115. #ifdef STARPU_HAVE_HWLOC
  116. static hwloc_topology_t hwtopology;
  117. #endif
  118. #if (defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)) && !defined(STARPU_SIMGRID)
  119. #ifdef STARPU_USE_CUDA
  120. static void measure_bandwidth_between_host_and_dev_on_cpu_with_cuda(int dev, int cpu, struct dev_timing *dev_timing_per_cpu)
  121. {
  122. struct _starpu_machine_config *config = _starpu_get_machine_config();
  123. _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
  124. size_t size = SIZE;
  125. /* Initialize CUDA context on the device */
  126. /* We do not need to enable OpenGL interoperability at this point,
  127. * since we cleanly shutdown CUDA before returning. */
  128. cudaSetDevice(dev);
  129. /* hack to avoid third party libs to rebind threads */
  130. _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
  131. /* hack to force the initialization */
  132. cudaFree(0);
  133. /* hack to avoid third party libs to rebind threads */
  134. _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
  135. /* Get the maximum size which can be allocated on the device */
  136. struct cudaDeviceProp prop;
  137. cudaError_t cures;
  138. cures = cudaGetDeviceProperties(&prop, dev);
  139. if (STARPU_UNLIKELY(cures)) STARPU_CUDA_REPORT_ERROR(cures);
  140. cuda_size[dev] = prop.totalGlobalMem;
  141. if (size > prop.totalGlobalMem/4) size = prop.totalGlobalMem/4;
  142. /* Allocate a buffer on the device */
  143. unsigned char *d_buffer;
  144. cures = cudaMalloc((void **)&d_buffer, size);
  145. STARPU_ASSERT(cures == cudaSuccess);
  146. /* hack to avoid third party libs to rebind threads */
  147. _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
  148. /* Allocate a buffer on the host */
  149. unsigned char *h_buffer;
  150. cures = cudaHostAlloc((void **)&h_buffer, size, 0);
  151. STARPU_ASSERT(cures == cudaSuccess);
  152. /* hack to avoid third party libs to rebind threads */
  153. _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
  154. /* Fill them */
  155. memset(h_buffer, 0, size);
  156. cudaMemset(d_buffer, 0, size);
  157. /* hack to avoid third party libs to rebind threads */
  158. _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
  159. unsigned iter;
  160. double timing;
  161. double start;
  162. double end;
  163. /* Measure upload bandwidth */
  164. start = starpu_timing_now();
  165. for (iter = 0; iter < NITER; iter++)
  166. {
  167. cudaMemcpy(d_buffer, h_buffer, size, cudaMemcpyHostToDevice);
  168. cudaThreadSynchronize();
  169. }
  170. end = starpu_timing_now();
  171. timing = end - start;
  172. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_htod = timing/NITER/size;
  173. /* Measure download bandwidth */
  174. start = starpu_timing_now();
  175. for (iter = 0; iter < NITER; iter++)
  176. {
  177. cudaMemcpy(h_buffer, d_buffer, size, cudaMemcpyDeviceToHost);
  178. cudaThreadSynchronize();
  179. }
  180. end = starpu_timing_now();
  181. timing = end - start;
  182. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_dtoh = timing/NITER/size;
  183. /* Measure upload latency */
  184. start = starpu_timing_now();
  185. for (iter = 0; iter < NITER; iter++)
  186. {
  187. cudaMemcpy(d_buffer, h_buffer, 1, cudaMemcpyHostToDevice);
  188. cudaThreadSynchronize();
  189. }
  190. end = starpu_timing_now();
  191. timing = end - start;
  192. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_htod = timing/NITER;
  193. /* Measure download latency */
  194. start = starpu_timing_now();
  195. for (iter = 0; iter < NITER; iter++)
  196. {
  197. cudaMemcpy(h_buffer, d_buffer, 1, cudaMemcpyDeviceToHost);
  198. cudaThreadSynchronize();
  199. }
  200. end = starpu_timing_now();
  201. timing = end - start;
  202. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_dtoh = timing/NITER;
  203. /* Free buffers */
  204. cudaFreeHost(h_buffer);
  205. cudaFree(d_buffer);
  206. cudaThreadExit();
  207. }
  208. #ifdef HAVE_CUDA_MEMCPY_PEER
  209. static void measure_bandwidth_between_dev_and_dev_cuda(int src, int dst)
  210. {
  211. size_t size = SIZE;
  212. int can;
  213. /* Get the maximum size which can be allocated on the device */
  214. struct cudaDeviceProp prop;
  215. cudaError_t cures;
  216. cures = cudaGetDeviceProperties(&prop, src);
  217. if (STARPU_UNLIKELY(cures)) STARPU_CUDA_REPORT_ERROR(cures);
  218. if (size > prop.totalGlobalMem/4) size = prop.totalGlobalMem/4;
  219. cures = cudaGetDeviceProperties(&prop, dst);
  220. if (STARPU_UNLIKELY(cures)) STARPU_CUDA_REPORT_ERROR(cures);
  221. if (size > prop.totalGlobalMem/4) size = prop.totalGlobalMem/4;
  222. /* Initialize CUDA context on the source */
  223. /* We do not need to enable OpenGL interoperability at this point,
  224. * since we cleanly shutdown CUDA before returning. */
  225. cudaSetDevice(src);
  226. if (starpu_get_env_number("STARPU_ENABLE_CUDA_GPU_GPU_DIRECT") != 0)
  227. {
  228. cures = cudaDeviceCanAccessPeer(&can, src, dst);
  229. if (!cures && can)
  230. {
  231. cures = cudaDeviceEnablePeerAccess(dst, 0);
  232. if (!cures)
  233. _STARPU_DISP("GPU-Direct %d -> %d\n", dst, src);
  234. }
  235. }
  236. /* Allocate a buffer on the device */
  237. unsigned char *s_buffer;
  238. cures = cudaMalloc((void **)&s_buffer, size);
  239. STARPU_ASSERT(cures == cudaSuccess);
  240. cudaMemset(s_buffer, 0, size);
  241. /* Initialize CUDA context on the destination */
  242. /* We do not need to enable OpenGL interoperability at this point,
  243. * since we cleanly shutdown CUDA before returning. */
  244. cudaSetDevice(dst);
  245. if (starpu_get_env_number("STARPU_ENABLE_CUDA_GPU_GPU_DIRECT") != 0)
  246. {
  247. cures = cudaDeviceCanAccessPeer(&can, dst, src);
  248. if (!cures && can)
  249. {
  250. cures = cudaDeviceEnablePeerAccess(src, 0);
  251. if (!cures)
  252. _STARPU_DISP("GPU-Direct %d -> %d\n", src, dst);
  253. }
  254. }
  255. /* Allocate a buffer on the device */
  256. unsigned char *d_buffer;
  257. cures = cudaMalloc((void **)&d_buffer, size);
  258. STARPU_ASSERT(cures == cudaSuccess);
  259. cudaMemset(d_buffer, 0, size);
  260. unsigned iter;
  261. double timing;
  262. double start;
  263. double end;
  264. /* Measure upload bandwidth */
  265. start = starpu_timing_now();
  266. for (iter = 0; iter < NITER; iter++)
  267. {
  268. cudaMemcpyPeer(d_buffer, dst, s_buffer, src, size);
  269. cudaThreadSynchronize();
  270. }
  271. end = starpu_timing_now();
  272. timing = end - start;
  273. cudadev_timing_dtod[src+1][dst+1] = timing/NITER/size;
  274. /* Measure upload latency */
  275. start = starpu_timing_now();
  276. for (iter = 0; iter < NITER; iter++)
  277. {
  278. cudaMemcpyPeer(d_buffer, dst, s_buffer, src, 1);
  279. cudaThreadSynchronize();
  280. }
  281. end = starpu_timing_now();
  282. timing = end - start;
  283. cudadev_latency_dtod[src+1][dst+1] = timing/NITER;
  284. /* Free buffers */
  285. cudaFree(d_buffer);
  286. cudaSetDevice(src);
  287. cudaFree(s_buffer);
  288. cudaThreadExit();
  289. }
  290. #endif
  291. #endif
  292. #ifdef STARPU_USE_OPENCL
  293. static void measure_bandwidth_between_host_and_dev_on_cpu_with_opencl(int dev, int cpu, struct dev_timing *dev_timing_per_cpu)
  294. {
  295. cl_context context;
  296. cl_command_queue queue;
  297. cl_int err=0;
  298. size_t size = SIZE;
  299. int not_initialized;
  300. struct _starpu_machine_config *config = _starpu_get_machine_config();
  301. _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
  302. /* Is the context already initialised ? */
  303. starpu_opencl_get_context(dev, &context);
  304. not_initialized = (context == NULL);
  305. if (not_initialized == 1)
  306. _starpu_opencl_init_context(dev);
  307. /* Get context and queue */
  308. starpu_opencl_get_context(dev, &context);
  309. starpu_opencl_get_queue(dev, &queue);
  310. /* Get the maximum size which can be allocated on the device */
  311. cl_device_id device;
  312. cl_ulong maxMemAllocSize, totalGlobalMem;
  313. starpu_opencl_get_device(dev, &device);
  314. err = clGetDeviceInfo(device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(maxMemAllocSize), &maxMemAllocSize, NULL);
  315. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  316. if (size > (size_t)maxMemAllocSize/4) size = maxMemAllocSize/4;
  317. err = clGetDeviceInfo(device, CL_DEVICE_GLOBAL_MEM_SIZE , sizeof(totalGlobalMem), &totalGlobalMem, NULL);
  318. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  319. opencl_size[dev] = totalGlobalMem;
  320. if (_starpu_opencl_get_device_type(dev) == CL_DEVICE_TYPE_CPU)
  321. {
  322. /* Let's not use too much RAM when running OpenCL on a CPU: it
  323. * would make the OS swap like crazy. */
  324. size /= 2;
  325. }
  326. /* hack to avoid third party libs to rebind threads */
  327. _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
  328. /* Allocate a buffer on the device */
  329. cl_mem d_buffer;
  330. d_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, size, NULL, &err);
  331. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  332. /* hack to avoid third party libs to rebind threads */
  333. _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
  334. /* Allocate a buffer on the host */
  335. unsigned char *h_buffer;
  336. h_buffer = (unsigned char *)malloc(size);
  337. STARPU_ASSERT(h_buffer);
  338. /* hack to avoid third party libs to rebind threads */
  339. _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
  340. /* Fill them */
  341. memset(h_buffer, 0, size);
  342. err = clEnqueueWriteBuffer(queue, d_buffer, CL_TRUE, 0, size, h_buffer, 0, NULL, NULL);
  343. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  344. clFinish(queue);
  345. /* hack to avoid third party libs to rebind threads */
  346. _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
  347. unsigned iter;
  348. double timing;
  349. double start;
  350. double end;
  351. /* Measure upload bandwidth */
  352. start = starpu_timing_now();
  353. for (iter = 0; iter < NITER; iter++)
  354. {
  355. err = clEnqueueWriteBuffer(queue, d_buffer, CL_TRUE, 0, size, h_buffer, 0, NULL, NULL);
  356. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  357. clFinish(queue);
  358. }
  359. end = starpu_timing_now();
  360. timing = end - start;
  361. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_htod = timing/NITER/size;
  362. /* Measure download bandwidth */
  363. start = starpu_timing_now();
  364. for (iter = 0; iter < NITER; iter++)
  365. {
  366. err = clEnqueueReadBuffer(queue, d_buffer, CL_TRUE, 0, size, h_buffer, 0, NULL, NULL);
  367. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  368. clFinish(queue);
  369. }
  370. end = starpu_timing_now();
  371. timing = end - start;
  372. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_dtoh = timing/NITER/size;
  373. /* Measure upload latency */
  374. start = starpu_timing_now();
  375. for (iter = 0; iter < NITER; iter++)
  376. {
  377. err = clEnqueueWriteBuffer(queue, d_buffer, CL_TRUE, 0, 1, h_buffer, 0, NULL, NULL);
  378. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  379. clFinish(queue);
  380. }
  381. end = starpu_timing_now();
  382. timing = end - start;
  383. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_htod = timing/NITER;
  384. /* Measure download latency */
  385. start = starpu_timing_now();
  386. for (iter = 0; iter < NITER; iter++)
  387. {
  388. err = clEnqueueReadBuffer(queue, d_buffer, CL_TRUE, 0, 1, h_buffer, 0, NULL, NULL);
  389. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  390. clFinish(queue);
  391. }
  392. end = starpu_timing_now();
  393. timing = end - start;
  394. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_dtoh = timing/NITER;
  395. /* Free buffers */
  396. err = clReleaseMemObject(d_buffer);
  397. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  398. STARPU_OPENCL_REPORT_ERROR(err);
  399. free(h_buffer);
  400. /* Uninitiliaze OpenCL context on the device */
  401. if (not_initialized == 1)
  402. _starpu_opencl_deinit_context(dev);
  403. }
  404. #endif
  405. /* NB: we want to sort the bandwidth by DECREASING order */
  406. static int compar_dev_timing(const void *left_dev_timing, const void *right_dev_timing)
  407. {
  408. const struct dev_timing *left = (const struct dev_timing *)left_dev_timing;
  409. const struct dev_timing *right = (const struct dev_timing *)right_dev_timing;
  410. double left_dtoh = left->timing_dtoh;
  411. double left_htod = left->timing_htod;
  412. double right_dtoh = right->timing_dtoh;
  413. double right_htod = right->timing_htod;
  414. double timing_sum2_left = left_dtoh*left_dtoh + left_htod*left_htod;
  415. double timing_sum2_right = right_dtoh*right_dtoh + right_htod*right_htod;
  416. /* it's for a decreasing sorting */
  417. return (timing_sum2_left > timing_sum2_right);
  418. }
  419. #ifdef STARPU_HAVE_HWLOC
  420. static int find_numa_node(hwloc_obj_t obj)
  421. {
  422. STARPU_ASSERT(obj);
  423. hwloc_obj_t current = obj;
  424. while (current->depth != HWLOC_OBJ_NODE)
  425. {
  426. current = current->parent;
  427. /* If we don't find a "node" obj before the root, this means
  428. * hwloc does not know whether there are numa nodes or not, so
  429. * we should not use a per-node sampling in that case. */
  430. STARPU_ASSERT(current);
  431. }
  432. STARPU_ASSERT(current->depth == HWLOC_OBJ_NODE);
  433. return current->logical_index;
  434. }
  435. #endif
  436. static void measure_bandwidth_between_cpus_and_dev(int dev, struct dev_timing *dev_timing_per_cpu, char *type)
  437. {
  438. /* Either we have hwloc and we measure the bandwith between each GPU
  439. * and each NUMA node, or we don't have such NUMA information and we
  440. * measure the bandwith for each pair of (CPU, GPU), which is slower.
  441. * */
  442. #ifdef STARPU_HAVE_HWLOC
  443. int cpu_depth = hwloc_get_type_depth(hwtopology, HWLOC_OBJ_PU);
  444. int nnuma_nodes = hwloc_get_nbobjs_by_depth(hwtopology, HWLOC_OBJ_NODE);
  445. /* If no NUMA node was found, we assume that we have a single memory
  446. * bank. */
  447. const unsigned no_node_obj_was_found = (nnuma_nodes == 0);
  448. unsigned *is_available_per_numa_node = NULL;
  449. double *dev_timing_htod_per_numa_node = NULL;
  450. double *dev_latency_htod_per_numa_node = NULL;
  451. double *dev_timing_dtoh_per_numa_node = NULL;
  452. double *dev_latency_dtoh_per_numa_node = NULL;
  453. if (!no_node_obj_was_found)
  454. {
  455. is_available_per_numa_node = (unsigned *)malloc(nnuma_nodes * sizeof(unsigned));
  456. STARPU_ASSERT(is_available_per_numa_node);
  457. dev_timing_htod_per_numa_node = (double *)malloc(nnuma_nodes * sizeof(double));
  458. STARPU_ASSERT(dev_timing_htod_per_numa_node);
  459. dev_latency_htod_per_numa_node = (double *)malloc(nnuma_nodes * sizeof(double));
  460. STARPU_ASSERT(dev_latency_htod_per_numa_node);
  461. dev_timing_dtoh_per_numa_node = (double *)malloc(nnuma_nodes * sizeof(double));
  462. STARPU_ASSERT(dev_timing_dtoh_per_numa_node);
  463. dev_latency_dtoh_per_numa_node = (double *)malloc(nnuma_nodes * sizeof(double));
  464. STARPU_ASSERT(dev_latency_dtoh_per_numa_node);
  465. memset(is_available_per_numa_node, 0, nnuma_nodes*sizeof(unsigned));
  466. }
  467. #endif
  468. unsigned cpu;
  469. for (cpu = 0; cpu < ncpus; cpu++)
  470. {
  471. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].cpu_id = cpu;
  472. #ifdef STARPU_HAVE_HWLOC
  473. int numa_id = 0;
  474. if (!no_node_obj_was_found)
  475. {
  476. hwloc_obj_t obj = hwloc_get_obj_by_depth(hwtopology, cpu_depth, cpu);
  477. numa_id = find_numa_node(obj);
  478. if (is_available_per_numa_node[numa_id])
  479. {
  480. /* We reuse the previous numbers for that NUMA node */
  481. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_htod =
  482. dev_timing_htod_per_numa_node[numa_id];
  483. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_htod =
  484. dev_latency_htod_per_numa_node[numa_id];
  485. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_dtoh =
  486. dev_timing_dtoh_per_numa_node[numa_id];
  487. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_dtoh =
  488. dev_latency_dtoh_per_numa_node[numa_id];
  489. continue;
  490. }
  491. }
  492. #endif
  493. #ifdef STARPU_USE_CUDA
  494. if (strncmp(type, "CUDA", 4) == 0)
  495. measure_bandwidth_between_host_and_dev_on_cpu_with_cuda(dev, cpu, dev_timing_per_cpu);
  496. #endif
  497. #ifdef STARPU_USE_OPENCL
  498. if (strncmp(type, "OpenCL", 6) == 0)
  499. measure_bandwidth_between_host_and_dev_on_cpu_with_opencl(dev, cpu, dev_timing_per_cpu);
  500. #endif
  501. #ifdef STARPU_HAVE_HWLOC
  502. if (!no_node_obj_was_found && !is_available_per_numa_node[numa_id])
  503. {
  504. /* Save the results for that NUMA node */
  505. dev_timing_htod_per_numa_node[numa_id] =
  506. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_htod;
  507. dev_latency_htod_per_numa_node[numa_id] =
  508. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_htod;
  509. dev_timing_dtoh_per_numa_node[numa_id] =
  510. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_dtoh;
  511. dev_latency_dtoh_per_numa_node[numa_id] =
  512. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_dtoh;
  513. is_available_per_numa_node[numa_id] = 1;
  514. }
  515. #endif
  516. }
  517. #ifdef STARPU_HAVE_HWLOC
  518. if (!no_node_obj_was_found)
  519. {
  520. free(is_available_per_numa_node);
  521. free(dev_timing_htod_per_numa_node);
  522. free(dev_latency_htod_per_numa_node);
  523. free(dev_timing_dtoh_per_numa_node);
  524. free(dev_latency_dtoh_per_numa_node);
  525. }
  526. #endif /* STARPU_HAVE_HWLOC */
  527. }
  528. static void measure_bandwidth_between_host_and_dev(int dev, double *dev_timing_htod, double *dev_latency_htod,
  529. double *dev_timing_dtoh, double *dev_latency_dtoh,
  530. struct dev_timing *dev_timing_per_cpu, char *type)
  531. {
  532. measure_bandwidth_between_cpus_and_dev(dev, dev_timing_per_cpu, type);
  533. /* sort the results */
  534. qsort(&(dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS]), ncpus,
  535. sizeof(struct dev_timing),
  536. compar_dev_timing);
  537. #ifdef STARPU_VERBOSE
  538. unsigned cpu;
  539. for (cpu = 0; cpu < ncpus; cpu++)
  540. {
  541. unsigned current_cpu = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].cpu_id;
  542. double bandwidth_dtoh = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_dtoh;
  543. double bandwidth_htod = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_htod;
  544. double bandwidth_sum2 = bandwidth_dtoh*bandwidth_dtoh + bandwidth_htod*bandwidth_htod;
  545. _STARPU_DISP("(%10s) BANDWIDTH GPU %d CPU %u - htod %f - dtoh %f - %f\n", type, dev, current_cpu, bandwidth_htod, bandwidth_dtoh, sqrt(bandwidth_sum2));
  546. }
  547. unsigned best_cpu = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+0].cpu_id;
  548. _STARPU_DISP("(%10s) BANDWIDTH GPU %d BEST CPU %u\n", type, dev, best_cpu);
  549. #endif
  550. /* The results are sorted in a decreasing order, so that the best
  551. * measurement is currently the first entry. */
  552. dev_timing_dtoh[dev+1] = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+0].timing_dtoh;
  553. dev_latency_dtoh[dev+1] = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+0].latency_dtoh;
  554. dev_timing_htod[dev+1] = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+0].timing_htod;
  555. dev_latency_htod[dev+1] = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+0].latency_htod;
  556. }
  557. #endif /* defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) */
  558. static void benchmark_all_gpu_devices(void)
  559. {
  560. #ifdef STARPU_SIMGRID
  561. _STARPU_DISP("can not measure bus in simgrid mode, please run starpu_calibrate_bus in non-simgrid mode to make sure the bus performance model was calibrated\n");
  562. STARPU_ABORT();
  563. #else /* !SIMGRID */
  564. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) || defined(STARPU_USE_MIC) || defined(STARPU_USE_MPI_MASTER_SLAVE)
  565. unsigned i;
  566. #endif
  567. #ifdef HAVE_CUDA_MEMCPY_PEER
  568. unsigned j;
  569. #endif
  570. _STARPU_DEBUG("Benchmarking the speed of the bus\n");
  571. #ifdef STARPU_HAVE_HWLOC
  572. hwloc_topology_init(&hwtopology);
  573. hwloc_topology_load(hwtopology);
  574. #endif
  575. #ifdef STARPU_HAVE_HWLOC
  576. hwloc_bitmap_t former_cpuset = hwloc_bitmap_alloc();
  577. hwloc_get_cpubind(hwtopology, former_cpuset, HWLOC_CPUBIND_THREAD);
  578. #elif __linux__
  579. /* Save the current cpu binding */
  580. cpu_set_t former_process_affinity;
  581. int ret;
  582. ret = sched_getaffinity(0, sizeof(former_process_affinity), &former_process_affinity);
  583. if (ret)
  584. {
  585. perror("sched_getaffinity");
  586. STARPU_ABORT();
  587. }
  588. #else
  589. #warning Missing binding support, StarPU will not be able to properly benchmark NUMA topology
  590. #endif
  591. struct _starpu_machine_config *config = _starpu_get_machine_config();
  592. ncpus = _starpu_topology_get_nhwcpu(config);
  593. #ifdef STARPU_USE_CUDA
  594. ncuda = _starpu_get_cuda_device_count();
  595. for (i = 0; i < ncuda; i++)
  596. {
  597. _STARPU_DISP("CUDA %d...\n", i);
  598. /* measure bandwidth between Host and Device i */
  599. measure_bandwidth_between_host_and_dev(i, cudadev_timing_htod, cudadev_latency_htod, cudadev_timing_dtoh, cudadev_latency_dtoh, cudadev_timing_per_cpu, "CUDA");
  600. }
  601. #ifdef HAVE_CUDA_MEMCPY_PEER
  602. for (i = 0; i < ncuda; i++)
  603. for (j = 0; j < ncuda; j++)
  604. if (i != j)
  605. {
  606. _STARPU_DISP("CUDA %d -> %d...\n", i, j);
  607. /* measure bandwidth between Host and Device i */
  608. measure_bandwidth_between_dev_and_dev_cuda(i, j);
  609. }
  610. #endif
  611. #endif
  612. #ifdef STARPU_USE_OPENCL
  613. nopencl = _starpu_opencl_get_device_count();
  614. for (i = 0; i < nopencl; i++)
  615. {
  616. _STARPU_DISP("OpenCL %d...\n", i);
  617. /* measure bandwith between Host and Device i */
  618. measure_bandwidth_between_host_and_dev(i, opencldev_timing_htod, opencldev_latency_htod, opencldev_timing_dtoh, opencldev_latency_dtoh, opencldev_timing_per_cpu, "OpenCL");
  619. }
  620. #endif
  621. #ifdef STARPU_USE_MIC
  622. /* TODO: implement real calibration ! For now we only put an arbitrary
  623. * value for each device during at the declaration as a bug fix, else
  624. * we get problems on heft scheduler */
  625. nmic = _starpu_mic_src_get_device_count();
  626. for (i = 0; i < STARPU_MAXNODES; i++)
  627. {
  628. mic_time_host_to_device[i] = 0.1;
  629. mic_time_device_to_host[i] = 0.1;
  630. }
  631. #endif /* STARPU_USE_MIC */
  632. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  633. _starpu_mpi_common_measure_bandwidth_latency(mpi_time_host_to_device, mpi_time_device_to_host, mpi_latency_host_to_device, mpi_latency_device_to_host);
  634. #endif /* STARPU_USE_MPI_MASTER_SLAVE */
  635. #ifdef STARPU_HAVE_HWLOC
  636. hwloc_set_cpubind(hwtopology, former_cpuset, HWLOC_CPUBIND_THREAD);
  637. hwloc_bitmap_free(former_cpuset);
  638. #elif __linux__
  639. /* Restore the former affinity */
  640. ret = sched_setaffinity(0, sizeof(former_process_affinity), &former_process_affinity);
  641. if (ret)
  642. {
  643. perror("sched_setaffinity");
  644. STARPU_ABORT();
  645. }
  646. #endif
  647. #ifdef STARPU_HAVE_HWLOC
  648. hwloc_topology_destroy(hwtopology);
  649. #endif
  650. _STARPU_DEBUG("Benchmarking the speed of the bus is done.\n");
  651. was_benchmarked = 1;
  652. #endif /* !SIMGRID */
  653. }
  654. static void get_bus_path(const char *type, char *path, size_t maxlen)
  655. {
  656. char hostname[65];
  657. _starpu_gethostname(hostname, sizeof(hostname));
  658. snprintf(path, maxlen, "%s%s.%s", _starpu_get_perf_model_dir_bus(), hostname, type);
  659. }
  660. /*
  661. * Affinity
  662. */
  663. static void get_affinity_path(char *path, size_t maxlen)
  664. {
  665. get_bus_path("affinity", path, maxlen);
  666. }
  667. #ifndef STARPU_SIMGRID
  668. static void load_bus_affinity_file_content(void)
  669. {
  670. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  671. FILE *f;
  672. char path[256];
  673. get_affinity_path(path, sizeof(path));
  674. _STARPU_DEBUG("loading affinities from %s\n", path);
  675. f = fopen(path, "r");
  676. STARPU_ASSERT(f);
  677. _starpu_frdlock(f);
  678. struct _starpu_machine_config *config = _starpu_get_machine_config();
  679. ncpus = _starpu_topology_get_nhwcpu(config);
  680. unsigned gpu;
  681. #ifdef STARPU_USE_CUDA
  682. ncuda = _starpu_get_cuda_device_count();
  683. for (gpu = 0; gpu < ncuda; gpu++)
  684. {
  685. int ret;
  686. unsigned dummy;
  687. _starpu_drop_comments(f);
  688. ret = fscanf(f, "%d\t", &dummy);
  689. STARPU_ASSERT(ret == 1);
  690. STARPU_ASSERT(dummy == gpu);
  691. unsigned cpu;
  692. for (cpu = 0; cpu < ncpus; cpu++)
  693. {
  694. ret = fscanf(f, "%d\t", &cuda_affinity_matrix[gpu][cpu]);
  695. STARPU_ASSERT(ret == 1);
  696. }
  697. ret = fscanf(f, "\n");
  698. STARPU_ASSERT(ret == 0);
  699. }
  700. #endif /* !STARPU_USE_CUDA */
  701. #ifdef STARPU_USE_OPENCL
  702. nopencl = _starpu_opencl_get_device_count();
  703. for (gpu = 0; gpu < nopencl; gpu++)
  704. {
  705. int ret;
  706. unsigned dummy;
  707. _starpu_drop_comments(f);
  708. ret = fscanf(f, "%d\t", &dummy);
  709. STARPU_ASSERT(ret == 1);
  710. STARPU_ASSERT(dummy == gpu);
  711. unsigned cpu;
  712. for (cpu = 0; cpu < ncpus; cpu++)
  713. {
  714. ret = fscanf(f, "%d\t", &opencl_affinity_matrix[gpu][cpu]);
  715. STARPU_ASSERT(ret == 1);
  716. }
  717. ret = fscanf(f, "\n");
  718. STARPU_ASSERT(ret == 0);
  719. }
  720. #endif /* !STARPU_USE_OPENCL */
  721. _starpu_frdunlock(f);
  722. fclose(f);
  723. #endif /* !(STARPU_USE_CUDA_ || STARPU_USE_OPENCL */
  724. }
  725. #ifndef STARPU_SIMGRID
  726. static void write_bus_affinity_file_content(void)
  727. {
  728. STARPU_ASSERT(was_benchmarked);
  729. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  730. FILE *f;
  731. char path[256];
  732. get_affinity_path(path, sizeof(path));
  733. _STARPU_DEBUG("writing affinities to %s\n", path);
  734. f = fopen(path, "w+");
  735. if (!f)
  736. {
  737. perror("fopen write_buf_affinity_file_content");
  738. _STARPU_DISP("path '%s'\n", path);
  739. fflush(stderr);
  740. STARPU_ABORT();
  741. }
  742. _starpu_frdlock(f);
  743. unsigned cpu;
  744. unsigned gpu;
  745. fprintf(f, "# GPU\t");
  746. for (cpu = 0; cpu < ncpus; cpu++)
  747. fprintf(f, "CPU%u\t", cpu);
  748. fprintf(f, "\n");
  749. #ifdef STARPU_USE_CUDA
  750. for (gpu = 0; gpu < ncuda; gpu++)
  751. {
  752. fprintf(f, "%d\t", gpu);
  753. for (cpu = 0; cpu < ncpus; cpu++)
  754. {
  755. fprintf(f, "%d\t", cudadev_timing_per_cpu[(gpu+1)*STARPU_MAXCPUS+cpu].cpu_id);
  756. }
  757. fprintf(f, "\n");
  758. }
  759. #endif
  760. #ifdef STARPU_USE_OPENCL
  761. for (gpu = 0; gpu < nopencl; gpu++)
  762. {
  763. fprintf(f, "%d\t", gpu);
  764. for (cpu = 0; cpu < ncpus; cpu++)
  765. {
  766. fprintf(f, "%d\t", opencldev_timing_per_cpu[(gpu+1)*STARPU_MAXCPUS+cpu].cpu_id);
  767. }
  768. fprintf(f, "\n");
  769. }
  770. #endif
  771. _starpu_frdunlock(f);
  772. fclose(f);
  773. #endif
  774. }
  775. #endif /* STARPU_SIMGRID */
  776. static void generate_bus_affinity_file(void)
  777. {
  778. if (!was_benchmarked)
  779. benchmark_all_gpu_devices();
  780. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  781. /* Slaves don't write files */
  782. if (!_starpu_mpi_common_is_src_node())
  783. return;
  784. #endif
  785. write_bus_affinity_file_content();
  786. }
  787. static void load_bus_affinity_file(void)
  788. {
  789. int res;
  790. char path[256];
  791. get_affinity_path(path, sizeof(path));
  792. res = access(path, F_OK);
  793. if (res)
  794. {
  795. /* File does not exist yet */
  796. generate_bus_affinity_file();
  797. }
  798. load_bus_affinity_file_content();
  799. }
  800. #ifdef STARPU_USE_CUDA
  801. int *_starpu_get_cuda_affinity_vector(unsigned gpuid)
  802. {
  803. return cuda_affinity_matrix[gpuid];
  804. }
  805. #endif /* STARPU_USE_CUDA */
  806. #ifdef STARPU_USE_OPENCL
  807. int *_starpu_get_opencl_affinity_vector(unsigned gpuid)
  808. {
  809. return opencl_affinity_matrix[gpuid];
  810. }
  811. #endif /* STARPU_USE_OPENCL */
  812. void starpu_bus_print_affinity(FILE *f)
  813. {
  814. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  815. unsigned cpu;
  816. unsigned gpu;
  817. #endif
  818. fprintf(f, "# GPU\tCPU in preference order (logical index)\n");
  819. #ifdef STARPU_USE_CUDA
  820. fprintf(f, "# CUDA\n");
  821. for(gpu = 0 ; gpu<ncuda ; gpu++)
  822. {
  823. fprintf(f, "%d\t", gpu);
  824. for (cpu = 0; cpu < ncpus; cpu++)
  825. {
  826. fprintf(f, "%d\t", cuda_affinity_matrix[gpu][cpu]);
  827. }
  828. fprintf(f, "\n");
  829. }
  830. #endif
  831. #ifdef STARPU_USE_OPENCL
  832. fprintf(f, "# OpenCL\n");
  833. for(gpu = 0 ; gpu<nopencl ; gpu++)
  834. {
  835. fprintf(f, "%d\t", gpu);
  836. for (cpu = 0; cpu < ncpus; cpu++)
  837. {
  838. fprintf(f, "%d\t", opencl_affinity_matrix[gpu][cpu]);
  839. }
  840. fprintf(f, "\n");
  841. }
  842. #endif
  843. }
  844. #endif /* STARPU_SIMGRID */
  845. /*
  846. * Latency
  847. */
  848. static void get_latency_path(char *path, size_t maxlen)
  849. {
  850. get_bus_path("latency", path, maxlen);
  851. }
  852. static int load_bus_latency_file_content(void)
  853. {
  854. int n;
  855. unsigned src, dst;
  856. FILE *f;
  857. double latency;
  858. char path[256];
  859. get_latency_path(path, sizeof(path));
  860. _STARPU_DEBUG("loading latencies from %s\n", path);
  861. f = fopen(path, "r");
  862. if (!f)
  863. {
  864. perror("fopen load_bus_latency_file_content");
  865. _STARPU_DISP("path '%s'\n", path);
  866. fflush(stderr);
  867. STARPU_ABORT();
  868. }
  869. _starpu_frdlock(f);
  870. for (src = 0; src < STARPU_MAXNODES; src++)
  871. {
  872. _starpu_drop_comments(f);
  873. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  874. {
  875. n = _starpu_read_double(f, "%le", &latency);
  876. if (n != 1)
  877. {
  878. _STARPU_DISP("Error while reading latency file <%s>. Expected a number\n", path);
  879. fclose(f);
  880. return 0;
  881. }
  882. n = getc(f);
  883. if (n == '\n')
  884. break;
  885. if (n != '\t')
  886. {
  887. _STARPU_DISP("bogus character '%c' (%d) in latency file %s\n", n, n, path);
  888. fclose(f);
  889. return 0;
  890. }
  891. latency_matrix[src][dst] = latency;
  892. /* Look out for \t\n */
  893. n = getc(f);
  894. if (n == '\n')
  895. break;
  896. ungetc(n, f);
  897. n = '\t';
  898. }
  899. /* No more values, take NAN */
  900. for ( ; dst < STARPU_MAXNODES; dst++)
  901. latency_matrix[src][dst] = NAN;
  902. while (n == '\t')
  903. {
  904. /* Look out for \t\n */
  905. n = getc(f);
  906. if (n == '\n')
  907. break;
  908. ungetc(n, f);
  909. n = _starpu_read_double(f, "%le", &latency);
  910. if (n && !isnan(latency))
  911. {
  912. _STARPU_DISP("Too many nodes in latency file %s for this configuration (%d)\n", path, STARPU_MAXNODES);
  913. fclose(f);
  914. return 0;
  915. }
  916. n = getc(f);
  917. }
  918. if (n != '\n')
  919. {
  920. _STARPU_DISP("Bogus character '%c' (%d) in latency file %s\n", n, n, path);
  921. fclose(f);
  922. return 0;
  923. }
  924. /* Look out for EOF */
  925. n = getc(f);
  926. if (n == EOF)
  927. break;
  928. ungetc(n, f);
  929. }
  930. _starpu_frdunlock(f);
  931. fclose(f);
  932. /* No more values, take NAN */
  933. for ( ; src < STARPU_MAXNODES; src++)
  934. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  935. latency_matrix[src][dst] = NAN;
  936. return 1;
  937. }
  938. #ifndef STARPU_SIMGRID
  939. static void write_bus_latency_file_content(void)
  940. {
  941. unsigned src, dst, maxnode;
  942. FILE *f;
  943. STARPU_ASSERT(was_benchmarked);
  944. char path[256];
  945. get_latency_path(path, sizeof(path));
  946. _STARPU_DEBUG("writing latencies to %s\n", path);
  947. f = fopen(path, "w+");
  948. if (!f)
  949. {
  950. perror("fopen write_bus_latency_file_content");
  951. _STARPU_DISP("path '%s'\n", path);
  952. fflush(stderr);
  953. STARPU_ABORT();
  954. }
  955. _starpu_fwrlock(f);
  956. _starpu_fftruncate(f, 0);
  957. fprintf(f, "# ");
  958. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  959. fprintf(f, "to %d\t\t", dst);
  960. fprintf(f, "\n");
  961. maxnode = ncuda;
  962. #ifdef STARPU_USE_OPENCL
  963. maxnode += nopencl;
  964. #endif
  965. #ifdef STARPU_USE_MIC
  966. maxnode += nmic;
  967. #endif
  968. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  969. maxnode += nmpi_ms;
  970. #endif
  971. for (src = 0; src < STARPU_MAXNODES; src++)
  972. {
  973. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  974. {
  975. double latency = 0.0;
  976. if ((src > maxnode) || (dst > maxnode))
  977. {
  978. /* convention */
  979. latency = NAN;
  980. }
  981. else if (src == dst)
  982. {
  983. latency = 0.0;
  984. }
  985. else
  986. {
  987. /* µs */
  988. #ifdef STARPU_USE_CUDA
  989. #ifdef HAVE_CUDA_MEMCPY_PEER
  990. if (src && src < ncuda && dst && dst <= ncuda)
  991. latency = cudadev_latency_dtod[src][dst];
  992. else
  993. #endif
  994. {
  995. if (src && src <= ncuda)
  996. latency += cudadev_latency_dtoh[src];
  997. if (dst && dst <= ncuda)
  998. latency += cudadev_latency_htod[dst];
  999. }
  1000. #endif
  1001. #ifdef STARPU_USE_OPENCL
  1002. if (src > ncuda && src <= ncuda + nopencl)
  1003. latency += opencldev_latency_dtoh[src-ncuda];
  1004. if (dst > ncuda && dst <= ncuda + nopencl)
  1005. latency += opencldev_latency_htod[dst-ncuda];
  1006. #endif
  1007. /* TODO Latency MIC */
  1008. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1009. if (src > ncuda + nopencl + nmic && src <= ncuda + nopencl + nmic + nmpi_ms)
  1010. latency += mpi_latency_device_to_host[src - (ncuda + nopencl + nmic) - 1];
  1011. if (dst > ncuda + nopencl + nmic && dst <= ncuda + nopencl + nmic + nmpi_ms)
  1012. latency += mpi_latency_host_to_device[dst - (ncuda + nopencl + nmic) - 1];
  1013. #endif
  1014. }
  1015. if (dst)
  1016. fputc('\t', f);
  1017. _starpu_write_double(f, "%e", latency);
  1018. }
  1019. fprintf(f, "\n");
  1020. }
  1021. _starpu_fwrunlock(f);
  1022. fclose(f);
  1023. }
  1024. #endif
  1025. static void generate_bus_latency_file(void)
  1026. {
  1027. if (!was_benchmarked)
  1028. benchmark_all_gpu_devices();
  1029. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1030. /* Slaves don't write files */
  1031. if (!_starpu_mpi_common_is_src_node())
  1032. return;
  1033. #endif
  1034. #ifndef STARPU_SIMGRID
  1035. write_bus_latency_file_content();
  1036. #endif
  1037. }
  1038. static void load_bus_latency_file(void)
  1039. {
  1040. int res;
  1041. char path[256];
  1042. get_latency_path(path, sizeof(path));
  1043. res = access(path, F_OK);
  1044. if (res || !load_bus_latency_file_content())
  1045. {
  1046. /* File does not exist yet or is bogus */
  1047. generate_bus_latency_file();
  1048. }
  1049. }
  1050. /*
  1051. * Bandwidth
  1052. */
  1053. static void get_bandwidth_path(char *path, size_t maxlen)
  1054. {
  1055. get_bus_path("bandwidth", path, maxlen);
  1056. }
  1057. static int load_bus_bandwidth_file_content(void)
  1058. {
  1059. int n;
  1060. unsigned src, dst;
  1061. FILE *f;
  1062. double bandwidth;
  1063. char path[256];
  1064. get_bandwidth_path(path, sizeof(path));
  1065. _STARPU_DEBUG("loading bandwidth from %s\n", path);
  1066. f = fopen(path, "r");
  1067. if (!f)
  1068. {
  1069. perror("fopen load_bus_bandwidth_file_content");
  1070. _STARPU_DISP("path '%s'\n", path);
  1071. fflush(stderr);
  1072. STARPU_ABORT();
  1073. }
  1074. _starpu_frdlock(f);
  1075. for (src = 0; src < STARPU_MAXNODES; src++)
  1076. {
  1077. _starpu_drop_comments(f);
  1078. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  1079. {
  1080. n = _starpu_read_double(f, "%le", &bandwidth);
  1081. if (n != 1)
  1082. {
  1083. _STARPU_DISP("Error while reading bandwidth file <%s>. Expected a number\n", path);
  1084. fclose(f);
  1085. return 0;
  1086. }
  1087. n = getc(f);
  1088. if (n == '\n')
  1089. break;
  1090. if (n != '\t')
  1091. {
  1092. _STARPU_DISP("bogus character '%c' (%d) in bandwidth file %s\n", n, n, path);
  1093. fclose(f);
  1094. return 0;
  1095. }
  1096. bandwidth_matrix[src][dst] = bandwidth;
  1097. /* Look out for \t\n */
  1098. n = getc(f);
  1099. if (n == '\n')
  1100. break;
  1101. ungetc(n, f);
  1102. n = '\t';
  1103. }
  1104. /* No more values, take NAN */
  1105. for ( ; dst < STARPU_MAXNODES; dst++)
  1106. bandwidth_matrix[src][dst] = NAN;
  1107. while (n == '\t')
  1108. {
  1109. /* Look out for \t\n */
  1110. n = getc(f);
  1111. if (n == '\n')
  1112. break;
  1113. ungetc(n, f);
  1114. n = _starpu_read_double(f, "%le", &bandwidth);
  1115. if (n && !isnan(bandwidth))
  1116. {
  1117. _STARPU_DISP("Too many nodes in bandwidth file %s for this configuration (%d)\n", path, STARPU_MAXNODES);
  1118. fclose(f);
  1119. return 0;
  1120. }
  1121. n = getc(f);
  1122. }
  1123. if (n != '\n')
  1124. {
  1125. _STARPU_DISP("Bogus character '%c' (%d) in bandwidth file %s\n", n, n, path);
  1126. fclose(f);
  1127. return 0;
  1128. }
  1129. /* Look out for EOF */
  1130. n = getc(f);
  1131. if (n == EOF)
  1132. break;
  1133. ungetc(n, f);
  1134. }
  1135. _starpu_frdunlock(f);
  1136. fclose(f);
  1137. /* No more values, take NAN */
  1138. for ( ; src < STARPU_MAXNODES; src++)
  1139. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  1140. latency_matrix[src][dst] = NAN;
  1141. return 1;
  1142. }
  1143. #ifndef STARPU_SIMGRID
  1144. static void write_bus_bandwidth_file_content(void)
  1145. {
  1146. unsigned src, dst, maxnode;
  1147. FILE *f;
  1148. STARPU_ASSERT(was_benchmarked);
  1149. char path[256];
  1150. get_bandwidth_path(path, sizeof(path));
  1151. _STARPU_DEBUG("writing bandwidth to %s\n", path);
  1152. f = fopen(path, "w+");
  1153. STARPU_ASSERT(f);
  1154. _starpu_fwrlock(f);
  1155. _starpu_fftruncate(f, 0);
  1156. fprintf(f, "# ");
  1157. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  1158. fprintf(f, "to %d\t\t", dst);
  1159. fprintf(f, "\n");
  1160. maxnode = ncuda;
  1161. #ifdef STARPU_USE_OPENCL
  1162. maxnode += nopencl;
  1163. #endif
  1164. #ifdef STARPU_USE_MIC
  1165. maxnode += nmic;
  1166. #endif
  1167. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1168. maxnode += nmpi_ms;
  1169. #endif
  1170. for (src = 0; src < STARPU_MAXNODES; src++)
  1171. {
  1172. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  1173. {
  1174. double bandwidth;
  1175. if ((src > maxnode) || (dst > maxnode))
  1176. {
  1177. bandwidth = NAN;
  1178. }
  1179. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) || defined(STARPU_USE_MIC) || defined(STARPU_USE_MPI_MASTER_SLAVE)
  1180. else if (src != dst)
  1181. {
  1182. double slowness = 0.0;
  1183. /* Total bandwidth is the harmonic mean of bandwidths */
  1184. #ifdef STARPU_USE_CUDA
  1185. #ifdef HAVE_CUDA_MEMCPY_PEER
  1186. if (src && src <= ncuda && dst && dst <= ncuda)
  1187. /* Direct GPU-GPU transfert */
  1188. slowness = cudadev_timing_dtod[src][dst];
  1189. else
  1190. #endif
  1191. {
  1192. if (src && src <= ncuda)
  1193. slowness += cudadev_timing_dtoh[src];
  1194. if (dst && dst <= ncuda)
  1195. slowness += cudadev_timing_htod[dst];
  1196. }
  1197. #endif
  1198. /* TODO: generalize computation */
  1199. #ifdef STARPU_USE_OPENCL
  1200. if (src > ncuda && src <= ncuda + nopencl)
  1201. slowness += opencldev_timing_dtoh[src-ncuda];
  1202. if (dst > ncuda && dst <= ncuda + nopencl)
  1203. slowness += opencldev_timing_htod[dst-ncuda];
  1204. #endif
  1205. #ifdef STARPU_USE_MIC
  1206. if (src > ncuda + nopencl && src <= ncuda + nopencl + nmic)
  1207. slowness += mic_time_device_to_host[src - (ncuda + nopencl)];
  1208. if (dst > ncuda + nopencl && dst <= ncuda + nopencl + nmic)
  1209. slowness += mic_time_host_to_device[dst - (ncuda + nopencl)];
  1210. #endif
  1211. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1212. /* here we have bandwidth */
  1213. if (src > ncuda + nopencl + nmic && src <= ncuda + nopencl + nmic + nmpi_ms)
  1214. slowness += 1.0/mpi_time_device_to_host[src - (ncuda + nopencl + nmic) - 1];
  1215. if (dst > ncuda + nopencl + nmic && dst <= ncuda + nopencl + nmic +nmpi_ms)
  1216. slowness += 1.0/mpi_time_host_to_device[dst - (ncuda + nopencl + nmic) - 1];
  1217. #endif
  1218. bandwidth = 1.0/slowness;
  1219. }
  1220. #endif
  1221. else
  1222. {
  1223. /* convention */
  1224. bandwidth = 0.0;
  1225. }
  1226. if (dst)
  1227. fputc('\t', f);
  1228. _starpu_write_double(f, "%e", bandwidth);
  1229. }
  1230. fprintf(f, "\n");
  1231. }
  1232. _starpu_fwrunlock(f);
  1233. fclose(f);
  1234. }
  1235. #endif /* STARPU_SIMGRID */
  1236. void starpu_bus_print_filenames(FILE *output)
  1237. {
  1238. char bandwidth_path[256];
  1239. char affinity_path[256];
  1240. char latency_path[256];
  1241. get_bandwidth_path(bandwidth_path, sizeof(bandwidth_path));
  1242. get_affinity_path(affinity_path, sizeof(affinity_path));
  1243. get_latency_path(latency_path, sizeof(latency_path));
  1244. fprintf(output, "bandwidth: <%s>\n", bandwidth_path);
  1245. fprintf(output, " affinity: <%s>\n", affinity_path);
  1246. fprintf(output, " latency: <%s>\n", latency_path);
  1247. }
  1248. void starpu_bus_print_bandwidth(FILE *f)
  1249. {
  1250. unsigned src, dst, maxnode;
  1251. maxnode = ncuda;
  1252. #ifdef STARPU_USE_OPENCL
  1253. maxnode += nopencl;
  1254. #endif
  1255. #ifdef STARPU_USE_MIC
  1256. maxnode += nmic;
  1257. #endif
  1258. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1259. maxnode += nmpi_ms;
  1260. #endif
  1261. fprintf(f, "from/to\t");
  1262. fprintf(f, "RAM\t");
  1263. for (dst = 0; dst < ncuda; dst++)
  1264. fprintf(f, "CUDA %d\t", dst);
  1265. for (dst = 0; dst < nopencl; dst++)
  1266. fprintf(f, "OpenCL%d\t", dst);
  1267. for (dst = 0; dst < nmic; dst++)
  1268. fprintf(f, "MIC%d\t", dst);
  1269. for (dst = 0; dst < nmpi_ms; dst++)
  1270. fprintf(f, "MPI_MS%d\t", dst);
  1271. fprintf(f, "\n");
  1272. for (src = 0; src <= maxnode; src++)
  1273. {
  1274. if (!src)
  1275. fprintf(f, "RAM\t");
  1276. else if (src <= ncuda)
  1277. fprintf(f, "CUDA %d\t", src-1);
  1278. else if (src <= ncuda + nopencl)
  1279. fprintf(f, "OpenCL%d\t", src-ncuda-1);
  1280. else if (src <= ncuda + nopencl + nmic)
  1281. fprintf(f, "MIC%d\t", src-ncuda-nopencl-1);
  1282. else
  1283. fprintf(f, "MPI_MS%d\t", src-ncuda-nopencl-nmic-1);
  1284. for (dst = 0; dst <= maxnode; dst++)
  1285. fprintf(f, "%.0f\t", bandwidth_matrix[src][dst]);
  1286. fprintf(f, "\n");
  1287. }
  1288. fprintf(f, "\n");
  1289. for (src = 0; src <= maxnode; src++)
  1290. {
  1291. if (!src)
  1292. fprintf(f, "RAM\t");
  1293. else if (src <= ncuda)
  1294. fprintf(f, "CUDA %d\t", src-1);
  1295. else if (src <= ncuda + nopencl)
  1296. fprintf(f, "OpenCL%d\t", src-ncuda-1);
  1297. else if (src <= ncuda + nopencl + nmic)
  1298. fprintf(f, "MIC%d\t", src-ncuda-nopencl-1);
  1299. else
  1300. fprintf(f, "MPI_MS%d\t", src-ncuda-nopencl-nmic-1);
  1301. for (dst = 0; dst <= maxnode; dst++)
  1302. fprintf(f, "%.0f\t", latency_matrix[src][dst]);
  1303. fprintf(f, "\n");
  1304. }
  1305. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  1306. if (ncuda != 0 || nopencl != 0)
  1307. fprintf(f, "\nGPU\tCPU in preference order (logical index), host-to-device, device-to-host\n");
  1308. for (src = 1; src <= ncuda + nopencl; src++)
  1309. {
  1310. struct dev_timing *timing;
  1311. struct _starpu_machine_config *config = _starpu_get_machine_config();
  1312. unsigned config_ncpus = _starpu_topology_get_nhwcpu(config);
  1313. unsigned cpu;
  1314. #ifdef STARPU_USE_CUDA
  1315. if (src <= ncuda)
  1316. {
  1317. fprintf(f, "CUDA %d\t", src-1);
  1318. for (cpu = 0; cpu < config_ncpus; cpu++)
  1319. {
  1320. timing = &cudadev_timing_per_cpu[src*STARPU_MAXCPUS+cpu];
  1321. if (timing->timing_htod)
  1322. fprintf(f, "%2d %.0f %.0f\t", timing->cpu_id, 1/timing->timing_htod, 1/timing->timing_dtoh);
  1323. else
  1324. fprintf(f, "%2d\t", cuda_affinity_matrix[src-1][cpu]);
  1325. }
  1326. }
  1327. #ifdef STARPU_USE_OPENCL
  1328. else
  1329. #endif
  1330. #endif
  1331. #ifdef STARPU_USE_OPENCL
  1332. {
  1333. fprintf(f, "OpenCL%d\t", src-ncuda-1);
  1334. for (cpu = 0; cpu < config_ncpus; cpu++)
  1335. {
  1336. timing = &opencldev_timing_per_cpu[(src-ncuda)*STARPU_MAXCPUS+cpu];
  1337. if (timing->timing_htod)
  1338. fprintf(f, "%2d %.0f %.0f\t", timing->cpu_id, 1/timing->timing_htod, 1/timing->timing_dtoh);
  1339. else
  1340. fprintf(f, "%2d\t", opencl_affinity_matrix[src-1][cpu]);
  1341. }
  1342. }
  1343. #endif
  1344. fprintf(f, "\n");
  1345. }
  1346. #endif
  1347. }
  1348. static void generate_bus_bandwidth_file(void)
  1349. {
  1350. if (!was_benchmarked)
  1351. benchmark_all_gpu_devices();
  1352. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1353. /* Slaves don't write files */
  1354. if (!_starpu_mpi_common_is_src_node())
  1355. return;
  1356. #endif
  1357. #ifndef STARPU_SIMGRID
  1358. write_bus_bandwidth_file_content();
  1359. #endif
  1360. }
  1361. static void load_bus_bandwidth_file(void)
  1362. {
  1363. int res;
  1364. char path[256];
  1365. get_bandwidth_path(path, sizeof(path));
  1366. res = access(path, F_OK);
  1367. if (res || !load_bus_bandwidth_file_content())
  1368. {
  1369. /* File does not exist yet or is bogus */
  1370. generate_bus_bandwidth_file();
  1371. }
  1372. }
  1373. #ifndef STARPU_SIMGRID
  1374. /*
  1375. * Config
  1376. */
  1377. static void get_config_path(char *path, size_t maxlen)
  1378. {
  1379. get_bus_path("config", path, maxlen);
  1380. }
  1381. static void compare_value_and_recalibrate(char * msg, unsigned val_file, unsigned val_detected)
  1382. {
  1383. if (val_file != val_detected)
  1384. {
  1385. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1386. /* Only the master prints the message */
  1387. if (_starpu_mpi_common_is_src_node())
  1388. #endif
  1389. _STARPU_DISP("Current configuration does not match the bus performance model (%s: (stored) %d != (current) %d), recalibrating...\n", msg, val_file, val_detected);
  1390. _starpu_bus_force_sampling();
  1391. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1392. if (_starpu_mpi_common_is_src_node())
  1393. #endif
  1394. _STARPU_DISP("... done\n");
  1395. }
  1396. }
  1397. static void check_bus_config_file(void)
  1398. {
  1399. int res;
  1400. char path[256];
  1401. struct _starpu_machine_config *config = _starpu_get_machine_config();
  1402. get_config_path(path, sizeof(path));
  1403. res = access(path, F_OK);
  1404. if (res || config->conf.bus_calibrate > 0)
  1405. {
  1406. if (res)
  1407. _STARPU_DISP("No performance model for the bus, calibrating...\n");
  1408. _starpu_bus_force_sampling();
  1409. if (res)
  1410. _STARPU_DISP("... done\n");
  1411. }
  1412. else
  1413. {
  1414. FILE *f;
  1415. int ret;
  1416. unsigned read_cuda = -1, read_opencl = -1, read_mic = -1, read_mpi_ms = -1;
  1417. unsigned read_cpus = -1;
  1418. // Loading configuration from file
  1419. f = fopen(path, "r");
  1420. STARPU_ASSERT(f);
  1421. _starpu_frdlock(f);
  1422. _starpu_drop_comments(f);
  1423. ret = fscanf(f, "%u\t", &read_cpus);
  1424. STARPU_ASSERT(ret == 1);
  1425. _starpu_drop_comments(f);
  1426. ret = fscanf(f, "%d\t", &read_cuda);
  1427. STARPU_ASSERT(ret == 1);
  1428. _starpu_drop_comments(f);
  1429. ret = fscanf(f, "%d\t", &read_opencl);
  1430. STARPU_ASSERT(ret == 1);
  1431. _starpu_drop_comments(f);
  1432. ret = fscanf(f, "%d\t", &read_mic);
  1433. if (ret == 0)
  1434. read_mic = 0;
  1435. _starpu_drop_comments(f);
  1436. ret = fscanf(f, "%d\t", &read_mpi_ms);
  1437. if (ret == 0)
  1438. read_mpi_ms = 0;
  1439. _starpu_drop_comments(f);
  1440. _starpu_frdunlock(f);
  1441. fclose(f);
  1442. // Loading current configuration
  1443. ncpus = _starpu_topology_get_nhwcpu(config);
  1444. #ifdef STARPU_USE_CUDA
  1445. ncuda = _starpu_get_cuda_device_count();
  1446. #endif
  1447. #ifdef STARPU_USE_OPENCL
  1448. nopencl = _starpu_opencl_get_device_count();
  1449. #endif
  1450. #ifdef STARPU_USE_MIC
  1451. nmic = _starpu_mic_src_get_device_count();
  1452. #endif /* STARPU_USE_MIC */
  1453. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1454. nmpi_ms = _starpu_mpi_src_get_device_count();
  1455. #endif /* STARPU_USE_MPI_MASTER_SLAVE */
  1456. // Checking if both configurations match
  1457. compare_value_and_recalibrate("CPUS", read_cpus, ncpus);
  1458. compare_value_and_recalibrate("CUDA", read_cuda, ncuda);
  1459. compare_value_and_recalibrate("OpenCL", read_opencl, nopencl);
  1460. compare_value_and_recalibrate("MIC", read_mic, nmic);
  1461. compare_value_and_recalibrate("MPI Master-Slave", read_mpi_ms, nmpi_ms);
  1462. }
  1463. }
  1464. static void write_bus_config_file_content(void)
  1465. {
  1466. FILE *f;
  1467. char path[256];
  1468. STARPU_ASSERT(was_benchmarked);
  1469. get_config_path(path, sizeof(path));
  1470. _STARPU_DEBUG("writing config to %s\n", path);
  1471. f = fopen(path, "w+");
  1472. STARPU_ASSERT(f);
  1473. _starpu_fwrlock(f);
  1474. _starpu_fftruncate(f, 0);
  1475. fprintf(f, "# Current configuration\n");
  1476. fprintf(f, "%u # Number of CPUs\n", ncpus);
  1477. fprintf(f, "%d # Number of CUDA devices\n", ncuda);
  1478. fprintf(f, "%d # Number of OpenCL devices\n", nopencl);
  1479. fprintf(f, "%d # Number of MIC devices\n", nmic);
  1480. fprintf(f, "%d # Number of MPI devices\n", nmpi_ms);
  1481. _starpu_fwrunlock(f);
  1482. fclose(f);
  1483. }
  1484. static void generate_bus_config_file(void)
  1485. {
  1486. if (!was_benchmarked)
  1487. benchmark_all_gpu_devices();
  1488. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  1489. /* Slaves don't write files */
  1490. if (!_starpu_mpi_common_is_src_node())
  1491. return;
  1492. #endif
  1493. write_bus_config_file_content();
  1494. }
  1495. #endif /* !SIMGRID */
  1496. void _starpu_simgrid_get_platform_path(int version, char *path, size_t maxlen)
  1497. {
  1498. if (version == 3)
  1499. get_bus_path("platform.xml", path, maxlen);
  1500. else
  1501. get_bus_path("platform.v4.xml", path, maxlen);
  1502. }
  1503. #ifndef STARPU_SIMGRID
  1504. /*
  1505. * Compute the precise PCI tree bandwidth and link shares
  1506. *
  1507. * We only have measurements from one leaf to another. We assume that the
  1508. * available bandwidth is greater at lower levels, and thus measurements from
  1509. * increasingly far GPUs provide the PCI bridges bandwidths at each level.
  1510. *
  1511. * The bandwidth of a PCI bridge is thus computed as the maximum of the speed
  1512. * of the various transfers that we have achieved through it. We thus browse
  1513. * the PCI tree three times:
  1514. *
  1515. * - first through all CUDA-CUDA possible transfers to compute the maximum
  1516. * measured bandwidth on each PCI link and hub used for that.
  1517. * - then through the whole tree to emit links for each PCI link and hub.
  1518. * - then through all CUDA-CUDA possible transfers again to emit routes.
  1519. */
  1520. #if defined(STARPU_USE_CUDA) && defined(HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX) && HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX && defined(HAVE_CUDA_MEMCPY_PEER)
  1521. /* Records, for each PCI link and hub, the maximum bandwidth seen through it */
  1522. struct pci_userdata
  1523. {
  1524. /* Uplink max measurement */
  1525. double bw_up;
  1526. double bw_down;
  1527. /* Hub max measurement */
  1528. double bw;
  1529. };
  1530. /* Allocate a pci_userdata structure for the given object */
  1531. static void allocate_userdata(hwloc_obj_t obj)
  1532. {
  1533. struct pci_userdata *data;
  1534. if (obj->userdata)
  1535. return;
  1536. data = obj->userdata = malloc(sizeof(*data));
  1537. data->bw_up = 0.0;
  1538. data->bw_down = 0.0;
  1539. data->bw = 0.0;
  1540. }
  1541. /* Update the maximum bandwidth seen going to upstream */
  1542. static void update_bandwidth_up(hwloc_obj_t obj, double bandwidth)
  1543. {
  1544. struct pci_userdata *data;
  1545. if (obj->type != HWLOC_OBJ_BRIDGE && obj->type != HWLOC_OBJ_PCI_DEVICE)
  1546. return;
  1547. allocate_userdata(obj);
  1548. data = obj->userdata;
  1549. if (data->bw_up < bandwidth)
  1550. data->bw_up = bandwidth;
  1551. }
  1552. /* Update the maximum bandwidth seen going from upstream */
  1553. static void update_bandwidth_down(hwloc_obj_t obj, double bandwidth)
  1554. {
  1555. struct pci_userdata *data;
  1556. if (obj->type != HWLOC_OBJ_BRIDGE && obj->type != HWLOC_OBJ_PCI_DEVICE)
  1557. return;
  1558. allocate_userdata(obj);
  1559. data = obj->userdata;
  1560. if (data->bw_down < bandwidth)
  1561. data->bw_down = bandwidth;
  1562. }
  1563. /* Update the maximum bandwidth seen going through this Hub */
  1564. static void update_bandwidth_through(hwloc_obj_t obj, double bandwidth)
  1565. {
  1566. struct pci_userdata *data;
  1567. allocate_userdata(obj);
  1568. data = obj->userdata;
  1569. if (data->bw < bandwidth)
  1570. data->bw = bandwidth;
  1571. }
  1572. /* find_* functions perform the first step: computing maximum bandwidths */
  1573. /* Our trafic had to go through the host, go back from target up to the host,
  1574. * updating uplink downstream bandwidth along the way */
  1575. static void find_platform_backward_path(hwloc_obj_t obj, double bandwidth)
  1576. {
  1577. if (!obj)
  1578. /* Oops, we should have seen a host bridge. Well, too bad. */
  1579. return;
  1580. /* Update uplink bandwidth of PCI Hub */
  1581. update_bandwidth_down(obj, bandwidth);
  1582. /* Update internal bandwidth of PCI Hub */
  1583. update_bandwidth_through(obj, bandwidth);
  1584. if (obj->type == HWLOC_OBJ_BRIDGE && obj->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST)
  1585. /* Finished */
  1586. return;
  1587. /* Continue up */
  1588. find_platform_backward_path(obj->parent, bandwidth);
  1589. }
  1590. /* Same, but update uplink upstream bandwidth */
  1591. static void find_platform_forward_path(hwloc_obj_t obj, double bandwidth)
  1592. {
  1593. if (!obj)
  1594. /* Oops, we should have seen a host bridge. Well, too bad. */
  1595. return;
  1596. /* Update uplink bandwidth of PCI Hub */
  1597. update_bandwidth_up(obj, bandwidth);
  1598. /* Update internal bandwidth of PCI Hub */
  1599. update_bandwidth_through(obj, bandwidth);
  1600. if (obj->type == HWLOC_OBJ_BRIDGE && obj->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST)
  1601. /* Finished */
  1602. return;
  1603. /* Continue up */
  1604. find_platform_forward_path(obj->parent, bandwidth);
  1605. }
  1606. /* Find the path from obj1 through parent down to obj2 (without ever going up),
  1607. * and update the maximum bandwidth along the path */
  1608. static int find_platform_path_down(hwloc_obj_t parent, hwloc_obj_t obj1, hwloc_obj_t obj2, double bandwidth)
  1609. {
  1610. unsigned i;
  1611. /* Base case, path is empty */
  1612. if (parent == obj2)
  1613. return 1;
  1614. /* Try to go down from parent */
  1615. for (i = 0; i < parent->arity; i++)
  1616. if (parent->children[i] != obj1 && find_platform_path_down(parent->children[i], NULL, obj2, bandwidth))
  1617. {
  1618. /* Found it down there, update bandwidth of parent */
  1619. update_bandwidth_down(parent->children[i], bandwidth);
  1620. update_bandwidth_through(parent, bandwidth);
  1621. return 1;
  1622. }
  1623. return 0;
  1624. }
  1625. /* Find the path from obj1 to obj2, and update the maximum bandwidth along the
  1626. * path */
  1627. static int find_platform_path_up(hwloc_obj_t obj1, hwloc_obj_t obj2, double bandwidth)
  1628. {
  1629. int ret;
  1630. hwloc_obj_t parent = obj1->parent;
  1631. if (!parent)
  1632. {
  1633. /* Oops, we should have seen a host bridge. Act as if we had seen it. */
  1634. find_platform_backward_path(obj2, bandwidth);
  1635. return 1;
  1636. }
  1637. if (find_platform_path_down(parent, obj1, obj2, bandwidth))
  1638. /* obj2 was a mere (sub)child of our parent */
  1639. return 1;
  1640. /* obj2 is not a (sub)child of our parent, we have to go up through the parent */
  1641. if (parent->type == HWLOC_OBJ_BRIDGE && parent->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST)
  1642. {
  1643. /* We have to go up to the Host, so obj2 is not in the same PCI
  1644. * tree, so we're for for obj1 to Host, and just find the path
  1645. * from obj2 to Host too.
  1646. */
  1647. find_platform_backward_path(obj2, bandwidth);
  1648. update_bandwidth_up(parent, bandwidth);
  1649. update_bandwidth_through(parent, bandwidth);
  1650. return 1;
  1651. }
  1652. /* Not at host yet, just go up */
  1653. ret = find_platform_path_up(parent, obj2, bandwidth);
  1654. update_bandwidth_up(parent, bandwidth);
  1655. update_bandwidth_through(parent, bandwidth);
  1656. return ret;
  1657. }
  1658. /* find the path between cuda i and cuda j, and update the maximum bandwidth along the path */
  1659. static int find_platform_cuda_path(hwloc_topology_t topology, unsigned i, unsigned j, double bandwidth)
  1660. {
  1661. hwloc_obj_t cudai, cudaj;
  1662. cudai = hwloc_cuda_get_device_osdev_by_index(topology, i);
  1663. cudaj = hwloc_cuda_get_device_osdev_by_index(topology, j);
  1664. if (!cudai || !cudaj)
  1665. return 0;
  1666. return find_platform_path_up(cudai, cudaj, bandwidth);
  1667. }
  1668. /* emit_topology_bandwidths performs the second step: emitting link names */
  1669. /* Emit the link name of the object */
  1670. static void emit_pci_hub(FILE *f, hwloc_obj_t obj)
  1671. {
  1672. STARPU_ASSERT(obj->type == HWLOC_OBJ_BRIDGE);
  1673. fprintf(f, "PCI:%04x:[%02x-%02x]", obj->attr->bridge.downstream.pci.domain, obj->attr->bridge.downstream.pci.secondary_bus, obj->attr->bridge.downstream.pci.subordinate_bus);
  1674. }
  1675. static void emit_pci_dev(FILE *f, struct hwloc_pcidev_attr_s *pcidev)
  1676. {
  1677. fprintf(f, "PCI:%04x:%02x:%02x.%1x", pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
  1678. }
  1679. /* Emit the links of the object */
  1680. static void emit_topology_bandwidths(FILE *f, hwloc_obj_t obj, const char *Bps, const char *s)
  1681. {
  1682. unsigned i;
  1683. if (obj->userdata)
  1684. {
  1685. struct pci_userdata *data = obj->userdata;
  1686. if (obj->type == HWLOC_OBJ_BRIDGE)
  1687. {
  1688. /* Uplink */
  1689. fprintf(f, " <link id=\"");
  1690. emit_pci_hub(f, obj);
  1691. fprintf(f, " up\" bandwidth=\"%f%s\" latency=\"0.000000%s\"/>\n", data->bw_up, Bps, s);
  1692. fprintf(f, " <link id=\"");
  1693. emit_pci_hub(f, obj);
  1694. fprintf(f, " down\" bandwidth=\"%f%s\" latency=\"0.000000%s\"/>\n", data->bw_down, Bps, s);
  1695. /* PCI Switches are assumed to have infinite internal bandwidth */
  1696. if (!obj->name || !strstr(obj->name, "Switch"))
  1697. {
  1698. /* We assume that PCI Hubs have double bandwidth in
  1699. * order to support full duplex but not more */
  1700. fprintf(f, " <link id=\"");
  1701. emit_pci_hub(f, obj);
  1702. fprintf(f, " through\" bandwidth=\"%f%s\" latency=\"0.000000%s\"/>\n", data->bw * 2, Bps, s);
  1703. }
  1704. }
  1705. else if (obj->type == HWLOC_OBJ_PCI_DEVICE)
  1706. {
  1707. fprintf(f, " <link id=\"");
  1708. emit_pci_dev(f, &obj->attr->pcidev);
  1709. fprintf(f, " up\" bandwidth=\"%f%s\" latency=\"0.000000%s\"/>\n", data->bw_up, Bps, s);
  1710. fprintf(f, " <link id=\"");
  1711. emit_pci_dev(f, &obj->attr->pcidev);
  1712. fprintf(f, " down\" bandwidth=\"%f%s\" latency=\"0.000000%s\"/>\n", data->bw_down, Bps, s);
  1713. }
  1714. }
  1715. for (i = 0; i < obj->arity; i++)
  1716. emit_topology_bandwidths(f, obj->children[i], Bps, s);
  1717. }
  1718. /* emit_pci_link_* functions perform the third step: emitting the routes */
  1719. static void emit_pci_link(FILE *f, hwloc_obj_t obj, const char *suffix)
  1720. {
  1721. if (obj->type == HWLOC_OBJ_BRIDGE)
  1722. {
  1723. fprintf(f, " <link_ctn id=\"");
  1724. emit_pci_hub(f, obj);
  1725. fprintf(f, " %s\"/>\n", suffix);
  1726. }
  1727. else if (obj->type == HWLOC_OBJ_PCI_DEVICE)
  1728. {
  1729. fprintf(f, " <link_ctn id=\"");
  1730. emit_pci_dev(f, &obj->attr->pcidev);
  1731. fprintf(f, " %s\"/>\n", suffix);
  1732. }
  1733. }
  1734. /* Go to upstream */
  1735. static void emit_pci_link_up(FILE *f, hwloc_obj_t obj)
  1736. {
  1737. emit_pci_link(f, obj, "up");
  1738. }
  1739. /* Go from upstream */
  1740. static void emit_pci_link_down(FILE *f, hwloc_obj_t obj)
  1741. {
  1742. emit_pci_link(f, obj, "down");
  1743. }
  1744. /* Go through PCI hub */
  1745. static void emit_pci_link_through(FILE *f, hwloc_obj_t obj)
  1746. {
  1747. /* We don't care about trafic going through PCI switches */
  1748. if (obj->type == HWLOC_OBJ_BRIDGE)
  1749. {
  1750. if (!obj->name || !strstr(obj->name, "Switch"))
  1751. emit_pci_link(f, obj, "through");
  1752. else
  1753. {
  1754. fprintf(f, " <!-- Switch ");
  1755. emit_pci_hub(f, obj);
  1756. fprintf(f, " through -->\n");
  1757. }
  1758. }
  1759. }
  1760. /* Our trafic has to go through the host, go back from target up to the host,
  1761. * using uplink downstream along the way */
  1762. static void emit_platform_backward_path(FILE *f, hwloc_obj_t obj)
  1763. {
  1764. if (!obj)
  1765. /* Oops, we should have seen a host bridge. Well, too bad. */
  1766. return;
  1767. /* Go through PCI Hub */
  1768. emit_pci_link_through(f, obj);
  1769. /* Go through uplink */
  1770. emit_pci_link_down(f, obj);
  1771. if (obj->type == HWLOC_OBJ_BRIDGE && obj->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST)
  1772. {
  1773. /* Finished, go through host */
  1774. fprintf(f, " <link_ctn id=\"Host\"/>\n");
  1775. return;
  1776. }
  1777. /* Continue up */
  1778. emit_platform_backward_path(f, obj->parent);
  1779. }
  1780. /* Same, but use upstream link */
  1781. static void emit_platform_forward_path(FILE *f, hwloc_obj_t obj)
  1782. {
  1783. if (!obj)
  1784. /* Oops, we should have seen a host bridge. Well, too bad. */
  1785. return;
  1786. /* Go through PCI Hub */
  1787. emit_pci_link_through(f, obj);
  1788. /* Go through uplink */
  1789. emit_pci_link_up(f, obj);
  1790. if (obj->type == HWLOC_OBJ_BRIDGE && obj->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST)
  1791. {
  1792. /* Finished, go through host */
  1793. fprintf(f, " <link_ctn id=\"Host\"/>\n");
  1794. return;
  1795. }
  1796. /* Continue up */
  1797. emit_platform_forward_path(f, obj->parent);
  1798. }
  1799. /* Find the path from obj1 through parent down to obj2 (without ever going up),
  1800. * and use the links along the path */
  1801. static int emit_platform_path_down(FILE *f, hwloc_obj_t parent, hwloc_obj_t obj1, hwloc_obj_t obj2)
  1802. {
  1803. unsigned i;
  1804. /* Base case, path is empty */
  1805. if (parent == obj2)
  1806. return 1;
  1807. /* Try to go down from parent */
  1808. for (i = 0; i < parent->arity; i++)
  1809. if (parent->children[i] != obj1 && emit_platform_path_down(f, parent->children[i], NULL, obj2))
  1810. {
  1811. /* Found it down there, path goes through this hub */
  1812. emit_pci_link_down(f, parent->children[i]);
  1813. emit_pci_link_through(f, parent);
  1814. return 1;
  1815. }
  1816. return 0;
  1817. }
  1818. /* Find the path from obj1 to obj2, and use the links along the path */
  1819. static int emit_platform_path_up(FILE *f, hwloc_obj_t obj1, hwloc_obj_t obj2)
  1820. {
  1821. int ret;
  1822. hwloc_obj_t parent = obj1->parent;
  1823. if (!parent)
  1824. {
  1825. /* Oops, we should have seen a host bridge. Act as if we had seen it. */
  1826. emit_platform_backward_path(f, obj2);
  1827. return 1;
  1828. }
  1829. if (emit_platform_path_down(f, parent, obj1, obj2))
  1830. /* obj2 was a mere (sub)child of our parent */
  1831. return 1;
  1832. /* obj2 is not a (sub)child of our parent, we have to go up through the parent */
  1833. if (parent->type == HWLOC_OBJ_BRIDGE && parent->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST)
  1834. {
  1835. /* We have to go up to the Host, so obj2 is not in the same PCI
  1836. * tree, so we're for for obj1 to Host, and just find the path
  1837. * from obj2 to Host too.
  1838. */
  1839. emit_platform_backward_path(f, obj2);
  1840. fprintf(f, " <link_ctn id=\"Host\"/>\n");
  1841. emit_pci_link_up(f, parent);
  1842. emit_pci_link_through(f, parent);
  1843. return 1;
  1844. }
  1845. /* Not at host yet, just go up */
  1846. ret = emit_platform_path_up(f, parent, obj2);
  1847. emit_pci_link_up(f, parent);
  1848. emit_pci_link_through(f, parent);
  1849. return ret;
  1850. }
  1851. /* Clean our mess in the topology before destroying it */
  1852. static void clean_topology(hwloc_obj_t obj)
  1853. {
  1854. unsigned i;
  1855. if (obj->userdata)
  1856. free(obj->userdata);
  1857. for (i = 0; i < obj->arity; i++)
  1858. clean_topology(obj->children[i]);
  1859. }
  1860. #endif
  1861. static void write_bus_platform_file_content(int version)
  1862. {
  1863. FILE *f;
  1864. char path[256];
  1865. unsigned i;
  1866. const char *speed, *flops, *Bps, *s;
  1867. char dash;
  1868. if (version == 3)
  1869. {
  1870. speed = "power";
  1871. flops = "";
  1872. Bps = "";
  1873. s = "";
  1874. dash = '_';
  1875. }
  1876. else
  1877. {
  1878. speed = "speed";
  1879. flops = "f";
  1880. Bps = "Bps";
  1881. s = "s";
  1882. dash = '-';
  1883. }
  1884. STARPU_ASSERT(was_benchmarked);
  1885. _starpu_simgrid_get_platform_path(version, path, sizeof(path));
  1886. _STARPU_DEBUG("writing platform to %s\n", path);
  1887. f = fopen(path, "w+");
  1888. if (!f)
  1889. {
  1890. perror("fopen write_bus_platform_file_content");
  1891. _STARPU_DISP("path '%s'\n", path);
  1892. fflush(stderr);
  1893. STARPU_ABORT();
  1894. }
  1895. _starpu_fwrlock(f);
  1896. _starpu_fftruncate(f, 0);
  1897. fprintf(f,
  1898. "<?xml version='1.0'?>\n"
  1899. "<!DOCTYPE platform SYSTEM '%s'>\n"
  1900. " <platform version=\"%u\">\n"
  1901. " <config id=\"General\">\n"
  1902. " <prop id=\"network/TCP%cgamma\" value=\"-1\"></prop>\n"
  1903. " <prop id=\"network/latency%cfactor\" value=\"1\"></prop>\n"
  1904. " <prop id=\"network/bandwidth%cfactor\" value=\"1\"></prop>\n"
  1905. " </config>\n"
  1906. " <AS id=\"AS0\" routing=\"Full\">\n"
  1907. " <host id=\"MAIN\" %s=\"1%s\"/>\n",
  1908. version == 3
  1909. ? "http://simgrid.gforge.inria.fr/simgrid.dtd"
  1910. : "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd",
  1911. version, dash, dash, dash, speed, flops);
  1912. for (i = 0; i < ncpus; i++)
  1913. /* TODO: host memory for out-of-core simulation */
  1914. fprintf(f, " <host id=\"CPU%d\" %s=\"2000000000%s\"/>\n", i, speed, flops);
  1915. for (i = 0; i < ncuda; i++)
  1916. {
  1917. fprintf(f, " <host id=\"CUDA%d\" %s=\"2000000000%s\">\n", i, speed, flops);
  1918. fprintf(f, " <prop id=\"memsize\" value=\"%llu\"/>\n", (unsigned long long) cuda_size[i]);
  1919. #ifdef HAVE_CUDA_MEMCPY_PEER
  1920. fprintf(f, " <prop id=\"memcpy_peer\" value=\"1\"/>\n");
  1921. #endif
  1922. fprintf(f, " </host>\n");
  1923. }
  1924. for (i = 0; i < nopencl; i++)
  1925. {
  1926. fprintf(f, " <host id=\"OpenCL%d\" %s=\"2000000000%s\">\n", i, speed, flops);
  1927. fprintf(f, " <prop id=\"memsize\" value=\"%llu\"/>\n", (unsigned long long) opencl_size[i]);
  1928. fprintf(f, " </host>\n");
  1929. }
  1930. fprintf(f, "\n <host id=\"RAM\" %s=\"1%s\"/>\n", speed, flops);
  1931. /*
  1932. * Compute maximum bandwidth, taken as host bandwidth
  1933. */
  1934. double max_bandwidth = 0;
  1935. #ifdef STARPU_USE_CUDA
  1936. for (i = 0; i < ncuda; i++)
  1937. {
  1938. double down_bw = 1.0 / cudadev_timing_dtoh[1+i];
  1939. double up_bw = 1.0 / cudadev_timing_htod[1+i];
  1940. if (max_bandwidth < down_bw)
  1941. max_bandwidth = down_bw;
  1942. if (max_bandwidth < up_bw)
  1943. max_bandwidth = up_bw;
  1944. }
  1945. #endif
  1946. #ifdef STARPU_USE_OPENCL
  1947. for (i = 0; i < nopencl; i++)
  1948. {
  1949. double down_bw = 1.0 / opencldev_timing_dtoh[1+i];
  1950. double up_bw = 1.0 / opencldev_timing_htod[1+i];
  1951. if (max_bandwidth < down_bw)
  1952. max_bandwidth = down_bw;
  1953. if (max_bandwidth < up_bw)
  1954. max_bandwidth = up_bw;
  1955. }
  1956. #endif
  1957. fprintf(f, "\n <link id=\"Host\" bandwidth=\"%f%s\" latency=\"0.000000%s\"/>\n\n", max_bandwidth*1000000, Bps, s);
  1958. /*
  1959. * OpenCL links
  1960. */
  1961. #ifdef STARPU_USE_OPENCL
  1962. for (i = 0; i < nopencl; i++)
  1963. {
  1964. char i_name[16];
  1965. snprintf(i_name, sizeof(i_name), "OpenCL%d", i);
  1966. fprintf(f, " <link id=\"RAM-%s\" bandwidth=\"%f%s\" latency=\"%f%s\"/>\n",
  1967. i_name,
  1968. 1000000 / opencldev_timing_htod[1+i], Bps,
  1969. opencldev_latency_htod[1+i]/1000000., s);
  1970. fprintf(f, " <link id=\"%s-RAM\" bandwidth=\"%f%s\" latency=\"%f%s\"/>\n",
  1971. i_name,
  1972. 1000000 / opencldev_timing_dtoh[1+i], Bps,
  1973. opencldev_latency_dtoh[1+i]/1000000., s);
  1974. }
  1975. fprintf(f, "\n");
  1976. #endif
  1977. /*
  1978. * CUDA links and routes
  1979. */
  1980. #ifdef STARPU_USE_CUDA
  1981. /* Write RAM/CUDA bandwidths and latencies */
  1982. for (i = 0; i < ncuda; i++)
  1983. {
  1984. char i_name[16];
  1985. snprintf(i_name, sizeof(i_name), "CUDA%d", i);
  1986. fprintf(f, " <link id=\"RAM-%s\" bandwidth=\"%f%s\" latency=\"%f%s\"/>\n",
  1987. i_name,
  1988. 1000000. / cudadev_timing_htod[1+i], Bps,
  1989. cudadev_latency_htod[1+i]/1000000., s);
  1990. fprintf(f, " <link id=\"%s-RAM\" bandwidth=\"%f%s\" latency=\"%f%s\"/>\n",
  1991. i_name,
  1992. 1000000. / cudadev_timing_dtoh[1+i], Bps,
  1993. cudadev_latency_dtoh[1+i]/1000000., s);
  1994. }
  1995. fprintf(f, "\n");
  1996. #ifdef HAVE_CUDA_MEMCPY_PEER
  1997. /* Write CUDA/CUDA bandwidths and latencies */
  1998. for (i = 0; i < ncuda; i++)
  1999. {
  2000. unsigned j;
  2001. char i_name[16];
  2002. snprintf(i_name, sizeof(i_name), "CUDA%d", i);
  2003. for (j = 0; j < ncuda; j++)
  2004. {
  2005. char j_name[16];
  2006. if (j == i)
  2007. continue;
  2008. snprintf(j_name, sizeof(j_name), "CUDA%d", j);
  2009. fprintf(f, " <link id=\"%s-%s\" bandwidth=\"%f%s\" latency=\"%f%s\"/>\n",
  2010. i_name, j_name,
  2011. 1000000. / cudadev_timing_dtod[1+i][1+j], Bps,
  2012. cudadev_latency_dtod[1+i][1+j]/1000000., s);
  2013. }
  2014. }
  2015. #endif
  2016. #if defined(HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX) && HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX && defined(HAVE_CUDA_MEMCPY_PEER)
  2017. /* If we have enough hwloc information, write PCI bandwidths and routes */
  2018. if (!starpu_get_env_number_default("STARPU_PCI_FLAT", 0))
  2019. {
  2020. hwloc_topology_t topology;
  2021. hwloc_topology_init(&topology);
  2022. hwloc_topology_set_flags(topology, HWLOC_TOPOLOGY_FLAG_IO_DEVICES | HWLOC_TOPOLOGY_FLAG_IO_BRIDGES);
  2023. hwloc_topology_load(topology);
  2024. /* First find paths and record measured bandwidth along the path */
  2025. for (i = 0; i < ncuda; i++)
  2026. {
  2027. unsigned j;
  2028. for (j = 0; j < ncuda; j++)
  2029. if (i != j)
  2030. if (!find_platform_cuda_path(topology, i, j, 1000000. / cudadev_timing_dtod[1+i][1+j]))
  2031. {
  2032. clean_topology(hwloc_get_root_obj(topology));
  2033. hwloc_topology_destroy(topology);
  2034. goto flat_cuda;
  2035. }
  2036. /* Record RAM/CUDA bandwidths */
  2037. find_platform_forward_path(hwloc_cuda_get_device_osdev_by_index(topology, i), 1000000. / cudadev_timing_dtoh[1+i]);
  2038. find_platform_backward_path(hwloc_cuda_get_device_osdev_by_index(topology, i), 1000000. / cudadev_timing_htod[1+i]);
  2039. }
  2040. /* Ok, found path in all cases, can emit advanced platform routes */
  2041. fprintf(f, "\n");
  2042. emit_topology_bandwidths(f, hwloc_get_root_obj(topology), Bps, s);
  2043. fprintf(f, "\n");
  2044. for (i = 0; i < ncuda; i++)
  2045. {
  2046. unsigned j;
  2047. for (j = 0; j < ncuda; j++)
  2048. if (i != j)
  2049. {
  2050. fprintf(f, " <route src=\"CUDA%u\" dst=\"CUDA%u\" symmetrical=\"NO\">\n", i, j);
  2051. fprintf(f, " <link_ctn id=\"CUDA%d-CUDA%d\"/>\n", i, j);
  2052. emit_platform_path_up(f,
  2053. hwloc_cuda_get_device_osdev_by_index(topology, i),
  2054. hwloc_cuda_get_device_osdev_by_index(topology, j));
  2055. fprintf(f, " </route>\n");
  2056. }
  2057. fprintf(f, " <route src=\"CUDA%d\" dst=\"RAM\" symmetrical=\"NO\">\n", i);
  2058. fprintf(f, " <link_ctn id=\"CUDA%d-RAM\"/>\n", i);
  2059. emit_platform_forward_path(f, hwloc_cuda_get_device_osdev_by_index(topology, i));
  2060. fprintf(f, " </route>\n");
  2061. fprintf(f, " <route src=\"RAM\" dst=\"CUDA%d\" symmetrical=\"NO\">\n", i);
  2062. fprintf(f, " <link_ctn id=\"RAM-CUDA%d\"/>\n", i);
  2063. emit_platform_backward_path(f, hwloc_cuda_get_device_osdev_by_index(topology, i));
  2064. fprintf(f, " </route>\n");
  2065. }
  2066. clean_topology(hwloc_get_root_obj(topology));
  2067. hwloc_topology_destroy(topology);
  2068. }
  2069. else
  2070. {
  2071. flat_cuda:
  2072. #else
  2073. {
  2074. #endif
  2075. /* If we don't have enough hwloc information, write trivial routes always through host */
  2076. for (i = 0; i < ncuda; i++)
  2077. {
  2078. char i_name[16];
  2079. snprintf(i_name, sizeof(i_name), "CUDA%d", i);
  2080. fprintf(f, " <route src=\"RAM\" dst=\"%s\" symmetrical=\"NO\"><link_ctn id=\"RAM-%s\"/><link_ctn id=\"Host\"/></route>\n", i_name, i_name);
  2081. fprintf(f, " <route src=\"%s\" dst=\"RAM\" symmetrical=\"NO\"><link_ctn id=\"%s-RAM\"/><link_ctn id=\"Host\"/></route>\n", i_name, i_name);
  2082. }
  2083. #ifdef HAVE_CUDA_MEMCPY_PEER
  2084. for (i = 0; i < ncuda; i++)
  2085. {
  2086. unsigned j;
  2087. char i_name[16];
  2088. snprintf(i_name, sizeof(i_name), "CUDA%d", i);
  2089. for (j = 0; j < ncuda; j++)
  2090. {
  2091. char j_name[16];
  2092. if (j == i)
  2093. continue;
  2094. snprintf(j_name, sizeof(j_name), "CUDA%d", j);
  2095. fprintf(f, " <route src=\"%s\" dst=\"%s\" symmetrical=\"NO\"><link_ctn id=\"%s-%s\"/><link_ctn id=\"Host\"/></route>\n", i_name, j_name, i_name, j_name);
  2096. }
  2097. }
  2098. #endif
  2099. } /* defined(STARPU_HAVE_HWLOC) && defined(HAVE_CUDA_MEMCPY_PEER) */
  2100. fprintf(f, "\n");
  2101. #endif /* STARPU_USE_CUDA */
  2102. /*
  2103. * OpenCL routes
  2104. */
  2105. #ifdef STARPU_USE_OPENCL
  2106. for (i = 0; i < nopencl; i++)
  2107. {
  2108. char i_name[16];
  2109. snprintf(i_name, sizeof(i_name), "OpenCL%d", i);
  2110. fprintf(f, " <route src=\"RAM\" dst=\"%s\" symmetrical=\"NO\"><link_ctn id=\"RAM-%s\"/><link_ctn id=\"Host\"/></route>\n", i_name, i_name);
  2111. fprintf(f, " <route src=\"%s\" dst=\"RAM\" symmetrical=\"NO\"><link_ctn id=\"%s-RAM\"/><link_ctn id=\"Host\"/></route>\n", i_name, i_name);
  2112. }
  2113. #endif
  2114. fprintf(f,
  2115. " </AS>\n"
  2116. " </platform>\n"
  2117. );
  2118. _starpu_fwrunlock(f);
  2119. fclose(f);
  2120. }
  2121. static void generate_bus_platform_file(void)
  2122. {
  2123. if (!was_benchmarked)
  2124. benchmark_all_gpu_devices();
  2125. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  2126. /* Slaves don't write files */
  2127. if (!_starpu_mpi_common_is_src_node())
  2128. return;
  2129. #endif
  2130. write_bus_platform_file_content(3);
  2131. write_bus_platform_file_content(4);
  2132. }
  2133. static void check_bus_platform_file(void)
  2134. {
  2135. int res;
  2136. char path[256];
  2137. _starpu_simgrid_get_platform_path(4, path, sizeof(path));
  2138. res = access(path, F_OK);
  2139. if (!res)
  2140. {
  2141. _starpu_simgrid_get_platform_path(3, path, sizeof(path));
  2142. res = access(path, F_OK);
  2143. }
  2144. if (res)
  2145. {
  2146. /* File does not exist yet */
  2147. generate_bus_platform_file();
  2148. }
  2149. }
  2150. /*
  2151. * Generic
  2152. */
  2153. static void _starpu_bus_force_sampling(void)
  2154. {
  2155. _STARPU_DEBUG("Force bus sampling ...\n");
  2156. _starpu_create_sampling_directory_if_needed();
  2157. generate_bus_affinity_file();
  2158. generate_bus_latency_file();
  2159. generate_bus_bandwidth_file();
  2160. generate_bus_config_file();
  2161. generate_bus_platform_file();
  2162. }
  2163. #endif /* !SIMGRID */
  2164. void _starpu_load_bus_performance_files(void)
  2165. {
  2166. _starpu_create_sampling_directory_if_needed();
  2167. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_SIMGRID)
  2168. ncuda = _starpu_get_cuda_device_count();
  2169. #endif
  2170. #if defined(STARPU_USE_OPENCL) || defined(STARPU_USE_SIMGRID)
  2171. nopencl = _starpu_opencl_get_device_count();
  2172. #endif
  2173. #if defined(STARPU_USE_MPI_MASTER_SLAVE) || defined(STARPU_USE_SIMGRID)
  2174. nmpi_ms = _starpu_mpi_src_get_device_count();
  2175. #endif
  2176. #ifndef STARPU_SIMGRID
  2177. check_bus_config_file();
  2178. #endif
  2179. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  2180. /* be sure that master wrote the perf files */
  2181. _starpu_mpi_common_barrier();
  2182. #endif
  2183. #ifndef STARPU_SIMGRID
  2184. load_bus_affinity_file();
  2185. #endif
  2186. load_bus_latency_file();
  2187. load_bus_bandwidth_file();
  2188. #ifndef STARPU_SIMGRID
  2189. check_bus_platform_file();
  2190. #endif
  2191. }
  2192. /* (in MB/s) */
  2193. double starpu_transfer_bandwidth(unsigned src_node, unsigned dst_node)
  2194. {
  2195. return bandwidth_matrix[src_node][dst_node];
  2196. }
  2197. /* (in µs) */
  2198. double starpu_transfer_latency(unsigned src_node, unsigned dst_node)
  2199. {
  2200. return latency_matrix[src_node][dst_node];
  2201. }
  2202. /* (in µs) */
  2203. double starpu_transfer_predict(unsigned src_node, unsigned dst_node, size_t size)
  2204. {
  2205. double bandwidth = bandwidth_matrix[src_node][dst_node];
  2206. double latency = latency_matrix[src_node][dst_node];
  2207. struct _starpu_machine_topology *topology = &_starpu_get_machine_config()->topology;
  2208. return latency + (size/bandwidth)*2*(topology->ncudagpus+topology->nopenclgpus);
  2209. }
  2210. /* calculate save bandwidth and latency */
  2211. /* bandwidth in MB/s - latency in µs */
  2212. void _starpu_save_bandwidth_and_latency_disk(double bandwidth_write, double bandwidth_read, double latency_write, double latency_read, unsigned node)
  2213. {
  2214. unsigned int i, j;
  2215. double slowness_disk_between_main_ram, slowness_main_ram_between_node;
  2216. /* save bandwith */
  2217. for(i = 0; i < STARPU_MAXNODES; ++i)
  2218. {
  2219. for(j = 0; j < STARPU_MAXNODES; ++j)
  2220. {
  2221. if (i == j && j == node) /* source == destination == node */
  2222. {
  2223. bandwidth_matrix[i][j] = 0;
  2224. }
  2225. else if (i == node) /* source == disk */
  2226. {
  2227. /* convert in slowness */
  2228. if(bandwidth_read != 0)
  2229. slowness_disk_between_main_ram = 1/bandwidth_read;
  2230. else
  2231. slowness_disk_between_main_ram = 0;
  2232. if(bandwidth_matrix[STARPU_MAIN_RAM][j] != 0)
  2233. slowness_main_ram_between_node = 1/bandwidth_matrix[STARPU_MAIN_RAM][j];
  2234. else
  2235. slowness_main_ram_between_node = 0;
  2236. bandwidth_matrix[i][j] = 1/(slowness_disk_between_main_ram+slowness_main_ram_between_node);
  2237. }
  2238. else if (j == node) /* destination == disk */
  2239. {
  2240. /* convert in slowness */
  2241. if(bandwidth_write != 0)
  2242. slowness_disk_between_main_ram = 1/bandwidth_write;
  2243. else
  2244. slowness_disk_between_main_ram = 0;
  2245. if(bandwidth_matrix[i][STARPU_MAIN_RAM] != 0)
  2246. slowness_main_ram_between_node = 1/bandwidth_matrix[i][STARPU_MAIN_RAM];
  2247. else
  2248. slowness_main_ram_between_node = 0;
  2249. bandwidth_matrix[i][j] = 1/(slowness_disk_between_main_ram+slowness_main_ram_between_node);
  2250. }
  2251. else if (j > node || i > node) /* not affected by the node */
  2252. {
  2253. bandwidth_matrix[i][j] = NAN;
  2254. }
  2255. }
  2256. }
  2257. /* save latency */
  2258. for(i = 0; i < STARPU_MAXNODES; ++i)
  2259. {
  2260. for(j = 0; j < STARPU_MAXNODES; ++j)
  2261. {
  2262. if (i == j && j == node) /* source == destination == node */
  2263. {
  2264. latency_matrix[i][j] = 0;
  2265. }
  2266. else if (i == node) /* source == disk */
  2267. {
  2268. latency_matrix[i][j] = (latency_write+latency_matrix[STARPU_MAIN_RAM][j]);
  2269. }
  2270. else if (j == node) /* destination == disk */
  2271. {
  2272. latency_matrix[i][j] = (latency_read+latency_matrix[i][STARPU_MAIN_RAM]);
  2273. }
  2274. else if (j > node || i > node) /* not affected by the node */
  2275. {
  2276. latency_matrix[i][j] = NAN;
  2277. }
  2278. }
  2279. }
  2280. }