cmdutils_opencl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (C) 2013 Lenny Wang
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/opt.h"
  21. #include "libavutil/time.h"
  22. #include "libavutil/log.h"
  23. #include "libavutil/opencl.h"
  24. #include "libavutil/avstring.h"
  25. #include "cmdutils.h"
  26. typedef struct {
  27. int platform_idx;
  28. int device_idx;
  29. char device_name[64];
  30. int64_t runtime;
  31. } OpenCLDeviceBenchmark;
  32. const char *ocl_bench_source = AV_OPENCL_KERNEL(
  33. inline unsigned char clip_uint8(int a)
  34. {
  35. if (a & (~0xFF))
  36. return (-a)>>31;
  37. else
  38. return a;
  39. }
  40. kernel void unsharp_bench(
  41. global unsigned char *src,
  42. global unsigned char *dst,
  43. global int *mask,
  44. int width,
  45. int height)
  46. {
  47. int i, j, local_idx, lc_idx, sum = 0;
  48. int2 thread_idx, block_idx, global_idx, lm_idx;
  49. thread_idx.x = get_local_id(0);
  50. thread_idx.y = get_local_id(1);
  51. block_idx.x = get_group_id(0);
  52. block_idx.y = get_group_id(1);
  53. global_idx.x = get_global_id(0);
  54. global_idx.y = get_global_id(1);
  55. local uchar data[32][32];
  56. local int lc[128];
  57. for (i = 0; i <= 1; i++) {
  58. lm_idx.y = -8 + (block_idx.y + i) * 16 + thread_idx.y;
  59. lm_idx.y = lm_idx.y < 0 ? 0 : lm_idx.y;
  60. lm_idx.y = lm_idx.y >= height ? height - 1: lm_idx.y;
  61. for (j = 0; j <= 1; j++) {
  62. lm_idx.x = -8 + (block_idx.x + j) * 16 + thread_idx.x;
  63. lm_idx.x = lm_idx.x < 0 ? 0 : lm_idx.x;
  64. lm_idx.x = lm_idx.x >= width ? width - 1: lm_idx.x;
  65. data[i*16 + thread_idx.y][j*16 + thread_idx.x] = src[lm_idx.y*width + lm_idx.x];
  66. }
  67. }
  68. local_idx = thread_idx.y*16 + thread_idx.x;
  69. if (local_idx < 128)
  70. lc[local_idx] = mask[local_idx];
  71. barrier(CLK_LOCAL_MEM_FENCE);
  72. \n#pragma unroll\n
  73. for (i = -4; i <= 4; i++) {
  74. lm_idx.y = 8 + i + thread_idx.y;
  75. \n#pragma unroll\n
  76. for (j = -4; j <= 4; j++) {
  77. lm_idx.x = 8 + j + thread_idx.x;
  78. lc_idx = (i + 4)*8 + j + 4;
  79. sum += (int)data[lm_idx.y][lm_idx.x] * lc[lc_idx];
  80. }
  81. }
  82. int temp = (int)data[thread_idx.y + 8][thread_idx.x + 8];
  83. int res = temp + (((temp - (int)((sum + 1<<15) >> 16))) >> 16);
  84. if (global_idx.x < width && global_idx.y < height)
  85. dst[global_idx.x + global_idx.y*width] = clip_uint8(res);
  86. }
  87. );
  88. #define OCLCHECK(method, ... ) \
  89. do { \
  90. status = method(__VA_ARGS__); \
  91. if (status != CL_SUCCESS) { \
  92. av_log(NULL, AV_LOG_ERROR, # method " error '%s'\n", \
  93. av_opencl_errstr(status)); \
  94. ret = AVERROR_EXTERNAL; \
  95. goto end; \
  96. } \
  97. } while (0)
  98. #define CREATEBUF(out, flags, size) \
  99. do { \
  100. out = clCreateBuffer(ext_opencl_env->context, flags, size, NULL, &status); \
  101. if (status != CL_SUCCESS) { \
  102. av_log(NULL, AV_LOG_ERROR, "Could not create OpenCL buffer\n"); \
  103. ret = AVERROR_EXTERNAL; \
  104. goto end; \
  105. } \
  106. } while (0)
  107. static void fill_rand_int(int *data, int n)
  108. {
  109. int i;
  110. srand(av_gettime());
  111. for (i = 0; i < n; i++)
  112. data[i] = rand();
  113. }
  114. #define OPENCL_NB_ITER 5
  115. static int64_t run_opencl_bench(AVOpenCLExternalEnv *ext_opencl_env)
  116. {
  117. int i, arg = 0, width = 1920, height = 1088;
  118. int64_t start, ret = 0;
  119. cl_int status;
  120. size_t kernel_len;
  121. char *inbuf;
  122. int *mask;
  123. int buf_size = width * height * sizeof(char);
  124. int mask_size = sizeof(uint32_t) * 128;
  125. cl_mem cl_mask, cl_inbuf, cl_outbuf;
  126. cl_kernel kernel = NULL;
  127. cl_program program = NULL;
  128. size_t local_work_size_2d[2] = {16, 16};
  129. size_t global_work_size_2d[2] = {(size_t)width, (size_t)height};
  130. if (!(inbuf = av_malloc(buf_size)) || !(mask = av_malloc(mask_size))) {
  131. av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
  132. ret = AVERROR(ENOMEM);
  133. goto end;
  134. }
  135. fill_rand_int((int*)inbuf, buf_size/4);
  136. fill_rand_int(mask, mask_size/4);
  137. CREATEBUF(cl_mask, CL_MEM_READ_ONLY, mask_size);
  138. CREATEBUF(cl_inbuf, CL_MEM_READ_ONLY, buf_size);
  139. CREATEBUF(cl_outbuf, CL_MEM_READ_WRITE, buf_size);
  140. kernel_len = strlen(ocl_bench_source);
  141. program = clCreateProgramWithSource(ext_opencl_env->context, 1, &ocl_bench_source,
  142. &kernel_len, &status);
  143. if (status != CL_SUCCESS || !program) {
  144. av_log(NULL, AV_LOG_ERROR, "OpenCL unable to create benchmark program\n");
  145. ret = AVERROR_EXTERNAL;
  146. goto end;
  147. }
  148. status = clBuildProgram(program, 1, &(ext_opencl_env->device_id), NULL, NULL, NULL);
  149. if (status != CL_SUCCESS) {
  150. av_log(NULL, AV_LOG_ERROR, "OpenCL unable to build benchmark program\n");
  151. ret = AVERROR_EXTERNAL;
  152. goto end;
  153. }
  154. kernel = clCreateKernel(program, "unsharp_bench", &status);
  155. if (status != CL_SUCCESS) {
  156. av_log(NULL, AV_LOG_ERROR, "OpenCL unable to create benchmark kernel\n");
  157. ret = AVERROR_EXTERNAL;
  158. goto end;
  159. }
  160. OCLCHECK(clEnqueueWriteBuffer, ext_opencl_env->command_queue, cl_inbuf, CL_TRUE, 0,
  161. buf_size, inbuf, 0, NULL, NULL);
  162. OCLCHECK(clEnqueueWriteBuffer, ext_opencl_env->command_queue, cl_mask, CL_TRUE, 0,
  163. mask_size, mask, 0, NULL, NULL);
  164. OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_mem), &cl_inbuf);
  165. OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_mem), &cl_outbuf);
  166. OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_mem), &cl_mask);
  167. OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_int), &width);
  168. OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_int), &height);
  169. start = av_gettime_relative();
  170. for (i = 0; i < OPENCL_NB_ITER; i++)
  171. OCLCHECK(clEnqueueNDRangeKernel, ext_opencl_env->command_queue, kernel, 2, NULL,
  172. global_work_size_2d, local_work_size_2d, 0, NULL, NULL);
  173. clFinish(ext_opencl_env->command_queue);
  174. ret = (av_gettime_relative() - start)/OPENCL_NB_ITER;
  175. end:
  176. if (kernel)
  177. clReleaseKernel(kernel);
  178. if (program)
  179. clReleaseProgram(program);
  180. if (cl_inbuf)
  181. clReleaseMemObject(cl_inbuf);
  182. if (cl_outbuf)
  183. clReleaseMemObject(cl_outbuf);
  184. if (cl_mask)
  185. clReleaseMemObject(cl_mask);
  186. av_free(inbuf);
  187. av_free(mask);
  188. return ret;
  189. }
  190. static int compare_ocl_device_desc(const void *a, const void *b)
  191. {
  192. return ((OpenCLDeviceBenchmark*)a)->runtime - ((OpenCLDeviceBenchmark*)b)->runtime;
  193. }
  194. int opt_opencl_bench(void *optctx, const char *opt, const char *arg)
  195. {
  196. int i, j, nb_devices = 0, count = 0;
  197. int64_t score = 0;
  198. AVOpenCLDeviceList *device_list;
  199. AVOpenCLDeviceNode *device_node = NULL;
  200. OpenCLDeviceBenchmark *devices = NULL;
  201. cl_platform_id platform;
  202. av_opencl_get_device_list(&device_list);
  203. for (i = 0; i < device_list->platform_num; i++)
  204. nb_devices += device_list->platform_node[i]->device_num;
  205. if (!nb_devices) {
  206. av_log(NULL, AV_LOG_ERROR, "No OpenCL device detected!\n");
  207. return AVERROR(EINVAL);
  208. }
  209. if (!(devices = av_malloc_array(nb_devices, sizeof(OpenCLDeviceBenchmark)))) {
  210. av_log(NULL, AV_LOG_ERROR, "Could not allocate buffer\n");
  211. return AVERROR(ENOMEM);
  212. }
  213. for (i = 0; i < device_list->platform_num; i++) {
  214. for (j = 0; j < device_list->platform_node[i]->device_num; j++) {
  215. device_node = device_list->platform_node[i]->device_node[j];
  216. platform = device_list->platform_node[i]->platform_id;
  217. score = av_opencl_benchmark(device_node, platform, run_opencl_bench);
  218. if (score > 0) {
  219. devices[count].platform_idx = i;
  220. devices[count].device_idx = j;
  221. devices[count].runtime = score;
  222. av_strlcpy(devices[count].device_name, device_node->device_name,
  223. sizeof(devices[count].device_name));
  224. count++;
  225. }
  226. }
  227. }
  228. qsort(devices, count, sizeof(OpenCLDeviceBenchmark), compare_ocl_device_desc);
  229. fprintf(stderr, "platform_idx\tdevice_idx\tdevice_name\truntime\n");
  230. for (i = 0; i < count; i++)
  231. fprintf(stdout, "%d\t%d\t%s\t%"PRId64"\n",
  232. devices[i].platform_idx, devices[i].device_idx,
  233. devices[i].device_name, devices[i].runtime);
  234. av_opencl_free_device_list(&device_list);
  235. av_free(devices);
  236. return 0;
  237. }
  238. int opt_opencl(void *optctx, const char *opt, const char *arg)
  239. {
  240. char *key, *value;
  241. const char *opts = arg;
  242. int ret = 0;
  243. while (*opts) {
  244. ret = av_opt_get_key_value(&opts, "=", ":", 0, &key, &value);
  245. if (ret < 0)
  246. return ret;
  247. ret = av_opencl_set_option(key, value);
  248. if (ret < 0)
  249. return ret;
  250. if (*opts)
  251. opts++;
  252. }
  253. return ret;
  254. }