From 8b06b1cbf112d55307ea8a6efe711db4e7506d89 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期二, 07 三月 2023 10:13:47 +0800
Subject: [PATCH] 'GPU内核方法优化'

---
 ConsoleApplication/OpenCLExcuter.cpp |   82 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/ConsoleApplication/OpenCLExcuter.cpp b/ConsoleApplication/OpenCLExcuter.cpp
index b16ac4c..be2924b 100644
--- a/ConsoleApplication/OpenCLExcuter.cpp
+++ b/ConsoleApplication/OpenCLExcuter.cpp
@@ -1098,6 +1098,88 @@
 
 }
 
+void OpenCLExcuter::splitRowData(cv::Mat img, int* rowIndexs, int lines, int ele_count_per_line, int* result)
+{
+
+
+	kernel = clCreateKernel(program, "splitRowData", &error);
+	if (kernel == NULL) {
+		throw string("Couldn't create kernel!\n");
+	}
+
+
+	int inputSize = img.cols * img.rows;
+	int resultSize = lines * ele_count_per_line * 4;
+
+
+	//创建缓存对象
+
+	cl_mem memObject1 = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(uchar) * inputSize, img.data, &error);
+	if (error < 0) {
+		throw string("Creat memObject1 failed!\n");
+	}
+
+	cl_mem memObject2 = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int) * lines * 4, rowIndexs, &error);
+	if (error < 0) {
+		throw string("Creat memObject2 failed!\n");
+	}
+
+
+	cl_mem memObject3 = clCreateBuffer(context, CL_MEM_WRITE_ONLY,
+		sizeof(int) * resultSize, NULL, &error);
+	if (error < 0) {
+		throw string("Creat memObject3 failed!\n");
+	}
+
+	try {
+		//设置内核参数
+		int min_empty_width = 5;
+		int threshhold_color = _IMG_BINARY_THRESHOLD;
+		error = clSetKernelArg(kernel, 0, sizeof(cl_mem), &memObject1);
+		error = clSetKernelArg(kernel, 1, sizeof(cl_mem), &memObject2);
+		error |= clSetKernelArg(kernel, 2, sizeof(int), &img.cols);
+		error |= clSetKernelArg(kernel, 3, sizeof(int), &ele_count_per_line);
+		error |= clSetKernelArg(kernel, 4, sizeof(int), &min_empty_width);
+		error |= clSetKernelArg(kernel, 5, sizeof(int), &threshhold_color);
+		error |= clSetKernelArg(kernel, 6, sizeof(cl_mem), &memObject3);
+
+		if (error != CL_SUCCESS) {
+			throw string("Error setting kernel arguments!\n");
+		}
+		//执行内核
+		size_t globalWorkSize[1] = { lines };
+		size_t localWorkSize[1] = { 1 };
+
+		const int work_dim = 1;
+
+		error = clEnqueueNDRangeKernel(queue, kernel, work_dim, NULL, globalWorkSize,
+			localWorkSize, 0, NULL, NULL);
+		if (error != CL_SUCCESS) {
+			throw string("Error queuing kernel for execution!\n");
+		}
+
+		//读取执行结果
+		error = clEnqueueReadBuffer(queue, memObject3, CL_TRUE, 0, resultSize * sizeof(int),
+			result, 0, NULL, NULL);
+		if (error != CL_SUCCESS) {
+			throw string("Error reading result buffer!\n");
+		}
+		clReleaseKernel(kernel);
+		clReleaseMemObject(memObject1);
+		clReleaseMemObject(memObject2);
+		clReleaseMemObject(memObject3);
+	}
+	catch (string st) {
+		clReleaseKernel(kernel);
+		clReleaseMemObject(memObject1);
+		clReleaseMemObject(memObject2);
+		clReleaseMemObject(memObject3);
+		excuteError();
+		throw st;
+	}
+
+}
+
 
 
 void OpenCLExcuter::splitPlateContentRowData(unsigned char* imgs, int imgWidth, int imgHeight, int* rowIndexs, int lines, int lineEleCount, int fromIndex, int toIndex,

--
Gitblit v1.8.0