perfmodel_bus.c 52 KB

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