driver_opencl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2010 (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 <math.h>
  17. #include <common/config.h>
  18. #include <common/utils.h>
  19. #include <core/debug.h>
  20. #include <starpu_opencl.h>
  21. #include "driver_opencl.h"
  22. #include "driver_opencl_utils.h"
  23. #include <common/utils.h>
  24. static cl_context contexts[STARPU_MAXOPENCLDEVS];
  25. static cl_device_id devices[STARPU_MAXOPENCLDEVS];
  26. static cl_command_queue queues[STARPU_MAXOPENCLDEVS];
  27. static cl_uint nb_devices = -1;
  28. static int init_done = 0;
  29. extern char *_starpu_opencl_codelet_dir;
  30. void starpu_opencl_get_context(int devid, cl_context *context)
  31. {
  32. *context = contexts[devid];
  33. }
  34. void starpu_opencl_get_device(int devid, cl_device_id *device)
  35. {
  36. *device = devices[devid];
  37. }
  38. void starpu_opencl_get_queue(int devid, cl_command_queue *queue)
  39. {
  40. *queue = queues[devid];
  41. }
  42. int _starpu_opencl_init_context(int devid)
  43. {
  44. cl_int err;
  45. cl_device_id device;
  46. _STARPU_OPENCL_DEBUG("Initialising context for dev %d\n", devid);
  47. // Create a compute context
  48. device = devices[devid];
  49. contexts[devid] = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
  50. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  51. // Create queue for the given device
  52. queues[devid] = clCreateCommandQueue(contexts[devid], devices[devid], 0, &err);
  53. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  54. _starpu_opencl_init_programs(devid);
  55. return EXIT_SUCCESS;
  56. }
  57. int _starpu_opencl_deinit_context(int devid)
  58. {
  59. int err;
  60. _STARPU_OPENCL_DEBUG("De-initialising context for dev %d\n", devid);
  61. err = clReleaseContext(contexts[devid]);
  62. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  63. err = clReleaseCommandQueue(queues[devid]);
  64. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  65. _starpu_opencl_release_programs(devid);
  66. return EXIT_SUCCESS;
  67. }
  68. int _starpu_opencl_allocate_memory(void **addr, size_t size, cl_mem_flags flags)
  69. {
  70. cl_int err;
  71. cl_mem address;
  72. struct starpu_worker_s *worker = _starpu_get_local_worker_key();
  73. address = clCreateBuffer(contexts[worker->devid], flags, size, NULL, &err);
  74. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  75. *addr = address;
  76. return EXIT_SUCCESS;
  77. }
  78. int _starpu_opencl_copy_to_opencl(void *ptr, cl_mem buffer, size_t size, size_t offset, cl_event *event)
  79. {
  80. int err;
  81. struct starpu_worker_s *worker = _starpu_get_local_worker_key();
  82. if (event == NULL) {
  83. err = clEnqueueWriteBuffer(queues[worker->devid], buffer, CL_TRUE, offset, size, ptr, 0, NULL, NULL);
  84. }
  85. else {
  86. err = clEnqueueWriteBuffer(queues[worker->devid], buffer, CL_FALSE, offset, size, ptr, 0, NULL, event);
  87. }
  88. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  89. return EXIT_SUCCESS;
  90. }
  91. int _starpu_opencl_copy_from_opencl(cl_mem buffer, void *ptr, size_t size, size_t offset, cl_event *event)
  92. {
  93. int err;
  94. struct starpu_worker_s *worker = _starpu_get_local_worker_key();
  95. if (event == NULL) {
  96. err = clEnqueueReadBuffer(queues[worker->devid], buffer, CL_TRUE, offset, size, ptr, 0, NULL, NULL);
  97. }
  98. else {
  99. err = clEnqueueReadBuffer(queues[worker->devid], buffer, CL_FALSE, offset, size, ptr, 0, NULL, event);
  100. }
  101. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  102. return EXIT_SUCCESS;
  103. }
  104. void _starpu_opencl_init()
  105. {
  106. if (!init_done) {
  107. cl_platform_id platform_id[STARPU_OPENCL_PLATFORM_MAX];
  108. cl_uint nb_platforms;
  109. cl_device_type device_type = CL_DEVICE_TYPE_GPU;
  110. cl_int err;
  111. _STARPU_OPENCL_DEBUG("Initialising OpenCL\n");
  112. // Get Platforms
  113. err = clGetPlatformIDs(STARPU_OPENCL_PLATFORM_MAX, platform_id, &nb_platforms);
  114. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  115. _STARPU_OPENCL_DEBUG("Platforms detected: %d\n", nb_platforms);
  116. // Get devices
  117. nb_devices = 0;
  118. {
  119. unsigned int i;
  120. for (i=0; i<nb_platforms; i++) {
  121. cl_uint num;
  122. #ifdef STARPU_VERBOSE
  123. {
  124. char name[1024], vendor[1024];
  125. clGetPlatformInfo(platform_id[i], CL_PLATFORM_NAME, 1024, name, NULL);
  126. clGetPlatformInfo(platform_id[i], CL_PLATFORM_VENDOR, 1024, vendor, NULL);
  127. _STARPU_OPENCL_DEBUG("Platform: %s - %s\n", name, vendor);
  128. }
  129. #endif
  130. err = clGetDeviceIDs(platform_id[i], device_type, STARPU_MAXOPENCLDEVS-nb_devices, &devices[nb_devices], &num);
  131. if (err == CL_DEVICE_NOT_FOUND) {
  132. _STARPU_OPENCL_DEBUG(" No devices detected on this platform\n");
  133. }
  134. else {
  135. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  136. _STARPU_OPENCL_DEBUG(" %d devices detected\n", num);
  137. nb_devices += num;
  138. }
  139. }
  140. }
  141. // Get location of OpenCl codelet source files
  142. _starpu_opencl_codelet_dir = getenv("STARPU_OPENCL_CODELET_DIR");
  143. init_done=1;
  144. }
  145. }
  146. static unsigned _starpu_opencl_get_device_name(int dev, char *name, int lname);
  147. static int _starpu_opencl_execute_job(starpu_job_t j, struct starpu_worker_s *args);
  148. void *_starpu_opencl_worker(void *arg)
  149. {
  150. struct starpu_worker_s* args = arg;
  151. int devid = args->devid;
  152. unsigned memory_node = args->memory_node;
  153. #ifdef USE_FXT
  154. fxt_register_thread(args->bindid);
  155. #endif
  156. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  157. _starpu_set_local_memory_node_key(&(args->memory_node));
  158. _starpu_set_local_queue(args->jobq);
  159. _starpu_set_local_worker_key(args);
  160. /* this is only useful (and meaningful) is there is a single
  161. memory node "related" to that queue */
  162. args->jobq->memory_node = memory_node;
  163. args->jobq->total_computation_time = 0.0;
  164. args->jobq->total_communication_time = 0.0;
  165. args->jobq->total_computation_time_error = 0.0;
  166. args->jobq->total_job_performed = 0;
  167. _starpu_opencl_init_context(devid);
  168. /* one more time to avoid hacks from third party lib :) */
  169. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  170. args->status = STATUS_UNKNOWN;
  171. /* get the device's name */
  172. char devname[128];
  173. _starpu_opencl_get_device_name(devid, devname, 128);
  174. snprintf(args->name, 32, "OpenCL %d (%s)", args->devid, devname);
  175. _STARPU_OPENCL_DEBUG("OpenCL (%s) dev id %d thread is ready to run on CPU %d !\n", devname, devid, args->bindid);
  176. STARPU_TRACE_WORKER_INIT_END
  177. /* tell the main thread that this one is ready */
  178. PTHREAD_MUTEX_LOCK(&args->mutex);
  179. args->worker_is_initialized = 1;
  180. PTHREAD_COND_SIGNAL(&args->ready_cond);
  181. PTHREAD_MUTEX_UNLOCK(&args->mutex);
  182. struct starpu_job_s * j;
  183. int res;
  184. struct starpu_sched_policy_s *policy = _starpu_get_sched_policy();
  185. struct starpu_jobq_s *queue = policy->starpu_get_local_queue(policy);
  186. unsigned memnode = args->memory_node;
  187. while (_starpu_machine_is_running())
  188. {
  189. STARPU_TRACE_START_PROGRESS(memnode);
  190. _starpu_datawizard_progress(memnode, 1);
  191. STARPU_TRACE_END_PROGRESS(memnode);
  192. _starpu_execute_registered_progression_hooks();
  193. _starpu_jobq_lock(queue);
  194. /* perhaps there is some local task to be executed first */
  195. j = _starpu_pop_local_task(args);
  196. /* otherwise ask a task to the scheduler */
  197. if (!j)
  198. j = _starpu_pop_task();
  199. if (j == NULL) {
  200. if (_starpu_worker_can_block(memnode))
  201. PTHREAD_COND_WAIT(&queue->activity_cond, &queue->activity_mutex);
  202. _starpu_jobq_unlock(queue);
  203. continue;
  204. }
  205. _starpu_jobq_unlock(queue);
  206. /* can OpenCL do that task ? */
  207. if (!STARPU_OPENCL_MAY_PERFORM(j))
  208. {
  209. /* this is not a OpenCL task */
  210. _starpu_push_task(j);
  211. continue;
  212. }
  213. _starpu_set_current_task(j->task);
  214. res = _starpu_opencl_execute_job(j, args);
  215. _starpu_set_current_task(NULL);
  216. if (res) {
  217. switch (res) {
  218. case -EAGAIN:
  219. fprintf(stderr, "ouch, put the codelet %p back ... \n", j);
  220. _starpu_push_task(j);
  221. STARPU_ABORT();
  222. continue;
  223. default:
  224. assert(0);
  225. }
  226. }
  227. _starpu_handle_job_termination(j);
  228. }
  229. STARPU_TRACE_WORKER_DEINIT_START
  230. _starpu_opencl_deinit_context(devid);
  231. #ifdef DATA_STATS
  232. fprintf(stderr, "OpenCL #%d computation %le comm %le (%lf \%%)\n", args->id, args->jobq->total_computation_time, args->jobq->total_communication_time, args->jobq->total_communication_time*100.0/args->jobq->total_computation_time);
  233. #endif
  234. #ifdef STARPU_VERBOSE
  235. double ratio = 0;
  236. if (args->jobq->total_job_performed != 0)
  237. {
  238. ratio = args->jobq->total_computation_time_error/args->jobq->total_computation_time;
  239. }
  240. _starpu_print_to_logfile("MODEL ERROR: OpenCL %d ERROR %lf EXEC %lf RATIO %lf NTASKS %d\n", args->devid, args->jobq->total_computation_time_error, args->jobq->total_computation_time, ratio, args->jobq->total_job_performed);
  241. #endif
  242. pthread_exit(NULL);
  243. return NULL;
  244. }
  245. static unsigned _starpu_opencl_get_device_name(int dev, char *name, int lname)
  246. {
  247. int err;
  248. if (!init_done) {
  249. _starpu_opencl_init();
  250. }
  251. // Get device name
  252. err = clGetDeviceInfo(devices[dev], CL_DEVICE_NAME, lname, name, NULL);
  253. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  254. _STARPU_OPENCL_DEBUG("Device %d : [%s]\n", dev, name);
  255. return EXIT_SUCCESS;
  256. }
  257. unsigned _starpu_opencl_get_device_count(void)
  258. {
  259. if (!init_done) {
  260. _starpu_opencl_init();
  261. }
  262. return nb_devices;
  263. }
  264. static int _starpu_opencl_execute_job(starpu_job_t j, struct starpu_worker_s *args)
  265. {
  266. int ret;
  267. // uint32_t mask = (1<<0);
  268. uint32_t mask = 0;
  269. STARPU_ASSERT(j);
  270. struct starpu_task *task = j->task;
  271. starpu_tick_t codelet_start, codelet_end;
  272. starpu_tick_t codelet_start_comm, codelet_end_comm;
  273. unsigned calibrate_model = 0;
  274. STARPU_ASSERT(task);
  275. struct starpu_codelet_t *cl = task->cl;
  276. STARPU_ASSERT(cl);
  277. if (cl->model && cl->model->benchmarking)
  278. calibrate_model = 1;
  279. /* we do not take communication into account when modeling the performance */
  280. if (STARPU_BENCHMARK_COMM)
  281. {
  282. //barrier(CLK_GLOBAL_MEM_FENCE);
  283. STARPU_GET_TICK(codelet_start_comm);
  284. }
  285. ret = _starpu_fetch_task_input(task, mask);
  286. if (ret != 0) {
  287. /* there was not enough memory, so the input of
  288. * the codelet cannot be fetched ... put the
  289. * codelet back, and try it later */
  290. return -EAGAIN;
  291. }
  292. if (calibrate_model || STARPU_BENCHMARK_COMM)
  293. {
  294. //barrier(CLK_GLOBAL_MEM_FENCE);
  295. STARPU_GET_TICK(codelet_end_comm);
  296. }
  297. STARPU_TRACE_START_CODELET_BODY(j);
  298. args->status = STATUS_EXECUTING;
  299. cl_func func = cl->opencl_func;
  300. STARPU_ASSERT(func);
  301. STARPU_GET_TICK(codelet_start);
  302. func(task->interface, task->cl_arg);
  303. cl->per_worker_stats[args->workerid]++;
  304. STARPU_GET_TICK(codelet_end);
  305. args->status = STATUS_UNKNOWN;
  306. STARPU_TRACE_END_CODELET_BODY(j);
  307. if (calibrate_model || STARPU_BENCHMARK_COMM)
  308. {
  309. double measured = _starpu_timing_delay(&codelet_start, &codelet_end);
  310. double measured_comm = _starpu_timing_delay(&codelet_start_comm, &codelet_end_comm);
  311. args->jobq->total_computation_time += measured;
  312. args->jobq->total_communication_time += measured_comm;
  313. double error;
  314. error = fabs(STARPU_MAX(measured, 0.0) - STARPU_MAX(j->predicted, 0.0));
  315. args->jobq->total_computation_time_error += error;
  316. if (calibrate_model)
  317. _starpu_update_perfmodel_history(j, args->perf_arch, (unsigned)args->devid, measured);
  318. }
  319. args->jobq->total_job_performed++;
  320. _starpu_push_task_output(task, mask);
  321. return EXIT_SUCCESS;
  322. }