__kernel void hello( __global char* string ){

	string[0] = 'H';
	string[1] = 'e';
	string[2] = 'l';
	string[3] = 'l';
	string[4] = 'o';
	string[5] = ',';
	string[6] = 'W';
	string[7] = 'o';
	string[8] = 'r';
	string[9] = 'l';
	string[10] = 'd';
	string[11] = '!';
	string[12] = '\0';

}

#include <stdio .h>

#include <stdlib .h>

#include <string .h>

#include <opencl /opencl.h>

#define MEM_SIZE (128)

#define MAX_SOURCE_SIZE (0x100000)

int main(){

	
	cl_device_id device_id = NULL;
	cl_context context = NULL;
	cl_command_queue command_queue = NULL;
	cl_mem memobj = NULL;
	cl_program program = NULL;
	cl_kernel kernel = NULL;
	cl_platform_id platform_id = NULL;
	cl_uint ret_num_devices;
	cl_uint ret_num_platforms;
	cl_int ret;
	
	char string[MEM_SIZE];
	FILE *fp;
	char *source_str;
	size_t source_size;
	
	fp = fopen("./hello.cl", "r");
	if(!fp){
		fprintf(stderr, "Failed to load kernel.\n");
		exit(1);
	}
	
	source_str = (char*)malloc(MAX_SOURCE_SIZE);
	source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);	
	
	// Platform and Device information
	ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
	ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, &ret_num_devices);
	
	// OpenCL Context
	context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret);
	
	// Command Queue , Memory buffer
	command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
	memobj = clCreateBuffer(context, CL_MEM_READ_WRITE, MEM_SIZE * sizeof(char), NULL, &ret);
	
	// make the kernel program and build.
	program = clCreateProgramWithSource(context, 1, (const char**)&source_str, (const size_t*)&source_size, &ret);
	ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
	
	// make the OpenCl Kernel and setting arguments
	kernel = clCreateKernel(program, "hello", &ret);
	ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&memobj);
	
	// Run Kernel and get answer
	ret = clEnqueueTask(command_queue, kernel, 0, NULL, NULL);
	ret = clEnqueueReadBuffer(command_queue, memobj, CL_TRUE, 0, MEM_SIZE * sizeof(char), string, 0, NULL, NULL);
	// output
	puts(string);
	
	// End
	ret = clFlush(command_queue);
	ret = clFinish(command_queue);
	ret = clReleaseKernel(kernel);
	ret = clReleaseProgram(program);
	ret = clReleaseMemObject(memobj);
	ret = clReleaseCommandQueue(command_queue);
	ret = clReleaseContext(context);
	
	free(source_str);
	return 0;

}


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2012-04-12 (木) 22:52:03 (1988d)