From 67a5e3d825fde17b7b00906ce42b0bd8cafebc4e Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期四, 18 八月 2022 18:22:14 +0800
Subject: [PATCH] '完善'

---
 ConsoleApplication/OpenCLExcuter.cpp |   97 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 89 insertions(+), 8 deletions(-)

diff --git a/ConsoleApplication/OpenCLExcuter.cpp b/ConsoleApplication/OpenCLExcuter.cpp
index 7df4069..7d556ee 100644
--- a/ConsoleApplication/OpenCLExcuter.cpp
+++ b/ConsoleApplication/OpenCLExcuter.cpp
@@ -1,6 +1,59 @@
 #include "OpenCLExcuter.h"
 #include <chrono>
+
+
+
+#define CHECK_ERRORS(ERR) \
+	if(ERR != CL_SUCCESS){ \
+		cerr << "OpenCL error code" << ERR << "file: " << __FILE__  << "line: " << __LINE__ << ".\nExiting..." << endl; \
+		exit(1); \
+	}
+
+int showDeviceInfo() {
+	cl_platform_id* platform;
+	cl_uint num_platform;
+	cl_int err;
+	err = clGetPlatformIDs(0, NULL, &num_platform);
+	platform = (cl_platform_id*)malloc(sizeof(cl_platform_id) * num_platform);
+	err = clGetPlatformIDs(num_platform, platform, NULL);
+	for (int i = 0; i < num_platform; i++) {
+		printf("\nPlatform %d information\n", i);
+		size_t size;
+		err = clGetPlatformInfo(platform[i], CL_PLATFORM_NAME, 0, NULL, &size);
+		char* PName = (char*)malloc(size);
+		err = clGetPlatformInfo(platform[i], CL_PLATFORM_NAME, size, PName, NULL);
+		printf("CL_PLATFORM_NAME: %s\n", PName);
+		err = clGetPlatformInfo(platform[i], CL_PLATFORM_VENDOR, 0, NULL, &size);
+		char* PVendor = (char*)malloc(size);
+		err = clGetPlatformInfo(platform[i], CL_PLATFORM_VENDOR, size, PVendor, NULL);
+		printf("CL_PLATFORM_VENDOR: %s\n", PVendor);
+		err = clGetPlatformInfo(platform[i], CL_PLATFORM_VERSION, 0, NULL, &size);
+		char* PVersion = (char*)malloc(size);
+		err = clGetPlatformInfo(platform[i], CL_PLATFORM_VERSION, size, PVersion, NULL);
+		printf("CL_PLATFORM_VERSION: %s\n", PVersion);
+		err = clGetPlatformInfo(platform[i], CL_PLATFORM_PROFILE, 0, NULL, &size);
+		char* PProfile = (char*)malloc(size);
+		err = clGetPlatformInfo(platform[i], CL_PLATFORM_PROFILE, size, PProfile, NULL);
+		printf("CL_PLATFORM_PROFILE: %s\n", PProfile);
+		err = clGetPlatformInfo(platform[i], CL_PLATFORM_EXTENSIONS, 0, NULL, &size);
+		char* PExten = (char*)malloc(size);
+		err = clGetPlatformInfo(platform[i], CL_PLATFORM_EXTENSIONS, size, PExten, NULL);
+		printf("CL_PLATFORM_EXTENSIONS: %s\n", PExten);
+		free(PName);
+		free(PVendor);
+		free(PVersion);
+		free(PProfile);
+		free(PExten);
+	}
+
+	return 0;
+
+}
+
+string OpenCLExcuter::rootPath;
+
 void OpenCLExcuter::init() {
+	showDeviceInfo();
 	//获取平台
 	error = clGetPlatformIDs(1, &platforms, &num_of_platforms);
 	if (error != 0) {
@@ -16,9 +69,9 @@
 	//}
 
 
-	error = clGetDeviceIDs(platforms, CL_DEVICE_TYPE_CPU, 1, &devices, NULL);
+	error = clGetDeviceIDs(platforms, CL_DEVICE_TYPE_GPU , 1, &devices, NULL);
 	if (error != 0) {
-		error = clGetDeviceIDs(platforms, CL_DEVICE_TYPE_GPU, 1, &devices, NULL);
+		error = clGetDeviceIDs(platforms, CL_DEVICE_TYPE_CPU, 1, &devices, NULL);
 		if (error != 0) {
 			throw string("Get device failed!");
 		}
@@ -30,7 +83,15 @@
 		throw string("Creat context failed!");
 	}
 	//创建程序;注意要用"rb"
-	fopen_s(&program_handle, "D:\\workspace\\CPlusTest\\ConsoleApplication\\ConsoleApplication\\kernel.cl", "rb");
+	string kernel_path = "";
+	if (rootPath.length() >0) {
+		kernel_path = kernel_path.append(rootPath);
+	}
+	
+	kernel_path.append("kernel.cl");
+	cout <<"kernel_path:"<< kernel_path << endl;
+	fopen_s(&program_handle, kernel_path.c_str(), "rb");
+
 	if (program_handle == NULL) {
 		throw string("The kernle can not be opened!");
 	}
@@ -48,7 +109,13 @@
 	program = clCreateProgramWithSource(context, 1, (const char**)&program_buffer,
 		&program_size, &error);
 	if (error < 0) {
-		throw string("Couldn't create the program!");
+		program = clCreateProgramWithSource(context, 0, (const char**)&program_buffer,
+			&program_size, &error);
+		if (error < 0) {
+			string st = "Couldn't create the program!";
+			st.append("  error:").append(to_string(error));
+			throw st;
+		}
 	}
 	//编译程序
 	error = clBuildProgram(program, 1, &devices, NULL, NULL, NULL);
@@ -67,9 +134,14 @@
 	free(program_buffer);
 
 	//创建命令队列
-	queue = clCreateCommandQueue(context, devices, CL_QUEUE_PROFILING_ENABLE, &error);
+	//queue = clCreateCommandQueue(context, devices, CL_QUEUE_PROFILING_ENABLE, &error);
+	queue = clCreateCommandQueueWithProperties(context, devices, 0, &error);
+	
 	if (error < 0) {
-		throw string("Coudn't create the command queue");
+		queue = clCreateCommandQueue(context, devices, CL_QUEUE_PROFILING_ENABLE, &error);
+		if (error < 0) {
+			throw string("Coudn't create the command queue");
+		}
 	}
 }
 
@@ -85,7 +157,7 @@
 		throw string("Couldn't create kernel!\n");
 	}
 	//初始化参数
