History

cmdutils & opencl: add -opencl_bench option to test and show available OpenCL devices

Lenny Wang committed 12 years ago

Commit 64f73ac1 parent 8e702bd11 files · +347 −27Browse files

Unified · Split

11 files
  1. Makefile +1 −0
  2. cmdutils.c +0 −24
  3. cmdutils.h +4 −0
  4. cmdutils_common_opts.h +1 −0
  5. cmdutils_opencl.c +274 −0
  6. doc/APIchanges +2 −0
  7. doc/fftools-common-opts.texi +4 −0
  8. doc/utils.texi +2 −2
  9. libavutil/opencl.c +42 −0
  10. libavutil/opencl.h +16 −0
  11. libavutil/version.h +1 −1
Makefile+1−0
@@ -25,6 +25,7 @@ ALLAVPROGS = $(AVBASENAMES:%=%$(PROGSSUF)$(EXESUF)) · 24 unmodified lines
2525 ALLAVPROGS_G = $(AVBASENAMES:%=%$(PROGSSUF)_g$(EXESUF))
2626
2727 $(foreach prog,$(AVBASENAMES),$(eval OBJS-$(prog) += cmdutils.o))
28+$(foreach prog,$(AVBASENAMES),$(eval OBJS-$(prog)-$(CONFIG_OPENCL) += cmdutils_opencl.o))
2829
2930 OBJS-ffmpeg += ffmpeg_opt.o ffmpeg_filter.o
3031 OBJS-ffmpeg-$(HAVE_VDPAU_X11) += ffmpeg_vdpau.o
cmdutils.c+0−24
@@ -58,10 +58,6 @@ · 57 unmodified lines
5858 #include <sys/time.h>
5959 #include <sys/resource.h>
6060 #endif
61−#if CONFIG_OPENCL
62−#include "libavutil/opencl.h"
63−#endif
64−
6561
6662 static int init_report(const char *env);
6763
@@ -985,26 +981,6 @@ int opt_timelimit(void *optctx, const char *opt, const char *arg) · 917 unmodified lines
985981 return 0;
986982 }
987983
988−#if CONFIG_OPENCL
989−int opt_opencl(void *optctx, const char *opt, const char *arg)
990−{
991− char *key, *value;
992− const char *opts = arg;
993− int ret = 0;
994− while (*opts) {
995− ret = av_opt_get_key_value(&opts, "=", ":", 0, &key, &value);
996− if (ret < 0)
997− return ret;
998− ret = av_opencl_set_option(key, value);
999− if (ret < 0)
1000− return ret;
1001− if (*opts)
1002− opts++;
1003− }
1004− return ret;
1005−}
1006−#endif
1007−
1008984 void print_error(const char *filename, int err)
1009985 {
1010986 char errbuf[128];
cmdutils.h+4−0
@@ -98,8 +98,12 @@ int opt_max_alloc(void *optctx, const char *opt, const char *arg); · 97 unmodified lines
9898
9999 int opt_codec_debug(void *optctx, const char *opt, const char *arg);
100100
101+#if CONFIG_OPENCL
101102 int opt_opencl(void *optctx, const char *opt, const char *arg);
102103
104+int opt_opencl_bench(void *optctx, const char *opt, const char *arg);
105+#endif
106+
103107 /**
104108 * Limit the execution time.
105109 */
cmdutils_common_opts.h+1−0
@@ -22,5 +22,6 @@ · 21 unmodified lines
2222 { "max_alloc" , HAS_ARG, {.func_arg = opt_max_alloc}, "set maximum size of a single allocated block", "bytes" },
2323 { "cpuflags" , HAS_ARG | OPT_EXPERT, { .func_arg = opt_cpuflags }, "force specific cpu flags", "flags" },
2424 #if CONFIG_OPENCL
25+ { "opencl_bench", OPT_EXIT, {.func_arg = opt_opencl_bench}, "run benchmark on all OpenCL devices and show results" },
2526 { "opencl_options", HAS_ARG, {.func_arg = opt_opencl}, "set OpenCL environment options" },
2627 #endif
cmdutils_opencl.c+274−0
@@ -0,0 +1,274 @@
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+
21+#include "libavutil/opt.h"
22+#include "libavutil/time.h"
23+#include "libavutil/log.h"
24+#include "libavutil/opencl.h"
25+#include "cmdutils.h"
26+
27+typedef struct {
28+ int platform_idx;
29+ int device_idx;
30+ char device_name[64];
31+ int64_t runtime;
32+} OpenCLDeviceBenchmark;
33+
34+const char *ocl_bench_source = AV_OPENCL_KERNEL(
35+inline unsigned char clip_uint8(int a)
36+{
37+ if (a & (~0xFF))
38+ return (-a)>>31;
39+ else
40+ return a;
41+}
42+
43+kernel void unsharp_bench(
44+ global unsigned char *src,
45+ global unsigned char *dst,
46+ global int *mask,
47+ int width,
48+ int height)
49+{
50+ int i, j, local_idx, lc_idx, sum = 0;
51+ int2 thread_idx, block_idx, global_idx, lm_idx;
52+ thread_idx.x = get_local_id(0);
53+ thread_idx.y = get_local_id(1);
54+ block_idx.x = get_group_id(0);
55+ block_idx.y = get_group_id(1);
56+ global_idx.x = get_global_id(0);
57+ global_idx.y = get_global_id(1);
58+ local uchar data[32][32];
59+ local int lc[128];
60+
61+ for (i = 0; i <= 1; i++) {
62+ lm_idx.y = -8 + (block_idx.y + i) * 16 + thread_idx.y;
63+ lm_idx.y = lm_idx.y < 0 ? 0 : lm_idx.y;
64+ lm_idx.y = lm_idx.y >= height ? height - 1: lm_idx.y;
65+ for (j = 0; j <= 1; j++) {
66+ lm_idx.x = -8 + (block_idx.x + j) * 16 + thread_idx.x;
67+ lm_idx.x = lm_idx.x < 0 ? 0 : lm_idx.x;
68+ lm_idx.x = lm_idx.x >= width ? width - 1: lm_idx.x;
69+ data[i*16 + thread_idx.y][j*16 + thread_idx.x] = src[lm_idx.y*width + lm_idx.x];
70+ }
71+ }
72+ local_idx = thread_idx.y*16 + thread_idx.x;
73+ if (local_idx < 128)
74+ lc[local_idx] = mask[local_idx];
75+ barrier(CLK_LOCAL_MEM_FENCE);
76+
77+ \n#pragma unroll\n
78+ for (i = -4; i <= 4; i++) {
79+ lm_idx.y = 8 + i + thread_idx.y;
80+ \n#pragma unroll\n
81+ for (j = -4; j <= 4; j++) {
82+ lm_idx.x = 8 + j + thread_idx.x;
83+ lc_idx = (i + 4)*8 + j + 4;
84+ sum += (int)data[lm_idx.y][lm_idx.x] * lc[lc_idx];
85+ }
86+ }
87+ int temp = (int)data[thread_idx.y + 8][thread_idx.x + 8];
88+ int res = temp + (((temp - (int)((sum + 1<<15) >> 16))) >> 16);
89+ if (global_idx.x < width && global_idx.y < height)
90+ dst[global_idx.x + global_idx.y*width] = clip_uint8(res);
91+}
92+);
93+
94+#define OCLCHECK(method, ... ) \
95+do { \
96+ status = method(__VA_ARGS__); \
97+ if (status != CL_SUCCESS) { \
98+ av_log(NULL, AV_LOG_ERROR, # method " error '%s'\n", \
99+ av_opencl_errstr(status)); \
100+ ret = AVERROR_EXTERNAL; \
101+ goto end; \
102+ } \
103+} while (0)
104+
105+#define CREATEBUF(out, flags, size) \
106+do { \
107+ out = clCreateBuffer(ext_opencl_env->context, flags, size, NULL, &status); \
108+ if (status != CL_SUCCESS) { \
109+ av_log(NULL, AV_LOG_ERROR, "Could not create OpenCL buffer\n"); \
110+ ret = AVERROR_EXTERNAL; \
111+ goto end; \
112+ } \
113+} while (0)
114+
115+static void fill_rand_int(int *data, int n)
116+{
117+ int i;
118+ srand(av_gettime());
119+ for (i = 0; i < n; i++)
120+ data[i] = rand();
121+}
122+
123+#define OPENCL_NB_ITER 5
124+static int64_t run_opencl_bench(AVOpenCLExternalEnv *ext_opencl_env)
125+{
126+ int i, arg = 0, width = 1920, height = 1088;
127+ int64_t start, ret = 0;
128+ cl_int status;
129+ size_t kernel_len;
130+ char *inbuf;
131+ int *mask;
132+ int buf_size = width * height * sizeof(char);
133+ int mask_size = sizeof(uint32_t) * 128;
134+
135+ cl_mem cl_mask, cl_inbuf, cl_outbuf;
136+ cl_kernel kernel = NULL;
137+ cl_program program = NULL;
138+ size_t local_work_size_2d[2] = {16, 16};
139+ size_t global_work_size_2d[2] = {(size_t)width, (size_t)height};
140+
141+ if (!(inbuf = av_malloc(buf_size)) || !(mask = av_malloc(mask_size))) {
142+ av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
143+ ret = AVERROR(ENOMEM);
144+ goto end;
145+ }
146+ fill_rand_int((int*)inbuf, buf_size/4);
147+ fill_rand_int(mask, mask_size/4);
148+
149+ CREATEBUF(cl_mask, CL_MEM_READ_ONLY, mask_size);
150+ CREATEBUF(cl_inbuf, CL_MEM_READ_ONLY, buf_size);
151+ CREATEBUF(cl_outbuf, CL_MEM_READ_WRITE, buf_size);
152+
153+ kernel_len = strlen(ocl_bench_source);
154+ program = clCreateProgramWithSource(ext_opencl_env->context, 1, &ocl_bench_source,
155+ &kernel_len, &status);
156+ if (status != CL_SUCCESS || !program) {
157+ av_log(NULL, AV_LOG_ERROR, "OpenCL unable to create benchmark program\n");
158+ ret = AVERROR_EXTERNAL;
159+ goto end;
160+ }
161+ status = clBuildProgram(program, 1, &(ext_opencl_env->device_id), NULL, NULL, NULL);
162+ if (status != CL_SUCCESS) {
163+ av_log(NULL, AV_LOG_ERROR, "OpenCL unable to build benchmark program\n");
164+ ret = AVERROR_EXTERNAL;
165+ goto end;
166+ }
167+ kernel = clCreateKernel(program, "unsharp_bench", &status);
168+ if (status != CL_SUCCESS) {
169+ av_log(NULL, AV_LOG_ERROR, "OpenCL unable to create benchmark kernel\n");
170+ ret = AVERROR_EXTERNAL;
171+ goto end;
172+ }
173+
174+ OCLCHECK(clEnqueueWriteBuffer, ext_opencl_env->command_queue, cl_inbuf, CL_TRUE, 0,
175+ buf_size, inbuf, 0, NULL, NULL);
176+ OCLCHECK(clEnqueueWriteBuffer, ext_opencl_env->command_queue, cl_mask, CL_TRUE, 0,
177+ mask_size, mask, 0, NULL, NULL);
178+ OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_mem), &cl_inbuf);
179+ OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_mem), &cl_outbuf);
180+ OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_mem), &cl_mask);
181+ OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_int), &width);
182+ OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_int), &height);
183+
184+ start = av_gettime();
185+ for (i = 0; i < OPENCL_NB_ITER; i++)
186+ OCLCHECK(clEnqueueNDRangeKernel, ext_opencl_env->command_queue, kernel, 2, NULL,
187+ global_work_size_2d, local_work_size_2d, 0, NULL, NULL);
188+ clFinish(ext_opencl_env->command_queue);
189+ ret = (av_gettime() - start)/OPENCL_NB_ITER;
190+end:
191+ if (kernel)
192+ clReleaseKernel(kernel);
193+ if (program)
194+ clReleaseProgram(program);
195+ if (cl_inbuf)
196+ clReleaseMemObject(cl_inbuf);
197+ if (cl_outbuf)
198+ clReleaseMemObject(cl_outbuf);
199+ if (cl_mask)
200+ clReleaseMemObject(cl_mask);
201+ av_free(inbuf);
202+ av_free(mask);
203+ return ret;
204+}
205+
206+static int compare_ocl_device_desc(const void *a, const void *b)
207+{
208+ return ((OpenCLDeviceBenchmark*)a)->runtime - ((OpenCLDeviceBenchmark*)b)->runtime;
209+}
210+
211+int opt_opencl_bench(void *optctx, const char *opt, const char *arg)
212+{
213+ int i, j, nb_devices = 0, count = 0;
214+ int64_t score = 0;
215+ AVOpenCLDeviceList *device_list;
216+ AVOpenCLDeviceNode *device_node = NULL;
217+ OpenCLDeviceBenchmark *devices = NULL;
218+ cl_platform_id platform;
219+
220+ av_opencl_get_device_list(&device_list);
221+ for (i = 0; i < device_list->platform_num; i++)
222+ nb_devices += device_list->platform_node[i]->device_num;
223+ if (!nb_devices) {
224+ av_log(NULL, AV_LOG_ERROR, "No OpenCL device detected!\n");
225+ return AVERROR(EINVAL);
226+ }
227+ if (!(devices = av_malloc(sizeof(OpenCLDeviceBenchmark) * nb_devices))) {
228+ av_log(NULL, AV_LOG_ERROR, "Could not allocate buffer\n");
229+ return AVERROR(ENOMEM);
230+ }
231+
232+ for (i = 0; i < device_list->platform_num; i++) {
233+ for (j = 0; j < device_list->platform_node[i]->device_num; j++) {
234+ device_node = device_list->platform_node[i]->device_node[j];
235+ platform = device_list->platform_node[i]->platform_id;
236+ score = av_opencl_benchmark(device_node, platform, run_opencl_bench);
237+ if (score > 0) {
238+ devices[count].platform_idx = i;
239+ devices[count].device_idx = j;
240+ devices[count].runtime = score;
241+ strcpy(devices[count].device_name, device_node->device_name);
242+ count++;
243+ }
244+ }
245+ }
246+ qsort(devices, count, sizeof(OpenCLDeviceBenchmark), compare_ocl_device_desc);
247+ fprintf(stderr, "platform_idx\tdevice_idx\tdevice_name\truntime\n");
248+ for (i = 0; i < count; i++)
249+ fprintf(stdout, "%d\t%d\t%s\t%"PRId64"\n",
250+ devices[i].platform_idx, devices[i].device_idx,
251+ devices[i].device_name, devices[i].runtime);
252+
253+ av_opencl_free_device_list(&device_list);
254+ av_free(devices);
255+ return 0;
256+}
257+
258+int opt_opencl(void *optctx, const char *opt, const char *arg)
259+{
260+ char *key, *value;
261+ const char *opts = arg;
262+ int ret = 0;
263+ while (*opts) {
264+ ret = av_opt_get_key_value(&opts, "=", ":", 0, &key, &value);
265+ if (ret < 0)
266+ return ret;
267+ ret = av_opencl_set_option(key, value);
268+ if (ret < 0)
269+ return ret;
270+ if (*opts)
271+ opts++;
272+ }
273+ return ret;
274+}
doc/APIchanges+2−0
@@ -14,6 +14,8 @@ libavutil: 2012-10-22 · 13 unmodified lines
1414
1515
1616 API changes, most recent first:
17+2013-12-xx - xxxxxxx - lavu 52.57.100 - opencl.h
18+ Add av_opencl_benchmark() function.
1719
1820 2013-11-xx - xxxxxxx - lavu 52.56.100 - ffversion.h
1921 Moves version.h to libavutil/ffversion.h.
doc/fftools-common-opts.texi+4−0
@@ -250,6 +250,10 @@ Possible flags for this option are: · 249 unmodified lines
250250 @end table
251251 @end table
252252
253+@item -opencl_bench
254+Benchmark all available OpenCL devices and show the results. This option
255+is only available when FFmpeg has been compiled with @code{--enable-opencl}.
256+
253257 @item -opencl_options options (@emph{global})
254258 Set OpenCL environment options. This option is only available when
255259 FFmpeg has been compiled with @code{--enable-opencl}.
doc/utils.texi+2−2
@@ -1051,13 +1051,13 @@ See reference "OpenCL Specification Version: 1.2 chapter 5.6.4". · 1050 unmodified lines
10511051 Select the index of the platform to run OpenCL code.
10521052
10531053 The specified index must be one of the indexes in the device list
1054−which can be obtained with @code{av_opencl_get_device_list()}.
1054+which can be obtained with @code{ffmpeg -opencl_bench} or @code{av_opencl_get_device_list()}.
10551055
10561056 @item device_idx
10571057 Select the index of the device used to run OpenCL code.
10581058
10591059 The specifed index must be one of the indexes in the device list which
1060−can be obtained with @code{av_opencl_get_device_list()}.
1060+can be obtained with @code{ffmpeg -opencl_bench} or @code{av_opencl_get_device_list()}.
10611061
10621062 @end table
10631063
libavutil/opencl.c+42−0
@@ -761,3 +761,45 @@ int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_n · 760 unmodified lines
761761 }
762762 return 0;
763763 }
764+
765+int64_t av_opencl_benchmark(AVOpenCLDeviceNode *device_node, cl_platform_id platform,
766+ int64_t (*benchmark)(AVOpenCLExternalEnv *ext_opencl_env))
767+{
768+ int64_t ret = 0;
769+ cl_int status;
770+ cl_context_properties cps[3];
771+ AVOpenCLExternalEnv *ext_opencl_env = NULL;
772+
773+ ext_opencl_env = av_opencl_alloc_external_env();
774+ ext_opencl_env->device_id = device_node->device_id;
775+ ext_opencl_env->device_type = device_node->device_type;
776+ av_log(&opencl_ctx, AV_LOG_VERBOSE, "Performing test on OpenCL device %s\n",
777+ device_node->device_name);
778+
779+ cps[0] = CL_CONTEXT_PLATFORM;
780+ cps[1] = (cl_context_properties)platform;
781+ cps[2] = 0;
782+ ext_opencl_env->context = clCreateContextFromType(cps, ext_opencl_env->device_type,
783+ NULL, NULL, &status);
784+ if (status != CL_SUCCESS || !ext_opencl_env->context) {
785+ ret = AVERROR_EXTERNAL;
786+ goto end;
787+ }
788+ ext_opencl_env->command_queue = clCreateCommandQueue(ext_opencl_env->context,
789+ ext_opencl_env->device_id, 0, &status);
790+ if (status != CL_SUCCESS || !ext_opencl_env->command_queue) {
791+ ret = AVERROR_EXTERNAL;
792+ goto end;
793+ }
794+ ret = benchmark(ext_opencl_env);
795+ if (ret < 0)
796+ av_log(&opencl_ctx, AV_LOG_ERROR, "Benchmark failed with OpenCL device %s\n",
797+ device_node->device_name);
798+end:
799+ if (ext_opencl_env->command_queue)
800+ clReleaseCommandQueue(ext_opencl_env->command_queue);
801+ if (ext_opencl_env->context)
802+ clReleaseContext(ext_opencl_env->context);
803+ av_opencl_free_external_env(&ext_opencl_env);
804+ return ret;
805+}
libavutil/opencl.h+16−0
@@ -310,4 +310,20 @@ void av_opencl_release_kernel(AVOpenCLKernelEnv *env); · 309 unmodified lines
310310 */
311311 void av_opencl_uninit(void);
312312
313+/**
314+ * Benchmark an OpenCL device with a user defined callback function. This function
315+ * sets up an external OpenCL environment including context and command queue on
316+ * the device then tears it down in the end. The callback function should perform
317+ * the rest of the work.
318+ *
319+ * @param device pointer to the OpenCL device to be used
320+ * @param platform cl_platform_id handle to which the device belongs to
321+ * @param benchmark callback function to perform the benchmark, return a
322+ * negative value in case of failure
323+ * @return the score passed from the callback function, a negative error code in case
324+ * of failure
325+ */
326+int64_t av_opencl_benchmark(AVOpenCLDeviceNode *device, cl_platform_id platform,
327+ int64_t (*benchmark)(AVOpenCLExternalEnv *ext_opencl_env));
328+
313329 #endif /* LIBAVUTIL_OPENCL_H */
libavutil/version.h+1−1
@@ -75,7 +75,7 @@ · 74 unmodified lines
7575 */
7676
7777 #define LIBAVUTIL_VERSION_MAJOR 52
78−#define LIBAVUTIL_VERSION_MINOR 56
78+#define LIBAVUTIL_VERSION_MINOR 57
7979 #define LIBAVUTIL_VERSION_MICRO 100
8080
8181 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t