使用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. Vue.js中 watch 的高级用法

    假设有如下代码: <div> <p>FullName: {{fullName}}</p> <p>FirstName: <input type=&q ...

  2. 数据挖掘算法:关联分析二(Apriori)

    二.Apriori算法 上文说到,大多数关联规则挖掘算法通常采用的策略是分解为两步: 频繁项集产生,其目标是发现满足具有最小支持度阈值的所有项集,称为频繁项集(frequent itemset). 规 ...

  3. Oracle数据库多表查询,子查询,集合运算

    记得自己要敲o~~~ select * from bonus; select * from salgrade; from dual; --笛卡尔积:两张表的乘积 select * from emp,d ...

  4. HDU Today hdu 2112

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2112 文章末有一些相应的测试数据供参考. 此题就是一个求最短路的问题,只不过现在的顶点名称变成了字符串而不 ...

  5. BZOJ 4289: PA2012 Tax 差分建图 最短路

    https://www.lydsy.com/JudgeOnline/problem.php?id=4289 https://www.cnblogs.com/clrs97/p/5046933.html  ...

  6. Codeforces Round #493 (Div 2) (A~E)

    目录 Codeforces 998 A.Balloons B.Cutting C.Convert to Ones D.Roman Digits E.Sky Full of Stars(容斥 计数) C ...

  7. 【枚举】AtCoder Regular Contest 095 C - Symmetric Grid

    题意:给你一个H*W的字符矩阵,一次操作可以任意将两行或者两列交换.问你是否能通过任意多次操作,使得其变为对称矩阵.对称的含义是:对于任何格子A(i,j),其都等于A(H-i+1,W-j+1). 显然 ...

  8. zoj 3229 上下界网络最大可行流带输出方案

    收获: 1. 上下界网络流求最大流步骤: 1) 建出无环无汇的网络,并看是否存在可行流 2) 如果存在,那么以原来的源汇跑一次最大流 3) 流量下界加上当前网络每条边的流量就是最大可行流了. 2. 输 ...

  9. Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造

    Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 ht ...

  10. linux 文件处理大杂烩

    1.对文件某行进行统计排序 awk  '{ printf "%-40s \n",$4}' /var/log/yum.log | sort | uniq -c | sort -nk ...