码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCL 获取Program信息

时间:2014-06-03 01:50:22      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   a   http   

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

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

还可以测试是否编译正确,如果不正确会有输出提示的。


下面程序运行如下:

bubuko.com,布布扣


前面都是读入的函数代码。然后后面检查这些函数是否正确,可以看到第二个函数不正确,因为*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信息,布布扣,bubuko.com

OpenCL 获取Program信息

标签:c   class   blog   code   a   http   

原文地址:http://blog.csdn.net/kenden23/article/details/27533189

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!