driver_cuda.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  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. #include <starpu.h>
  19. #include <starpu_cuda.h>
  20. #include <starpu_profiling.h>
  21. #include <common/utils.h>
  22. #include <common/config.h>
  23. #include <core/debug.h>
  24. #include <drivers/driver_common/driver_common.h>
  25. #include "driver_cuda.h"
  26. #include <core/sched_policy.h>
  27. #include <profiling/profiling.h>
  28. /* the number of CUDA devices */
  29. static int ncudagpus;
  30. static cudaStream_t streams[STARPU_NMAXWORKERS];
  31. static cudaStream_t transfer_streams[STARPU_NMAXWORKERS];
  32. /* In case we want to cap the amount of memory available on the GPUs by the
  33. * mean of the STARPU_LIMIT_GPU_MEM, we allocate a big buffer when the driver
  34. * is launched. */
  35. static char *wasted_memory[STARPU_NMAXWORKERS];
  36. static void limit_gpu_mem_if_needed(int devid)
  37. {
  38. cudaError_t cures;
  39. int limit = starpu_get_env_number("STARPU_LIMIT_GPU_MEM");
  40. if (limit == -1)
  41. {
  42. wasted_memory[devid] = NULL;
  43. return;
  44. }
  45. /* Find the size of the memory on the device */
  46. struct cudaDeviceProp prop;
  47. cures = cudaGetDeviceProperties(&prop, devid);
  48. if (STARPU_UNLIKELY(cures))
  49. STARPU_CUDA_REPORT_ERROR(cures);
  50. size_t totalGlobalMem = prop.totalGlobalMem;
  51. /* How much memory to waste ? */
  52. size_t to_waste = totalGlobalMem - (size_t)limit*1024*1024;
  53. _STARPU_DEBUG("CUDA device %d: Wasting %ld MB / Limit %ld MB / Total %ld MB / Remains %ld MB\n",
  54. devid, (size_t)to_waste/(1024*1024), (size_t)limit, (size_t)totalGlobalMem/(1024*1024),
  55. (size_t)(totalGlobalMem - to_waste)/(1024*1024));
  56. /* Allocate a large buffer to waste memory and constraint the amount of available memory. */
  57. cures = cudaMalloc((void **)&wasted_memory[devid], to_waste);
  58. if (STARPU_UNLIKELY(cures))
  59. STARPU_CUDA_REPORT_ERROR(cures);
  60. }
  61. static void unlimit_gpu_mem_if_needed(int devid)
  62. {
  63. cudaError_t cures;
  64. if (wasted_memory[devid])
  65. {
  66. cures = cudaFree(wasted_memory[devid]);
  67. if (STARPU_UNLIKELY(cures))
  68. STARPU_CUDA_REPORT_ERROR(cures);
  69. wasted_memory[devid] = NULL;
  70. }
  71. }
  72. cudaStream_t starpu_cuda_get_local_transfer_stream(void)
  73. {
  74. int worker = starpu_worker_get_id();
  75. return transfer_streams[worker];
  76. }
  77. cudaStream_t starpu_cuda_get_local_stream(void)
  78. {
  79. int worker = starpu_worker_get_id();
  80. return streams[worker];
  81. }
  82. static void init_context(int devid)
  83. {
  84. cudaError_t cures;
  85. int workerid = starpu_worker_get_id();
  86. cures = cudaSetDevice(devid);
  87. if (STARPU_UNLIKELY(cures))
  88. STARPU_CUDA_REPORT_ERROR(cures);
  89. /* force CUDA to initialize the context for real */
  90. cudaFree(0);
  91. limit_gpu_mem_if_needed(devid);
  92. cures = cudaStreamCreate(&streams[workerid]);
  93. if (STARPU_UNLIKELY(cures))
  94. STARPU_CUDA_REPORT_ERROR(cures);
  95. cures = cudaStreamCreate(&transfer_streams[workerid]);
  96. if (STARPU_UNLIKELY(cures))
  97. STARPU_CUDA_REPORT_ERROR(cures);
  98. }
  99. static void deinit_context(int workerid, int devid)
  100. {
  101. cudaError_t cures;
  102. cudaStreamDestroy(streams[workerid]);
  103. cudaStreamDestroy(transfer_streams[workerid]);
  104. unlimit_gpu_mem_if_needed(devid);
  105. /* cleanup the runtime API internal stuffs (which CUBLAS is using) */
  106. cures = cudaThreadExit();
  107. if (cures)
  108. STARPU_CUDA_REPORT_ERROR(cures);
  109. }
  110. unsigned _starpu_get_cuda_device_count(void)
  111. {
  112. int cnt;
  113. cudaError_t cures;
  114. cures = cudaGetDeviceCount(&cnt);
  115. if (STARPU_UNLIKELY(cures))
  116. return 0;
  117. return (unsigned)cnt;
  118. }
  119. void _starpu_init_cuda(void)
  120. {
  121. ncudagpus = _starpu_get_cuda_device_count();
  122. assert(ncudagpus <= STARPU_MAXCUDADEVS);
  123. }
  124. static int execute_job_on_cuda(starpu_job_t j, struct starpu_worker_s *args)
  125. {
  126. int ret;
  127. uint32_t mask = 0;
  128. cudaError_t cures;
  129. STARPU_ASSERT(j);
  130. struct starpu_task *task = j->task;
  131. struct timespec codelet_start, codelet_end;
  132. unsigned calibrate_model = 0;
  133. int workerid = args->workerid;
  134. STARPU_ASSERT(task);
  135. struct starpu_codelet_t *cl = task->cl;
  136. STARPU_ASSERT(cl);
  137. if (cl->model && cl->model->benchmarking)
  138. calibrate_model = 1;
  139. ret = _starpu_fetch_task_input(task, mask);
  140. if (ret != 0) {
  141. /* there was not enough memory, so the input of
  142. * the codelet cannot be fetched ... put the
  143. * codelet back, and try it later */
  144. return -EAGAIN;
  145. }
  146. if (calibrate_model)
  147. {
  148. cures = cudaStreamSynchronize(starpu_cuda_get_local_transfer_stream());
  149. if (STARPU_UNLIKELY(cures))
  150. STARPU_CUDA_REPORT_ERROR(cures);
  151. }
  152. STARPU_TRACE_START_CODELET_BODY(j);
  153. struct starpu_task_profiling_info *profiling_info;
  154. int profiling = starpu_profiling_status_get();
  155. profiling_info = task->profiling_info;
  156. #ifdef HAVE_CUDA_MEMCPY_PEER
  157. /* We make sure we do manipulate the proper device */
  158. cures = cudaSetDevice(args->devid);
  159. #endif
  160. if ((profiling && profiling_info) || calibrate_model)
  161. {
  162. starpu_clock_gettime(&codelet_start);
  163. _starpu_worker_register_executing_start_date(workerid, &codelet_start);
  164. }
  165. args->status = STATUS_EXECUTING;
  166. task->status = STARPU_TASK_RUNNING;
  167. cl_func func = cl->cuda_func;
  168. STARPU_ASSERT(func);
  169. func(task->interfaces, task->cl_arg);
  170. cl->per_worker_stats[workerid]++;
  171. if ((profiling && profiling_info) || calibrate_model)
  172. starpu_clock_gettime(&codelet_end);
  173. enum starpu_perf_archtype archtype = args->perf_arch;
  174. STARPU_TRACE_END_CODELET_BODY(j, archtype);
  175. args->status = STATUS_UNKNOWN;
  176. _starpu_push_task_output(task, mask);
  177. _starpu_driver_update_job_feedback(j, args, profiling_info, archtype,
  178. &codelet_start, &codelet_end);
  179. return 0;
  180. }
  181. void *_starpu_cuda_worker(void *arg)
  182. {
  183. struct starpu_worker_s* args = arg;
  184. int devid = args->devid;
  185. int workerid = args->workerid;
  186. unsigned memnode = args->memory_node;
  187. #ifdef STARPU_USE_FXT
  188. _starpu_fxt_register_thread(args->bindid);
  189. #endif
  190. STARPU_TRACE_WORKER_INIT_START(STARPU_FUT_CUDA_KEY, devid, memnode);
  191. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  192. _starpu_set_local_memory_node_key(&memnode);
  193. _starpu_set_local_worker_key(args);
  194. init_context(devid);
  195. /* one more time to avoid hacks from third party lib :) */
  196. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  197. args->status = STATUS_UNKNOWN;
  198. /* get the device's name */
  199. char devname[128];
  200. struct cudaDeviceProp prop;
  201. cudaGetDeviceProperties(&prop, devid);
  202. strncpy(devname, prop.name, 128);
  203. snprintf(args->name, 32, "CUDA %d (%s)", args->devid, devname);
  204. _STARPU_DEBUG("cuda (%s) dev id %d thread is ready to run on CPU %d !\n", devname, devid, args->bindid);
  205. STARPU_TRACE_WORKER_INIT_END
  206. /* tell the main thread that this one is ready */
  207. PTHREAD_MUTEX_LOCK(&args->mutex);
  208. args->worker_is_initialized = 1;
  209. PTHREAD_COND_SIGNAL(&args->ready_cond);
  210. PTHREAD_MUTEX_UNLOCK(&args->mutex);
  211. struct starpu_job_s * j;
  212. struct starpu_task *task;
  213. int res;
  214. while (_starpu_machine_is_running())
  215. {
  216. STARPU_TRACE_START_PROGRESS(memnode);
  217. _starpu_datawizard_progress(memnode, 1);
  218. STARPU_TRACE_END_PROGRESS(memnode);
  219. PTHREAD_MUTEX_LOCK(args->sched_mutex);
  220. task = _starpu_pop_task(args);
  221. if (task == NULL)
  222. {
  223. if (_starpu_worker_can_block(memnode))
  224. _starpu_block_worker(workerid, args->sched_cond, args->sched_mutex);
  225. PTHREAD_MUTEX_UNLOCK(args->sched_mutex);
  226. continue;
  227. };
  228. PTHREAD_MUTEX_UNLOCK(args->sched_mutex);
  229. STARPU_ASSERT(task);
  230. j = _starpu_get_job_associated_to_task(task);
  231. /* can CUDA do that task ? */
  232. if (!STARPU_CUDA_MAY_PERFORM(j))
  233. {
  234. /* this is neither a cuda or a cublas task */
  235. _starpu_push_task(j, 0);
  236. continue;
  237. }
  238. _starpu_set_current_task(task);
  239. res = execute_job_on_cuda(j, args);
  240. _starpu_set_current_task(NULL);
  241. if (res) {
  242. switch (res) {
  243. case -EAGAIN:
  244. _STARPU_DISP("ouch, put the codelet %p back ... \n", j);
  245. _starpu_push_task(j, 0);
  246. STARPU_ABORT();
  247. continue;
  248. default:
  249. assert(0);
  250. }
  251. }
  252. _starpu_handle_job_termination(j, 0);
  253. }
  254. STARPU_TRACE_WORKER_DEINIT_START
  255. /* In case there remains some memory that was automatically
  256. * allocated by StarPU, we release it now. Note that data
  257. * coherency is not maintained anymore at that point ! */
  258. _starpu_free_all_automatically_allocated_buffers(memnode);
  259. deinit_context(args->workerid, args->devid);
  260. STARPU_TRACE_WORKER_DEINIT_END(STARPU_FUT_CUDA_KEY);
  261. pthread_exit(NULL);
  262. return NULL;
  263. }
  264. void starpu_cublas_report_error(const char *func, cublasStatus status)
  265. {
  266. char *errormsg;
  267. switch (status) {
  268. case CUBLAS_STATUS_SUCCESS:
  269. errormsg = "success";
  270. break;
  271. case CUBLAS_STATUS_NOT_INITIALIZED:
  272. errormsg = "not initialized";
  273. break;
  274. case CUBLAS_STATUS_ALLOC_FAILED:
  275. errormsg = "alloc failed";
  276. break;
  277. case CUBLAS_STATUS_INVALID_VALUE:
  278. errormsg = "invalid value";
  279. break;
  280. case CUBLAS_STATUS_ARCH_MISMATCH:
  281. errormsg = "arch mismatch";
  282. break;
  283. case CUBLAS_STATUS_EXECUTION_FAILED:
  284. errormsg = "execution failed";
  285. break;
  286. case CUBLAS_STATUS_INTERNAL_ERROR:
  287. errormsg = "internal error";
  288. break;
  289. default:
  290. errormsg = "unknown error";
  291. break;
  292. }
  293. printf("oops in %s ... %s \n", func, errormsg);
  294. assert(0);
  295. }
  296. void starpu_cuda_report_error(const char *func, CUresult status)
  297. {
  298. const char *errormsg = cudaGetErrorString(status);
  299. printf("oops in %s ... %s \n", func, errormsg);
  300. assert(0);
  301. }