本程序生成一个OpenCL Program,然后获取Program的source,事实上它的source就是一个char[],能够打印出来。

然后我们把这些内容和原来文本的内容对照,看看是否是我们想要读入的内容。

还能够測试是否编译正确,假设不对会有输出提示的。

以下程序执行例如以下:

前面都是读入的函数代码。然后后面检查这些函数是否正确,能够看到第二个函数不对,由于*r未定义。

以下是完整代码:

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> #ifdef MAC
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif namespace program_build
{ const static int NUM_FILES = 2;
const char PROGRAM_FILE_1[] = "good.cl";
const char *PROGRAM_FILE_2 = "bad.cl"; int run()
{
/*program能够包括多个kernel,一个kernel相当于一个功能函数,由program包括在内存中,然后就能够使用kernel的功能了。
1 须要使用kernel,创建program,把kernel读入内存
2 须要把program和device连接起来
Host/device data structures */
cl_platform_id platform;
cl_device_id device;
cl_context context;
cl_int i, err; /* Program data structures */
cl_program program;
FILE *program_handle;
char *program_buffer[NUM_FILES];
char *program_log;
const char *file_name[] = {PROGRAM_FILE_1, PROGRAM_FILE_2};
const char options[] = "-cl-finite-math-only -cl-no-signed-zeros";
size_t program_size[NUM_FILES];
size_t log_size; /* Access the first installed platform */
err = clGetPlatformIDs(1, &platform, NULL);
if(err < 0) {
perror("Couldn't find any platforms");
exit(1);
} /* Access the first GPU/CPU */
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
if(err == CL_DEVICE_NOT_FOUND) {
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &device, NULL);
}
if(err < 0) {
perror("Couldn't find any devices");
exit(1);
} /* Create a context */
context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
if(err < 0) {
perror("Couldn't create a context");
exit(1);
} /* Read each program file and place content into buffer array */
for(i=0; i<NUM_FILES; i++) { program_handle = fopen(file_name[i], "r");
if(program_handle == NULL) {
perror("Couldn't find the program file");
exit(1);
}
fseek(program_handle, 0, SEEK_END);
program_size[i] = ftell(program_handle);
rewind(program_handle);
program_buffer[i] = (char*)malloc(program_size[i]+1);
program_buffer[i][program_size[i]] = '\0';
fread(program_buffer[i], sizeof(char), program_size[i],
program_handle);
fclose(program_handle);
} /* Create a program containing all program content */
program = clCreateProgramWithSource(context, NUM_FILES,
(const char**)program_buffer, program_size, &err);
if(err < 0) {
perror("Couldn't create the program");
exit(1);
} /* Build program
but one provision is crucial: every compiler must be accessible through clBuild-Program. This function compiles and links a cl_program for devices associated with the platform. It doesn’t return a new cl_program, but instead modifies the input data structure.
*/ err = clBuildProgram(program, 1, &device, options, NULL, NULL); int bufSize = program_size[0] + program_size[1] + 1;
char *programBuffer = (char *) malloc(bufSize);
clGetProgramInfo(program, CL_PROGRAM_SOURCE, bufSize, programBuffer, NULL);
printf("Print Program Source:\n");
printf("\n %s \n", programBuffer); printf("Check if it is correct:\n");
for (int i = 0; i < NUM_FILES; i++)
{
printf("\n %s \n", program_buffer[i]);
} if(err < 0)
{
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
0, NULL, &log_size);
program_log = (char*) malloc(log_size+1);
program_log[log_size] = '\0';
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
log_size+1, program_log, NULL);
printf("%s\n", program_log);
free(program_log);
system("pause");
exit(1);
} /* Deallocate resources */
for(i=0; i<NUM_FILES; i++) {
free(program_buffer[i]);
}
clReleaseProgram(program);
clReleaseContext(context);
system("pause"); return 0;
} }

OpenCL 获取Program信息的更多相关文章

  1. GPGPU OpenCL 获取设备信息

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

  2. opencl(2)平台、设备、上下文的获取与信息获取

    1:平台 1)获取平台id cl_int clGetPlatformIDs( cl_uint num_entries,      //想要获取的平台数 cl_platform_id * flatfor ...

  3. (部署新java程序,程序报错,需copy的一个包)——java使用siger 获取服务器硬件信息

    mcat-siger.sh  查看是否安装siger rsync -aPuv /usr/lib64/libsigar-amd64-linux.so $i:/usr/lib64/ java使用siger ...

  4. C#通过WinAPI获取内存信息,32位64位可用

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runti ...

  5. 通过GeoIP2分析访问者IP获取地理位置信息

    原文链接:http://blog.csdn.net/johnnycode/article/details/42028841 MaxMind GeoIP2 服务能识别互联网用户的地点位置与其他特征,应用 ...

  6. 【转】windows c++获取文件信息——_stat函数的使用

    _stat函数的功能 _stat函数用来获取指定路径的文件或者文件夹的信息. 函数声明 int _stat( const char *path, struct _stat *buffer ); 参数: ...

  7. PowerShell_零基础自学课程_6_PS中获取帮助信息详解、管道、格式化输

    前些文章陆续的说了一些关于这些主题,但是讨论的都不够深入,今天我们深入的了解一下获取帮助信息.管道以及格式化输出的内容. 一.获取帮助信息 在PS中获取帮助信息,最常用的有: -? .get-comm ...

  8. LINUX获取文件信息

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  9. C# -- 使用 DriveInfo 获取磁盘驱动器信息

    C# -- 使用 DriveInfo 获取磁盘驱动器信息 1. 代码实现 class Program { static void Main(string[] args) { GetComputerDi ...

随机推荐

  1. html-注册邮箱

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Android Studio 升级为3.1 踩到的坑

    原文:https://blog.csdn.net/xiariluoxue/article/details/80050700 AndroidStudio.gradle.buildToolsVersion ...

  3. USACO 4.3 Letter Game (字典树)

    Letter GameIOI 1995 Figure 1: Each of the 26 lowercase letters and its value Letter games are popula ...

  4. mysql 存储过程详解

    MySQL 存储过程是从 MySQL 5.0 开始增加的新功能.存储过程的优点有一箩筐.不过最主要的还是执行效率和SQL 代码封装.特别是 SQL 代码封装功能,如果没有存储过程,在外部程序访问数据库 ...

  5. thinkphp每次跳转时都会显示笑脸的修改

    Success和error方法都有对应的模板,并且是可以设置的,默认的设置是两个方法对应的模板都是://默认错误跳转对应的模板文件'TMPL_ACTION_ERROR' => THINK_PAT ...

  6. JAVAEE——BOS物流项目02:学习计划、动态添加选项卡、ztree、项目底层代码构建

    1 学习计划 1.jQuery easyUI中动态添加选项卡 2.jquery ztree插件使用 n 下载ztree n 基于标准json数据构造ztree n 基于简单json数据构造ztree( ...

  7. c#程序员机试题

    一.题目: 有一数组: int[] arr = new int[] { 48,1,3,55,15,29,12,33,26,41,56,32}; 1.求出最大值 2.按每个数字的10位数分组(说明:0~ ...

  8. android studio 汉化包 美化包

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...

  9. 谁是最快的Go Web框架

    根据Julien Schmidt测试框架中测试到的go web框架,在加上lion,fasthttp,一共测试了下面的web框架. default http macaron go-json-rest ...

  10. [CC-SUBWAY]Subway Ride

    [CC-SUBWAY]Subway Ride 题目大意: 一棵\(n(n\le5\times10^5)\)个点的含重边的树,总边数为\(m(m\le10^6)\),每条边有一个颜色.\(q(q\le5 ...