perfmodel_bus.c 26 KB

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