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

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

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

以下程序执行例如以下:

OpenCL 获取Program信息-LMLPHP

前面都是读入的函数代码。然后后面检查这些函数是否正确,能够看到第二个函数不对,由于*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;
} }
05-22 08:47