浅谈OpenCL之Platform API(2)

本文深入探讨了OpenCL中Context的概念,它是OpenCL程序的核心,负责内存管理与设备交互。文章详细介绍了Context的创建方法,包括如何指定平台和设备类型,以及如何通过API获取Context的相关信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

OpenCL Contexts

context是贯串 opencl 程序的核心,也是host 与device 交互的唯一通道,是内存申请维护,命令队列的创建等功能的基础。

不同的plaform,可以申请不同的context,不同的context内的内存不能直接共享,在同一个context下的 不同device的内存是相同的,可以相互访问。

创建Contexts API 有两个:

cl_context clCreateContext (const cl_context_properties *properties,
                            cl_uint num_devices,
                            const cl_device_id *devices,
                           void (CL_CALLBACK *pfn_notify) (const char *errinfo, const void *private_info,  size_t cb, void *user_data),
                          void *user_data,
                         cl_int *errcode_ret)
cl_context clCreateContextFromType (const cl_context_properties *properties,
                                    cl_device_type device_type,
                                    void (CL_CALLBACK *pfn_notify)(const char *errinfo,const void *private_info,size_t cb,void *user_data),
                                   void *user_data,
                                   cl_int *errcode_ret)

第二个创建context API是可以根据所需要的设备类型选择创建context.

参数:

const cl_context_properties *properties

context 属性,创建context一般为:

cl_context_properties contextProperties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platformId, 0};

cl_device_type device_type

  设备类型

cl_uint num_devices

 为该platform device数量

const cl_device_id *devices

   platform下的device id

void (CL_CALLBACK *pfn_notify) (const char *errinfo, const void *private_info,  size_t cb, void *user_data):

用于注册回调,opencl通知错误信息

void *user_data

   为用户数据

cl_int *errcode_ret

    返回错误码

使用范例:

    cl_context_properties contextProperties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platformId, 0};
    ocl->context = clCreateContextFromType(contextProperties, deviceType, NULL, NULL, &err);
    if ((CL_SUCCESS != err) || (NULL == ocl->context))
    {
        LogError("Couldn't create a context, clCreateContextFromType() returned '%s'.\n", TranslateOpenCLError(err));
        return err;
    }

     创建完成context之后,就可以获取conext 属性,其API为:

cl_int clGetContextInfo (cl_context context,
                         cl_context_info param_name,
                         size_t param_value_size,
                         void * param_value,
                         size_t * param_value_size_ret)

   参数:

      cl_context context: 为context

      cl_context_info param_name: 参数名称

      size_t param_value_size:输出param_value的长度

       void * param_value:获取参数值

       size_t * param_value_size_ret:错误码返回值

其用法和之前的API类似,先获取长度,然后再次调用获取到参数的值

      cl_context_info param_name 列表如下:

     

cl_context_infoReturn TypeDescription
CL_CONTEXT_REFERENCE_COUNTcl_uintReturns the context reference count.
CL_CONTEXT_NUM_DEVICEScl_uintReturns the number of devices in context.
CL_CONTEXT_DEVICEScl_device_id[]Returns the list of devices in context.
CL_CONTEXT_PROPERTIEScl_context_properties[]

Returns the properties argument specified in clCreateContext or clCreateContextFromType.If the properties argument specified in clCreateContext or clCreateContextFrom-Type used to create context is not NULL, the implementation must return the values specified in the properties argument. If the properties argument specified in clCreateContext or clCreateContextFrom-Type used to create context is NULL, the imple-mentation may return either a param_value_size_ret of 0 (that is, there is no context property
value to be returned) or a context property value of 0 (where 0 is used to terminate the context proper- ties list) in the memory that param_value points to.

     使用范例:

   

    // Query for OpenCL device which was used for context creation
    err = clGetContextInfo(ocl->context, CL_CONTEXT_DEVICES, sizeof(cl_device_id), &ocl->device, NULL);
    if (CL_SUCCESS != err)
    {
        LogError("Error: clGetContextInfo() to get list of devices returned %s.\n", TranslateOpenCLError(err));
        return err;
    }

    自此platform API讲完,主要包括platform, device ,context

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Huo的藏经阁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值