perfmodel_bus.c 52 KB

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