首页 > 其他 > 详细

OpenCL 操作context

时间:2014-06-05 07:00:09      阅读:584      评论:0      收藏:0      [点我收藏+]

本程序主要测试:

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;
}

}

bubuko.com,布布扣


OpenCL 操作context,布布扣,bubuko.com

OpenCL 操作context

原文:http://blog.csdn.net/kenden23/article/details/27526785

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!