driver_cuda.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 <core/sched_ctx.h>
  28. #include <profiling/profiling.h>
  29. /* the number of CUDA devices */
  30. static int ncudagpus;
  31. static cudaStream_t streams[STARPU_NMAXWORKERS];
  32. static cudaStream_t transfer_streams[STARPU_NMAXWORKERS];
  33. /* In case we want to cap the amount of memory available on the GPUs by the
  34. * mean of the STARPU_LIMIT_GPU_MEM, we allocate a big buffer when the driver
  35. * is launched. */
  36. static char *wasted_memory[STARPU_NMAXWORKERS];
  37. static void limit_gpu_mem_if_needed(int devid)
  38. {
  39. cudaError_t cures;
  40. int limit = starpu_get_env_number("STARPU_LIMIT_GPU_MEM");
  41. if (limit == -1)
  42. {
  43. wasted_memory[devid] = NULL;
  44. return;
  45. }
  46. /* Find the size of the memory on the device */
  47. struct cudaDeviceProp prop;
  48. cures = cudaGetDeviceProperties(&prop, devid);
  49. if (STARPU_UNLIKELY(cures))
  50. STARPU_CUDA_REPORT_ERROR(cures);
  51. size_t totalGlobalMem = prop.totalGlobalMem;
  52. /* How much memory to waste ? */
  53. size_t to_waste = totalGlobalMem - (size_t)limit*1024*1024;
  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. cudaStream_t starpu_cuda_get_local_transfer_stream(void)
  74. {
  75. int worker = starpu_worker_get_id();
  76. return transfer_streams[worker];
  77. }
  78. cudaStream_t starpu_cuda_get_local_stream(void)
  79. {
  80. int worker = starpu_worker_get_id();
  81. return streams[worker];
  82. }
  83. static void init_context(int devid)
  84. {
  85. cudaError_t cures;
  86. int workerid = starpu_worker_get_id();
  87. cures = cudaSetDevice(devid);
  88. if (STARPU_UNLIKELY(cures))
  89. STARPU_CUDA_REPORT_ERROR(cures);
  90. /* force CUDA to initialize the context for real */
  91. cudaFree(0);
  92. limit_gpu_mem_if_needed(devid);
  93. cures = cudaStreamCreate(&streams[workerid]);
  94. if (STARPU_UNLIKELY(cures))
  95. STARPU_CUDA_REPORT_ERROR(cures);
  96. cures = cudaStreamCreate(&transfer_streams[workerid]);
  97. if (STARPU_UNLIKELY(cures))
  98. STARPU_CUDA_REPORT_ERROR(cures);
  99. }
  100. static void deinit_context(int workerid, int devid)
  101. {
  102. cudaError_t cures;
  103. cudaStreamDestroy(streams[workerid]);
  104. cudaStreamDestroy(transfer_streams[workerid]);
  105. unlimit_gpu_mem_if_needed(devid);
  106. /* cleanup the runtime API internal stuffs (which CUBLAS is using) */
  107. cures = cudaThreadExit();
  108. if (cures)
  109. STARPU_CUDA_REPORT_ERROR(cures);
  110. }
  111. unsigned _starpu_get_cuda_device_count(void)
  112. {
  113. int cnt;
  114. cudaError_t cures;
  115. cures = cudaGetDeviceCount(&cnt);
  116. if (STARPU_UNLIKELY(cures))
  117. return 0;
  118. return (unsigned)cnt;
  119. }
  120. void _starpu_init_cuda(void)
  121. {
  122. ncudagpus = _starpu_get_cuda_device_count();
  123. assert(ncudagpus <= STARPU_MAXCUDADEVS);
  124. }
  125. static int execute_job_on_cuda(starpu_job_t j, struct starpu_worker_s *args)
  126. {
  127. int ret;
  128. uint32_t mask = 0;
  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 th 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. cudaError_t 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. if ((profiling && profiling_info) || calibrate_model)
  157. {
  158. starpu_clock_gettime(&codelet_start);
  159. _starpu_worker_register_executing_start_date(workerid, &codelet_start);
  160. }
  161. args->status = STATUS_EXECUTING;
  162. task->status = STARPU_TASK_RUNNING;
  163. cl_func func = cl->cuda_func;
  164. STARPU_ASSERT(func);
  165. func(task->interface, task->cl_arg);
  166. cl->per_worker_stats[workerid]++;
  167. if ((profiling && profiling_info) || calibrate_model)
  168. starpu_clock_gettime(&codelet_end);
  169. STARPU_TRACE_END_CODELET_BODY(j);
  170. args->status = STATUS_UNKNOWN;
  171. _starpu_push_task_output(task, mask);
  172. _starpu_driver_update_job_feedback(j, args, profiling_info, args->perf_arch,
  173. &codelet_start, &codelet_end);
  174. return 0;
  175. }
  176. void *_starpu_cuda_worker(void *arg)
  177. {
  178. struct starpu_worker_s* args = arg;
  179. int devid = args->devid;
  180. int workerid = args->workerid;
  181. unsigned memnode = args->memory_node;
  182. #ifdef STARPU_USE_FXT
  183. _starpu_fxt_register_thread(args->bindid);
  184. #endif
  185. STARPU_TRACE_WORKER_INIT_START(STARPU_FUT_CUDA_KEY, devid, memnode);
  186. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  187. _starpu_set_local_memory_node_key(&memnode);
  188. _starpu_set_local_worker_key(args);
  189. init_context(devid);
  190. /* one more time to avoid hacks from third party lib :) */
  191. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  192. args->status = STATUS_UNKNOWN;
  193. /* get the device's name */
  194. char devname[128];
  195. struct cudaDeviceProp prop;
  196. cudaGetDeviceProperties(&prop, devid);
  197. strncpy(devname, prop.name, 128);
  198. snprintf(args->name, 32, "CUDA %d (%s)", args->devid, devname);
  199. _STARPU_DEBUG("cuda (%s) dev id %d thread is ready to run on CPU %d !\n", devname, devid, args->bindid);
  200. STARPU_TRACE_WORKER_INIT_END
  201. /* tell the main thread that this one is ready */
  202. PTHREAD_MUTEX_LOCK(&args->mutex);
  203. args->worker_is_initialized = 1;
  204. PTHREAD_COND_SIGNAL(&args->ready_cond);
  205. PTHREAD_MUTEX_UNLOCK(&args->mutex);
  206. struct starpu_job_s * j;
  207. struct starpu_task *task;
  208. int res;
  209. pthread_cond_t *sched_cond = args->sched_cond;
  210. pthread_mutex_t *sched_mutex = args->sched_mutex;
  211. pthread_cond_t *changing_ctx_cond = &args->changing_ctx_cond;
  212. pthread_mutex_t *changing_ctx_mutex = &args->changing_ctx_mutex;
  213. while (_starpu_machine_is_running())
  214. {
  215. STARPU_TRACE_START_PROGRESS(memnode);
  216. _starpu_datawizard_progress(memnode, 1);
  217. STARPU_TRACE_END_PROGRESS(memnode);
  218. /*when contex is changing block the threads belonging to it*/
  219. PTHREAD_MUTEX_LOCK(changing_ctx_mutex);
  220. if(args->status == STATUS_CHANGING_CTX){
  221. _starpu_increment_nblocked_ths(args->nworkers_of_next_ctx);
  222. _starpu_block_worker(workerid, changing_ctx_cond, changing_ctx_mutex);
  223. _starpu_decrement_nblocked_ths();
  224. }
  225. PTHREAD_MUTEX_UNLOCK(changing_ctx_mutex);
  226. PTHREAD_MUTEX_LOCK(sched_mutex);
  227. task = _starpu_pop_task(args);
  228. if (task == NULL)
  229. {
  230. if (_starpu_worker_can_block(memnode))
  231. _starpu_block_worker(workerid, sched_cond, sched_mutex);
  232. PTHREAD_MUTEX_UNLOCK(sched_mutex);
  233. continue;
  234. };
  235. PTHREAD_MUTEX_UNLOCK(sched_mutex);
  236. STARPU_ASSERT(task);
  237. j = _starpu_get_job_associated_to_task(task);
  238. /* can CUDA do that task ? */
  239. if (!STARPU_CUDA_MAY_PERFORM(j))
  240. {
  241. /* this is neither a cuda or a cublas task */
  242. _starpu_push_task(j, 0);
  243. continue;
  244. }
  245. _starpu_set_current_task(task);
  246. struct starpu_sched_ctx *local_sched_ctx = _starpu_get_sched_ctx(j->task->sched_ctx);
  247. res = execute_job_on_cuda(j, args);
  248. _starpu_set_current_task(NULL);
  249. if (res) {
  250. switch (res) {
  251. case -EAGAIN:
  252. _STARPU_DISP("ouch, put the codelet %p back ... \n", j);
  253. _starpu_push_task(j, 0);
  254. STARPU_ABORT();
  255. continue;
  256. default:
  257. assert(0);
  258. }
  259. }
  260. _starpu_handle_job_termination(j, 0);
  261. _starpu_decrement_nsubmitted_tasks_of_worker(args->workerid);
  262. _starpu_decrement_nsubmitted_tasks_of_sched_ctx(local_sched_ctx);
  263. }
  264. STARPU_TRACE_WORKER_DEINIT_START
  265. /* In case there remains some memory that was automatically
  266. * allocated by StarPU, we release it now. Note that data
  267. * coherency is not maintained anymore at that point ! */
  268. _starpu_free_all_automatically_allocated_buffers(memnode);
  269. deinit_context(args->workerid, args->devid);
  270. STARPU_TRACE_WORKER_DEINIT_END(STARPU_FUT_CUDA_KEY);
  271. pthread_exit(NULL);
  272. return NULL;
  273. }