-	unsigned char* result = (unsigned char*)malloc(sizeof(unsigned char) * resultSize);
+
 
 	//创建缓存对象
 	cl_mem memObject1 = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(unsigned char) * inputSize, data, &error);
@@ -128,10 +200,12 @@
 		throw string("Error queuing kernel for execution!\n");
 	}
 
+	unsigned char* result = (unsigned char*)malloc(sizeof(unsigned char) * resultSize);
 	//读取执行结果
 	error = clEnqueueReadBuffer(queue, memObject4, CL_TRUE, 0, resultSize * sizeof(unsigned char),
 		result, 0, NULL, NULL);
 	if (error != CL_SUCCESS) {
+		free(result);
 		throw string("Error reading result buffer!\n");
 	}
 	/*
@@ -147,15 +221,17 @@
 
 	//开始第二阶段的计算
 	int resultSize2 = resultSize / 10;
-	unsigned char* result2 = (unsigned char*)malloc(sizeof(unsigned char) * resultSize2);
+
 
 	kernel = clCreateKernel(program, "recognition_numbers_2", &error);
 	if (kernel == NULL) {
+		free(result);
 		throw string("Couldn't create kernel!\n");
 	}
 
 	cl_mem memObject21 = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(unsigned char) * resultSize, result, &error);
 	if (error < 0) {
+		free(result);
 		throw string("Creat memObject1 failed!\n");
 	}
 
@@ -166,19 +242,24 @@
 	error |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &memObject22);
 
 	if (error != CL_SUCCESS) {
+		free(result);
 		throw string("Error setting kernel arguments!\n");
 	}
 	size_t	globalWorkSize2[1] = { resultSize2 };
 	error = clEnqueueNDRangeKernel(queue, kernel, work_dim, NULL, globalWorkSize2,
 		localWorkSize, 0, NULL, NULL);
 	if (error != CL_SUCCESS) {
+		free(result);
 		throw string("Error queuing kernel for execution!\n");
 	}
 
+	unsigned char* result2 = (unsigned char*)malloc(sizeof(unsigned char) * resultSize2);
 	//读取执行结果
 	error = clEnqueueReadBuffer(queue, memObject22, CL_TRUE, 0, resultSize2 * sizeof(unsigned char),
 		result2, 0, NULL, NULL);
 	if (error != CL_SUCCESS) {
+		free(result);
+		free(result2);
 		throw string("Error reading result buffer!\n");
 	}
 

--
Gitblit v1.8.0