perfmodel_bus.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #ifdef STARPU_USE_CUDA
  18. #ifndef _GNU_SOURCE
  19. #define _GNU_SOURCE
  20. #endif
  21. #include <sched.h>
  22. #endif
  23. #include <unistd.h>
  24. #include <sys/time.h>
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include <starpu.h>
  28. #include <starpu_cuda.h>
  29. #include <starpu_opencl.h>
  30. #include <common/config.h>
  31. #include <core/workers.h>
  32. #include <core/perfmodel/perfmodel.h>
  33. #ifdef STARPU_USE_OPENCL
  34. #include <starpu_opencl.h>
  35. #endif
  36. #ifdef STARPU_HAVE_WINDOWS
  37. #include <windows.h>
  38. #endif
  39. #define SIZE (32*1024*1024*sizeof(char))
  40. #define NITER 128
  41. #define MAXCPUS 32
  42. struct dev_timing {
  43. int cpu_id;
  44. double timing_htod;
  45. double timing_dtoh;
  46. };
  47. static double bandwidth_matrix[STARPU_MAXNODES][STARPU_MAXNODES] = {{-1.0}};
  48. static double latency_matrix[STARPU_MAXNODES][STARPU_MAXNODES] = {{ -1.0}};
  49. static unsigned was_benchmarked = 0;
  50. static unsigned ncpus = 0;
  51. static int ncuda = 0;
  52. static int nopencl = 0;
  53. /* Benchmarking the performance of the bus */
  54. #ifdef STARPU_USE_CUDA
  55. static int cuda_affinity_matrix[STARPU_MAXCUDADEVS][MAXCPUS];
  56. static double cudadev_timing_htod[STARPU_MAXNODES] = {0.0};
  57. static double cudadev_timing_dtoh[STARPU_MAXNODES] = {0.0};
  58. static struct dev_timing cudadev_timing_per_cpu[STARPU_MAXNODES*MAXCPUS];
  59. static size_t cuda_size = SIZE;
  60. #endif
  61. #ifdef STARPU_USE_OPENCL
  62. static int opencl_affinity_matrix[STARPU_MAXOPENCLDEVS][MAXCPUS];
  63. static double opencldev_timing_htod[STARPU_MAXNODES] = {0.0};
  64. static double opencldev_timing_dtoh[STARPU_MAXNODES] = {0.0};
  65. static struct dev_timing opencldev_timing_per_cpu[STARPU_MAXNODES*MAXCPUS];
  66. static size_t opencl_size = SIZE;
  67. #endif
  68. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  69. #ifdef STARPU_HAVE_HWLOC
  70. static hwloc_topology_t hwtopology;
  71. #endif
  72. #ifdef STARPU_USE_CUDA
  73. static void measure_bandwidth_between_host_and_dev_on_cpu_with_cuda(int dev, int cpu, struct dev_timing *dev_timing_per_cpu)
  74. {
  75. struct starpu_machine_config_s *config = _starpu_get_machine_config();
  76. _starpu_bind_thread_on_cpu(config, cpu);
  77. /* Initiliaze CUDA context on the device */
  78. cudaSetDevice(dev);
  79. /* hack to avoid third party libs to rebind threads */
  80. _starpu_bind_thread_on_cpu(config, cpu);
  81. /* hack to force the initialization */
  82. cudaFree(0);
  83. /* hack to avoid third party libs to rebind threads */
  84. _starpu_bind_thread_on_cpu(config, cpu);
  85. /* Get the maximum size which can be allocated on the device */
  86. struct cudaDeviceProp prop;
  87. cudaError_t cures;
  88. cures = cudaGetDeviceProperties(&prop, dev);
  89. if (STARPU_UNLIKELY(cures)) STARPU_CUDA_REPORT_ERROR(cures);
  90. if (cuda_size > prop.totalGlobalMem/4) cuda_size = prop.totalGlobalMem/4;
  91. /* Allocate a buffer on the device */
  92. unsigned char *d_buffer;
  93. cudaMalloc((void **)&d_buffer, cuda_size);
  94. assert(d_buffer);
  95. /* hack to avoid third party libs to rebind threads */
  96. _starpu_bind_thread_on_cpu(config, cpu);
  97. /* Allocate a buffer on the host */
  98. unsigned char *h_buffer;
  99. cudaHostAlloc((void **)&h_buffer, cuda_size, 0);
  100. assert(h_buffer);
  101. /* hack to avoid third party libs to rebind threads */
  102. _starpu_bind_thread_on_cpu(config, cpu);
  103. /* Fill them */
  104. memset(h_buffer, 0, cuda_size);
  105. cudaMemset(d_buffer, 0, cuda_size);
  106. /* hack to avoid third party libs to rebind threads */
  107. _starpu_bind_thread_on_cpu(config, cpu);
  108. unsigned iter;
  109. double timing;
  110. struct timeval start;
  111. struct timeval end;
  112. /* Measure upload bandwidth */
  113. gettimeofday(&start, NULL);
  114. for (iter = 0; iter < NITER; iter++)
  115. {
  116. cudaMemcpy(d_buffer, h_buffer, cuda_size, cudaMemcpyHostToDevice);
  117. cudaThreadSynchronize();
  118. }
  119. gettimeofday(&end, NULL);
  120. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  121. dev_timing_per_cpu[(dev+1)*MAXCPUS+cpu].timing_htod = timing/NITER;
  122. /* Measure download bandwidth */
  123. gettimeofday(&start, NULL);
  124. for (iter = 0; iter < NITER; iter++)
  125. {
  126. cudaMemcpy(h_buffer, d_buffer, cuda_size, cudaMemcpyDeviceToHost);
  127. cudaThreadSynchronize();
  128. }
  129. gettimeofday(&end, NULL);
  130. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  131. dev_timing_per_cpu[(dev+1)*MAXCPUS+cpu].timing_dtoh = timing/NITER;
  132. /* Free buffers */
  133. cudaFreeHost(h_buffer);
  134. cudaFree(d_buffer);
  135. cudaThreadExit();
  136. }
  137. #endif
  138. #ifdef STARPU_USE_OPENCL
  139. static void measure_bandwidth_between_host_and_dev_on_cpu_with_opencl(int dev, int cpu, struct dev_timing *dev_timing_per_cpu)
  140. {
  141. cl_context context;
  142. cl_command_queue queue;
  143. cl_int err=0;
  144. struct starpu_machine_config_s *config = _starpu_get_machine_config();
  145. _starpu_bind_thread_on_cpu(config, cpu);
  146. /* Initialize OpenCL context on the device */
  147. _starpu_opencl_init_context(dev);
  148. starpu_opencl_get_context(dev, &context);
  149. starpu_opencl_get_queue(dev, &queue);
  150. /* Get the maximum size which can be allocated on the device */
  151. cl_device_id device;
  152. cl_ulong maxMemAllocSize;
  153. starpu_opencl_get_device(dev, &device);
  154. err = clGetDeviceInfo(device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(maxMemAllocSize), &maxMemAllocSize, NULL);
  155. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  156. if (opencl_size > (size_t)maxMemAllocSize/4) opencl_size = maxMemAllocSize/4;
  157. /* hack to avoid third party libs to rebind threads */
  158. _starpu_bind_thread_on_cpu(config, cpu);
  159. /* Allocate a buffer on the device */
  160. cl_mem d_buffer;
  161. d_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, opencl_size, NULL, &err);
  162. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  163. /* hack to avoid third party libs to rebind threads */
  164. _starpu_bind_thread_on_cpu(config, cpu);
  165. /* Allocate a buffer on the host */
  166. unsigned char *h_buffer;
  167. h_buffer = malloc(opencl_size);
  168. assert(h_buffer);
  169. /* hack to avoid third party libs to rebind threads */
  170. _starpu_bind_thread_on_cpu(config, cpu);
  171. /* Fill them */
  172. memset(h_buffer, 0, opencl_size);
  173. err = clEnqueueWriteBuffer(queue, d_buffer, CL_TRUE, 0, opencl_size, h_buffer, 0, NULL, NULL);
  174. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  175. /* hack to avoid third party libs to rebind threads */
  176. _starpu_bind_thread_on_cpu(config, cpu);
  177. unsigned iter;
  178. double timing;
  179. struct timeval start;
  180. struct timeval end;
  181. /* Measure upload bandwidth */
  182. gettimeofday(&start, NULL);
  183. for (iter = 0; iter < NITER; iter++)
  184. {
  185. err = clEnqueueWriteBuffer(queue, d_buffer, CL_TRUE, 0, opencl_size, h_buffer, 0, NULL, NULL);
  186. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  187. }
  188. gettimeofday(&end, NULL);
  189. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  190. dev_timing_per_cpu[(dev+1)*MAXCPUS+cpu].timing_htod = timing/NITER;
  191. /* Measure download bandwidth */
  192. gettimeofday(&start, NULL);
  193. for (iter = 0; iter < NITER; iter++)
  194. {
  195. err = clEnqueueReadBuffer(queue, d_buffer, CL_TRUE, 0, opencl_size, h_buffer, 0, NULL, NULL);
  196. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  197. }
  198. gettimeofday(&end, NULL);
  199. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  200. dev_timing_per_cpu[(dev+1)*MAXCPUS+cpu].timing_dtoh = timing/NITER;
  201. /* Free buffers */
  202. clReleaseMemObject(d_buffer);
  203. free(h_buffer);
  204. /* Uninitiliaze OpenCL context on the device */
  205. _starpu_opencl_deinit_context(dev);
  206. }
  207. #endif
  208. /* NB: we want to sort the bandwidth by DECREASING order */
  209. static int compar_dev_timing(const void *left_dev_timing, const void *right_dev_timing)
  210. {
  211. const struct dev_timing *left = left_dev_timing;
  212. const struct dev_timing *right = right_dev_timing;
  213. double left_dtoh = left->timing_dtoh;
  214. double left_htod = left->timing_htod;
  215. double right_dtoh = right->timing_dtoh;
  216. double right_htod = right->timing_htod;
  217. double bandwidth_sum2_left = left_dtoh*left_dtoh + left_htod*left_htod;
  218. double bandwidth_sum2_right = right_dtoh*right_dtoh + right_htod*right_htod;
  219. /* it's for a decreasing sorting */
  220. return (bandwidth_sum2_left < bandwidth_sum2_right);
  221. }
  222. #ifdef STARPU_HAVE_HWLOC
  223. static int find_numa_node(hwloc_obj_t obj)
  224. {
  225. STARPU_ASSERT(obj);
  226. hwloc_obj_t current = obj;
  227. while (current->depth != HWLOC_OBJ_NODE)
  228. {
  229. current = current->parent;
  230. /* If we don't find a "node" obj before the root, this means
  231. * hwloc does not know whether there are numa nodes or not, so
  232. * we should not use a per-node sampling in that case. */
  233. STARPU_ASSERT(current);
  234. }
  235. STARPU_ASSERT(current->depth == HWLOC_OBJ_NODE);
  236. return current->logical_index;
  237. }
  238. #endif
  239. static void measure_bandwidth_between_cpus_and_dev(int dev, struct dev_timing *dev_timing_per_cpu, char type)
  240. {
  241. /* Either we have hwloc and we measure the bandwith between each GPU
  242. * and each NUMA node, or we don't have such NUMA information and we
  243. * measure the bandwith for each pair of (CPU, GPU), which is slower.
  244. * */
  245. #ifdef STARPU_HAVE_HWLOC
  246. int cpu_depth = hwloc_get_type_depth(hwtopology, HWLOC_OBJ_CORE);
  247. int nnuma_nodes = hwloc_get_nbobjs_by_depth(hwtopology, HWLOC_OBJ_NODE);
  248. /* If no NUMA node was found, we assume that we have a single memory
  249. * bank. */
  250. const unsigned no_node_obj_was_found = (nnuma_nodes == 0);
  251. unsigned is_available_per_numa_node[nnuma_nodes];
  252. double dev_timing_htod_per_numa_node[nnuma_nodes];
  253. double dev_timing_dtoh_per_numa_node[nnuma_nodes];
  254. memset(is_available_per_numa_node, 0, nnuma_nodes*sizeof(unsigned));
  255. #endif
  256. unsigned cpu;
  257. for (cpu = 0; cpu < ncpus; cpu++)
  258. {
  259. dev_timing_per_cpu[(dev+1)*MAXCPUS+cpu].cpu_id = cpu;
  260. #ifdef STARPU_HAVE_HWLOC
  261. int numa_id = 0;
  262. if (!no_node_obj_was_found)
  263. {
  264. hwloc_obj_t obj = hwloc_get_obj_by_depth(hwtopology, cpu_depth, cpu);
  265. numa_id = find_numa_node(obj);
  266. if (is_available_per_numa_node[numa_id])
  267. {
  268. /* We reuse the previous numbers for that NUMA node */
  269. dev_timing_per_cpu[(dev+1)*MAXCPUS+cpu].timing_htod =
  270. dev_timing_htod_per_numa_node[numa_id];
  271. dev_timing_per_cpu[(dev+1)*MAXCPUS+cpu].timing_dtoh =
  272. dev_timing_dtoh_per_numa_node[numa_id];
  273. continue;
  274. }
  275. }
  276. #endif
  277. #ifdef STARPU_USE_CUDA
  278. if (type == 'C')
  279. measure_bandwidth_between_host_and_dev_on_cpu_with_cuda(dev, cpu, dev_timing_per_cpu);
  280. #endif
  281. #ifdef STARPU_USE_OPENCL
  282. if (type == 'O')
  283. measure_bandwidth_between_host_and_dev_on_cpu_with_opencl(dev, cpu, dev_timing_per_cpu);
  284. #endif
  285. #ifdef STARPU_HAVE_HWLOC
  286. if (!no_node_obj_was_found && !is_available_per_numa_node[numa_id])
  287. {
  288. /* Save the results for that NUMA node */
  289. dev_timing_htod_per_numa_node[numa_id] =
  290. dev_timing_per_cpu[(dev+1)*MAXCPUS+cpu].timing_htod;
  291. dev_timing_dtoh_per_numa_node[numa_id] =
  292. dev_timing_per_cpu[(dev+1)*MAXCPUS+cpu].timing_dtoh;
  293. is_available_per_numa_node[numa_id] = 1;
  294. }
  295. #endif
  296. }
  297. }
  298. static void measure_bandwidth_between_host_and_dev(int dev, double *dev_timing_htod, double *dev_timing_dtoh,
  299. struct dev_timing *dev_timing_per_cpu, char type)
  300. {
  301. measure_bandwidth_between_cpus_and_dev(dev, dev_timing_per_cpu, type);
  302. /* sort the results */
  303. qsort(&(dev_timing_per_cpu[(dev+1)*MAXCPUS]), ncpus,
  304. sizeof(struct dev_timing),
  305. compar_dev_timing);
  306. #ifdef STARPU_VERBOSE
  307. unsigned cpu;
  308. for (cpu = 0; cpu < ncpus; cpu++)
  309. {
  310. unsigned current_cpu = dev_timing_per_cpu[(dev+1)*MAXCPUS+cpu].cpu_id;
  311. double bandwidth_dtoh = dev_timing_per_cpu[(dev+1)*MAXCPUS+cpu].timing_dtoh;
  312. double bandwidth_htod = dev_timing_per_cpu[(dev+1)*MAXCPUS+cpu].timing_htod;
  313. double bandwidth_sum2 = bandwidth_dtoh*bandwidth_dtoh + bandwidth_htod*bandwidth_htod;
  314. _STARPU_DISP("BANDWIDTH GPU %d CPU %u - htod %f - dtoh %f - %f\n", dev, current_cpu, bandwidth_htod, bandwidth_dtoh, sqrt(bandwidth_sum2));
  315. }
  316. unsigned best_cpu = dev_timing_per_cpu[(dev+1)*MAXCPUS+0].cpu_id;
  317. _STARPU_DISP("BANDWIDTH GPU %d BEST CPU %u\n", dev, best_cpu);
  318. #endif
  319. /* The results are sorted in a decreasing order, so that the best
  320. * measurement is currently the first entry. */
  321. dev_timing_dtoh[dev+1] = dev_timing_per_cpu[(dev+1)*MAXCPUS+0].timing_dtoh;
  322. dev_timing_htod[dev+1] = dev_timing_per_cpu[(dev+1)*MAXCPUS+0].timing_htod;
  323. }
  324. #endif /* defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) */
  325. static void benchmark_all_gpu_devices(void)
  326. {
  327. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  328. int i;
  329. _STARPU_DEBUG("Benchmarking the speed of the bus\n");
  330. #ifdef STARPU_HAVE_HWLOC
  331. hwloc_topology_init(&hwtopology);
  332. hwloc_topology_load(hwtopology);
  333. #endif
  334. /* TODO: use hwloc */
  335. #ifdef __linux__
  336. /* Save the current cpu binding */
  337. cpu_set_t former_process_affinity;
  338. int ret;
  339. ret = sched_getaffinity(0, sizeof(former_process_affinity), &former_process_affinity);
  340. if (ret)
  341. {
  342. perror("sched_getaffinity");
  343. STARPU_ABORT();
  344. }
  345. #else
  346. #warning Missing binding support, StarPU will not be able to properly benchmark NUMA topology
  347. #endif
  348. struct starpu_machine_config_s *config = _starpu_get_machine_config();
  349. ncpus = _starpu_topology_get_nhwcpu(config);
  350. /* TODO: measure bandwidth between GPU-GPU */
  351. #ifdef STARPU_USE_CUDA
  352. ncuda = _starpu_get_cuda_device_count();
  353. for (i = 0; i < ncuda; i++)
  354. {
  355. fprintf(stderr," CUDA %d...", i);
  356. /* measure bandwidth between Host and Device i */
  357. measure_bandwidth_between_host_and_dev(i, cudadev_timing_htod, cudadev_timing_dtoh, cudadev_timing_per_cpu, 'C');
  358. }
  359. #endif
  360. #ifdef STARPU_USE_OPENCL
  361. nopencl = _starpu_opencl_get_device_count();
  362. for (i = 0; i < nopencl; i++)
  363. {
  364. fprintf(stderr," OpenCL %d...", i);
  365. /* measure bandwith between Host and Device i */
  366. measure_bandwidth_between_host_and_dev(i, opencldev_timing_htod, opencldev_timing_dtoh, opencldev_timing_per_cpu, 'O');
  367. }
  368. #endif
  369. /* FIXME: use hwloc */
  370. #ifdef __linux__
  371. /* Restore the former affinity */
  372. ret = sched_setaffinity(0, sizeof(former_process_affinity), &former_process_affinity);
  373. if (ret)
  374. {
  375. perror("sched_setaffinity");
  376. STARPU_ABORT();
  377. }
  378. #endif
  379. #ifdef STARPU_HAVE_HWLOC
  380. hwloc_topology_destroy(hwtopology);
  381. #endif
  382. _STARPU_DEBUG("Benchmarking the speed of the bus is done.\n");
  383. #endif /* defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) */
  384. was_benchmarked = 1;
  385. }
  386. static void get_bus_path(const char *type, char *path, size_t maxlen)
  387. {
  388. _starpu_get_perf_model_dir_bus(path, maxlen);
  389. strncat(path, type, maxlen);
  390. char hostname[32];
  391. char *forced_hostname = getenv("STARPU_HOSTNAME");
  392. if (forced_hostname && forced_hostname[0])
  393. snprintf(hostname, sizeof(hostname), "%s", forced_hostname);
  394. else
  395. gethostname(hostname, sizeof(hostname));
  396. strncat(path, ".", maxlen);
  397. strncat(path, hostname, maxlen);
  398. }
  399. /*
  400. * Affinity
  401. */
  402. static void get_affinity_path(char *path, size_t maxlen)
  403. {
  404. get_bus_path("affinity", path, maxlen);
  405. }
  406. static void load_bus_affinity_file_content(void)
  407. {
  408. FILE *f;
  409. char path[256];
  410. get_affinity_path(path, 256);
  411. f = fopen(path, "r");
  412. STARPU_ASSERT(f);
  413. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  414. struct starpu_machine_config_s *config = _starpu_get_machine_config();
  415. ncpus = _starpu_topology_get_nhwcpu(config);
  416. int gpu;
  417. #ifdef STARPU_USE_CUDA
  418. ncuda = _starpu_get_cuda_device_count();
  419. for (gpu = 0; gpu < ncuda; gpu++)
  420. {
  421. int ret;
  422. int dummy;
  423. _starpu_drop_comments(f);
  424. ret = fscanf(f, "%d\t", &dummy);
  425. STARPU_ASSERT(ret == 1);
  426. STARPU_ASSERT(dummy == gpu);
  427. unsigned cpu;
  428. for (cpu = 0; cpu < ncpus; cpu++)
  429. {
  430. ret = fscanf(f, "%d\t", &cuda_affinity_matrix[gpu][cpu]);
  431. STARPU_ASSERT(ret == 1);
  432. }
  433. ret = fscanf(f, "\n");
  434. STARPU_ASSERT(ret == 0);
  435. }
  436. #endif
  437. #ifdef STARPU_USE_OPENCL
  438. nopencl = _starpu_opencl_get_device_count();
  439. for (gpu = 0; gpu < nopencl; gpu++)
  440. {
  441. int ret;
  442. int dummy;
  443. _starpu_drop_comments(f);
  444. ret = fscanf(f, "%d\t", &dummy);
  445. STARPU_ASSERT(ret == 1);
  446. STARPU_ASSERT(dummy == gpu);
  447. unsigned cpu;
  448. for (cpu = 0; cpu < ncpus; cpu++)
  449. {
  450. ret = fscanf(f, "%d\t", &opencl_affinity_matrix[gpu][cpu]);
  451. STARPU_ASSERT(ret == 1);
  452. }
  453. ret = fscanf(f, "\n");
  454. STARPU_ASSERT(ret == 0);
  455. }
  456. #endif
  457. #endif
  458. fclose(f);
  459. }
  460. static void write_bus_affinity_file_content(void)
  461. {
  462. FILE *f;
  463. STARPU_ASSERT(was_benchmarked);
  464. char path[256];
  465. get_affinity_path(path, 256);
  466. f = fopen(path, "w+");
  467. if (!f)
  468. {
  469. perror("fopen write_buf_affinity_file_content");
  470. _STARPU_DISP("path '%s'\n", path);
  471. fflush(stderr);
  472. STARPU_ABORT();
  473. }
  474. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  475. unsigned cpu;
  476. int gpu;
  477. fprintf(f, "# GPU\t");
  478. for (cpu = 0; cpu < ncpus; cpu++)
  479. fprintf(f, "CPU%u\t", cpu);
  480. fprintf(f, "\n");
  481. #ifdef STARPU_USE_CUDA
  482. for (gpu = 0; gpu < ncuda; gpu++)
  483. {
  484. fprintf(f, "%d\t", gpu);
  485. for (cpu = 0; cpu < ncpus; cpu++)
  486. {
  487. fprintf(f, "%d\t", cudadev_timing_per_cpu[(gpu+1)*MAXCPUS+cpu].cpu_id);
  488. }
  489. fprintf(f, "\n");
  490. }
  491. #endif
  492. #ifdef STARPU_USE_OPENCL
  493. for (gpu = 0; gpu < nopencl; gpu++)
  494. {
  495. fprintf(f, "%d\t", gpu);
  496. for (cpu = 0; cpu < ncpus; cpu++)
  497. {
  498. fprintf(f, "%d\t", opencldev_timing_per_cpu[(gpu+1)*MAXCPUS+cpu].cpu_id);
  499. }
  500. fprintf(f, "\n");
  501. }
  502. #endif
  503. fclose(f);
  504. #endif
  505. }
  506. static void generate_bus_affinity_file(void)
  507. {
  508. if (!was_benchmarked)
  509. benchmark_all_gpu_devices();
  510. write_bus_affinity_file_content();
  511. }
  512. static void load_bus_affinity_file(void)
  513. {
  514. int res;
  515. char path[256];
  516. get_affinity_path(path, 256);
  517. res = access(path, F_OK);
  518. if (res)
  519. {
  520. /* File does not exist yet */
  521. generate_bus_affinity_file();
  522. }
  523. load_bus_affinity_file_content();
  524. }
  525. #ifdef STARPU_USE_CUDA
  526. int *_starpu_get_cuda_affinity_vector(unsigned gpuid)
  527. {
  528. return cuda_affinity_matrix[gpuid];
  529. }
  530. #endif /* STARPU_USE_CUDA */
  531. #ifdef STARPU_USE_OPENCL
  532. int *_starpu_get_opencl_affinity_vector(unsigned gpuid)
  533. {
  534. return opencl_affinity_matrix[gpuid];
  535. }
  536. #endif /* STARPU_USE_OPENCL */
  537. /*
  538. * Latency
  539. */
  540. static void get_latency_path(char *path, size_t maxlen)
  541. {
  542. get_bus_path("latency", path, maxlen);
  543. }
  544. static int load_bus_latency_file_content(void)
  545. {
  546. int n;
  547. unsigned src, dst;
  548. FILE *f;
  549. char path[256];
  550. get_latency_path(path, 256);
  551. f = fopen(path, "r");
  552. STARPU_ASSERT(f);
  553. for (src = 0; src < STARPU_MAXNODES; src++)
  554. {
  555. _starpu_drop_comments(f);
  556. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  557. {
  558. double latency;
  559. n = fscanf(f, "%lf", &latency);
  560. if (n != 1) {
  561. fclose(f);
  562. return 0;
  563. }
  564. n = getc(f);
  565. if (n != '\t') {
  566. fclose(f);
  567. return 0;
  568. }
  569. latency_matrix[src][dst] = latency;
  570. }
  571. n = getc(f);
  572. if (n != '\n') {
  573. fclose(f);
  574. return 0;
  575. }
  576. }
  577. fclose(f);
  578. return 1;
  579. }
  580. static void write_bus_latency_file_content(void)
  581. {
  582. int src, dst, maxnode;
  583. FILE *f;
  584. STARPU_ASSERT(was_benchmarked);
  585. char path[256];
  586. get_latency_path(path, 256);
  587. f = fopen(path, "w+");
  588. if (!f)
  589. {
  590. perror("fopen write_bus_latency_file_content");
  591. _STARPU_DISP("path '%s'\n", path);
  592. fflush(stderr);
  593. STARPU_ABORT();
  594. }
  595. fprintf(f, "# ");
  596. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  597. fprintf(f, "to %d\t\t", dst);
  598. fprintf(f, "\n");
  599. maxnode = ncuda;
  600. #ifdef STARPU_USE_OPENCL
  601. maxnode += nopencl;
  602. #endif
  603. for (src = 0; src < STARPU_MAXNODES; src++)
  604. {
  605. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  606. {
  607. double latency;
  608. if ((src > maxnode) || (dst > maxnode))
  609. {
  610. /* convention */
  611. latency = -1.0;
  612. }
  613. else if (src == dst)
  614. {
  615. latency = 0.0;
  616. }
  617. else {
  618. latency = ((src && dst)?2000.0:500.0);
  619. }
  620. fprintf(f, "%f\t", latency);
  621. }
  622. fprintf(f, "\n");
  623. }
  624. fclose(f);
  625. }
  626. static void generate_bus_latency_file(void)
  627. {
  628. if (!was_benchmarked)
  629. benchmark_all_gpu_devices();
  630. write_bus_latency_file_content();
  631. }
  632. static void load_bus_latency_file(void)
  633. {
  634. int res;
  635. char path[256];
  636. get_latency_path(path, 256);
  637. res = access(path, F_OK);
  638. if (res || !load_bus_latency_file_content())
  639. {
  640. /* File does not exist yet or is bogus */
  641. generate_bus_latency_file();
  642. }
  643. }
  644. /*
  645. * Bandwidth
  646. */
  647. static void get_bandwidth_path(char *path, size_t maxlen)
  648. {
  649. get_bus_path("bandwidth", path, maxlen);
  650. }
  651. static int load_bus_bandwidth_file_content(void)
  652. {
  653. int n;
  654. unsigned src, dst;
  655. FILE *f;
  656. char path[256];
  657. get_bandwidth_path(path, 256);
  658. f = fopen(path, "r");
  659. if (!f)
  660. {
  661. perror("fopen load_bus_bandwidth_file_content");
  662. _STARPU_DISP("path '%s'\n", path);
  663. fflush(stderr);
  664. STARPU_ABORT();
  665. }
  666. for (src = 0; src < STARPU_MAXNODES; src++)
  667. {
  668. _starpu_drop_comments(f);
  669. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  670. {
  671. double bandwidth;
  672. n = fscanf(f, "%lf", &bandwidth);
  673. if (n != 1) {
  674. fprintf(stderr,"didn't get a number\n");
  675. fclose(f);
  676. return 0;
  677. }
  678. n = getc(f);
  679. if (n != '\t') {
  680. fclose(f);
  681. return 0;
  682. }
  683. bandwidth_matrix[src][dst] = bandwidth;
  684. }
  685. n = getc(f);
  686. if (n != '\n') {
  687. fclose(f);
  688. return 0;
  689. }
  690. }
  691. fclose(f);
  692. return 1;
  693. }
  694. static void write_bus_bandwidth_file_content(void)
  695. {
  696. int src, dst, maxnode;
  697. FILE *f;
  698. STARPU_ASSERT(was_benchmarked);
  699. char path[256];
  700. get_bandwidth_path(path, 256);
  701. f = fopen(path, "w+");
  702. STARPU_ASSERT(f);
  703. fprintf(f, "# ");
  704. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  705. fprintf(f, "to %d\t\t", dst);
  706. fprintf(f, "\n");
  707. maxnode = ncuda;
  708. #ifdef STARPU_USE_OPENCL
  709. maxnode += nopencl;
  710. #endif
  711. for (src = 0; src < STARPU_MAXNODES; src++)
  712. {
  713. for (dst = 0; dst < STARPU_MAXNODES; dst++)
  714. {
  715. double bandwidth;
  716. if ((src > maxnode) || (dst > maxnode))
  717. {
  718. bandwidth = -1.0;
  719. }
  720. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  721. else if (src != dst)
  722. {
  723. double slowness_src_to_ram=0.0, slowness_ram_to_dst=0.0;
  724. /* Total bandwidth is the harmonic mean of bandwidths */
  725. #ifdef STARPU_USE_CUDA
  726. if (src && src <= ncuda)
  727. slowness_src_to_ram = cudadev_timing_dtoh[src]/cuda_size;
  728. if (dst && dst <= ncuda)
  729. slowness_ram_to_dst = cudadev_timing_htod[dst]/cuda_size;
  730. #endif
  731. #ifdef STARPU_USE_OPENCL
  732. if (src > ncuda)
  733. slowness_src_to_ram = opencldev_timing_dtoh[src-ncuda]/opencl_size;
  734. if (dst > ncuda)
  735. slowness_ram_to_dst = opencldev_timing_htod[dst-ncuda]/opencl_size;
  736. #endif
  737. bandwidth = 1.0/(slowness_src_to_ram + slowness_ram_to_dst);
  738. }
  739. #endif
  740. else {
  741. /* convention */
  742. bandwidth = 0.0;
  743. }
  744. fprintf(f, "%f\t", bandwidth);
  745. }
  746. fprintf(f, "\n");
  747. }
  748. fclose(f);
  749. }
  750. void starpu_print_bus_bandwidth(FILE *f)
  751. {
  752. int src, dst, maxnode;
  753. maxnode = ncuda;
  754. #ifdef STARPU_USE_OPENCL
  755. maxnode += nopencl;
  756. #endif
  757. fprintf(f, "from\t");
  758. fprintf(f, "to RAM\t\t");
  759. for (dst = 0; dst < ncuda; dst++)
  760. fprintf(f, "to CUDA %d\t", dst);
  761. for (dst = 0; dst < nopencl; dst++)
  762. fprintf(f, "to OpenCL %d\t", dst);
  763. fprintf(f, "\n");
  764. for (src = 0; src <= maxnode; src++)
  765. {
  766. if (!src)
  767. fprintf(f, "RAM\t");
  768. else if (src <= ncuda)
  769. fprintf(f, "CUDA %d\t", src-1);
  770. else
  771. fprintf(f, "OpenCL%d\t", src-ncuda-1);
  772. for (dst = 0; dst <= maxnode; dst++)
  773. fprintf(f, "%f\t", bandwidth_matrix[src][dst]);
  774. fprintf(f, "\n");
  775. }
  776. }
  777. static void generate_bus_bandwidth_file(void)
  778. {
  779. if (!was_benchmarked)
  780. benchmark_all_gpu_devices();
  781. write_bus_bandwidth_file_content();
  782. }
  783. static void load_bus_bandwidth_file(void)
  784. {
  785. int res;
  786. char path[256];
  787. get_bandwidth_path(path, 256);
  788. res = access(path, F_OK);
  789. if (res || !load_bus_bandwidth_file_content())
  790. {
  791. /* File does not exist yet or is bogus */
  792. generate_bus_bandwidth_file();
  793. }
  794. }
  795. /*
  796. * Config
  797. */
  798. static void get_config_path(char *path, size_t maxlen)
  799. {
  800. get_bus_path("config", path, maxlen);
  801. }
  802. static void check_bus_config_file()
  803. {
  804. int res;
  805. char path[256];
  806. get_config_path(path, 256);
  807. res = access(path, F_OK);
  808. if (res) {
  809. fprintf(stderr, "No performance model for the bus, calibrating...");
  810. starpu_force_bus_sampling();
  811. fprintf(stderr, "done\n");
  812. }
  813. else {
  814. FILE *f;
  815. int ret, read_cuda, read_opencl;
  816. unsigned read_cpus;
  817. struct starpu_machine_config_s *config = _starpu_get_machine_config();
  818. // Loading configuration from file
  819. f = fopen(path, "r");
  820. STARPU_ASSERT(f);
  821. _starpu_drop_comments(f);
  822. ret = fscanf(f, "%u\t", &read_cpus);
  823. STARPU_ASSERT(ret == 1);
  824. _starpu_drop_comments(f);
  825. ret = fscanf(f, "%d\t", &read_cuda);
  826. STARPU_ASSERT(ret == 1);
  827. _starpu_drop_comments(f);
  828. ret = fscanf(f, "%d\t", &read_opencl);
  829. STARPU_ASSERT(ret == 1);
  830. _starpu_drop_comments(f);
  831. fclose(f);
  832. // Loading current configuration
  833. ncpus = _starpu_topology_get_nhwcpu(config);
  834. #ifdef STARPU_USE_CUDA
  835. ncuda = _starpu_get_cuda_device_count();
  836. #endif
  837. #ifdef STARPU_USE_OPENCL
  838. nopencl = _starpu_opencl_get_device_count();
  839. #endif
  840. // Checking if both configurations match
  841. if (read_cpus != ncpus) {
  842. fprintf(stderr, "Current configuration does not match the bus performance model (CPUS: (stored) %u != (current) %u), recalibrating...", read_cpus, ncpus);
  843. starpu_force_bus_sampling();
  844. fprintf(stderr, "done\n");
  845. }
  846. else if (read_cuda != ncuda) {
  847. fprintf(stderr, "Current configuration does not match the bus performance model (CUDA: (stored) %d != (current) %d), recalibrating...", read_cuda, ncuda);
  848. starpu_force_bus_sampling();
  849. fprintf(stderr, "done\n");
  850. }
  851. else if (read_opencl != nopencl) {
  852. fprintf(stderr, "Current configuration does not match the bus performance model (OpenCL: (stored) %d != (current) %d), recalibrating...", read_opencl, nopencl);
  853. starpu_force_bus_sampling();
  854. fprintf(stderr, "done\n");
  855. }
  856. }
  857. }
  858. static void write_bus_config_file_content(void)
  859. {
  860. FILE *f;
  861. char path[256];
  862. STARPU_ASSERT(was_benchmarked);
  863. get_config_path(path, 256);
  864. f = fopen(path, "w+");
  865. STARPU_ASSERT(f);
  866. fprintf(f, "# Current configuration\n");
  867. fprintf(f, "%u # Number of CPUs\n", ncpus);
  868. fprintf(f, "%d # Number of CUDA devices\n", ncuda);
  869. fprintf(f, "%d # Number of OpenCL devices\n", nopencl);
  870. fclose(f);
  871. }
  872. static void generate_bus_config_file()
  873. {
  874. if (!was_benchmarked)
  875. benchmark_all_gpu_devices();
  876. write_bus_config_file_content();
  877. }
  878. /*
  879. * Generic
  880. */
  881. void starpu_force_bus_sampling(void)
  882. {
  883. _starpu_create_sampling_directory_if_needed();
  884. generate_bus_affinity_file();
  885. generate_bus_latency_file();
  886. generate_bus_bandwidth_file();
  887. generate_bus_config_file();
  888. }
  889. void _starpu_load_bus_performance_files(void)
  890. {
  891. _starpu_create_sampling_directory_if_needed();
  892. check_bus_config_file();
  893. load_bus_affinity_file();
  894. load_bus_latency_file();
  895. load_bus_bandwidth_file();
  896. }
  897. double _starpu_predict_transfer_time(unsigned src_node, unsigned dst_node, size_t size)
  898. {
  899. double bandwidth = bandwidth_matrix[src_node][dst_node];
  900. double latency = latency_matrix[src_node][dst_node];
  901. struct starpu_machine_topology_s *topology = &_starpu_get_machine_config()->topology;
  902. return latency + (size/bandwidth)*2*(topology->ncudagpus+topology->nopenclgpus);
  903. }