使用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. CentOS 使用命令设置代理

    全局的代理设置, vim /etc/profile 添加下面内容 http_proxy = http://username:password@yourproxy:8080/ ftp_proxy = h ...

  2. ACM 水果 hdu 1263 一题多解

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1263 文章末有相应的一些测试数据供参考. 传统的数组解题方式 思路一: 三种属性的数据放在一个结构体里面, ...

  3. nc工具学习

    0x00.命令详解 基本使用 想要连接到某处:nc  [-options] ip port 绑定端口等待连接:nc -l -p port ip 参数: -e prog 程序重定向,一旦连接,就执行 [ ...

  4. [BZOJ2669] [cqoi2012]局部极小值

    [BZOJ2669] [cqoi2012]局部极小值 Description 有一个n行m列的整数矩阵,其中1到nm之间的每个整数恰好出现一次.如果一个格子比所有相邻格子(相邻是指有公共边或公共顶点) ...

  5. 王彪-20162321-Java程序设计与数据结构2nd-第十周学习总结

    学习目标 讨论有向图和无向图 定义带权图并讨论它们的应用 定义图的广度优先遍历和深度优先遍历 定义最小生成树 讨论图的实现策略 书中图的基本定义 图是由结点及结点间的连接组成的,结点称为顶点,结点间的 ...

  6. Java并发(十九):final实现原理

    final在Java中是一个保留的关键字,可以声明成员变量.方法.类以及本地变量. 一旦你将引用声明作final,你将不能改变这个引用了,编译器会检查代码,如果你试图将变量再次初始化的话,编译器会报编 ...

  7. js的继承实现方式

    1. 使用call或者apply来实现js对象继承 function Animal(age){ this.age = age; this.say = function(){ console.log(' ...

  8. ACdream 速攻组~

    1007 a + b /*这题就是一个快速幂,但是十分猥琐的是,模是1e10 + 7,不是1e9 + 7,这就产生了一个爆long long的问题.所以要对快速幂中的乘法操作进行一下改造.请教了BIT ...

  9. IO流-批量修改文件名称案例

    /* *   源文件名:   桌面-我们今天学习IO流了哈哈哈哈-001.jpg *   修改后文件名:  桌面-000x.jpg */public class File_listFiles_upda ...

  10. Go语言Web框架gwk介绍 (三)

    上一篇忘了ChanResult ChanResult 可以用来模拟BigPipe,定义如下 type ChanResult struct { Wait sync.WaitGroup Chan chan ...