使用OpenCL编程时,kernel写成一个单独的文件或者将文件内容保存在一个string中。可以使用clBuildProgram对kernel进行编译链接(compiles & links),如果失败,可以使用clGetProgramBuildInfo获取OpenCL编译器对kernel的编译信息。

1.clBuildProgram 

 cl_int clBuildProgram (

    cl_program program,  //program
    cl_uint num_devices,  //the number of device
    const cl_device_id *device_list,   //devices id
    const char *options,  //the option of compiler
    void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data),  //the callback function
    void *user_data)  //the data of callback function
  )

2.clGetProgramBuildInfo

  cl_int clGetProgramBuildInfo (

    cl_program program,   //program
    cl_device_id device,  //the id of device
    cl_program_build_info param_name,
    size_t param_value_size,
    void *param_value,
    size_t *param_value_size_ret
  )

3.代码实例(获取编译器对kernel的编译信息)

3.1 kernel(build_info_kernel.cl)

 __kernel void good(__global float *a,
__global float *b,
__global float *c) { *c = *a + *b;
} __kernel void good(__global float *a,
__global float *b,
__global float *c) {
__local int var=;
int size=get_local_sze();
*c = *a + *b;
}

3.2 tool.h

 #ifndef TOOLH
#define TOOLH
#include <CL/cl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std; /** convert the kernel file into a string */
int convertToString(const char *filename, std::string& s); /**Getting platforms and choose an available one.*/
int getPlatform(cl_platform_id &platform); /**Step 2:Query the platform and choose the first GPU device if has one.*/
cl_device_id *getCl_device_id(cl_platform_id &platform); /**获取编译program出错时,编译器的出错信息*/
int getProgramBuildInfo(cl_program program,cl_device_id device);
#endif

    tool.cpp

 #include <CL/cl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
#include "tool.h"
using namespace std; /** convert the kernel file into a string */
int convertToString(const char *filename, std::string& s)
{
size_t size;
char* str;
std::fstream f(filename, (std::fstream::in | std::fstream::binary)); if(f.is_open())
{
size_t fileSize;
f.seekg(, std::fstream::end);
size = fileSize = (size_t)f.tellg();
f.seekg(, std::fstream::beg);
str = new char[size+];
if(!str)
{
f.close();
return ;
} f.read(str, fileSize);
f.close();
str[size] = '\0';
s = str;
delete[] str;
return ;
}
cout<<"Error: failed to open file\n:"<<filename<<endl;
return -;
} /**Getting platforms and choose an available one.*/
int getPlatform(cl_platform_id &platform)
{
platform = NULL;//the chosen platform cl_uint numPlatforms;//the NO. of platforms
cl_int status = clGetPlatformIDs(, NULL, &numPlatforms);
if (status != CL_SUCCESS)
{
cout<<"Error: Getting platforms!"<<endl;
return -;
} /**For clarity, choose the first available platform. */
if(numPlatforms > )
{
cl_platform_id* platforms =
(cl_platform_id* )malloc(numPlatforms* sizeof(cl_platform_id));
status = clGetPlatformIDs(numPlatforms, platforms, NULL);
platform = platforms[];
free(platforms);
}
else
return -;
} /**Step 2:Query the platform and choose the GPU device*/
cl_device_id *getCl_device_id(cl_platform_id &platform)
{
cl_uint numDevices = ;
cl_device_id *devices=NULL;
cl_int status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, , NULL, &numDevices);
if (numDevices > ) //GPU available.
{
devices = (cl_device_id*)malloc(numDevices * sizeof(cl_device_id));
status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
}
return devices;
} /**获取编译program出错时,编译器的出错信息*/
int getProgramBuildInfo(cl_program program,cl_device_id device)
{
size_t log_size;
char *program_log;
/* Find size of log and print to std output */
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
, NULL, &log_size);
program_log = (char*) malloc(log_size+);
program_log[log_size] = '\0';
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
log_size+, program_log, NULL);
printf("%s\n", program_log);
free(program_log);
return ;
}

3.3 buildInfo.cpp

 #include "tool.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std; void CL_CALLBACK checkData(cl_program platform, void* data){
printf("%s\n",(char*)data);
} int main(int argc, char* argv[])
{
cl_int status;
/** Getting platforms and choose an available one(first).*/
cl_platform_id platform;
getPlatform(platform); /**Query the platform and choose the GPU device.*/
cl_device_id *devices=getCl_device_id(platform); /**Create context use the frist device.*/
cl_context context = clCreateContext(NULL,, devices,NULL,NULL,NULL); /**Create program object */
const char *filename = "build_info_kernel.cl";
string sourceStr;
status = convertToString(filename, sourceStr);
const char *source = sourceStr.c_str();
size_t sourceSize[] = {strlen(source)};
cl_program program = clCreateProgramWithSource(context, , &source, sourceSize, NULL); /**Build program. */
//status=clBuildProgram(program, 1,devices,NULL,checkData,"sdf");
status=clBuildProgram(program, ,devices,NULL,NULL,NULL);
if(status < ) //get the build info
getProgramBuildInfo(program ,devices[]);
else
printf("Build Success\n"); status = clReleaseProgram(program); //Release the program object.
status = clReleaseContext(context);//Release context.
free(devices); getchar();
return ;
}

