driver_cuda.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  6. * Copyright (C) 2011 Télécom-SudParis
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu.h>
  20. #include <starpu_cuda.h>
  21. #include <starpu_profiling.h>
  22. #include <common/utils.h>
  23. #include <common/config.h>
  24. #include <core/debug.h>
  25. #include <drivers/driver_common/driver_common.h>
  26. #include "driver_cuda.h"
  27. #include <core/sched_policy.h>
  28. #ifdef HAVE_CUDA_GL_INTEROP_H
  29. #include <cuda_gl_interop.h>
  30. #endif
  31. /* the number of CUDA devices */
  32. static int ncudagpus;
  33. static cudaStream_t streams[STARPU_NMAXWORKERS];
  34. static cudaStream_t transfer_streams[STARPU_NMAXWORKERS];
  35. static struct cudaDeviceProp props[STARPU_MAXCUDADEVS];
  36. /* In case we want to cap the amount of memory available on the GPUs by the
  37. * mean of the STARPU_LIMIT_GPU_MEM, we allocate a big buffer when the driver
  38. * is launched. */
  39. static char *wasted_memory[STARPU_NMAXWORKERS];
  40. static void limit_gpu_mem_if_needed(int devid)
  41. {
  42. cudaError_t cures;
  43. int limit = starpu_get_env_number("STARPU_LIMIT_GPU_MEM");
  44. if (limit == -1)
  45. {
  46. wasted_memory[devid] = NULL;
  47. return;
  48. }
  49. /* Find the size of the memory on the device */
  50. size_t totalGlobalMem = props[devid].totalGlobalMem;
  51. /* How much memory to waste ? */
  52. size_t to_waste = totalGlobalMem - (size_t)limit*1024*1024;
  53. props[devid].totalGlobalMem -= to_waste;
  54. _STARPU_DEBUG("CUDA device %d: Wasting %ld MB / Limit %ld MB / Total %ld MB / Remains %ld MB\n",
  55. devid, (size_t)to_waste/(1024*1024), (size_t)limit, (size_t)totalGlobalMem/(1024*1024),
  56. (size_t)(totalGlobalMem - to_waste)/(1024*1024));
  57. /* Allocate a large buffer to waste memory and constraint the amount of available memory. */
  58. cures = cudaMalloc((void **)&wasted_memory[devid], to_waste);
  59. if (STARPU_UNLIKELY(cures))
  60. STARPU_CUDA_REPORT_ERROR(cures);
  61. }
  62. static void unlimit_gpu_mem_if_needed(int devid)
  63. {
  64. cudaError_t cures;
  65. if (wasted_memory[devid])
  66. {
  67. cures = cudaFree(wasted_memory[devid]);
  68. if (STARPU_UNLIKELY(cures))
  69. STARPU_CUDA_REPORT_ERROR(cures);
  70. wasted_memory[devid] = NULL;
  71. }
  72. }
  73. size_t starpu_cuda_get_global_mem_size(int devid)
  74. {
  75. return (size_t)props[devid].totalGlobalMem;
  76. }
  77. cudaStream_t starpu_cuda_get_local_transfer_stream(void)
  78. {
  79. int worker = starpu_worker_get_id();
  80. return transfer_streams[worker];
  81. }
  82. cudaStream_t starpu_cuda_get_local_stream(void)
  83. {
  84. int worker = starpu_worker_get_id();
  85. return streams[worker];
  86. }
  87. const struct cudaDeviceProp *starpu_cuda_get_device_properties(unsigned workerid)
  88. {
  89. struct _starpu_machine_config *config = _starpu_get_machine_config();
  90. unsigned devid = config->workers[workerid].devid;
  91. return &props[devid];
  92. }
  93. void starpu_cuda_set_device(int devid)
  94. {
  95. cudaError_t cures;
  96. struct starpu_conf *conf = _starpu_get_machine_config()->conf;
  97. #if !defined(HAVE_CUDA_MEMCPY_PEER) && defined(HAVE_CUDA_GL_INTEROP_H)
  98. unsigned i;
  99. #endif
  100. #ifdef HAVE_CUDA_MEMCPY_PEER
  101. if (conf->n_cuda_opengl_interoperability) {
  102. fprintf(stderr, "OpenGL interoperability was requested, but StarPU was built with multithread GPU control support, please reconfigure with --disable-cuda-memcpy-peer but that will disable the memcpy-peer optimizations\n");
  103. STARPU_ABORT();
  104. }
  105. #elif !defined(HAVE_CUDA_GL_INTEROP_H)
  106. if (conf->n_cuda_opengl_interoperability) {
  107. fprintf(stderr,"OpenGL interoperability was requested, but cuda_gl_interop.h could not be compiled, please make sure that OpenGL headers were available before ./configure run.");
  108. STARPU_ABORT();
  109. }
  110. #else
  111. for (i = 0; i < conf->n_cuda_opengl_interoperability; i++)
  112. if (conf->cuda_opengl_interoperability[i] == devid) {
  113. cures = cudaGLSetGLDevice(devid);
  114. goto done;
  115. }
  116. #endif
  117. cures = cudaSetDevice(devid);
  118. #if !defined(HAVE_CUDA_MEMCPY_PEER) && defined(HAVE_CUDA_GL_INTEROP_H)
  119. done:
  120. #endif
  121. if (STARPU_UNLIKELY(cures))
  122. STARPU_CUDA_REPORT_ERROR(cures);
  123. }
  124. static void init_context(int devid)
  125. {
  126. cudaError_t cures;
  127. int workerid;
  128. /* TODO: cudaSetDeviceFlag(cudaDeviceMapHost) */
  129. starpu_cuda_set_device(devid);
  130. #ifdef HAVE_CUDA_MEMCPY_PEER
  131. if (starpu_get_env_number("STARPU_DISABLE_CUDA_GPU_GPU_DIRECT") <= 0) {
  132. int nworkers = starpu_worker_get_count();
  133. for (workerid = 0; workerid < nworkers; workerid++) {
  134. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  135. if (worker->arch == STARPU_CUDA_WORKER && worker->devid != devid) {
  136. int can;
  137. cures = cudaDeviceCanAccessPeer(&can, devid, worker->devid);
  138. if (!cures && can) {
  139. cures = cudaDeviceEnablePeerAccess(worker->devid, 0);
  140. if (!cures)
  141. _STARPU_DEBUG("GPU-Direct %d -> %d\n", worker->devid, devid);
  142. }
  143. }
  144. }
  145. }
  146. #endif
  147. /* force CUDA to initialize the context for real */
  148. cures = cudaFree(0);
  149. if (STARPU_UNLIKELY(cures)) {
  150. if (cures == cudaErrorDevicesUnavailable) {
  151. fprintf(stderr,"All CUDA-capable devices are busy or unavailable\n");
  152. exit(77);
  153. }
  154. STARPU_CUDA_REPORT_ERROR(cures);
  155. }
  156. cures = cudaGetDeviceProperties(&props[devid], devid);
  157. if (STARPU_UNLIKELY(cures))
  158. STARPU_CUDA_REPORT_ERROR(cures);
  159. #ifdef HAVE_CUDA_MEMCPY_PEER
  160. if (props[devid].computeMode == cudaComputeModeExclusive) {
  161. fprintf(stderr, "CUDA is in EXCLUSIVE-THREAD mode, but StarPU was built with multithread GPU control support, please either ask your administrator to use EXCLUSIVE-PROCESS mode (which should really be fine), or reconfigure with --disable-cuda-memcpy-peer but that will disable the memcpy-peer optimizations\n");
  162. STARPU_ABORT();
  163. }
  164. #endif
  165. limit_gpu_mem_if_needed(devid);
  166. workerid = starpu_worker_get_id();
  167. cures = cudaStreamCreate(&streams[workerid]);
  168. if (STARPU_UNLIKELY(cures))
  169. STARPU_CUDA_REPORT_ERROR(cures);
  170. cures = cudaStreamCreate(&transfer_streams[workerid]);
  171. if (STARPU_UNLIKELY(cures))
  172. STARPU_CUDA_REPORT_ERROR(cures);
  173. }
  174. static void deinit_context(int workerid, int devid)
  175. {
  176. cudaError_t cures;
  177. cudaStreamDestroy(streams[workerid]);
  178. cudaStreamDestroy(transfer_streams[workerid]);
  179. unlimit_gpu_mem_if_needed(devid);
  180. /* cleanup the runtime API internal stuffs (which CUBLAS is using) */
  181. cures = cudaThreadExit();
  182. if (cures)
  183. STARPU_CUDA_REPORT_ERROR(cures);
  184. }
  185. /* Return the number of devices usable in the system.
  186. * The value returned cannot be greater than MAXCUDADEVS */
  187. unsigned _starpu_get_cuda_device_count(void)
  188. {
  189. int cnt;
  190. cudaError_t cures;
  191. cures = cudaGetDeviceCount(&cnt);
  192. if (STARPU_UNLIKELY(cures))
  193. return 0;
  194. if (cnt > STARPU_MAXCUDADEVS)
  195. {
  196. fprintf(stderr, "# Warning: %d CUDA devices available. Only %d enabled. Use configure option --enable-maxcudadev=xxx to update the maximum value of supported CUDA devices.\n", cnt, STARPU_MAXCUDADEVS);
  197. cnt = STARPU_MAXCUDADEVS;
  198. }
  199. return (unsigned)cnt;
  200. }
  201. void _starpu_init_cuda(void)
  202. {
  203. ncudagpus = _starpu_get_cuda_device_count();
  204. STARPU_ASSERT(ncudagpus <= STARPU_MAXCUDADEVS);
  205. }
  206. static int execute_job_on_cuda(struct _starpu_job *j, struct _starpu_worker *args)
  207. {
  208. int ret;
  209. uint32_t mask = 0;
  210. cudaError_t cures;
  211. STARPU_ASSERT(j);
  212. struct starpu_task *task = j->task;
  213. struct timespec codelet_start, codelet_end;
  214. int profiling = starpu_profiling_status_get();
  215. unsigned calibrate_model = 0;
  216. STARPU_ASSERT(task);
  217. struct starpu_codelet *cl = task->cl;
  218. STARPU_ASSERT(cl);
  219. if (cl->model && cl->model->benchmarking)
  220. calibrate_model = 1;
  221. ret = _starpu_fetch_task_input(j, mask);
  222. if (ret != 0)
  223. {
  224. /* there was not enough memory, so the input of
  225. * the codelet cannot be fetched ... put the
  226. * codelet back, and try it later */
  227. return -EAGAIN;
  228. }
  229. if (calibrate_model)
  230. {
  231. cures = cudaStreamSynchronize(starpu_cuda_get_local_transfer_stream());
  232. if (STARPU_UNLIKELY(cures))
  233. STARPU_CUDA_REPORT_ERROR(cures);
  234. }
  235. _starpu_driver_start_job(args, j, &codelet_start, 0, profiling);
  236. #ifdef HAVE_CUDA_MEMCPY_PEER
  237. /* We make sure we do manipulate the proper device */
  238. starpu_cuda_set_device(args->devid);
  239. #endif
  240. starpu_cuda_func_t func = _starpu_task_get_cuda_nth_implementation(cl, j->nimpl);
  241. STARPU_ASSERT(func);
  242. func(task->interfaces, task->cl_arg);
  243. _starpu_driver_end_job(args, j, args->perf_arch, &codelet_end, 0, profiling);
  244. _starpu_driver_update_job_feedback(j, args, args->perf_arch, &codelet_start, &codelet_end, profiling);
  245. _starpu_push_task_output(j, mask);
  246. return 0;
  247. }
  248. static struct _starpu_worker*
  249. _starpu_get_worker_from_driver(struct starpu_driver *d)
  250. {
  251. unsigned nworkers = starpu_worker_get_count();
  252. unsigned workerid;
  253. for (workerid = 0; workerid < nworkers; workerid++)
  254. {
  255. if (starpu_worker_get_type(workerid) == d->type)
  256. {
  257. struct _starpu_worker *worker;
  258. worker = _starpu_get_worker_struct(workerid);
  259. if (worker->devid == d->id.cuda_id)
  260. return worker;
  261. }
  262. }
  263. return NULL;
  264. }
  265. /* XXX Should this be merged with _starpu_init_cuda ? */
  266. int _starpu_cuda_driver_init(struct starpu_driver *d)
  267. {
  268. struct _starpu_worker* args = _starpu_get_worker_from_driver(d);
  269. STARPU_ASSERT(args);
  270. int devid = args->devid;
  271. unsigned memory_node = args->memory_node;
  272. #ifdef STARPU_USE_FXT
  273. _starpu_fxt_register_thread(args->bindid);
  274. #endif
  275. _STARPU_TRACE_WORKER_INIT_START(_STARPU_FUT_CUDA_KEY, devid, memory_node);
  276. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  277. _starpu_set_local_memory_node_key(&args->memory_node);
  278. _starpu_set_local_worker_key(args);
  279. init_context(devid);
  280. /* one more time to avoid hacks from third party lib :) */
  281. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  282. args->status = STATUS_UNKNOWN;
  283. /* get the device's name */
  284. char devname[128];
  285. strncpy(devname, props[devid].name, 128);
  286. float size = (float) props[devid].totalGlobalMem / (1<<30);
  287. #ifdef STARPU_HAVE_BUSID
  288. #ifdef STARPU_HAVE_DOMAINID
  289. if (props[devid].pciDomainID)
  290. snprintf(args->name, sizeof(args->name), "CUDA %d (%s %.1f GiB %04x:%02x:%02x.0)", args->devid, devname, size, props[devid].pciDomainID, props[devid].pciBusID, props[devid].pciDeviceID);
  291. else
  292. #endif
  293. snprintf(args->name, sizeof(args->name), "CUDA %d (%s %.1f GiB %02x:%02x.0)", args->devid, devname, size, props[devid].pciBusID, props[devid].pciDeviceID);
  294. #else
  295. snprintf(args->name, sizeof(args->name), "CUDA %d (%s %.1f GiB)", args->devid, devname, size);
  296. #endif
  297. snprintf(args->short_name, sizeof(args->short_name), "CUDA %d", args->devid);
  298. _STARPU_DEBUG("cuda (%s) dev id %d thread is ready to run on CPU %d !\n", devname, devid, args->bindid);
  299. _STARPU_TRACE_WORKER_INIT_END
  300. /* tell the main thread that this one is ready */
  301. _STARPU_PTHREAD_MUTEX_LOCK(&args->mutex);
  302. args->worker_is_initialized = 1;
  303. _STARPU_PTHREAD_COND_SIGNAL(&args->ready_cond);
  304. _STARPU_PTHREAD_MUTEX_UNLOCK(&args->mutex);
  305. return 0;
  306. }
  307. int _starpu_cuda_driver_run_once(struct starpu_driver *d)
  308. {
  309. struct _starpu_worker* args = _starpu_get_worker_from_driver(d);
  310. STARPU_ASSERT(args);
  311. unsigned memnode = args->memory_node;
  312. int workerid = args->workerid;
  313. _STARPU_TRACE_START_PROGRESS(memnode);
  314. _starpu_datawizard_progress(memnode, 1);
  315. _STARPU_TRACE_END_PROGRESS(memnode);
  316. struct starpu_task *task;
  317. struct _starpu_job *j = NULL;
  318. task = _starpu_get_worker_task(args, workerid, memnode);
  319. if (!task)
  320. return 0;
  321. j = _starpu_get_job_associated_to_task(task);
  322. /* can CUDA do that task ? */
  323. if (!_STARPU_CUDA_MAY_PERFORM(j))
  324. {
  325. /* this is neither a cuda or a cublas task */
  326. _starpu_push_task(j);
  327. return 0;
  328. }
  329. _starpu_set_current_task(task);
  330. args->current_task = j->task;
  331. int res = execute_job_on_cuda(j, args);
  332. _starpu_set_current_task(NULL);
  333. args->current_task = NULL;
  334. if (res)
  335. {
  336. switch (res)
  337. {
  338. case -EAGAIN:
  339. _STARPU_DISP("ouch, put the codelet %p back ... \n", j);
  340. _starpu_push_task(j);
  341. STARPU_ABORT();
  342. default:
  343. STARPU_ABORT();
  344. }
  345. }
  346. _starpu_handle_job_termination(j);
  347. return 0;
  348. }
  349. int _starpu_cuda_driver_deinit(struct starpu_driver *d)
  350. {
  351. struct _starpu_worker* args = _starpu_get_worker_from_driver(d);
  352. STARPU_ASSERT(args);
  353. unsigned memnode = args->memory_node;
  354. _STARPU_TRACE_WORKER_DEINIT_START
  355. _starpu_handle_all_pending_node_data_requests(memnode);
  356. /* In case there remains some memory that was automatically
  357. * allocated by StarPU, we release it now. Note that data
  358. * coherency is not maintained anymore at that point ! */
  359. _starpu_free_all_automatically_allocated_buffers(memnode);
  360. deinit_context(args->workerid, args->devid);
  361. _STARPU_TRACE_WORKER_DEINIT_END(_STARPU_FUT_CUDA_KEY);
  362. return 0;
  363. }
  364. void *_starpu_cuda_worker(void *arg)
  365. {
  366. struct _starpu_worker* args = arg;
  367. struct starpu_driver d = {
  368. .type = STARPU_CUDA_WORKER,
  369. .id.cuda_id = args->devid
  370. };
  371. _starpu_cuda_driver_init(&d);
  372. while (_starpu_machine_is_running())
  373. _starpu_cuda_driver_run_once(&d);
  374. _starpu_cuda_driver_deinit(&d);
  375. return NULL;
  376. }
  377. void starpu_cublas_report_error(const char *func, const char *file, int line, cublasStatus status)
  378. {
  379. char *errormsg;
  380. switch (status)
  381. {
  382. case CUBLAS_STATUS_SUCCESS:
  383. errormsg = "success";
  384. break;
  385. case CUBLAS_STATUS_NOT_INITIALIZED:
  386. errormsg = "not initialized";
  387. break;
  388. case CUBLAS_STATUS_ALLOC_FAILED:
  389. errormsg = "alloc failed";
  390. break;
  391. case CUBLAS_STATUS_INVALID_VALUE:
  392. errormsg = "invalid value";
  393. break;
  394. case CUBLAS_STATUS_ARCH_MISMATCH:
  395. errormsg = "arch mismatch";
  396. break;
  397. case CUBLAS_STATUS_EXECUTION_FAILED:
  398. errormsg = "execution failed";
  399. break;
  400. case CUBLAS_STATUS_INTERNAL_ERROR:
  401. errormsg = "internal error";
  402. break;
  403. default:
  404. errormsg = "unknown error";
  405. break;
  406. }
  407. fprintf(stderr, "oops in %s (%s:%u)... %d: %s \n", func, file, line, status, errormsg);
  408. STARPU_ABORT();
  409. }
  410. void starpu_cuda_report_error(const char *func, const char *file, int line, cudaError_t status)
  411. {
  412. const char *errormsg = cudaGetErrorString(status);
  413. printf("oops in %s (%s:%u)... %d: %s \n", func, file, line, status, errormsg);
  414. STARPU_ABORT();
  415. }
  416. int starpu_cuda_copy_async_sync(void *src_ptr, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_ptr, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, size_t ssize, cudaStream_t stream, enum cudaMemcpyKind kind)
  417. {
  418. cudaError_t cures = 0;
  419. if (stream)
  420. {
  421. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  422. cures = cudaMemcpyAsync((char *)dst_ptr, (char *)src_ptr, ssize, kind, stream);
  423. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  424. }
  425. /* Test if the asynchronous copy has failed or if the caller only asked for a synchronous copy */
  426. if (stream == NULL || cures)
  427. {
  428. /* do it in a synchronous fashion */
  429. cures = cudaMemcpy((char *)dst_ptr, (char *)src_ptr, ssize, kind);
  430. if (STARPU_UNLIKELY(cures))
  431. STARPU_CUDA_REPORT_ERROR(cures);
  432. return 0;
  433. }
  434. return -EAGAIN;
  435. }
  436. int _starpu_run_cuda(struct starpu_driver *d)
  437. {
  438. STARPU_ASSERT(d && d->type == STARPU_CUDA_WORKER);
  439. int workers[d->id.cuda_id + 1];
  440. int nworkers;
  441. nworkers = starpu_worker_get_ids_by_type(STARPU_CUDA_WORKER, workers, d->id.cuda_id+1);
  442. if (nworkers >= 0 && (unsigned) nworkers < d->id.cuda_id)
  443. return -ENODEV;
  444. _STARPU_DEBUG("Running cuda %d from the application\n", d->id.cuda_id);
  445. struct _starpu_worker *workerarg = _starpu_get_worker_struct(workers[d->id.cuda_id]);
  446. workerarg->set = NULL;
  447. workerarg->worker_is_initialized = 0;
  448. /* Let's go ! */
  449. _starpu_cuda_worker(workerarg);
  450. /* XXX: Should we wait for the driver to be ready, as it is done when
  451. * launching it the usual way ? Cf. the end of _starpu_launch_drivers()
  452. */
  453. return 0;
  454. }