OpenCL 操作context

本程序主要测试:

context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);

创建一个context

clRetainContext(context);//Context的reference +1

clReleaseContext(context);//Context的reference -1


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef MAC
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif

namespace context_count
{

int run() 
{
	cl_platform_id platform;
	cl_device_id device;
	cl_context context;
	cl_int err;
	cl_uint ref_count;

	err = clGetPlatformIDs(1, &platform, NULL);
	if(err < 0) {
		perror("Couldn't find any platforms");
		exit(1);
	}

	err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
	if(err == CL_DEVICE_NOT_FOUND) {
		err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &device, NULL);
	}
	if(err < 0) {
		perror("Couldn't find any devices");
		exit(1);
	}

	/* 创建 context */
	context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
	if(err < 0) {
		perror("Couldn't create a context");
		exit(1);   
	}

	/* 获取reference count的数量,使用ref_count返回值*/
	err = clGetContextInfo(context, CL_CONTEXT_REFERENCE_COUNT, 	
		sizeof(ref_count), &ref_count, NULL);			
	if(err < 0) {		
		perror("Couldn't read the reference count.");
		exit(1);
	}
	printf("Initial reference count: %u\n", ref_count);

	/* 每次函数访问context的时候,调用clRetainContext,就是把context reference + 1,因为context并不是像platform和device那样delete的,而是clRetainContext的时候+1,当调用clReleaseContext的时候-1,当为零的时候,系统自动删除context。这就可以方便cl_context数据存活超过创建它的函数,可以让第三方库什么的继续访问context */
	clRetainContext(context);clRetainContext(context);						
	clGetContextInfo(context, CL_CONTEXT_REFERENCE_COUNT, 		
		sizeof(ref_count), &ref_count, NULL);			
	printf("Reference count: %u\n", ref_count);			

	clReleaseContext(context);						
	clGetContextInfo(context, CL_CONTEXT_REFERENCE_COUNT, 		
		sizeof(ref_count), &ref_count, NULL);			
	printf("Reference count: %u\n", ref_count);			

	clReleaseContext(context);clReleaseContext(context);	
	system("pause");
	return 0;
}

}



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值