对kernel的编译结果:

GPGPU OpenCL 获取kernel函数编译信息的更多相关文章

  1. GPGPU OpenCL 获取设备信息

    在使用OpenCL编程中,需要对GPU设备的底层理解,这样才能更好的进行代码优化. 比如计算单元CU数量,每个CU的执行单元PE数量,每个CU中的共享内存大小等等.只有了解了这些才能更好的使用共享内存 ...

  2. Linux Kernel ‘mp_get_count()’函数本地信息泄露漏洞

    漏洞名称: Linux Kernel ‘mp_get_count()’函数本地信息泄露漏洞 CNNVD编号: CNNVD-201311-054 发布时间: 2013-11-06 更新时间: 2013- ...

  3. python装饰器内获取函数有用信息方法

    装饰器内获取函数有用信息方法 .__doc__用于得到函数注释信息 .__name_用于得到函数名 在函数引用装饰器的时候,函数名会变为装饰器内部执行该函数的名字,所有在直接执行函数名加.__doc_ ...

  4. QMetaMethod 获取成员函数的元信息

    在上一篇中,我们将的是QMetaEnum类,它可以获得一个类中由Q_ENUM宏或Q_FLAG宏声明的枚举类型的元信息.同样,QMetaMethod类是用来获取成员方法的元信息的一个类.通过该类,我们可 ...

  5. C/C++通过WMI和系统API函数获取获取系统硬件配置信息

    转载:http://www.cnblogs.com/renyuan/archive/2012/12/29/2838716.html 转载:http://blog.csdn.net/jhqin/arti ...

  6. 【并行计算-CUDA开发】GPGPU OpenCL/CUDA 高性能编程的10大注意事项

    GPGPU OpenCL/CUDA 高性能编程的10大注意事项 1.展开循环 如果提前知道了循环的次数,可以进行循环展开,这样省去了循环条件的比较次数.但是同时也不能使得kernel代码太大. 循环展 ...

  7. GPGPU OpenCL/CUDA 高性能编程的10大注意事项

    转载自:http://hc.csdn.net/contents/content_details?type=1&id=341 1.展开循环 如果提前知道了循环的次数,可以进行循环展开,这样省去了 ...

  8. kernel(一)编译体验

    目录 打补丁 配置 总结 配置方式 配置体验 配置详解 Makefile解析 子目录的Makefile 架构下面的Makefile 顶层Makefile Make解析 编译 链接 链接脚本 烧写内核 ...

  9. make V=1 查看完整的gcc编译信息

    Linux内核make命令选项 2012年5月28日lenky发表评论阅读评论6,289 次浏览   升级Linux内核的操作已经变得很简单,基本的几个命令即可搞定:make menuconfig.m ...

随机推荐

  1. 使用chrales抓包IOS的https(pc+手机)

    1.安装SSL证书到手机 点击 Help -> SSL Proxying -> Install Charles Root Certificate on a Mobile Device 2. ...

  2. DNS 设置

    用上 ip 靓号1.1.1.1,Cloudflare 花了多少钱? ipv4: 1.1.1.1 和 1.0.0.1 是目前最快的 DNS. 通过ipv6***之二--使用IPv6 DNS服务器

  3. CSUOJ 1224 ACM小组的古怪象棋

    Description ACM小组的Samsara和Staginner对中国象棋特别感兴趣,尤其对马(可能是因为这个棋子的走法比较多吧)的使用进行深入研究.今天他们又在 构思一个古怪的棋局:假如Sam ...

  4. [leetcode DP]72. Edit Distance

    计算最少用多少不把word1变为word2, 思路:建立一个dp表,行为word1的长度,宽为word2的长度 1.边界条件,dp[i][0] = i,dp[0][j]=j 2.最优子问题,考虑已经知 ...

  5. python中的递归问题,求圆周率

    以上面一个公式为例: import numpy as np def getPi(n): if n == 0: return np.power(-1,n)*(1.0/(2*n+1)) else: ret ...

  6. 【BZOJ 2724】 2724: [Violet 6]蒲公英 (区间众数不带修改版本)

    2724: [Violet 6]蒲公英 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 1908  Solved: 678 Description In ...

  7. [BZOJ5317][JSOI2018]部落战争(闵可夫斯基和)

    对于点集$A$,$B$,闵可夫斯基和$C=\{(x1+x2,y1+y2)|(x1,x2)\in A,(y1,y2)\in B\}$.由此可知,对于两个凸包$A$,$B$的闵可夫斯基和$C$满足,$C$ ...

  8. 详解Android基本布局

    一.线性布局 LinearLayout又称为线性布局,是一种非常常用的布局.这个布局会将它包含的控件在线性方向上依次排列.我们可以通过指定它的orientation属性来决定它是垂直方向排列还是水平方 ...

  9. Problem D: 深入浅出学算法005-数7

    Description 逢年过节,三五好友,相约小聚,酒过三旬,围桌数七. “数七”是一个酒桌上玩的小游戏.就是按照顺序,某人报一个10以下的数字,然后后面的人依次在原来的数字上加1,并喊出来,当然如 ...

  10. bzoj 1069

    最开始想到的是枚举3个点,另一个点用卡壳的思想,但实际上可以只枚举两个点(对角线上的两个点),其余两个点用卡壳. /****************************************** ...