driver_cuda.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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. #include <starpu.h>
  17. #include <starpu_cuda.h>
  18. #include <starpu_profiling.h>
  19. #include <common/utils.h>
  20. #include <common/config.h>
  21. #include <core/debug.h>
  22. #include <drivers/driver_common/driver_common.h>
  23. #include "driver_cuda.h"
  24. #include <core/policies/sched_policy.h>
  25. #include <profiling/profiling.h>
  26. /* the number of CUDA devices */
  27. static int ncudagpus;
  28. static cudaStream_t streams[STARPU_NMAXWORKERS];
  29. cudaStream_t *starpu_cuda_get_local_stream(void)
  30. {
  31. int worker = starpu_worker_get_id();
  32. return &streams[worker];
  33. }
  34. static void init_context(int devid)
  35. {
  36. cudaError_t cures;
  37. cures = cudaSetDevice(devid);
  38. if (STARPU_UNLIKELY(cures))
  39. STARPU_CUDA_REPORT_ERROR(cures);
  40. /* force CUDA to initialize the context for real */
  41. cudaFree(0);
  42. cures = cudaStreamCreate(starpu_cuda_get_local_stream());
  43. if (STARPU_UNLIKELY(cures))
  44. STARPU_CUDA_REPORT_ERROR(cures);
  45. }
  46. static void deinit_context(int workerid)
  47. {
  48. cudaError_t cures;
  49. cudaStreamDestroy(streams[workerid]);
  50. /* cleanup the runtime API internal stuffs (which CUBLAS is using) */
  51. cures = cudaThreadExit();
  52. if (cures)
  53. STARPU_CUDA_REPORT_ERROR(cures);
  54. }
  55. unsigned _starpu_get_cuda_device_count(void)
  56. {
  57. int cnt;
  58. cudaError_t cures;
  59. cures = cudaGetDeviceCount(&cnt);
  60. if (STARPU_UNLIKELY(cures))
  61. STARPU_CUDA_REPORT_ERROR(cures);
  62. return (unsigned)cnt;
  63. }
  64. void _starpu_init_cuda(void)
  65. {
  66. ncudagpus = _starpu_get_cuda_device_count();
  67. assert(ncudagpus <= STARPU_MAXCUDADEVS);
  68. }
  69. static int execute_job_on_cuda(starpu_job_t j, struct starpu_worker_s *args)
  70. {
  71. int ret;
  72. uint32_t mask = 0;
  73. STARPU_ASSERT(j);
  74. struct starpu_task *task = j->task;
  75. cudaError_t cures;
  76. struct timespec codelet_start, codelet_end;
  77. struct timespec codelet_start_comm, codelet_end_comm;
  78. unsigned calibrate_model = 0;
  79. int workerid = args->workerid;
  80. STARPU_ASSERT(task);
  81. struct starpu_codelet_t *cl = task->cl;
  82. STARPU_ASSERT(cl);
  83. if (cl->model && cl->model->benchmarking)
  84. calibrate_model = 1;
  85. /* we do not take communication into account when modeling the performance */
  86. if (STARPU_BENCHMARK_COMM)
  87. {
  88. cures = cudaThreadSynchronize();
  89. if (STARPU_UNLIKELY(cures))
  90. STARPU_CUDA_REPORT_ERROR(cures);
  91. starpu_clock_gettime(&codelet_start_comm);
  92. }
  93. ret = _starpu_fetch_task_input(task, mask);
  94. if (ret != 0) {
  95. /* there was not enough memory, so the input of
  96. * the codelet cannot be fetched ... put the
  97. * codelet back, and try it later */
  98. return -EAGAIN;
  99. }
  100. if (STARPU_BENCHMARK_COMM)
  101. {
  102. cures = cudaThreadSynchronize();
  103. if (STARPU_UNLIKELY(cures))
  104. STARPU_CUDA_REPORT_ERROR(cures);
  105. starpu_clock_gettime(&codelet_end_comm);
  106. }
  107. STARPU_TRACE_START_CODELET_BODY(j);
  108. struct starpu_task_profiling_info *profiling_info;
  109. profiling_info = task->profiling_info;
  110. if (profiling_info || calibrate_model || STARPU_BENCHMARK_COMM)
  111. {
  112. starpu_clock_gettime(&codelet_start);
  113. _starpu_worker_register_executing_start_date(workerid, &codelet_start);
  114. }
  115. args->status = STATUS_EXECUTING;
  116. task->status = STARPU_TASK_RUNNING;
  117. cl_func func = cl->cuda_func;
  118. STARPU_ASSERT(func);
  119. func(task->interface, task->cl_arg);
  120. cl->per_worker_stats[workerid]++;
  121. if (profiling_info || calibrate_model || STARPU_BENCHMARK_COMM)
  122. starpu_clock_gettime(&codelet_end);
  123. STARPU_TRACE_END_CODELET_BODY(j);
  124. args->status = STATUS_UNKNOWN;
  125. _starpu_push_task_output(task, mask);
  126. _starpu_driver_update_job_feedback(j, args, profiling_info, calibrate_model,
  127. &codelet_start, &codelet_end, &codelet_start_comm, &codelet_end_comm);
  128. (void)STARPU_ATOMIC_ADD(&args->jobq->total_job_performed, 1);
  129. return 0;
  130. }
  131. void *_starpu_cuda_worker(void *arg)
  132. {
  133. struct starpu_worker_s* args = arg;
  134. struct starpu_jobq_s *jobq = args->jobq;
  135. int devid = args->devid;
  136. int workerid = args->workerid;
  137. unsigned memnode = args->memory_node;
  138. #ifdef STARPU_USE_FXT
  139. _starpu_fxt_register_thread(args->bindid);
  140. #endif
  141. STARPU_TRACE_WORKER_INIT_START(STARPU_FUT_CUDA_KEY, devid, memnode);
  142. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  143. _starpu_set_local_memory_node_key(&memnode);
  144. _starpu_set_local_queue(jobq);
  145. _starpu_set_local_worker_key(args);
  146. init_context(devid);
  147. /* one more time to avoid hacks from third party lib :) */
  148. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  149. args->status = STATUS_UNKNOWN;
  150. /* get the device's name */
  151. char devname[128];
  152. struct cudaDeviceProp prop;
  153. cudaGetDeviceProperties(&prop, devid);
  154. strncpy(devname, prop.name, 128);
  155. snprintf(args->name, 32, "CUDA %d (%s)", args->devid, devname);
  156. #ifdef STARPU_VERBOSE
  157. fprintf(stderr, "cuda (%s) dev id %d thread is ready to run on CPU %d !\n", devname, devid, args->bindid);
  158. #endif
  159. STARPU_TRACE_WORKER_INIT_END
  160. /* tell the main thread that this one is ready */
  161. PTHREAD_MUTEX_LOCK(&args->mutex);
  162. args->worker_is_initialized = 1;
  163. PTHREAD_COND_SIGNAL(&args->ready_cond);
  164. PTHREAD_MUTEX_UNLOCK(&args->mutex);
  165. struct starpu_job_s * j;
  166. int res;
  167. struct starpu_sched_policy_s *policy = _starpu_get_sched_policy();
  168. struct starpu_jobq_s *queue = policy->starpu_get_local_queue(policy);
  169. while (_starpu_machine_is_running())
  170. {
  171. STARPU_TRACE_START_PROGRESS(memnode);
  172. _starpu_datawizard_progress(memnode, 1);
  173. STARPU_TRACE_END_PROGRESS(memnode);
  174. _starpu_execute_registered_progression_hooks();
  175. _starpu_jobq_lock(queue);
  176. /* perhaps there is some local task to be executed first */
  177. j = _starpu_pop_local_task(args);
  178. /* otherwise ask a task to the scheduler */
  179. if (!j)
  180. j = _starpu_pop_task();
  181. if (j == NULL)
  182. {
  183. if (_starpu_worker_can_block(memnode))
  184. _starpu_block_worker(workerid, &queue->activity_cond, &queue->activity_mutex);
  185. _starpu_jobq_unlock(queue);
  186. continue;
  187. };
  188. _starpu_jobq_unlock(queue);
  189. /* can CUDA do that task ? */
  190. if (!STARPU_CUDA_MAY_PERFORM(j))
  191. {
  192. /* this is neither a cuda or a cublas task */
  193. _starpu_push_task(j, 0);
  194. continue;
  195. }
  196. _starpu_set_current_task(j->task);
  197. res = execute_job_on_cuda(j, args);
  198. _starpu_set_current_task(NULL);
  199. if (res) {
  200. switch (res) {
  201. case -EAGAIN:
  202. fprintf(stderr, "ouch, put the codelet %p back ... \n", j);
  203. _starpu_push_task(j, 0);
  204. STARPU_ABORT();
  205. continue;
  206. default:
  207. assert(0);
  208. }
  209. }
  210. _starpu_handle_job_termination(j, 0);
  211. }
  212. STARPU_TRACE_WORKER_DEINIT_START
  213. /* In case there remains some memory that was automatically
  214. * allocated by StarPU, we release it now. Note that data
  215. * coherency is not maintained anymore at that point ! */
  216. _starpu_free_all_automatically_allocated_buffers(memnode);
  217. deinit_context(args->workerid);
  218. #ifdef STARPU_DATA_STATS
  219. fprintf(stderr, "CUDA #%d computation %le comm %le (%lf \%%)\n", args->id, jobq->total_computation_time, jobq->total_communication_time, jobq->total_communication_time*100.0/jobq->total_computation_time);
  220. #endif
  221. #ifdef STARPU_VERBOSE
  222. double ratio = 0;
  223. if (jobq->total_job_performed != 0)
  224. {
  225. ratio = jobq->total_computation_time_error/jobq->total_computation_time;
  226. }
  227. _starpu_print_to_logfile("MODEL ERROR: CUDA %d ERROR %lf EXEC %lf RATIO %lf NTASKS %d\n", args->devid, jobq->total_computation_time_error, jobq->total_computation_time, ratio, jobq->total_job_performed);
  228. #endif
  229. STARPU_TRACE_WORKER_DEINIT_END(STARPU_FUT_CUDA_KEY);
  230. pthread_exit(NULL);
  231. return NULL;
  232. }