driver_cuda.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 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. /* In case we want to cap the amount of memory available on the GPUs by the
  32. * mean of the STARPU_LIMIT_GPU_MEM, we allocate a big buffer when the driver
  33. * is launched. */
  34. static char *wasted_memory[STARPU_NMAXWORKERS];
  35. static void limit_gpu_mem_if_needed(int devid)
  36. {
  37. cudaError_t cures;
  38. int limit = starpu_get_env_number("STARPU_LIMIT_GPU_MEM");
  39. if (limit == -1)
  40. {
  41. wasted_memory[devid] = NULL;
  42. return;
  43. }
  44. /* Find the size of the memory on the device */
  45. struct cudaDeviceProp prop;
  46. cures = cudaGetDeviceProperties(&prop, devid);
  47. if (STARPU_UNLIKELY(cures))
  48. STARPU_CUDA_REPORT_ERROR(cures);
  49. size_t totalGlobalMem = prop.totalGlobalMem;
  50. /* How much memory to waste ? */
  51. size_t to_waste = totalGlobalMem - (size_t)limit*1024*1024;
  52. _STARPU_DEBUG("CUDA device %d: Wasting %ld MB / Limit %ld MB / Total %ld MB / Remains %ld MB\n",
  53. devid, (size_t)to_waste/(1024*1024), (size_t)limit, (size_t)totalGlobalMem/(1024*1024),
  54. (size_t)(totalGlobalMem - to_waste)/(1024*1024));
  55. /* Allocate a large buffer to waste memory and constraint the amount of available memory. */
  56. cures = cudaMalloc((void **)&wasted_memory[devid], to_waste);
  57. if (STARPU_UNLIKELY(cures))
  58. STARPU_CUDA_REPORT_ERROR(cures);
  59. }
  60. static void unlimit_gpu_mem_if_needed(int devid)
  61. {
  62. cudaError_t cures;
  63. if (wasted_memory[devid])
  64. {
  65. cures = cudaFree(wasted_memory[devid]);
  66. if (STARPU_UNLIKELY(cures))
  67. STARPU_CUDA_REPORT_ERROR(cures);
  68. wasted_memory[devid] = NULL;
  69. }
  70. }
  71. cudaStream_t starpu_cuda_get_local_stream(void)
  72. {
  73. int worker = starpu_worker_get_id();
  74. return streams[worker];
  75. }
  76. static void init_context(int devid)
  77. {
  78. cudaError_t cures;
  79. int workerid = starpu_worker_get_id();
  80. cures = cudaSetDevice(devid);
  81. if (STARPU_UNLIKELY(cures))
  82. STARPU_CUDA_REPORT_ERROR(cures);
  83. /* force CUDA to initialize the context for real */
  84. cudaFree(0);
  85. limit_gpu_mem_if_needed(devid);
  86. cures = cudaStreamCreate(&streams[workerid]);
  87. if (STARPU_UNLIKELY(cures))
  88. STARPU_CUDA_REPORT_ERROR(cures);
  89. }
  90. static void deinit_context(int workerid, int devid)
  91. {
  92. cudaError_t cures;
  93. cudaStreamDestroy(streams[workerid]);
  94. unlimit_gpu_mem_if_needed(devid);
  95. /* cleanup the runtime API internal stuffs (which CUBLAS is using) */
  96. cures = cudaThreadExit();
  97. if (cures)
  98. STARPU_CUDA_REPORT_ERROR(cures);
  99. }
  100. unsigned _starpu_get_cuda_device_count(void)
  101. {
  102. int cnt;
  103. cudaError_t cures;
  104. cures = cudaGetDeviceCount(&cnt);
  105. if (STARPU_UNLIKELY(cures))
  106. return 0;
  107. return (unsigned)cnt;
  108. }
  109. void _starpu_init_cuda(void)
  110. {
  111. ncudagpus = _starpu_get_cuda_device_count();
  112. assert(ncudagpus <= STARPU_MAXCUDADEVS);
  113. }
  114. static int execute_job_on_cuda(starpu_job_t j, struct starpu_worker_s *args)
  115. {
  116. int ret;
  117. uint32_t mask = 0;
  118. STARPU_ASSERT(j);
  119. struct starpu_task *task = j->task;
  120. struct timespec codelet_start, codelet_end;
  121. unsigned calibrate_model = 0;
  122. int workerid = args->workerid;
  123. STARPU_ASSERT(task);
  124. struct starpu_codelet_t *cl = task->cl;
  125. STARPU_ASSERT(cl);
  126. if (cl->model && cl->model->benchmarking)
  127. calibrate_model = 1;
  128. ret = _starpu_fetch_task_input(task, mask);
  129. if (ret != 0) {
  130. /* there was not enough memory, so the input of
  131. * the codelet cannot be fetched ... put the
  132. * codelet back, and try it later */
  133. return -EAGAIN;
  134. }
  135. if (calibrate_model)
  136. {
  137. cudaError_t cures = cudaThreadSynchronize();
  138. if (STARPU_UNLIKELY(cures))
  139. STARPU_CUDA_REPORT_ERROR(cures);
  140. }
  141. STARPU_TRACE_START_CODELET_BODY(j);
  142. struct starpu_task_profiling_info *profiling_info;
  143. int profiling = starpu_profiling_status_get();
  144. profiling_info = task->profiling_info;
  145. if ((profiling && profiling_info) || calibrate_model)
  146. {
  147. starpu_clock_gettime(&codelet_start);
  148. _starpu_worker_register_executing_start_date(workerid, &codelet_start);
  149. }
  150. args->status = STATUS_EXECUTING;
  151. task->status = STARPU_TASK_RUNNING;
  152. cl_func func = cl->cuda_func;
  153. STARPU_ASSERT(func);
  154. func(task->interface, task->cl_arg);
  155. cl->per_worker_stats[workerid]++;
  156. if ((profiling && profiling_info) || calibrate_model)
  157. starpu_clock_gettime(&codelet_end);
  158. STARPU_TRACE_END_CODELET_BODY(j);
  159. args->status = STATUS_UNKNOWN;
  160. _starpu_push_task_output(task, mask);
  161. _starpu_driver_update_job_feedback(j, args, profiling_info, args->perf_arch,
  162. &codelet_start, &codelet_end);
  163. return 0;
  164. }
  165. void *_starpu_cuda_worker(void *arg)
  166. {
  167. struct starpu_worker_s* args = arg;
  168. int devid = args->devid;
  169. int workerid = args->workerid;
  170. unsigned memnode = args->memory_node;
  171. #ifdef STARPU_USE_FXT
  172. _starpu_fxt_register_thread(args->bindid);
  173. #endif
  174. STARPU_TRACE_WORKER_INIT_START(STARPU_FUT_CUDA_KEY, devid, memnode);
  175. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  176. _starpu_set_local_memory_node_key(&memnode);
  177. _starpu_set_local_worker_key(args);
  178. init_context(devid);
  179. /* one more time to avoid hacks from third party lib :) */
  180. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  181. args->status = STATUS_UNKNOWN;
  182. /* get the device's name */
  183. char devname[128];
  184. struct cudaDeviceProp prop;
  185. cudaGetDeviceProperties(&prop, devid);
  186. strncpy(devname, prop.name, 128);
  187. snprintf(args->name, 32, "CUDA %d (%s)", args->devid, devname);
  188. _STARPU_DEBUG("cuda (%s) dev id %d thread is ready to run on CPU %d !\n", devname, devid, args->bindid);
  189. STARPU_TRACE_WORKER_INIT_END
  190. /* tell the main thread that this one is ready */
  191. PTHREAD_MUTEX_LOCK(&args->mutex);
  192. args->worker_is_initialized = 1;
  193. PTHREAD_COND_SIGNAL(&args->ready_cond);
  194. PTHREAD_MUTEX_UNLOCK(&args->mutex);
  195. struct starpu_job_s * j;
  196. struct starpu_task *task;
  197. int res;
  198. while (_starpu_machine_is_running())
  199. {
  200. STARPU_TRACE_START_PROGRESS(memnode);
  201. _starpu_datawizard_progress(memnode, 1);
  202. STARPU_TRACE_END_PROGRESS(memnode);
  203. PTHREAD_MUTEX_LOCK(args->sched_mutex);
  204. /* perhaps there is some local task to be executed first */
  205. task = _starpu_pop_local_task(args);
  206. /* otherwise ask a task to the scheduler */
  207. if (!task)
  208. task = _starpu_pop_task();
  209. if (task == NULL)
  210. {
  211. if (_starpu_worker_can_block(memnode))
  212. _starpu_block_worker(workerid, args->sched_cond, args->sched_mutex);
  213. PTHREAD_MUTEX_UNLOCK(args->sched_mutex);
  214. continue;
  215. };
  216. PTHREAD_MUTEX_UNLOCK(args->sched_mutex);
  217. STARPU_ASSERT(task);
  218. j = _starpu_get_job_associated_to_task(task);
  219. /* can CUDA do that task ? */
  220. if (!STARPU_CUDA_MAY_PERFORM(j))
  221. {
  222. /* this is neither a cuda or a cublas task */
  223. _starpu_push_task(j, 0);
  224. continue;
  225. }
  226. _starpu_set_current_task(task);
  227. res = execute_job_on_cuda(j, args);
  228. _starpu_set_current_task(NULL);
  229. if (res) {
  230. switch (res) {
  231. case -EAGAIN:
  232. _STARPU_DISP("ouch, put the codelet %p back ... \n", j);
  233. _starpu_push_task(j, 0);
  234. STARPU_ABORT();
  235. continue;
  236. default:
  237. assert(0);
  238. }
  239. }
  240. _starpu_handle_job_termination(j, 0);
  241. }
  242. STARPU_TRACE_WORKER_DEINIT_START
  243. /* In case there remains some memory that was automatically
  244. * allocated by StarPU, we release it now. Note that data
  245. * coherency is not maintained anymore at that point ! */
  246. _starpu_free_all_automatically_allocated_buffers(memnode);
  247. deinit_context(args->workerid, args->devid);
  248. STARPU_TRACE_WORKER_DEINIT_END(STARPU_FUT_CUDA_KEY);
  249. pthread_exit(NULL);
  250. return NULL;
  251. }