perfmodel_bus.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #ifdef STARPU_USE_CUDA
  18. #ifndef _GNU_SOURCE
  19. #define _GNU_SOURCE
  20. #endif
  21. #include <sched.h>
  22. #endif
  23. #include <unistd.h>
  24. #include <sys/time.h>
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include <starpu.h>
  28. #include <starpu_cuda.h>
  29. #include <starpu_opencl.h>
  30. #include <common/config.h>
  31. #include <core/workers.h>
  32. #include <core/perfmodel/perfmodel.h>
  33. #include <core/simgrid.h>
  34. #include <common/utils.h>
  35. #ifdef STARPU_USE_OPENCL
  36. #include <starpu_opencl.h>
  37. #endif
  38. #ifdef STARPU_HAVE_WINDOWS
  39. #include <windows.h>
  40. #endif
  41. #define SIZE (32*1024*1024*sizeof(char))
  42. #define NITER 128
  43. #ifndef STARPU_SIMGRID
  44. static void starpu_force_bus_sampling(void);
  45. #endif
  46. /* timing is in µs per byte (i.e. slowness, inverse of bandwidth) */
  47. struct dev_timing
  48. {
  49. int cpu_id;
  50. double timing_htod;
  51. double latency_htod;
  52. double timing_dtoh;
  53. double latency_dtoh;
  54. };
  55. /* TODO: measure latency */
  56. static double bandwidth_matrix[STARPU_MAXNODES][STARPU_MAXNODES];
  57. static double latency_matrix[STARPU_MAXNODES][STARPU_MAXNODES];
  58. static unsigned was_benchmarked = 0;
  59. static unsigned ncpus = 0;
  60. static unsigned ncuda = 0;
  61. static unsigned nopencl = 0;
  62. /* Benchmarking the performance of the bus */
  63. #ifdef STARPU_USE_CUDA
  64. static int cuda_affinity_matrix[STARPU_MAXCUDADEVS][STARPU_MAXCPUS];
  65. static double cudadev_timing_htod[STARPU_MAXNODES] = {0.0};
  66. static double cudadev_latency_htod[STARPU_MAXNODES] = {0.0};
  67. static double cudadev_timing_dtoh[STARPU_MAXNODES] = {0.0};
  68. static double cudadev_latency_dtoh[STARPU_MAXNODES] = {0.0};
  69. #ifdef HAVE_CUDA_MEMCPY_PEER
  70. static double cudadev_timing_dtod[STARPU_MAXNODES][STARPU_MAXNODES] = {{0.0}};
  71. static double cudadev_latency_dtod[STARPU_MAXNODES][STARPU_MAXNODES] = {{0.0}};
  72. #endif
  73. static struct dev_timing cudadev_timing_per_cpu[STARPU_MAXNODES*STARPU_MAXCPUS];
  74. #endif
  75. #ifdef STARPU_USE_OPENCL
  76. static int opencl_affinity_matrix[STARPU_MAXOPENCLDEVS][STARPU_MAXCPUS];
  77. static double opencldev_timing_htod[STARPU_MAXNODES] = {0.0};
  78. static double opencldev_latency_htod[STARPU_MAXNODES] = {0.0};
  79. static double opencldev_timing_dtoh[STARPU_MAXNODES] = {0.0};
  80. static double opencldev_latency_dtoh[STARPU_MAXNODES] = {0.0};
  81. static struct dev_timing opencldev_timing_per_cpu[STARPU_MAXNODES*STARPU_MAXCPUS];
  82. #endif
  83. #ifdef STARPU_HAVE_HWLOC
  84. static hwloc_topology_t hwtopology;
  85. #endif
  86. #if (defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)) && !defined(STARPU_SIMGRID)
  87. #ifdef STARPU_USE_CUDA
  88. static void measure_bandwidth_between_host_and_dev_on_cpu_with_cuda(int dev, int cpu, struct dev_timing *dev_timing_per_cpu)
  89. {
  90. struct _starpu_machine_config *config = _starpu_get_machine_config();
  91. _starpu_bind_thread_on_cpu(config, cpu);
  92. size_t size = SIZE;
  93. /* Initialize CUDA context on the device */
  94. /* We do not need to enable OpenGL interoperability at this point,
  95. * since we cleanly shutdown CUDA before returning. */
  96. cudaSetDevice(dev);
  97. /* hack to avoid third party libs to rebind threads */
  98. _starpu_bind_thread_on_cpu(config, cpu);
  99. /* hack to force the initialization */
  100. cudaFree(0);
  101. /* hack to avoid third party libs to rebind threads */
  102. _starpu_bind_thread_on_cpu(config, cpu);
  103. /* Get the maximum size which can be allocated on the device */
  104. struct cudaDeviceProp prop;
  105. cudaError_t cures;
  106. cures = cudaGetDeviceProperties(&prop, dev);
  107. if (STARPU_UNLIKELY(cures)) STARPU_CUDA_REPORT_ERROR(cures);
  108. if (size > prop.totalGlobalMem/4) size = prop.totalGlobalMem/4;
  109. /* Allocate a buffer on the device */
  110. unsigned char *d_buffer;
  111. cures = cudaMalloc((void **)&d_buffer, size);
  112. STARPU_ASSERT(cures == cudaSuccess);
  113. /* hack to avoid third party libs to rebind threads */
  114. _starpu_bind_thread_on_cpu(config, cpu);
  115. /* Allocate a buffer on the host */
  116. unsigned char *h_buffer;
  117. cures = cudaHostAlloc((void **)&h_buffer, size, 0);
  118. STARPU_ASSERT(cures == cudaSuccess);
  119. /* hack to avoid third party libs to rebind threads */
  120. _starpu_bind_thread_on_cpu(config, cpu);
  121. /* Fill them */
  122. memset(h_buffer, 0, size);
  123. cudaMemset(d_buffer, 0, size);
  124. /* hack to avoid third party libs to rebind threads */
  125. _starpu_bind_thread_on_cpu(config, cpu);
  126. unsigned iter;
  127. double timing;
  128. struct timeval start;
  129. struct timeval end;
  130. /* Measure upload bandwidth */
  131. gettimeofday(&start, NULL);
  132. for (iter = 0; iter < NITER; iter++)
  133. {
  134. cudaMemcpy(d_buffer, h_buffer, size, cudaMemcpyHostToDevice);
  135. cudaThreadSynchronize();
  136. }
  137. gettimeofday(&end, NULL);
  138. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  139. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_htod = timing/NITER/size;
  140. /* Measure download bandwidth */
  141. gettimeofday(&start, NULL);
  142. for (iter = 0; iter < NITER; iter++)
  143. {
  144. cudaMemcpy(h_buffer, d_buffer, size, cudaMemcpyDeviceToHost);
  145. cudaThreadSynchronize();
  146. }
  147. gettimeofday(&end, NULL);
  148. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  149. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_dtoh = timing/NITER/size;
  150. /* Measure upload latency */
  151. gettimeofday(&start, NULL);
  152. for (iter = 0; iter < NITER; iter++)
  153. {
  154. cudaMemcpy(d_buffer, h_buffer, 1, cudaMemcpyHostToDevice);
  155. cudaThreadSynchronize();
  156. }
  157. gettimeofday(&end, NULL);
  158. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  159. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_htod = timing/NITER;
  160. /* Measure download latency */
  161. gettimeofday(&start, NULL);
  162. for (iter = 0; iter < NITER; iter++)
  163. {
  164. cudaMemcpy(d_buffer, h_buffer, 1, cudaMemcpyHostToDevice);
  165. cudaThreadSynchronize();
  166. }
  167. gettimeofday(&end, NULL);
  168. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  169. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_dtoh = timing/NITER;
  170. /* Free buffers */
  171. cudaFreeHost(h_buffer);
  172. cudaFree(d_buffer);
  173. cudaThreadExit();
  174. }
  175. #ifdef HAVE_CUDA_MEMCPY_PEER
  176. static void measure_bandwidth_between_dev_and_dev_cuda(int src, int dst)
  177. {
  178. size_t size = SIZE;
  179. int can;
  180. /* Get the maximum size which can be allocated on the device */
  181. struct cudaDeviceProp prop;
  182. cudaError_t cures;
  183. cures = cudaGetDeviceProperties(&prop, src);
  184. if (STARPU_UNLIKELY(cures)) STARPU_CUDA_REPORT_ERROR(cures);
  185. if (size > prop.totalGlobalMem/4) size = prop.totalGlobalMem/4;
  186. cures = cudaGetDeviceProperties(&prop, dst);
  187. if (STARPU_UNLIKELY(cures)) STARPU_CUDA_REPORT_ERROR(cures);
  188. if (size > prop.totalGlobalMem/4) size = prop.totalGlobalMem/4;
  189. /* Initialize CUDA context on the source */
  190. /* We do not need to enable OpenGL interoperability at this point,
  191. * since we cleanly shutdown CUDA before returning. */
  192. cudaSetDevice(src);
  193. if (starpu_get_env_number("STARPU_DISABLE_CUDA_GPU_GPU_DIRECT") <= 0)
  194. {
  195. cures = cudaDeviceCanAccessPeer(&can, src, dst);
  196. if (!cures && can)
  197. {
  198. cures = cudaDeviceEnablePeerAccess(dst, 0);
  199. if (!cures)
  200. _STARPU_DISP("GPU-Direct %d -> %d\n", dst, src);
  201. }
  202. }
  203. /* Allocate a buffer on the device */
  204. unsigned char *s_buffer;
  205. cures = cudaMalloc((void **)&s_buffer, size);
  206. STARPU_ASSERT(cures == cudaSuccess);
  207. cudaMemset(s_buffer, 0, size);
  208. /* Initialize CUDA context on the destination */
  209. /* We do not need to enable OpenGL interoperability at this point,
  210. * since we cleanly shutdown CUDA before returning. */
  211. cudaSetDevice(dst);
  212. if (starpu_get_env_number("STARPU_DISABLE_CUDA_GPU_GPU_DIRECT") <= 0)
  213. {
  214. cures = cudaDeviceCanAccessPeer(&can, dst, src);
  215. if (!cures && can)
  216. {
  217. cures = cudaDeviceEnablePeerAccess(src, 0);
  218. if (!cures)
  219. _STARPU_DISP("GPU-Direct %d -> %d\n", src, dst);
  220. }
  221. }
  222. /* Allocate a buffer on the device */
  223. unsigned char *d_buffer;
  224. cures = cudaMalloc((void **)&d_buffer, size);
  225. STARPU_ASSERT(cures == cudaSuccess);
  226. cudaMemset(d_buffer, 0, size);
  227. unsigned iter;
  228. double timing;
  229. struct timeval start;
  230. struct timeval end;
  231. /* Measure upload bandwidth */
  232. gettimeofday(&start, NULL);
  233. for (iter = 0; iter < NITER; iter++)
  234. {
  235. cudaMemcpyPeer(d_buffer, dst, s_buffer, src, size);
  236. cudaThreadSynchronize();
  237. }
  238. gettimeofday(&end, NULL);
  239. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  240. cudadev_timing_dtod[src+1][dst+1] = timing/NITER/size;
  241. /* Measure upload latency */
  242. gettimeofday(&start, NULL);
  243. for (iter = 0; iter < NITER; iter++)
  244. {
  245. cudaMemcpyPeer(d_buffer, dst, s_buffer, src, 1);
  246. cudaThreadSynchronize();
  247. }
  248. gettimeofday(&end, NULL);
  249. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  250. cudadev_latency_dtod[src+1][dst+1] = timing/NITER;
  251. /* Free buffers */
  252. cudaFree(d_buffer);
  253. cudaSetDevice(src);
  254. cudaFree(s_buffer);
  255. cudaThreadExit();
  256. }
  257. #endif
  258. #endif
  259. #ifdef STARPU_USE_OPENCL
  260. static void measure_bandwidth_between_host_and_dev_on_cpu_with_opencl(int dev, int cpu, struct dev_timing *dev_timing_per_cpu)
  261. {
  262. cl_context context;
  263. cl_command_queue queue;
  264. cl_int err=0;
  265. size_t size = SIZE;
  266. int not_initialized;
  267. struct _starpu_machine_config *config = _starpu_get_machine_config();
  268. _starpu_bind_thread_on_cpu(config, cpu);
  269. /* Is the context already initialised ? */
  270. starpu_opencl_get_context(dev, &context);
  271. not_initialized = (context == NULL);
  272. if (not_initialized == 1)
  273. _starpu_opencl_init_context(dev);
  274. /* Get context and queue */
  275. starpu_opencl_get_context(dev, &context);
  276. starpu_opencl_get_queue(dev, &queue);
  277. /* Get the maximum size which can be allocated on the device */
  278. cl_device_id device;
  279. cl_ulong maxMemAllocSize;
  280. starpu_opencl_get_device(dev, &device);
  281. err = clGetDeviceInfo(device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(maxMemAllocSize), &maxMemAllocSize, NULL);
  282. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  283. if (size > (size_t)maxMemAllocSize/4) size = maxMemAllocSize/4;
  284. if (_starpu_opencl_get_device_type(dev) == CL_DEVICE_TYPE_CPU)
  285. {
  286. /* Let's not use too much RAM when running OpenCL on a CPU: it
  287. * would make the OS swap like crazy. */
  288. size /= 2;
  289. }
  290. /* hack to avoid third party libs to rebind threads */
  291. _starpu_bind_thread_on_cpu(config, cpu);
  292. /* Allocate a buffer on the device */
  293. cl_mem d_buffer;
  294. d_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, size, NULL, &err);
  295. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  296. /* hack to avoid third party libs to rebind threads */
  297. _starpu_bind_thread_on_cpu(config, cpu);
  298. /* Allocate a buffer on the host */
  299. unsigned char *h_buffer;
  300. h_buffer = (unsigned char *)malloc(size);
  301. STARPU_ASSERT(h_buffer);
  302. /* hack to avoid third party libs to rebind threads */
  303. _starpu_bind_thread_on_cpu(config, cpu);
  304. /* Fill them */
  305. memset(h_buffer, 0, size);
  306. err = clEnqueueWriteBuffer(queue, d_buffer, CL_TRUE, 0, size, h_buffer, 0, NULL, NULL);
  307. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  308. clFinish(queue);
  309. /* hack to avoid third party libs to rebind threads */
  310. _starpu_bind_thread_on_cpu(config, cpu);
  311. unsigned iter;
  312. double timing;
  313. struct timeval start;
  314. struct timeval end;
  315. /* Measure upload bandwidth */
  316. gettimeofday(&start, NULL);
  317. for (iter = 0; iter < NITER; iter++)
  318. {
  319. err = clEnqueueWriteBuffer(queue, d_buffer, CL_TRUE, 0, size, h_buffer, 0, NULL, NULL);
  320. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  321. clFinish(queue);
  322. }
  323. gettimeofday(&end, NULL);
  324. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  325. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_htod = timing/NITER/size;
  326. /* Measure download bandwidth */
  327. gettimeofday(&start, NULL);
  328. for (iter = 0; iter < NITER; iter++)
  329. {
  330. err = clEnqueueReadBuffer(queue, d_buffer, CL_TRUE, 0, size, h_buffer, 0, NULL, NULL);
  331. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  332. clFinish(queue);
  333. }
  334. gettimeofday(&end, NULL);
  335. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  336. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_dtoh = timing/NITER/size;
  337. /* Measure upload latency */
  338. gettimeofday(&start, NULL);
  339. for (iter = 0; iter < NITER; iter++)
  340. {
  341. err = clEnqueueWriteBuffer(queue, d_buffer, CL_TRUE, 0, 1, h_buffer, 0, NULL, NULL);
  342. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  343. clFinish(queue);
  344. }
  345. gettimeofday(&end, NULL);
  346. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  347. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_htod = timing/NITER;
  348. /* Measure download latency */
  349. gettimeofday(&start, NULL);
  350. for (iter = 0; iter < NITER; iter++)
  351. {
  352. err = clEnqueueReadBuffer(queue, d_buffer, CL_TRUE, 0, 1, h_buffer, 0, NULL, NULL);
  353. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  354. clFinish(queue);
  355. }
  356. gettimeofday(&end, NULL);
  357. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  358. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_dtoh = timing/NITER;
  359. /* Free buffers */
  360. err = clReleaseMemObject(d_buffer);
  361. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  362. STARPU_OPENCL_REPORT_ERROR(err);
  363. free(h_buffer);
  364. /* Uninitiliaze OpenCL context on the device */
  365. if (not_initialized == 1)
  366. _starpu_opencl_deinit_context(dev);
  367. }
  368. #endif
  369. /* NB: we want to sort the bandwidth by DECREASING order */
  370. static int compar_dev_timing(const void *left_dev_timing, const void *right_dev_timing)
  371. {
  372. const struct dev_timing *left = (const struct dev_timing *)left_dev_timing;
  373. const struct dev_timing *right = (const struct dev_timing *)right_dev_timing;
  374. double left_dtoh = left->timing_dtoh;
  375. double left_htod = left->timing_htod;
  376. double right_dtoh = right->timing_dtoh;
  377. double right_htod = right->timing_htod;
  378. double timing_sum2_left = left_dtoh*left_dtoh + left_htod*left_htod;
  379. double timing_sum2_right = right_dtoh*right_dtoh + right_htod*right_htod;
  380. /* it's for a decreasing sorting */
  381. return (timing_sum2_left > timing_sum2_right);
  382. }
  383. #ifdef STARPU_HAVE_HWLOC
  384. static int find_numa_node(hwloc_obj_t obj)
  385. {
  386. STARPU_ASSERT(obj);
  387. hwloc_obj_t current = obj;
  388. while (current->depth != HWLOC_OBJ_NODE)
  389. {
  390. current = current->parent;
  391. /* If we don't find a "node" obj before the root, this means
  392. * hwloc does not know whether there are numa nodes or not, so
  393. * we should not use a per-node sampling in that case. */
  394. STARPU_ASSERT(current);
  395. }
  396. STARPU_ASSERT(current->depth == HWLOC_OBJ_NODE);
  397. return current->logical_index;
  398. }
  399. #endif
  400. static void measure_bandwidth_between_cpus_and_dev(int dev, struct dev_timing *dev_timing_per_cpu, char *type)
  401. {
  402. /* Either we have hwloc and we measure the bandwith between each GPU
  403. * and each NUMA node, or we don't have such NUMA information and we
  404. * measure the bandwith for each pair of (CPU, GPU), which is slower.
  405. * */
  406. #ifdef STARPU_HAVE_HWLOC
  407. int cpu_depth = hwloc_get_type_depth(hwtopology, HWLOC_OBJ_CORE);
  408. int nnuma_nodes = hwloc_get_nbobjs_by_depth(hwtopology, HWLOC_OBJ_NODE);
  409. /* If no NUMA node was found, we assume that we have a single memory
  410. * bank. */
  411. const unsigned no_node_obj_was_found = (nnuma_nodes == 0);
  412. unsigned *is_available_per_numa_node = NULL;
  413. double *dev_timing_htod_per_numa_node = NULL;
  414. double *dev_latency_htod_per_numa_node = NULL;
  415. double *dev_timing_dtoh_per_numa_node = NULL;
  416. double *dev_latency_dtoh_per_numa_node = NULL;
  417. if (!no_node_obj_was_found)
  418. {
  419. is_available_per_numa_node = (unsigned *)malloc(nnuma_nodes * sizeof(unsigned));
  420. STARPU_ASSERT(is_available_per_numa_node);
  421. dev_timing_htod_per_numa_node = (double *)malloc(nnuma_nodes * sizeof(double));
  422. STARPU_ASSERT(dev_timing_htod_per_numa_node);
  423. dev_latency_htod_per_numa_node = (double *)malloc(nnuma_nodes * sizeof(double));
  424. STARPU_ASSERT(dev_latency_htod_per_numa_node);
  425. dev_timing_dtoh_per_numa_node = (double *)malloc(nnuma_nodes * sizeof(double));
  426. STARPU_ASSERT(dev_timing_dtoh_per_numa_node);
  427. dev_latency_dtoh_per_numa_node = (double *)malloc(nnuma_nodes * sizeof(double));
  428. STARPU_ASSERT(dev_latency_dtoh_per_numa_node);
  429. memset(is_available_per_numa_node, 0, nnuma_nodes*sizeof(unsigned));
  430. }
  431. #endif
  432. unsigned cpu;
  433. for (cpu = 0; cpu < ncpus; cpu++)
  434. {
  435. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].cpu_id = cpu;
  436. #ifdef STARPU_HAVE_HWLOC
  437. int numa_id = 0;
  438. if (!no_node_obj_was_found)
  439. {
  440. hwloc_obj_t obj = hwloc_get_obj_by_depth(hwtopology, cpu_depth, cpu);
  441. numa_id = find_numa_node(obj);
  442. if (is_available_per_numa_node[numa_id])
  443. {
  444. /* We reuse the previous numbers for that NUMA node */
  445. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_htod =
  446. dev_timing_htod_per_numa_node[numa_id];
  447. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_htod =
  448. dev_latency_htod_per_numa_node[numa_id];
  449. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_dtoh =
  450. dev_timing_dtoh_per_numa_node[numa_id];
  451. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_dtoh =
  452. dev_latency_dtoh_per_numa_node[numa_id];
  453. continue;
  454. }
  455. }
  456. #endif
  457. #ifdef STARPU_USE_CUDA
  458. if (strncmp(type, "CUDA", 4) == 0)
  459. measure_bandwidth_between_host_and_dev_on_cpu_with_cuda(dev, cpu, dev_timing_per_cpu);
  460. #endif
  461. #ifdef STARPU_USE_OPENCL
  462. if (strncmp(type, "OpenCL", 6) == 0)
  463. measure_bandwidth_between_host_and_dev_on_cpu_with_opencl(dev, cpu, dev_timing_per_cpu);
  464. #endif
  465. #ifdef STARPU_HAVE_HWLOC
  466. if (!no_node_obj_was_found && !is_available_per_numa_node[numa_id])
  467. {
  468. /* Save the results for that NUMA node */
  469. dev_timing_htod_per_numa_node[numa_id] =
  470. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_htod;
  471. dev_latency_htod_per_numa_node[numa_id] =
  472. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_htod;
  473. dev_timing_dtoh_per_numa_node[numa_id] =
  474. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_dtoh;
  475. dev_latency_dtoh_per_numa_node[numa_id] =
  476. dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].latency_dtoh;
  477. is_available_per_numa_node[numa_id] = 1;
  478. }
  479. #endif
  480. }
  481. #ifdef STARPU_HAVE_HWLOC
  482. if (!no_node_obj_was_found)
  483. {
  484. free(is_available_per_numa_node);
  485. free(dev_timing_htod_per_numa_node);
  486. free(dev_latency_htod_per_numa_node);
  487. free(dev_timing_dtoh_per_numa_node);
  488. free(dev_latency_dtoh_per_numa_node);
  489. }
  490. #endif /* STARPU_HAVE_HWLOC */
  491. }
  492. static void measure_bandwidth_between_host_and_dev(int dev, double *dev_timing_htod, double *dev_latency_htod,
  493. double *dev_timing_dtoh, double *dev_latency_dtoh,
  494. struct dev_timing *dev_timing_per_cpu, char *type)
  495. {
  496. measure_bandwidth_between_cpus_and_dev(dev, dev_timing_per_cpu, type);
  497. /* sort the results */
  498. qsort(&(dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS]), ncpus,
  499. sizeof(struct dev_timing),
  500. compar_dev_timing);
  501. #ifdef STARPU_VERBOSE
  502. unsigned cpu;
  503. for (cpu = 0; cpu < ncpus; cpu++)
  504. {
  505. unsigned current_cpu = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].cpu_id;
  506. double bandwidth_dtoh = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_dtoh;
  507. double bandwidth_htod = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+cpu].timing_htod;
  508. double bandwidth_sum2 = bandwidth_dtoh*bandwidth_dtoh + bandwidth_htod*bandwidth_htod;
  509. _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));
  510. }
  511. unsigned best_cpu = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+0].cpu_id;
  512. _STARPU_DISP("(%10s) BANDWIDTH GPU %d BEST CPU %u\n", type, dev, best_cpu);
  513. #endif
  514. /* The results are sorted in a decreasing order, so that the best
  515. * measurement is currently the first entry. */
  516. dev_timing_dtoh[dev+1] = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+0].timing_dtoh;
  517. dev_latency_dtoh[dev+1] = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+0].latency_dtoh;
  518. dev_timing_htod[dev+1] = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+0].timing_htod;
  519. dev_latency_htod[dev+1] = dev_timing_per_cpu[(dev+1)*STARPU_MAXCPUS+0].latency_htod;
  520. }
  521. #endif /* defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) */
  522. static void benchmark_all_gpu_devices(void)
  523. {
  524. #ifdef STARPU_SIMGRID
  525. _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");
  526. STARPU_ABORT();
  527. #else /* !SIMGRID */
  528. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  529. unsigned i;
  530. #endif
  531. #ifdef HAVE_CUDA_MEMCPY_PEER
  532. unsigned j;
  533. #endif
  534. _STARPU_DEBUG("Benchmarking the speed of the bus\n");
  535. #ifdef STARPU_HAVE_HWLOC
  536. hwloc_topology_init(&hwtopology);
  537. hwloc_topology_load(hwtopology);
  538. #endif
  539. #ifdef STARPU_HAVE_HWLOC
  540. hwloc_bitmap_t former_cpuset = hwloc_bitmap_alloc();
  541. hwloc_get_cpubind(hwtopology, former_cpuset, HWLOC_CPUBIND_THREAD);
  542. #elif __linux__
  543. /* Save the current cpu binding */
  544. cpu_set_t former_process_affinity;
  545. int ret;
  546. ret = sched_getaffinity(0, sizeof(former_process_affinity), &former_process_affinity);
  547. if (ret)
  548. {
  549. perror("sched_getaffinity");
  550. STARPU_ABORT();
  551. }
  552. #else
  553. #warning Missing binding support, StarPU will not be able to properly benchmark NUMA topology
  554. #endif
  555. struct _starpu_machine_config *config = _starpu_get_machine_config();
  556. ncpus = _starpu_topology_get_nhwcpu(config);
  557. #ifdef STARPU_USE_CUDA
  558. ncuda = _starpu_get_cuda_device_count();
  559. for (i = 0; i < ncuda; i++)
  560. {
  561. _STARPU_DISP("CUDA %d...\n", i);
  562. /* measure bandwidth between Host and Device i */
  563. measure_bandwidth_between_host_and_dev(i, cudadev_timing_htod, cudadev_latency_htod, cudadev_timing_dtoh, cudadev_latency_dtoh, cudadev_timing_per_cpu, "CUDA");
  564. }
  565. #ifdef HAVE_CUDA_MEMCPY_PEER
  566. for (i = 0; i < ncuda; i++)
  567. for (j = 0; j < ncuda; j++)
  568. if (i != j)
  569. {
  570. _STARPU_DISP("CUDA %d -> %d...\n", i, j);
  571. /* measure bandwidth between Host and Device i */
  572. measure_bandwidth_between_dev_and_dev_cuda(i, j);
  573. }
  574. #endif
  575. #endif
  576. #ifdef STARPU_USE_OPENCL
  577. nopencl = _starpu_opencl_get_device_count();
  578. for (i = 0; i < nopencl; i++)
  579. {
  580. _STARPU_DISP("OpenCL %d...\n", i);
  581. /* measure bandwith between Host and Device i */
  582. measure_bandwidth_between_host_and_dev(i, opencldev_timing_htod, opencldev_latency_htod, opencldev_timing_dtoh, opencldev_latency_dtoh, opencldev_timing_per_cpu, "OpenCL");
  583. }
  584. #endif
  585. #ifdef STARPU_HAVE_HWLOC
  586. hwloc_set_cpubind(hwtopology, former_cpuset, HWLOC_CPUBIND_THREAD);
  587. #elif __linux__
  588. /* Restore the former affinity */
  589. ret = sched_setaffinity(0, sizeof(former_process_affinity), &former_process_affinity);
  590. if (ret)
  591. {
  592. perror("sched_setaffinity");
  593. STARPU_ABORT();
  594. }
  595. #endif
  596. #ifdef STARPU_HAVE_HWLOC
  597. hwloc_topology_destroy(hwtopology);
  598. #endif
  599. _STARPU_DEBUG("Benchmarking the speed of the bus is done.\n");
  600. was_benchmarked = 1;
  601. #endif /* !SIMGRID */
  602. }
  603. static void get_bus_path(const char *type, char *path, size_t maxlen)
  604. {
  605. _starpu_get_perf_model_dir_bus(path, maxlen);
  606. char hostname[65];
  607. _starpu_gethostname(hostname, sizeof(hostname));
  608. strncat(path, hostname, maxlen);
  609. strncat(path, ".", maxlen);
  610. strncat(path, type, maxlen);
  611. }
  612. /*
  613. * Affinity
  614. */
  615. #ifndef STARPU_SIMGRID
  616. static void get_affinity_path(char *path, size_t maxlen)
  617. {
  618. get_bus_path("affinity", path, maxlen);
  619. }
  620. static void load_bus_affinity_file_content(void)
  621. {
  622. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  623. FILE *f;
  624. char path[256];
  625. get_affinity_path(path, sizeof(path));
  626. _STARPU_DEBUG("loading affinities from %s\n", path);
  627. f = fopen(path, "r");
  628. STARPU_ASSERT(f);
  629. struct _starpu_machine_config *config = _starpu_get_machine_config();
  630. ncpus = _starpu_topology_get_nhwcpu(config);
  631. unsigned gpu;
  632. #ifdef STARPU_USE_CUDA
  633. ncuda = _starpu_get_cuda_device_count();
  634. for (gpu = 0; gpu < ncuda; gpu++)
  635. {
  636. int ret;
  637. unsigned dummy;
  638. _starpu_drop_comments(f);
  639. ret = fscanf(f, "%d\t", &dummy);
  640. STARPU_ASSERT(ret == 1);
  641. STARPU_ASSERT(dummy == gpu);
  642. unsigned cpu;
  643. for (cpu = 0; cpu < ncpus; cpu++)
  644. {
  645. ret = fscanf(f, "%d\t", &cuda_affinity_matrix[gpu][cpu]);
  646. STARPU_ASSERT(ret == 1);
  647. }
  648. ret = fscanf(f, "\n");
  649. STARPU_ASSERT(ret == 0);
  650. }
  651. #endif /* !STARPU_USE_CUDA */
  652. #ifdef STARPU_USE_OPENCL
  653. nopencl = _starpu_opencl_get_device_count();
  654. for (gpu = 0; gpu < nopencl; gpu++)
  655. {
  656. int ret;
  657. unsigned dummy;
  658. _starpu_drop_comments(f);
  659. ret = fscanf(f, "%d\t", &dummy);
  660. STARPU_ASSERT(ret == 1);
  661. STARPU_ASSERT(dummy == gpu);
  662. unsigned cpu;
  663. for (cpu = 0; cpu < ncpus; cpu++)
  664. {
  665. ret = fscanf(f, "%d\t", &opencl_affinity_matrix[gpu][cpu]);
  666. STARPU_ASSERT(ret == 1);
  667. }
  668. ret = fscanf(f, "\n");
  669. STARPU_ASSERT(ret == 0);
  670. }
  671. #endif /* !STARPU_USE_OPENCL */
  672. fclose(f);
  673. #endif /* !(STARPU_USE_CUDA_ || STARPU_USE_OPENCL */
  674. }
  675. #ifndef STARPU_SIMGRID
  676. static void write_bus_affinity_file_content(void)
  677. {
  678. STARPU_ASSERT(was_benchmarked);
  679. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  680. FILE *f;
  681. char path[256];
  682. get_affinity_path(path, sizeof(path));
  683. _STARPU_DEBUG("writing affinities to %s\n", path);
  684. f = fopen(path, "w+");
  685. if (!f)
  686. {
  687. perror("fopen write_buf_affinity_file_content");
  688. _STARPU_DISP("path '%s'\n", path);
  689. fflush(stderr);
  690. STARPU_ABORT();
  691. }
  692. unsigned cpu;
  693. unsigned gpu;
  694. fprintf(f, "# GPU\t");
  695. for (cpu = 0; cpu < ncpus; cpu++)
  696. fprintf(f, "CPU%u\t", cpu);
  697. fprintf(f, "\n");
  698. #ifdef STARPU_USE_CUDA
  699. for (gpu = 0; gpu < ncuda; gpu++)
  700. {
  701. fprintf(f, "%d\t", gpu);
  702. for (cpu = 0; cpu < ncpus; cpu++)
  703. {
  704. fprintf(f, "%d\t", cudadev_timing_per_cpu[(gpu+1)*STARPU_MAXCPUS+cpu].cpu_id);
  705. }
  706. fprintf(f, "\n");
  707. }
  708. #endif
  709. #ifdef STARPU_USE_OPENCL
  710. for (gpu = 0; gpu < nopencl; gpu++)
  711. {
  712. fprintf(f, "%d\t", gpu);
  713. for (cpu = 0; cpu < ncpus; cpu++)
  714. {
  715. fprintf(f, "%d\t", opencldev_timing_per_cpu[(gpu+1)*STARPU_MAXCPUS+cpu].cpu_id);
  716. }
  717. fprintf(f, "\n");
  718. }
  719. #endif
  720. fclose(f);
  721. #endif
  722. }
  723. #endif /* STARPU_SIMGRID */
  724. static void generate_bus_affinity_file(void)
  725. {
  726. if (!was_benchmarked)
  727. benchmark_all_gpu_devices();
  728. write_bus_affinity_file_content();
  729. }
  730. static void load_bus_affinity_file(void)
  731. {
  732. int res;
  733. char path[256];
  734. get_affinity_path(path, sizeof(path));
  735. res = access(path, F_OK);
  736. if (res)
  737. {
  738. /* File does not exist yet */
  739. generate_bus_affinity_file();
  740. }
  741. load_bus_affinity_file_content();
  742. }
  743. #ifdef STARPU_USE_CUDA
  744. int *_starpu_get_cuda_affinity_vector(unsigned gpuid)
  745. {
  746. return cuda_affinity_matrix[gpuid];
  747. }
  748. #endif /* STARPU_USE_CUDA */
  749. #ifdef STARPU_USE_OPENCL
  750. int *_starpu_get_opencl_affinity_vector(unsigned gpuid)
  751. {
  752. return opencl_affinity_matrix[gpuid];
  753. }
  754. #endif /* STARPU_USE_OPENCL */
  755. void starpu_bus_print_affinity(FILE *f)
  756. {
  757. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  758. unsigned cpu;
  759. unsigned gpu;
  760. #endif
  761. fprintf(f, "# GPU\tCPU in preference order (logical index)\n");
  762. #ifdef STARPU_USE_CUDA
  763. fprintf(f, "# CUDA\n");
  764. for(gpu = 0 ; gpu<ncuda ; gpu++)
  765. {
  766. fprintf(f, "%d\t", gpu);
  767. for (cpu = 0; cpu < ncpus; cpu++)
  768. {
  769. fprintf(f, "%d\t", cuda_affinity_matrix[gpu][cpu]);
  770. }
  771. fprintf(f, "\n");
  772. }
  773. #endif
  774. #ifdef STARPU_USE_OPENCL
  775. fprintf(f, "# OpenCL\n");
  776. for(gpu = 0 ; gpu<nopencl ; gpu++)
  777. {
  778. fprintf(f, "%d\t", gpu);
  779. for (cpu = 0; cpu < ncpus; cpu++)
  780. {
  781. fprintf(f, "%d\t", opencl_affinity_matrix[gpu][cpu]);
  782. }
  783. fprintf(f, "\n");
  784. }
  785. #endif
  786. }
  787. #endif /* STARPU_SIMGRID */
  788. /*
  789. * Latency
  790. */
  791. static void get_latency_path(char *path, size_t maxlen)
  792. {
  793. get_bus_path("latency", path, maxlen);
  794. }
  795. static int load_bus_latency_file_content(void)
  796. {
  797. int n;
  798. unsigned src, dst;
  799. FILE *f;
  800. double latency;
  801. char path[256];
  802. get_latency_path(path, sizeof(path));
  803. _STARPU_DEBUG("loading latencies from %s\n", path);
  804. f = fopen(path, "r");
  805. if (!f)
  806. {
  807. perror("fopen load_bus_latency_file_content");
  808. _STARPU_DISP("path '%s'\n", path);
  809. fflush(stderr);
  810. STARPU_ABORT();
  811. }
  812. for (src = 0; src < STARPU_MAXNODES; src++)
  813. {
  814. _starpu_drop_comments(f);
  815. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  816. {
  817. n = fscanf(f, "%lf", &latency);
  818. if (n != 1)
  819. {
  820. _STARPU_DISP("Error while reading latency file <%s>. Expected a number\n", path);
  821. fclose(f);
  822. return 0;
  823. }
  824. n = getc(f);
  825. if (n == '\n')
  826. break;
  827. if (n != '\t')
  828. {
  829. _STARPU_DISP("bogus character %c in latency file %s\n", n, path);
  830. fclose(f);
  831. return 0;
  832. }
  833. latency_matrix[src][dst] = latency;
  834. /* Look out for \t\n */
  835. n = getc(f);
  836. if (n == '\n')
  837. break;
  838. ungetc(n, f);
  839. }
  840. /* No more values, take NAN */
  841. for ( ; dst < STARPU_MAXNODES; dst++)
  842. latency_matrix[src][dst] = NAN;
  843. while (n == '\t')
  844. {
  845. /* Look out for \t\n */
  846. n = getc(f);
  847. if (n == '\n')
  848. break;
  849. ungetc(n, f);
  850. n = fscanf(f, "%lf", &latency);
  851. if (n && !isnan(latency))
  852. {
  853. _STARPU_DISP("Too many nodes in latency file %s for this configuration (%d)\n", path, STARPU_MAXNODES);
  854. fclose(f);
  855. return 0;
  856. }
  857. n = getc(f);
  858. }
  859. if (n != '\n')
  860. {
  861. _STARPU_DISP("Bogus character %c in latency file %s\n", n, path);
  862. fclose(f);
  863. return 0;
  864. }
  865. /* Look out for EOF */
  866. n = getc(f);
  867. if (n == EOF)
  868. break;
  869. ungetc(n, f);
  870. }
  871. /* No more values, take NAN */
  872. for ( ; src < STARPU_MAXNODES; src++)
  873. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  874. latency_matrix[src][dst] = NAN;
  875. fclose(f);
  876. return 1;
  877. }
  878. #ifndef STARPU_SIMGRID
  879. static void write_bus_latency_file_content(void)
  880. {
  881. unsigned src, dst, maxnode;
  882. FILE *f;
  883. STARPU_ASSERT(was_benchmarked);
  884. char path[256];
  885. get_latency_path(path, sizeof(path));
  886. _STARPU_DEBUG("writing latencies to %s\n", path);
  887. f = fopen(path, "w+");
  888. if (!f)
  889. {
  890. perror("fopen write_bus_latency_file_content");
  891. _STARPU_DISP("path '%s'\n", path);
  892. fflush(stderr);
  893. STARPU_ABORT();
  894. }
  895. fprintf(f, "# ");
  896. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  897. fprintf(f, "to %d\t\t", dst);
  898. fprintf(f, "\n");
  899. maxnode = ncuda;
  900. #ifdef STARPU_USE_OPENCL
  901. maxnode += nopencl;
  902. #endif
  903. for (src = 0; src < STARPU_MAXNODES; src++)
  904. {
  905. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  906. {
  907. double latency = 0.0;
  908. if ((src > maxnode) || (dst > maxnode))
  909. {
  910. /* convention */
  911. latency = NAN;
  912. }
  913. else if (src == dst)
  914. {
  915. latency = 0.0;
  916. }
  917. else
  918. {
  919. /* µs */
  920. #ifdef STARPU_USE_CUDA
  921. #ifdef HAVE_CUDA_MEMCPY_PEER
  922. if (src && src < ncuda && dst && dst <= ncuda)
  923. latency = cudadev_latency_dtod[src][dst];
  924. else
  925. #endif
  926. {
  927. if (src && src <= ncuda)
  928. latency += cudadev_latency_dtoh[src];
  929. if (dst && dst <= ncuda)
  930. latency += cudadev_latency_htod[dst];
  931. }
  932. #endif
  933. #ifdef STARPU_USE_OPENCL
  934. if (src > ncuda)
  935. latency += opencldev_latency_dtoh[src-ncuda];
  936. if (dst > ncuda)
  937. latency += opencldev_latency_htod[dst-ncuda];
  938. #endif
  939. }
  940. if (dst)
  941. fputc('\t', f);
  942. fprintf(f, "%f", latency);
  943. }
  944. fprintf(f, "\n");
  945. }
  946. fclose(f);
  947. }
  948. #endif
  949. static void generate_bus_latency_file(void)
  950. {
  951. if (!was_benchmarked)
  952. benchmark_all_gpu_devices();
  953. #ifndef STARPU_SIMGRID
  954. write_bus_latency_file_content();
  955. #endif
  956. }
  957. static void load_bus_latency_file(void)
  958. {
  959. int res;
  960. char path[256];
  961. get_latency_path(path, sizeof(path));
  962. res = access(path, F_OK);
  963. if (res || !load_bus_latency_file_content())
  964. {
  965. /* File does not exist yet or is bogus */
  966. generate_bus_latency_file();
  967. }
  968. }
  969. /*
  970. * Bandwidth
  971. */
  972. static void get_bandwidth_path(char *path, size_t maxlen)
  973. {
  974. get_bus_path("bandwidth", path, maxlen);
  975. }
  976. static int load_bus_bandwidth_file_content(void)
  977. {
  978. int n;
  979. unsigned src, dst;
  980. FILE *f;
  981. double bandwidth;
  982. char path[256];
  983. get_bandwidth_path(path, sizeof(path));
  984. _STARPU_DEBUG("loading bandwidth from %s\n", path);
  985. f = fopen(path, "r");
  986. if (!f)
  987. {
  988. perror("fopen load_bus_bandwidth_file_content");
  989. _STARPU_DISP("path '%s'\n", path);
  990. fflush(stderr);
  991. STARPU_ABORT();
  992. }
  993. for (src = 0; src < STARPU_MAXNODES; src++)
  994. {
  995. _starpu_drop_comments(f);
  996. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  997. {
  998. n = fscanf(f, "%lf", &bandwidth);
  999. if (n != 1)
  1000. {
  1001. _STARPU_DISP("Error while reading bandwidth file <%s>. Expected a number\n", path);
  1002. fclose(f);
  1003. return 0;
  1004. }
  1005. n = getc(f);
  1006. if (n == '\n')
  1007. break;
  1008. if (n != '\t')
  1009. {
  1010. _STARPU_DISP("bogus character %c in bandwidth file %s\n", n, path);
  1011. fclose(f);
  1012. return 0;
  1013. }
  1014. bandwidth_matrix[src][dst] = bandwidth;
  1015. /* Look out for \t\n */
  1016. n = getc(f);
  1017. if (n == '\n')
  1018. break;
  1019. ungetc(n, f);
  1020. }
  1021. /* No more values, take NAN */
  1022. for ( ; dst < STARPU_MAXNODES; dst++)
  1023. bandwidth_matrix[src][dst] = NAN;
  1024. while (n == '\t')
  1025. {
  1026. /* Look out for \t\n */
  1027. n = getc(f);
  1028. if (n == '\n')
  1029. break;
  1030. ungetc(n, f);
  1031. n = fscanf(f, "%lf", &bandwidth);
  1032. if (n && !isnan(bandwidth))
  1033. {
  1034. _STARPU_DISP("Too many nodes in bandwidth file %s for this configuration (%d)\n", path, STARPU_MAXNODES);
  1035. fclose(f);
  1036. return 0;
  1037. }
  1038. n = getc(f);
  1039. }
  1040. if (n != '\n')
  1041. {
  1042. _STARPU_DISP("Bogus character %c in bandwidth file %s\n", n, path);
  1043. fclose(f);
  1044. return 0;
  1045. }
  1046. /* Look out for EOF */
  1047. n = getc(f);
  1048. if (n == EOF)
  1049. break;
  1050. ungetc(n, f);
  1051. }
  1052. /* No more values, take NAN */
  1053. for ( ; src < STARPU_MAXNODES; src++)
  1054. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  1055. latency_matrix[src][dst] = NAN;
  1056. fclose(f);
  1057. return 1;
  1058. }
  1059. #ifndef STARPU_SIMGRID
  1060. static void write_bus_bandwidth_file_content(void)
  1061. {
  1062. unsigned src, dst, maxnode;
  1063. FILE *f;
  1064. STARPU_ASSERT(was_benchmarked);
  1065. char path[256];
  1066. get_bandwidth_path(path, sizeof(path));
  1067. _STARPU_DEBUG("writing bandwidth to %s\n", path);
  1068. f = fopen(path, "w+");
  1069. STARPU_ASSERT(f);
  1070. fprintf(f, "# ");
  1071. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  1072. fprintf(f, "to %d\t\t", dst);
  1073. fprintf(f, "\n");
  1074. maxnode = ncuda;
  1075. #ifdef STARPU_USE_OPENCL
  1076. maxnode += nopencl;
  1077. #endif
  1078. for (src = 0; src < STARPU_MAXNODES; src++)
  1079. {
  1080. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  1081. {
  1082. double bandwidth;
  1083. if ((src > maxnode) || (dst > maxnode))
  1084. {
  1085. bandwidth = NAN;
  1086. }
  1087. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  1088. else if (src != dst)
  1089. {
  1090. double slowness = 0.0;
  1091. /* Total bandwidth is the harmonic mean of bandwidths */
  1092. #ifdef STARPU_USE_CUDA
  1093. #ifdef HAVE_CUDA_MEMCPY_PEER
  1094. if (src && src <= ncuda && dst && dst <= ncuda)
  1095. /* Direct GPU-GPU transfert */
  1096. slowness = cudadev_timing_dtod[src][dst];
  1097. else
  1098. #endif
  1099. {
  1100. if (src && src <= ncuda)
  1101. slowness += cudadev_timing_dtoh[src];
  1102. if (dst && dst <= ncuda)
  1103. slowness += cudadev_timing_htod[dst];
  1104. }
  1105. #endif
  1106. #ifdef STARPU_USE_OPENCL
  1107. if (src > ncuda)
  1108. slowness += opencldev_timing_dtoh[src-ncuda];
  1109. if (dst > ncuda)
  1110. slowness += opencldev_timing_htod[dst-ncuda];
  1111. #endif
  1112. bandwidth = 1.0/slowness;
  1113. }
  1114. #endif
  1115. else
  1116. {
  1117. /* convention */
  1118. bandwidth = 0.0;
  1119. }
  1120. if (dst)
  1121. fputc('\t', f);
  1122. fprintf(f, "%f", bandwidth);
  1123. }
  1124. fprintf(f, "\n");
  1125. }
  1126. fclose(f);
  1127. }
  1128. #endif /* STARPU_SIMGRID */
  1129. double starpu_get_bandwidth_RAM_CUDA(unsigned cudadev)
  1130. {
  1131. return bandwidth_matrix[0][cudadev+1];
  1132. }
  1133. double starpu_get_latency_RAM_CUDA(unsigned cudadev)
  1134. {
  1135. return latency_matrix[0][cudadev+1];
  1136. }
  1137. void starpu_bus_print_bandwidth(FILE *f)
  1138. {
  1139. unsigned src, dst, maxnode;
  1140. maxnode = ncuda;
  1141. #ifdef STARPU_USE_OPENCL
  1142. maxnode += nopencl;
  1143. #endif
  1144. fprintf(f, "from/to\t");
  1145. fprintf(f, "RAM\t");
  1146. for (dst = 0; dst < ncuda; dst++)
  1147. fprintf(f, "CUDA %d\t", dst);
  1148. for (dst = 0; dst < nopencl; dst++)
  1149. fprintf(f, "OpenCL%d\t", dst);
  1150. fprintf(f, "\n");
  1151. for (src = 0; src <= maxnode; src++)
  1152. {
  1153. if (!src)
  1154. fprintf(f, "RAM\t");
  1155. else if (src <= ncuda)
  1156. fprintf(f, "CUDA %d\t", src-1);
  1157. else
  1158. fprintf(f, "OpenCL%d\t", src-ncuda-1);
  1159. for (dst = 0; dst <= maxnode; dst++)
  1160. fprintf(f, "%.0f\t", bandwidth_matrix[src][dst]);
  1161. fprintf(f, "\n");
  1162. }
  1163. fprintf(f, "\n");
  1164. for (src = 0; src <= maxnode; src++)
  1165. {
  1166. if (!src)
  1167. fprintf(f, "RAM\t");
  1168. else if (src <= ncuda)
  1169. fprintf(f, "CUDA %d\t", src-1);
  1170. else
  1171. fprintf(f, "OpenCL%d\t", src-ncuda-1);
  1172. for (dst = 0; dst <= maxnode; dst++)
  1173. fprintf(f, "%.0f\t", latency_matrix[src][dst]);
  1174. fprintf(f, "\n");
  1175. }
  1176. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  1177. if (ncuda != 0 || nopencl != 0)
  1178. fprintf(f, "\nGPU\tCPU in preference order (logical index), host-to-device, device-to-host\n");
  1179. for (src = 1; src <= maxnode; src++)
  1180. {
  1181. struct dev_timing *timing;
  1182. struct _starpu_machine_config *config = _starpu_get_machine_config();
  1183. unsigned config_ncpus = _starpu_topology_get_nhwcpu(config);
  1184. unsigned cpu;
  1185. #ifdef STARPU_USE_CUDA
  1186. if (src <= ncuda)
  1187. {
  1188. fprintf(f, "CUDA %d\t", src-1);
  1189. for (cpu = 0; cpu < config_ncpus; cpu++)
  1190. {
  1191. timing = &cudadev_timing_per_cpu[src*STARPU_MAXCPUS+cpu];
  1192. if (timing->timing_htod)
  1193. fprintf(f, "%2d %.0f %.0f\t", timing->cpu_id, 1/timing->timing_htod, 1/timing->timing_dtoh);
  1194. else
  1195. fprintf(f, "%2d\t", cuda_affinity_matrix[src-1][cpu]);
  1196. }
  1197. }
  1198. #ifdef STARPU_USE_OPENCL
  1199. else
  1200. #endif
  1201. #endif
  1202. #ifdef STARPU_USE_OPENCL
  1203. {
  1204. fprintf(f, "OpenCL%d\t", src-ncuda-1);
  1205. for (cpu = 0; cpu < config_ncpus; cpu++)
  1206. {
  1207. timing = &opencldev_timing_per_cpu[(src-ncuda)*STARPU_MAXCPUS+cpu];
  1208. if (timing->timing_htod)
  1209. fprintf(f, "%2d %.0f %.0f\t", timing->cpu_id, 1/timing->timing_htod, 1/timing->timing_dtoh);
  1210. else
  1211. fprintf(f, "%2d\t", opencl_affinity_matrix[src-1][cpu]);
  1212. }
  1213. }
  1214. #endif
  1215. fprintf(f, "\n");
  1216. }
  1217. #endif
  1218. }
  1219. static void generate_bus_bandwidth_file(void)
  1220. {
  1221. if (!was_benchmarked)
  1222. benchmark_all_gpu_devices();
  1223. #ifndef STARPU_SIMGRID
  1224. write_bus_bandwidth_file_content();
  1225. #endif
  1226. }
  1227. static void load_bus_bandwidth_file(void)
  1228. {
  1229. int res;
  1230. char path[256];
  1231. get_bandwidth_path(path, sizeof(path));
  1232. res = access(path, F_OK);
  1233. if (res || !load_bus_bandwidth_file_content())
  1234. {
  1235. /* File does not exist yet or is bogus */
  1236. generate_bus_bandwidth_file();
  1237. }
  1238. }
  1239. #ifndef STARPU_SIMGRID
  1240. /*
  1241. * Config
  1242. */
  1243. static void get_config_path(char *path, size_t maxlen)
  1244. {
  1245. get_bus_path("config", path, maxlen);
  1246. }
  1247. static void check_bus_config_file(void)
  1248. {
  1249. int res;
  1250. char path[256];
  1251. struct _starpu_machine_config *config = _starpu_get_machine_config();
  1252. get_config_path(path, sizeof(path));
  1253. res = access(path, F_OK);
  1254. if (res || config->conf->bus_calibrate > 0)
  1255. {
  1256. if (res)
  1257. _STARPU_DISP("No performance model for the bus, calibrating...\n");
  1258. starpu_force_bus_sampling();
  1259. if (res)
  1260. _STARPU_DISP("... done\n");
  1261. }
  1262. else
  1263. {
  1264. FILE *f;
  1265. int ret;
  1266. unsigned read_cuda = -1, read_opencl = -1;
  1267. unsigned read_cpus = -1;
  1268. // Loading configuration from file
  1269. f = fopen(path, "r");
  1270. STARPU_ASSERT(f);
  1271. _starpu_drop_comments(f);
  1272. ret = fscanf(f, "%u\t", &read_cpus);
  1273. STARPU_ASSERT(ret == 1);
  1274. _starpu_drop_comments(f);
  1275. ret = fscanf(f, "%d\t", &read_cuda);
  1276. STARPU_ASSERT(ret == 1);
  1277. _starpu_drop_comments(f);
  1278. ret = fscanf(f, "%d\t", &read_opencl);
  1279. STARPU_ASSERT(ret == 1);
  1280. _starpu_drop_comments(f);
  1281. fclose(f);
  1282. // Loading current configuration
  1283. ncpus = _starpu_topology_get_nhwcpu(config);
  1284. #ifdef STARPU_USE_CUDA
  1285. ncuda = _starpu_get_cuda_device_count();
  1286. #endif
  1287. #ifdef STARPU_USE_OPENCL
  1288. nopencl = _starpu_opencl_get_device_count();
  1289. #endif
  1290. // Checking if both configurations match
  1291. if (read_cpus != ncpus)
  1292. {
  1293. _STARPU_DISP("Current configuration does not match the bus performance model (CPUS: (stored) %u != (current) %u), recalibrating...\n", read_cpus, ncpus);
  1294. starpu_force_bus_sampling();
  1295. _STARPU_DISP("... done\n");
  1296. }
  1297. else if (read_cuda != ncuda)
  1298. {
  1299. _STARPU_DISP("Current configuration does not match the bus performance model (CUDA: (stored) %d != (current) %d), recalibrating...\n", read_cuda, ncuda);
  1300. starpu_force_bus_sampling();
  1301. _STARPU_DISP("... done\n");
  1302. }
  1303. else if (read_opencl != nopencl)
  1304. {
  1305. _STARPU_DISP("Current configuration does not match the bus performance model (OpenCL: (stored) %d != (current) %d), recalibrating...\n", read_opencl, nopencl);
  1306. starpu_force_bus_sampling();
  1307. _STARPU_DISP("... done\n");
  1308. }
  1309. }
  1310. }
  1311. static void write_bus_config_file_content(void)
  1312. {
  1313. FILE *f;
  1314. char path[256];
  1315. STARPU_ASSERT(was_benchmarked);
  1316. get_config_path(path, sizeof(path));
  1317. _STARPU_DEBUG("writing config to %s\n", path);
  1318. f = fopen(path, "w+");
  1319. STARPU_ASSERT(f);
  1320. fprintf(f, "# Current configuration\n");
  1321. fprintf(f, "%u # Number of CPUs\n", ncpus);
  1322. fprintf(f, "%d # Number of CUDA devices\n", ncuda);
  1323. fprintf(f, "%d # Number of OpenCL devices\n", nopencl);
  1324. fclose(f);
  1325. }
  1326. static void generate_bus_config_file(void)
  1327. {
  1328. if (!was_benchmarked)
  1329. benchmark_all_gpu_devices();
  1330. write_bus_config_file_content();
  1331. }
  1332. #endif /* !SIMGRID */
  1333. void _starpu_simgrid_get_platform_path(char *path, size_t maxlen)
  1334. {
  1335. get_bus_path("platform.xml", path, maxlen);
  1336. }
  1337. #ifndef STARPU_SIMGRID
  1338. static void write_bus_platform_file_content(void)
  1339. {
  1340. FILE *f;
  1341. char path[256];
  1342. unsigned i;
  1343. STARPU_ASSERT(was_benchmarked);
  1344. _starpu_simgrid_get_platform_path(path, sizeof(path));
  1345. _STARPU_DEBUG("writing platform to %s\n", path);
  1346. f = fopen(path, "w+");
  1347. if (!f)
  1348. {
  1349. perror("fopen write_bus_platform_file_content");
  1350. _STARPU_DISP("path '%s'\n", path);
  1351. fflush(stderr);
  1352. STARPU_ABORT();
  1353. }
  1354. fprintf(f,
  1355. "<?xml version='1.0'?>\n"
  1356. " <!DOCTYPE platform SYSTEM 'http://simgrid.gforge.inria.fr/simgrid.dtd'>\n"
  1357. " <platform version='3'>\n"
  1358. " <config id='General'>\n"
  1359. " <prop id='network/TCP_gamma' value='-1'></prop>\n"
  1360. " <prop id='network/latency_factor' value='1'></prop>\n"
  1361. " <prop id='network/bandwidth_factor' value='1'></prop>\n"
  1362. " </config>\n"
  1363. " <AS id='AS0' routing='Full'>\n"
  1364. " <host id='MAIN' power='1'/>\n"
  1365. );
  1366. for (i = 0; i < ncpus; i++)
  1367. fprintf(f, " <host id='CPU%d' power='2000000000'/>\n", i);
  1368. for (i = 0; i < ncuda; i++)
  1369. fprintf(f, " <host id='CUDA%d' power='2000000000'/>\n", i);
  1370. for (i = 0; i < nopencl; i++)
  1371. fprintf(f, " <host id='OpenCL%d' power='2000000000'/>\n", i);
  1372. fprintf(f, "\n <host id='RAM' power='1'/>\n");
  1373. /* Compute maximum bandwidth, taken as machine bandwidth */
  1374. double max_bandwidth = 0;
  1375. #ifdef STARPU_USE_CUDA
  1376. for (i = 0; i < ncuda; i++)
  1377. {
  1378. double down_bw = 1.0 / cudadev_timing_dtoh[1+i];
  1379. double up_bw = 1.0 / cudadev_timing_htod[1+i];
  1380. if (max_bandwidth < down_bw)
  1381. max_bandwidth = down_bw;
  1382. if (max_bandwidth < up_bw)
  1383. max_bandwidth = up_bw;
  1384. }
  1385. #endif
  1386. #ifdef STARPU_USE_OPENCL
  1387. for (i = 0; i < nopencl; i++)
  1388. {
  1389. double down_bw = 1.0 / opencldev_timing_dtoh[1+i];
  1390. double up_bw = 1.0 / opencldev_timing_htod[1+i];
  1391. if (max_bandwidth < down_bw)
  1392. max_bandwidth = down_bw;
  1393. if (max_bandwidth < up_bw)
  1394. max_bandwidth = up_bw;
  1395. }
  1396. #endif
  1397. fprintf(f, "\n <link id='Share' bandwidth='%f' latency='0.000000'/>\n\n", max_bandwidth*1000000);
  1398. /* Write bandwidths & latencies */
  1399. #ifdef STARPU_USE_CUDA
  1400. for (i = 0; i < ncuda; i++)
  1401. {
  1402. char i_name[16];
  1403. snprintf(i_name, sizeof(i_name), "CUDA%d", i);
  1404. fprintf(f, " <link id='RAM-%s' bandwidth='%f' latency='%f'/>\n",
  1405. i_name,
  1406. 1000000. / cudadev_timing_htod[1+i],
  1407. cudadev_latency_htod[1+i]/1000000.);
  1408. fprintf(f, " <link id='%s-RAM' bandwidth='%f' latency='%f'/>\n",
  1409. i_name,
  1410. 1000000. / cudadev_timing_dtoh[1+i],
  1411. cudadev_latency_dtoh[1+i]/1000000.);
  1412. }
  1413. #ifdef HAVE_CUDA_MEMCPY_PEER
  1414. for (i = 0; i < ncuda; i++)
  1415. {
  1416. unsigned j;
  1417. char i_name[16];
  1418. snprintf(i_name, sizeof(i_name), "CUDA%d", i);
  1419. for (j = 0; j < ncuda; j++)
  1420. {
  1421. char j_name[16];
  1422. if (j == i)
  1423. continue;
  1424. snprintf(j_name, sizeof(j_name), "CUDA%d", j);
  1425. fprintf(f, " <link id='%s-%s' bandwidth='%f' latency='%f'/>\n",
  1426. i_name, j_name,
  1427. 1000000. / cudadev_timing_dtod[1+i][1+j],
  1428. cudadev_latency_dtod[1+i][1+j]/1000000.);
  1429. }
  1430. }
  1431. #endif
  1432. #endif
  1433. #ifdef STARPU_USE_OPENCL
  1434. for (i = 0; i < nopencl; i++)
  1435. {
  1436. char i_name[16];
  1437. snprintf(i_name, sizeof(i_name), "OpenCL%d", i);
  1438. fprintf(f, " <link id='RAM-%s' bandwidth='%f' latency='%f'/>\n",
  1439. i_name,
  1440. 1000000 / opencldev_timing_htod[1+i],
  1441. opencldev_latency_htod[1+i]/1000000.);
  1442. fprintf(f, " <link id='%s-RAM' bandwidth='%f' latency='%f'/>\n",
  1443. i_name,
  1444. 1000000 / opencldev_timing_dtoh[1+i],
  1445. opencldev_latency_dtoh[1+i]/1000000.);
  1446. }
  1447. #endif
  1448. /* Write routes */
  1449. #ifdef STARPU_USE_CUDA
  1450. for (i = 0; i < ncuda; i++)
  1451. {
  1452. char i_name[16];
  1453. snprintf(i_name, sizeof(i_name), "CUDA%d", i);
  1454. fprintf(f, " <route src='RAM' dst='%s' symmetrical='NO'><link_ctn id='RAM-%s'/><link_ctn id='Share'/></route>\n", i_name, i_name);
  1455. fprintf(f, " <route src='%s' dst='RAM' symmetrical='NO'><link_ctn id='%s-RAM'/><link_ctn id='Share'/></route>\n", i_name, i_name);
  1456. }
  1457. #ifdef HAVE_CUDA_MEMCPY_PEER
  1458. for (i = 0; i < ncuda; i++)
  1459. {
  1460. unsigned j;
  1461. char i_name[16];
  1462. snprintf(i_name, sizeof(i_name), "CUDA%d", i);
  1463. for (j = 0; j < ncuda; j++)
  1464. {
  1465. char j_name[16];
  1466. if (j == i)
  1467. continue;
  1468. snprintf(j_name, sizeof(j_name), "CUDA%d", j);
  1469. fprintf(f, " <route src='%s' dst='%s' symmetrical='NO'><link_ctn id='%s-%s'/><link_ctn id='Share'/></route>\n", i_name, j_name, i_name, j_name);
  1470. }
  1471. }
  1472. #endif
  1473. #endif
  1474. #ifdef STARPU_USE_OPENCL
  1475. for (i = 0; i < nopencl; i++)
  1476. {
  1477. char i_name[16];
  1478. snprintf(i_name, sizeof(i_name), "OpenCL%d", i);
  1479. fprintf(f, " <route src='RAM' dst='%s' symmetrical='NO'><link_ctn id='RAM-%s'/><link_ctn id='Share'/></route>\n", i_name, i_name);
  1480. fprintf(f, " <route src='%s' dst='RAM' symmetrical='NO'><link_ctn id='%s-RAM'/><link_ctn id='Share'/></route>\n", i_name, i_name);
  1481. }
  1482. #endif
  1483. fprintf(f,
  1484. " </AS>\n"
  1485. " </platform>\n"
  1486. );
  1487. fclose(f);
  1488. }
  1489. static void generate_bus_platform_file(void)
  1490. {
  1491. if (!was_benchmarked)
  1492. benchmark_all_gpu_devices();
  1493. write_bus_platform_file_content();
  1494. }
  1495. static void check_bus_platform_file(void)
  1496. {
  1497. int res;
  1498. char path[256];
  1499. _starpu_simgrid_get_platform_path(path, sizeof(path));
  1500. res = access(path, F_OK);
  1501. if (res)
  1502. {
  1503. /* File does not exist yet */
  1504. generate_bus_platform_file();
  1505. }
  1506. }
  1507. /*
  1508. * Generic
  1509. */
  1510. static void starpu_force_bus_sampling(void)
  1511. {
  1512. _STARPU_DEBUG("Force bus sampling ...\n");
  1513. _starpu_create_sampling_directory_if_needed();
  1514. generate_bus_affinity_file();
  1515. generate_bus_latency_file();
  1516. generate_bus_bandwidth_file();
  1517. generate_bus_config_file();
  1518. generate_bus_platform_file();
  1519. }
  1520. #endif /* !SIMGRID */
  1521. void _starpu_load_bus_performance_files(void)
  1522. {
  1523. _starpu_create_sampling_directory_if_needed();
  1524. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_SIMGRID)
  1525. ncuda = _starpu_get_cuda_device_count();
  1526. #endif
  1527. #if defined(STARPU_USE_OPENCL) || defined(STARPU_USE_SIMGRID)
  1528. nopencl = _starpu_opencl_get_device_count();
  1529. #endif
  1530. #ifndef STARPU_SIMGRID
  1531. check_bus_config_file();
  1532. load_bus_affinity_file();
  1533. #endif
  1534. load_bus_latency_file();
  1535. load_bus_bandwidth_file();
  1536. #ifndef STARPU_SIMGRID
  1537. check_bus_platform_file();
  1538. #endif
  1539. }
  1540. /* (in MB/s) */
  1541. double _starpu_transfer_bandwidth(unsigned src_node, unsigned dst_node)
  1542. {
  1543. return bandwidth_matrix[src_node][dst_node];
  1544. }
  1545. /* (in µs) */
  1546. double _starpu_transfer_latency(unsigned src_node, unsigned dst_node)
  1547. {
  1548. return latency_matrix[src_node][dst_node];
  1549. }
  1550. /* (in µs) */
  1551. double _starpu_predict_transfer_time(unsigned src_node, unsigned dst_node, size_t size)
  1552. {
  1553. double bandwidth = bandwidth_matrix[src_node][dst_node];
  1554. double latency = latency_matrix[src_node][dst_node];
  1555. struct starpu_machine_topology *topology = &_starpu_get_machine_config()->topology;
  1556. return latency + (size/bandwidth)*2*(topology->ncudagpus+topology->nopenclgpus);
  1557. }