本程序主要测试:
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; } }
OpenCL 操作context,布布扣,bubuko.com
时间: 2024-11-01 22:29:33