[FFmpeg-devel] [PATCH] libavutil/opencl:optimize compile kernel operation

Stefano Sabatini stefasab at gmail.com
Sat Apr 20 12:14:58 CEST 2013


On date Saturday 2013-04-20 12:41:14 +0800, Wei Gao encoded:
> 

> From 1b3e213a2bcc866445208b7bcbe4d9ae926709ea Mon Sep 17 00:00:00 2001
> From: highgod0401 <highgod0401 at gmail.com>
> Date: Sat, 20 Apr 2013 12:35:22 +0800
> Subject: [PATCH] optimize compile kernel operation

nit: lavu/opencl: ...

> 
> ---
>  libavutil/opencl.c | 45 ++++++++++++++++-----------------------------
>  1 file changed, 16 insertions(+), 29 deletions(-)
> 
> diff --git a/libavutil/opencl.c b/libavutil/opencl.c
> index 92f0c02..7e3e5d2 100644
> --- a/libavutil/opencl.c
> +++ b/libavutil/opencl.c
> @@ -544,57 +544,44 @@ static int init_opencl_env(OpenclContext *opencl_ctx, AVOpenCLExternalEnv *ext_o
>  static int compile_kernel_file(OpenclContext *opencl_ctx)
>  {
>      cl_int status;
> -    char *temp, *source_str = NULL;
> -    size_t source_str_len = 0;
> -    int i, ret = 0;

> +    int i, kernel_code_counter = 0;
> +    const char *kernel_codes[MAX_KERNEL_CODE_NUM] = {NULL};
> +    size_t kernel_code_len[MAX_KERNEL_CODE_NUM] = {0};

nit:

kernel_code_count
kernel_code
kernel_code_len

for consistency

>  
>      for (i = 0; i < opencl_ctx->kernel_code_count; i++) {
> -        if (!opencl_ctx->kernel_code[i].is_compiled)
> -            source_str_len += strlen(opencl_ctx->kernel_code[i].kernel_string);
> -    }
> -    if (!source_str_len) {
> -        return 0;
> -    }
> -    source_str = av_mallocz(source_str_len + 1);
> -    if (!source_str) {
> -        return AVERROR(ENOMEM);
> -    }
> -    temp = source_str;
> -    for (i = 0; i < opencl_ctx->kernel_code_count; i++) {
>          if (!opencl_ctx->kernel_code[i].is_compiled) {
> -            memcpy(temp, opencl_ctx->kernel_code[i].kernel_string,
> -                        strlen(opencl_ctx->kernel_code[i].kernel_string));
> +            kernel_codes[kernel_code_counter] = opencl_ctx->kernel_code[i].kernel_string;
> +            kernel_code_len[kernel_code_counter] = strlen(opencl_ctx->kernel_code[i].kernel_string);
>              opencl_ctx->kernel_code[i].is_compiled = 1;
> -            temp += strlen(opencl_ctx->kernel_code[i].kernel_string);
> +            kernel_code_counter++;
>          }
>      }
> +    if (!kernel_code_counter)
> +        return 0;
>      /* create a CL program using the kernel source */
>      opencl_ctx->programs[opencl_ctx->program_count] = clCreateProgramWithSource(opencl_ctx->context,
> -                                                           1, (const char **)(&source_str),
> -                                                                   &source_str_len, &status);
> +                                                                                kernel_code_counter,
> +                                                                                kernel_codes,
> +                                                                                kernel_code_len,
> +                                                                                &status);
>      if(status != CL_SUCCESS) {
>          av_log(opencl_ctx, AV_LOG_ERROR,
>                 "Could not create OpenCL program with source code: %s\n", opencl_errstr(status));
> -        ret = AVERROR_EXTERNAL;
> -        goto end;
> +        return AVERROR_EXTERNAL;
>      }
>      if (!opencl_ctx->programs[opencl_ctx->program_count]) {
>          av_log(opencl_ctx, AV_LOG_ERROR, "Created program is NULL\n");
> -        ret = AVERROR_EXTERNAL;
> -        goto end;
> +        return AVERROR_EXTERNAL;
>      }
>      status = clBuildProgram(opencl_ctx->programs[opencl_ctx->program_count], 1, &(opencl_ctx->device_id),
>                              opencl_ctx->build_options, NULL, NULL);
>      if (status != CL_SUCCESS) {
>          av_log(opencl_ctx, AV_LOG_ERROR,
>                 "Could not compile OpenCL kernel: %s\n", opencl_errstr(status));
> -        ret = AVERROR_EXTERNAL;
> -        goto end;
> +        return AVERROR_EXTERNAL;
>      }
>      opencl_ctx->program_count++;
> -end:
> -    av_free(source_str);
> -    return ret;
> +    return 0;
>  }

LGTM otherwise, nice simplification.
-- 
FFmpeg = Faithless Fostering Marvellous Peaceless Ermetic Gangster


More information about the ffmpeg-devel mailing list