[FFmpeg-devel] [PATCH 5/5] libavutil: add user specfic device type

Stefano Sabatini stefasab at gmail.com
Thu Apr 4 20:22:43 CEST 2013


On date Thursday 2013-04-04 20:34:52 +0800, Wei Gao encoded:
> 

> From b9d6433b9ad5f9dc2e8688f42227eca1a58e5fcb Mon Sep 17 00:00:00 2001
> From: highgod0401 <highgod0401 at gmail.com>
> Date: Thu, 4 Apr 2013 20:25:29 +0800
> Subject: [PATCH 5/5] add user specfic device type

typo, also missing topic, e.g.:
lavu/opencl: add user specified device type

> 
> ---
>  libavutil/opencl.c | 80 +++++++++++++++++++++++++++++++-----------------------
>  libavutil/opencl.h |  4 +--
>  2 files changed, 48 insertions(+), 36 deletions(-)
> 
> diff --git a/libavutil/opencl.c b/libavutil/opencl.c
> index 9876a42..4c0b56f 100644
> --- a/libavutil/opencl.c
> +++ b/libavutil/opencl.c
> @@ -45,6 +45,7 @@ static pthread_mutex_t atomic_opencl_lock = PTHREAD_MUTEX_INITIALIZER;
>  typedef struct {
>      int dev_idx;
>      int platform_idx;

> +    int dev_type_idx;

device_type?

>  } UserSpecDevInfo;
>  
>  typedef struct {
> @@ -91,6 +92,9 @@ static OpenclUtils openclutils = {&openclutils_class};
>  
>  static GPUEnv gpu_env;
>  
> +static const cl_device_type device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU, CL_DEVICE_TYPE_DEFAULT};
> +
> +
>  typedef struct {
>      int err_code;
>      const char *err_str;
> @@ -249,7 +253,6 @@ end:
>      return ret;
>  }
>  
> -
>  void av_opencl_release_kernel(AVOpenCLKernelEnv *env)
>  {
>      cl_int status;
> @@ -277,8 +280,7 @@ static int init_opencl_env(GPUEnv *gpu_env, AVOpenCLExternalEnv *ext_opencl_env)
>      cl_platform_id *platform_ids = NULL;
>      cl_context_properties cps[3];
>      char platform_name[100];
> -    int i, ret = 0;
> -    cl_device_type device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU, CL_DEVICE_TYPE_DEFAULT};
> +    int i, j, ret = 0;
>      if (ext_opencl_env) {
>          if (gpu_env->is_user_created)
>              return 0;
> @@ -329,22 +331,21 @@ static int init_opencl_env(GPUEnv *gpu_env, AVOpenCLExternalEnv *ext_opencl_env)
>                          goto end;
>                      }
>                      gpu_env->platform_id = platform_ids[i];
> -                    status = clGetDeviceIDs(gpu_env->platform_id, CL_DEVICE_TYPE_GPU,
> -                                            0, NULL, &num_devices);
> -                    if (status != CL_SUCCESS) {
> -                        av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL device number:%s\n", opencl_errstr(status));
> -                        ret = AVERROR_EXTERNAL;
> -                        goto end;
> -                    }
> -                    if (num_devices == 0) {
> -                        //find CPU device
> -                        status = clGetDeviceIDs(gpu_env->platform_id, CL_DEVICE_TYPE_CPU,
> -                                             0, NULL, &num_devices);

> +                    j = 0;
> +                    if (gpu_env->usr_spec_dev_info.dev_type_idx >= 0) {
> +                        j = gpu_env->usr_spec_dev_info.dev_type_idx;
>                      }
> -                    if (status != CL_SUCCESS) {
> -                        av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL device ids: %s\n", opencl_errstr(status));
> -                        ret = AVERROR(EINVAL);
> -                        goto end;

> +                    while (j < sizeof(device_type)/sizeof(cl_device_type)) {


    for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++)

> +                        status = clGetDeviceIDs(gpu_env->platform_id, device_type[j],
> +                                            0, NULL, &num_devices);

weird indent

> +                        if (status == CL_SUCCESS) {
> +                            gpu_env->device_type = device_type[j];
> +                            break;
> +                        }
> +                        if (gpu_env->usr_spec_dev_info.dev_type_idx >= 0) {
> +                            break; // find using other platforms
> +                        }
> +                        j++;
>                      }
>                      if (num_devices)
>                         break;
> @@ -353,8 +354,14 @@ static int init_opencl_env(GPUEnv *gpu_env, AVOpenCLExternalEnv *ext_opencl_env)
>                          ret = AVERROR_EXTERNAL;
>                          goto end;
>                      }
> +                    if (i >= num_platforms - 1) {
> +                        if (status != CL_SUCCESS) {
> +                            av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL device ids: %s\n", opencl_errstr(status));
> +                            ret = AVERROR(EINVAL);
> +                            goto end;
> +                        }
> +                    }
>                      i++;
> -
>                  }
>              }
>              if (!gpu_env->platform_id) {
> @@ -379,22 +386,11 @@ static int init_opencl_env(GPUEnv *gpu_env, AVOpenCLExternalEnv *ext_opencl_env)
>              cps[2] = 0;
>  
>              /* Check for GPU. */
> -            for (i = 0; i < sizeof(device_type); i++) {
> -                gpu_env->device_type = device_type[i];
> -                gpu_env->context     = clCreateContextFromType(cps, gpu_env->device_type,
> -                                                               NULL, NULL, &status);
> -                if (status != CL_SUCCESS) {
> -                    av_log(&openclutils, AV_LOG_ERROR,
> -                           "Could not get OpenCL context from device type: %s\n", opencl_errstr(status));
> -                    ret = AVERROR_EXTERNAL;
> -                    goto end;
> -                }
> -                if (gpu_env->context)
> -                    break;
> -            }
> -            if (!gpu_env->context) {
> +            gpu_env->context     = clCreateContextFromType(cps, gpu_env->device_type,
> +                                                           NULL, NULL, &status);
> +            if (status != CL_SUCCESS) {
>                  av_log(&openclutils, AV_LOG_ERROR,
> -                       "Could not get OpenCL context from device type\n");
> +                       "Could not get OpenCL context from device type: %s\n", opencl_errstr(status));
>                  ret = AVERROR_EXTERNAL;
>                  goto end;
>              }

> @@ -519,14 +515,17 @@ int av_opencl_init(AVDictionary *options, AVOpenCLExternalEnv *ext_opencl_env)
>      AVDictionaryEntry *opt_build_entry;
>      AVDictionaryEntry *opt_platform_entry;
>      AVDictionaryEntry *opt_device_entry;
> +    AVDictionaryEntry *opt_device_type_entry;
>      char *pos;
>      LOCK_OPENCL
>      if (!gpu_env.init_count) {
>          opt_platform_entry = av_dict_get(options, "platform_idx", NULL, 0);
>          opt_device_entry   = av_dict_get(options, "device_idx", NULL, 0);
> +        opt_device_type_entry = av_dict_get(options, "device_type", NULL, 0);
>          /* initialize devices, context, command_queue */
>          gpu_env.usr_spec_dev_info.platform_idx = -1;
>          gpu_env.usr_spec_dev_info.dev_idx = -1;
> +        gpu_env.usr_spec_dev_info.dev_type_idx = -1;
>          if (opt_platform_entry) {
>              gpu_env.usr_spec_dev_info.platform_idx = strtol(opt_platform_entry->value, &pos, 10);
>              if (pos == opt_platform_entry->value) {
> @@ -543,6 +542,19 @@ int av_opencl_init(AVDictionary *options, AVOpenCLExternalEnv *ext_opencl_env)
>                  goto end;
>              }
>          }
> +        if (opt_device_type_entry) {
> +            if (!av_strcasecmp(opt_device_type_entry->value, "GPU")) {
> +                gpu_env.usr_spec_dev_info.dev_type_idx = 0;
> +            } else if (!av_strcasecmp(opt_device_type_entry->value, "CPU")) {
> +                gpu_env.usr_spec_dev_info.dev_type_idx = 1;
> +            } else if (!av_strcasecmp(opt_device_type_entry->value, "default")) {
> +                gpu_env.usr_spec_dev_info.dev_type_idx = 2;
> +            } else {
> +                av_log(&openclutils, AV_LOG_ERROR, "Device type should be CPU or GPU or default\n");
> +                ret = AVERROR(EINVAL);
> +                goto end;
> +            }
> +        }

We should use an option context and avoid manual parsing.

[...]
-- 
FFmpeg = Fostering and Frightening Minimalistic Philosophical Extended Gnome


More information about the ffmpeg-devel mailing list