GPGPU OpenCL 获取kernel函数编译信息
使用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函数编译信息的更多相关文章
- GPGPU OpenCL 获取设备信息
在使用OpenCL编程中,需要对GPU设备的底层理解,这样才能更好的进行代码优化. 比如计算单元CU数量,每个CU的执行单元PE数量,每个CU中的共享内存大小等等.只有了解了这些才能更好的使用共享内存 ...
- Linux Kernel ‘mp_get_count()’函数本地信息泄露漏洞
漏洞名称: Linux Kernel ‘mp_get_count()’函数本地信息泄露漏洞 CNNVD编号: CNNVD-201311-054 发布时间: 2013-11-06 更新时间: 2013- ...
- python装饰器内获取函数有用信息方法
装饰器内获取函数有用信息方法 .__doc__用于得到函数注释信息 .__name_用于得到函数名 在函数引用装饰器的时候,函数名会变为装饰器内部执行该函数的名字,所有在直接执行函数名加.__doc_ ...
- QMetaMethod 获取成员函数的元信息
在上一篇中,我们将的是QMetaEnum类,它可以获得一个类中由Q_ENUM宏或Q_FLAG宏声明的枚举类型的元信息.同样,QMetaMethod类是用来获取成员方法的元信息的一个类.通过该类,我们可 ...
- C/C++通过WMI和系统API函数获取获取系统硬件配置信息
转载:http://www.cnblogs.com/renyuan/archive/2012/12/29/2838716.html 转载:http://blog.csdn.net/jhqin/arti ...
- 【并行计算-CUDA开发】GPGPU OpenCL/CUDA 高性能编程的10大注意事项
GPGPU OpenCL/CUDA 高性能编程的10大注意事项 1.展开循环 如果提前知道了循环的次数,可以进行循环展开,这样省去了循环条件的比较次数.但是同时也不能使得kernel代码太大. 循环展 ...
- GPGPU OpenCL/CUDA 高性能编程的10大注意事项
转载自:http://hc.csdn.net/contents/content_details?type=1&id=341 1.展开循环 如果提前知道了循环的次数,可以进行循环展开,这样省去了 ...
- kernel(一)编译体验
目录 打补丁 配置 总结 配置方式 配置体验 配置详解 Makefile解析 子目录的Makefile 架构下面的Makefile 顶层Makefile Make解析 编译 链接 链接脚本 烧写内核 ...
- make V=1 查看完整的gcc编译信息
Linux内核make命令选项 2012年5月28日lenky发表评论阅读评论6,289 次浏览 升级Linux内核的操作已经变得很简单,基本的几个命令即可搞定:make menuconfig.m ...
随机推荐
- Scala入门4(_的用法)
从网上找了一篇博客,详细讲解了Scala下划线的用法,这里做保留 博客链接
- Mendeley文献管理软件使用介绍
<!DOCTYPE html> New Document /* GitHub stylesheet for MarkdownPad (http://markdownpad.com) / / ...
- django信号调度的用法
Django中提供了"信号调度",用于在框架执行操作时解耦. 一些动作发生的时候,系统会根据信号定义的函数执行相应的操作 Django中内置的signal Model_signal ...
- sublime3176注册码破解汉化及常用插件
官方网站下载地址:https://www.sublimetext.com/3 破解软件下载地址:https://www.lanzous.com/i1a7zfi 破解软件下载地址备用:https://d ...
- 维护直线的线段树:Bzoj1568,Bzoj3938(Uoj88)
有这样一类线段树,可以维护一些直线方程并对每个点求出最大值. 首先先看BZOJ1568,输入给你条直线的方程,你需要对于指定的位置求出最大的函数值. 看到数据范围nlog^2n可做,考虑用线段树去维护 ...
- BZOJ 2120: 数颜色 带修改的莫队算法 树状数组套主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=2120 标题里是两种不同的解法. 带修改的莫队和普通莫队比多了个修改操作,影响不大,但是注意一下细节 ...
- BZOJ.5329.[SDOI2018]战略游戏(圆方树 虚树)
题目链接 显然先建圆方树,方点权值为0圆点权值为1,两点间的答案就是路径权值和减去起点终点. 对于询问,显然可以建虚树.但是只需要计算两关键点间路径权值,所以不需要建出虚树.统计DFS序相邻的两关键点 ...
- 在windows安装配置Git开发环境
开始配置Git的开发环境.首先从google code下载最新的windows的git安装包msysgit,当时我下载的是Git-1.7.4-preview20110204.exe,然后就开始安装了 ...
- 【10.6校内测试】【小模拟】【hash+线段树维护覆盖序列】
一开始看到题就果断跳到T2了!!没想到T2才是个大坑,浪费了两个小时QAQ!! 就是一道小模拟,它怎么说就怎么走就好了! 为什么要用这么多感叹号!!因为统计答案要边走边统计!!如果每个数据都扫一遍20 ...
- Codeforces Round #352 (Div. 1) B. Robin Hood 二分
B. Robin Hood 题目连接: http://www.codeforces.com/contest/671/problem/B Description We all know the impr ...