cmdutils_opencl.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 "cmdutils.h"
  25. typedef struct {
  26. int platform_idx;
  27. int device_idx;
  28. char device_name[64];
  29. int64_t runtime;
  30. } OpenCLDeviceBenchmark;
  31. const char *ocl_bench_source = AV_OPENCL_KERNEL(
  32. inline unsigned char clip_uint8(int a)
  33. {
  34. if (a & (~0xFF))
  35. return (-a)>>31;
  36. else
  37. return a;
  38. }
  39. kernel void unsharp_bench(
  40. global unsigned char *src,
  41. global unsigned char *dst,
  42. global int *mask,
  43. int width,
  44. int height)
  45. {
  46. int i, j, local_idx, lc_idx, sum = 0;
  47. int2 thread_idx, block_idx, global_idx, lm_idx;
  48. thread_idx.x = get_local_id(0);
  49. thread_idx.y = get_local_id(1);
  50. block_idx.x = get_group_id(0);
  51. block_idx.y = get_group_id(1);
  52. global_idx.x = get_global_id(0);
  53. global_idx.y = get_global_id(1);
  54. local uchar data[32][32];
  55. local int lc[128];
  56. for (i = 0; i <= 1; i++) {
  57. lm_idx.y = -8 + (block_idx.y + i) * 16 + thread_idx.y;
  58. lm_idx.y = lm_idx.y < 0 ? 0 : lm_idx.y;
  59. lm_idx.y = lm_idx.y >= height ? height - 1: lm_idx.y;
  60. for (j = 0; j <= 1; j++) {
  61. lm_idx.x = -8 + (block_idx.x + j) * 16 + thread_idx.x;
  62. lm_idx.x = lm_idx.x < 0 ? 0 : lm_idx.x;
  63. lm_idx.x = lm_idx.x >= width ? width - 1: lm_idx.x;
  64. data[i*16 + thread_idx.y][j*16 + thread_idx.x] = src[lm_idx.y*width + lm_idx.x];
  65. }
  66. }
  67. local_idx = thread_idx.y*16 + thread_idx.x;
  68. if (local_idx < 128)
  69. lc[local_idx] = mask[local_idx];
  70. barrier(CLK_LOCAL_MEM_FENCE);
  71. \n#pragma unroll\n
  72. for (i = -4; i <= 4; i++) {
  73. lm_idx.y = 8 + i + thread_idx.y;
  74. \n#pragma unroll\n
  75. for (j = -4; j <= 4; j++) {
  76. lm_idx.x = 8 + j + thread_idx.x;
  77. lc_idx = (i + 4)*8 + j + 4;
  78. sum += (int)data[lm_idx.y][lm_idx.x] * lc[lc_idx];
  79. }
  80. }
  81. int temp = (int)data[thread_idx.y + 8][thread_idx.x + 8];
  82. int res = temp + (((temp - (int)((sum + 1<<15) >> 16))) >> 16);
  83. if (global_idx.x < width && global_idx.y < height)
  84. dst[global_idx.x + global_idx.y*width] = clip_uint8(res);
  85. }
  86. );
  87. #define OCLCHECK(method, ... ) \
  88. do { \
  89. status = method(__VA_ARGS__); \
  90. if (status != CL_SUCCESS) { \
  91. av_log(NULL, AV_LOG_ERROR, # method " error '%s'\n", \
  92. av_opencl_errstr(status)); \
  93. ret = AVERROR_EXTERNAL; \
  94. goto end; \
  95. } \
  96. } while (0)
  97. #define CREATEBUF(out, flags, size) \
  98. do { \
  99. out = clCreateBuffer(ext_opencl_env->context, flags, size, NULL, &status); \
  100. if (status != CL_SUCCESS) { \
  101. av_log(NULL, AV_LOG_ERROR, "Could not create OpenCL buffer\n"); \
  102. ret = AVERROR_EXTERNAL; \
  103. goto end; \
  104. } \
  105. } while (0)
  106. static void fill_rand_int(int *data, int n)
  107. {
  108. int i;
  109. srand(av_gettime());
  110. for (i = 0; i < n; i++)
  111. data[i] = rand();
  112. }
  113. #define OPENCL_NB_ITER 5
  114. static int64_t run_opencl_bench(AVOpenCLExternalEnv *ext_opencl_env)
  115. {
  116. int i, arg = 0, width = 1920, height = 1088;
  117. int64_t start, ret = 0;
  118. cl_int status;
  119. size_t kernel_len;
  120. char *inbuf;
  121. int *mask;
  122. int buf_size = width * height * sizeof(char);
  123. int mask_size = sizeof(uint32_t) * 128;
  124. cl_mem cl_mask, cl_inbuf, cl_outbuf;
  125. cl_kernel kernel = NULL;
  126. cl_program program = NULL;
  127. size_t local_work_size_2d[2] = {16, 16};
  128. size_t global_work_size_2d[2] = {(size_t)width, (size_t)height};
  129. if (!(inbuf = av_malloc(buf_size)) || !(mask = av_malloc(mask_size))) {
  130. av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
  131. ret = AVERROR(ENOMEM);
  132. goto end;
  133. }
  134. fill_rand_int((int*)inbuf, buf_size/4);
  135. fill_rand_int(mask, mask_size/4);
  136. CREATEBUF(cl_mask, CL_MEM_READ_ONLY, mask_size);
  137. CREATEBUF(cl_inbuf, CL_MEM_READ_ONLY, buf_size);
  138. CREATEBUF(cl_outbuf, CL_MEM_READ_WRITE, buf_size);
  139. kernel_len = strlen(ocl_bench_source);
  140. program = clCreateProgramWithSource(ext_opencl_env->context, 1, &ocl_bench_source,
  141. &kernel_len, &status);
  142. if (status != CL_SUCCESS || !program) {
  143. av_log(NULL, AV_LOG_ERROR, "OpenCL unable to create benchmark program\n");
  144. ret = AVERROR_EXTERNAL;
  145. goto end;
  146. }
  147. status = clBuildProgram(program, 1, &(ext_opencl_env->device_id), NULL, NULL, NULL);
  148. if (status != CL_SUCCESS) {
  149. av_log(NULL, AV_LOG_ERROR, "OpenCL unable to build benchmark program\n");
  150. ret = AVERROR_EXTERNAL;
  151. goto end;
  152. }
  153. kernel = clCreateKernel(program, "unsharp_bench", &status);
  154. if (status != CL_SUCCESS) {
  155. av_log(NULL, AV_LOG_ERROR, "OpenCL unable to create benchmark kernel\n");
  156. ret = AVERROR_EXTERNAL;
  157. goto end;
  158. }
  159. OCLCHECK(clEnqueueWriteBuffer, ext_opencl_env->command_queue, cl_inbuf, CL_TRUE, 0,
  160. buf_size, inbuf, 0, NULL, NULL);
  161. OCLCHECK(clEnqueueWriteBuffer, ext_opencl_env->command_queue, cl_mask, CL_TRUE, 0,
  162. mask_size, mask, 0, NULL, NULL);
  163. OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_mem), &cl_inbuf);
  164. OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_mem), &cl_outbuf);
  165. OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_mem), &cl_mask);
  166. OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_int), &width);
  167. OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_int), &height);
  168. start = av_gettime_relative();
  169. for (i = 0; i < OPENCL_NB_ITER; i++)
  170. OCLCHECK(clEnqueueNDRangeKernel, ext_opencl_env->command_queue, kernel, 2, NULL,
  171. global_work_size_2d, local_work_size_2d, 0, NULL, NULL);
  172. clFinish(ext_opencl_env->command_queue);
  173. ret = (av_gettime_relative() - start)/OPENCL_NB_ITER;
  174. end:
  175. if (kernel)
  176. clReleaseKernel(kernel);
  177. if (program)
  178. clReleaseProgram(program);
  179. if (cl_inbuf)
  180. clReleaseMemObject(cl_inbuf);
  181. if (cl_outbuf)
  182. clReleaseMemObject(cl_outbuf);
  183. if (cl_mask)
  184. clReleaseMemObject(cl_mask);
  185. av_free(inbuf);
  186. av_free(mask);
  187. return ret;
  188. }
  189. static int compare_ocl_device_desc(const void *a, const void *b)
  190. {
  191. return ((OpenCLDeviceBenchmark*)a)->runtime - ((OpenCLDeviceBenchmark*)b)->runtime;
  192. }
  193. int opt_opencl_bench(void *optctx, const char *opt, const char *arg)
  194. {
  195. int i, j, nb_devices = 0, count = 0;
  196. int64_t score = 0;
  197. AVOpenCLDeviceList *device_list;
  198. AVOpenCLDeviceNode *device_node = NULL;
  199. OpenCLDeviceBenchmark *devices = NULL;
  200. cl_platform_id platform;
  201. av_opencl_get_device_list(&device_list);
  202. for (i = 0; i < device_list->platform_num; i++)
  203. nb_devices += device_list->platform_node[i]->device_num;
  204. if (!nb_devices) {
  205. av_log(NULL, AV_LOG_ERROR, "No OpenCL device detected!\n");
  206. return AVERROR(EINVAL);
  207. }
  208. if (!(devices = av_malloc_array(nb_devices, sizeof(OpenCLDeviceBenchmark)))) {
  209. av_log(NULL, AV_LOG_ERROR, "Could not allocate buffer\n");
  210. return AVERROR(ENOMEM);
  211. }
  212. for (i = 0; i < device_list->platform_num; i++) {
  213. for (j = 0; j < device_list->platform_node[i]->device_num; j++) {
  214. device_node = device_list->platform_node[i]->device_node[j];
  215. platform = device_list->platform_node[i]->platform_id;
  216. score = av_opencl_benchmark(device_node, platform, run_opencl_bench);
  217. if (score > 0) {
  218. devices[count].platform_idx = i;
  219. devices[count].device_idx = j;
  220. devices[count].runtime = score;
  221. strcpy(devices[count].device_name, device_node->device_name);
  222. count++;
  223. }
  224. }
  225. }
  226. qsort(devices, count, sizeof(OpenCLDeviceBenchmark), compare_ocl_device_desc);
  227. fprintf(stderr, "platform_idx\tdevice_idx\tdevice_name\truntime\n");
  228. for (i = 0; i < count; i++)
  229. fprintf(stdout, "%d\t%d\t%s\t%"PRId64"\n",
  230. devices[i].platform_idx, devices[i].device_idx,
  231. devices[i].device_name, devices[i].runtime);
  232. av_opencl_free_device_list(&device_list);
  233. av_free(devices);
  234. return 0;
  235. }
  236. int opt_opencl(void *optctx, const char *opt, const char *arg)
  237. {
  238. char *key, *value;
  239. const char *opts = arg;
  240. int ret = 0;
  241. while (*opts) {
  242. ret = av_opt_get_key_value(&opts, "=", ":", 0, &key, &value);
  243. if (ret < 0)
  244. return ret;
  245. ret = av_opencl_set_option(key, value);
  246. if (ret < 0)
  247. return ret;
  248. if (*opts)
  249. opts++;
  250. }
  251. return ret;
  252. }