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 ...
随机推荐
- 构建第一个Spring Boot项目
1.启动IntelliJ IDEA,点击"Create New Project"  2.选择"Spring initializr",设定SDK及Spring ...
- Ionic Js十五:对话框
$ionicPopup ionic 对话框服务允许程序创建.显示弹出窗口. $ionicPopup 提供了3个方法:alert(), prompt(),以及 confirm() . 实例 HTML 代 ...
- 10 Best jQuery and HTML5 WYSIWYG Plugins
https://www.sitepoint.com/10-best-html-wysiwyg-plugins/
- Scala入门4(_的用法)
从网上找了一篇博客,详细讲解了Scala下划线的用法,这里做保留 博客链接
- PHP isset和empty 详细比较
找了几篇博文,这应该是说的最清楚的.链接如下:http://blog.chinaunix.net/uid-25311424-id-3966622.html
- react篇章-React State(状态)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- CSU - 2056 a simple game
Description 这一天,小A和小B在玩一个游戏,他俩每人都有一个整数,然后两人轮流对他们的整数进行操作,每次在下列两个操作任选一个: (1)对整数进行翻转,如1234翻转成4321 ,1200 ...
- mongodb的yum源配置和安装
安装前注意: 此教程是通过yum安装的.仅限64位centos系统 安装步骤: 1.创建仓库文件: vi /etc/yum.repos.d/mongodb-org-3.4.repo 然后复制下面配置, ...
- [leetcode tree]98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- hdu1527下沙小面的(二)
B - 下沙小面的(2) Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...