driver_opencl_utils.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  4. * Copyright (C) 2010, 2011 Université de Bordeaux 1
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <sys/stat.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <starpu_opencl.h>
  25. #include <starpu_profiling.h>
  26. #include <core/workers.h>
  27. #include "driver_opencl_utils.h"
  28. #include "driver_opencl.h"
  29. char *_starpu_opencl_program_dir;
  30. #define _STARPU_STRINGIFY_(x) #x
  31. #define _STARPU_STRINGIFY(x) _STARPU_STRINGIFY_(x)
  32. static
  33. int _starpu_opencl_locate_file(const char *source_file_name, char *located_file_name) {
  34. _STARPU_DEBUG("Trying to locate <%s>\n", source_file_name);
  35. if (access(source_file_name, R_OK) == 0) {
  36. strcpy(located_file_name, source_file_name);
  37. return EXIT_SUCCESS;
  38. }
  39. if (_starpu_opencl_program_dir) {
  40. sprintf(located_file_name, "%s/%s", _starpu_opencl_program_dir, source_file_name);
  41. _STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
  42. if (access(located_file_name, R_OK) == 0) return EXIT_SUCCESS;
  43. }
  44. sprintf(located_file_name, "%s/%s", _STARPU_STRINGIFY(STARPU_OPENCL_DATADIR), source_file_name);
  45. _STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
  46. if (access(located_file_name, R_OK) == 0) return EXIT_SUCCESS;
  47. sprintf(located_file_name, "%s/%s", STARPU_SRC_DIR, source_file_name);
  48. _STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
  49. if (access(located_file_name, R_OK) == 0) return EXIT_SUCCESS;
  50. strcpy(located_file_name, "");
  51. _STARPU_ERROR("Cannot locate file <%s>\n", source_file_name);
  52. return EXIT_FAILURE;
  53. }
  54. cl_int starpu_opencl_load_kernel(cl_kernel *kernel, cl_command_queue *queue, struct starpu_opencl_program *opencl_programs,
  55. const char *kernel_name, int devid)
  56. {
  57. cl_int err;
  58. cl_device_id device;
  59. cl_context context;
  60. cl_program program;
  61. starpu_opencl_get_device(devid, &device);
  62. starpu_opencl_get_context(devid, &context);
  63. starpu_opencl_get_queue(devid, queue);
  64. program = opencl_programs->programs[devid];
  65. if (!program) {
  66. _STARPU_DISP("Program not available\n");
  67. return CL_INVALID_PROGRAM;
  68. }
  69. // Create the compute kernel in the program we wish to run
  70. *kernel = clCreateKernel(program, kernel_name, &err);
  71. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  72. return CL_SUCCESS;
  73. }
  74. cl_int starpu_opencl_release_kernel(cl_kernel kernel) {
  75. cl_int err;
  76. err = clReleaseKernel(kernel);
  77. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  78. return CL_SUCCESS;
  79. }
  80. static
  81. char *_starpu_opencl_load_program_source(const char *filename)
  82. {
  83. struct stat statbuf;
  84. FILE *fh;
  85. char *source;
  86. int x;
  87. char c;
  88. fh = fopen(filename, "r");
  89. if (fh == 0)
  90. return NULL;
  91. stat(filename, &statbuf);
  92. source = (char *) malloc(statbuf.st_size + 1);
  93. for(c=fgetc(fh), x=0 ; c != EOF ; c = fgetc(fh), x++) {
  94. source[x] = c;
  95. }
  96. source[x] = '\0';
  97. _STARPU_DEBUG("OpenCL kernel <%s>\n", source);
  98. fclose(fh);
  99. return source;
  100. }
  101. int starpu_opencl_load_opencl_from_string(const char *opencl_program_source, struct starpu_opencl_program *opencl_programs)
  102. {
  103. unsigned int dev;
  104. unsigned int nb_devices;
  105. nb_devices = _starpu_opencl_get_device_count();
  106. // Iterate over each device
  107. for(dev = 0; dev < nb_devices; dev ++) {
  108. cl_device_id device;
  109. cl_context context;
  110. cl_program program;
  111. cl_int err;
  112. starpu_opencl_get_device(dev, &device);
  113. starpu_opencl_get_context(dev, &context);
  114. if (context == NULL) {
  115. _STARPU_DEBUG("[%d] is not a valid OpenCL context\n", dev);
  116. continue;
  117. }
  118. opencl_programs->programs[dev] = NULL;
  119. if (context == NULL) continue;
  120. // Create the compute program from the source buffer
  121. program = clCreateProgramWithSource(context, 1, (const char **) &opencl_program_source, NULL, &err);
  122. if (!program || err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  123. // Build the program executable
  124. err = clBuildProgram(program, 1, &device, "-Werror -cl-mad-enable", NULL, NULL);
  125. if (err != CL_SUCCESS) {
  126. size_t len;
  127. static char buffer[4096];
  128. _STARPU_DISP("Error: Failed to build program executable!\n");
  129. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
  130. _STARPU_DISP("<%s>\n", buffer);
  131. return EXIT_FAILURE;
  132. }
  133. // Store program
  134. opencl_programs->programs[dev] = program;
  135. }
  136. return EXIT_SUCCESS;
  137. }
  138. int starpu_opencl_load_opencl_from_file(const char *source_file_name, struct starpu_opencl_program *opencl_programs)
  139. {
  140. int nb_devices;
  141. char located_file_name[1024];
  142. // Do not try to load and compile the file if there is no devices
  143. nb_devices = _starpu_opencl_get_device_count();
  144. if (nb_devices == 0) return EXIT_SUCCESS;
  145. // Locate source file
  146. _starpu_opencl_locate_file(source_file_name, located_file_name);
  147. _STARPU_DEBUG("Source file name : <%s>\n", located_file_name);
  148. // Load the compute program from disk into a cstring buffer
  149. char *opencl_program_source = _starpu_opencl_load_program_source(located_file_name);
  150. if(!opencl_program_source)
  151. _STARPU_ERROR("Failed to load compute program from file <%s>!\n", located_file_name);
  152. return starpu_opencl_load_opencl_from_string(opencl_program_source, opencl_programs);
  153. }
  154. cl_int starpu_opencl_unload_opencl(struct starpu_opencl_program *opencl_programs)
  155. {
  156. unsigned int dev;
  157. unsigned int nb_devices;
  158. nb_devices = _starpu_opencl_get_device_count();
  159. // Iterate over each device
  160. for(dev = 0; dev < nb_devices; dev ++) {
  161. if (opencl_programs->programs[dev])
  162. clReleaseProgram(opencl_programs->programs[dev]);
  163. }
  164. return CL_SUCCESS;
  165. }
  166. int starpu_opencl_collect_stats(cl_event event __attribute__((unused)))
  167. {
  168. #if defined(CL_PROFILING_CLOCK_CYCLE_COUNT)||defined(CL_PROFILING_STALL_CYCLE_COUNT)||defined(CL_PROFILING_POWER_CONSUMED)
  169. struct starpu_task *task = starpu_get_current_task();
  170. struct starpu_task_profiling_info *info = task->profiling_info;
  171. #endif
  172. #ifdef CL_PROFILING_CLOCK_CYCLE_COUNT
  173. if (starpu_profiling_status_get() && info) {
  174. cl_int err;
  175. unsigned int clock_cycle_count;
  176. size_t size;
  177. err = clGetEventProfilingInfo(event, CL_PROFILING_CLOCK_CYCLE_COUNT, sizeof(clock_cycle_count), &clock_cycle_count, &size);
  178. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  179. STARPU_ASSERT(size == sizeof(clock_cycle_count));
  180. info->used_cycles += clock_cycle_count;
  181. }
  182. #endif
  183. #ifdef CL_PROFILING_STALL_CYCLE_COUNT
  184. if (starpu_profiling_status_get() && info) {
  185. cl_int err;
  186. unsigned int stall_cycle_count;
  187. size_t size;
  188. err = clGetEventProfilingInfo(event, CL_PROFILING_STALL_CYCLE_COUNT, sizeof(stall_cycle_count), &stall_cycle_count, &size);
  189. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  190. STARPU_ASSERT(size == sizeof(stall_cycle_count));
  191. info->stall_cycles += stall_cycle_count;
  192. }
  193. #endif
  194. #ifdef CL_PROFILING_POWER_CONSUMED
  195. if (info && (starpu_profiling_status_get() || (task->cl && task->cl->power_model && task->cl->power_model->benchmarking))) {
  196. cl_int err;
  197. double power_consumed;
  198. size_t size;
  199. err = clGetEventProfilingInfo(event, CL_PROFILING_POWER_CONSUMED, sizeof(power_consumed), &power_consumed, &size);
  200. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  201. STARPU_ASSERT(size == sizeof(power_consumed));
  202. info->power_consumed += power_consumed;
  203. }
  204. #endif
  205. return 0;
  206. }