perfmodel_bus.c 52 KB

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