Skip to content

Commit

Permalink
Merge pull request tesseract-ocr#975 from stweil/ocl
Browse files Browse the repository at this point in the history
Clean OpenCL code
  • Loading branch information
zdenop authored Jun 3, 2017
2 parents 34bdf99 + 1bbcbd8 commit 1e5522d
Showing 1 changed file with 9 additions and 68 deletions.
77 changes: 9 additions & 68 deletions opencl/openclwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,6 @@ static const l_uint32 rmask32[] = {
0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff, 0x1fffffff, 0x3fffffff,
0x7fffffff, 0xffffffff};

struct tiff_transform {
int vflip; /* if non-zero, image needs a vertical fip */
int hflip; /* if non-zero, image needs a horizontal flip */
int rotate; /* -1 -> counterclockwise 90-degree rotation,
0 -> no rotation
1 -> clockwise 90-degree rotation */
};

static struct tiff_transform tiff_orientation_transforms[] = {
{0, 0, 0},
{0, 1, 0},
{1, 1, 0},
{1, 0, 0},
{0, 1, -1},
{0, 0, 1},
{0, 1, 1},
{0, 0, -1}
};

static const l_int32 MAX_PAGES_IN_TIFF_FILE = 3000;

static cl_mem pixsCLBuffer, pixdCLBuffer, pixdCLIntermediate; //Morph operations buffers
static cl_mem pixThBuffer; //output from thresholdtopix calculation
static cl_int clStatus;
Expand Down Expand Up @@ -351,7 +330,6 @@ static ds_status readProfileFromFile(ds_profile *profile,
const char *currentPosition;
const char *dataStart;
const char *dataEnd;
size_t versionStringLength;

contentEnd = contentStart + contentSize;
currentPosition = contentStart;
Expand All @@ -370,8 +348,8 @@ static ds_status readProfileFromFile(ds_profile *profile,
goto cleanup;
}

versionStringLength = strlen(profile->version);
if (versionStringLength != (dataEnd - dataStart) ||
size_t versionStringLength = strlen(profile->version);
if (versionStringLength + dataStart != dataEnd ||
strncmp(profile->version, dataStart, versionStringLength) != 0) {
// version mismatch
status = DS_PROFILE_FILE_ERROR;
Expand Down Expand Up @@ -460,8 +438,8 @@ static ds_status readProfileFromFile(ds_profile *profile,

actualDeviceNameLength = strlen(profile->devices[i].oclDeviceName);
driverVersionLength = strlen(profile->devices[i].oclDriverVersion);
if (actualDeviceNameLength == (deviceNameEnd - deviceNameStart) &&
driverVersionLength == (deviceDriverEnd - deviceDriverStart) &&
if (deviceNameStart + actualDeviceNameLength == deviceNameEnd &&
deviceDriverStart + driverVersionLength == deviceDriverEnd &&
strncmp(profile->devices[i].oclDeviceName, deviceNameStart,
actualDeviceNameLength) == 0 &&
strncmp(profile->devices[i].oclDriverVersion, deviceDriverStart,
Expand Down Expand Up @@ -607,7 +585,7 @@ static void legalizeFileName( char *fileName) {
const char *invalidChars =
"/\?:*\"><| "; // space is valid but can cause headaches
// for each invalid char
for (int i = 0; i < strlen(invalidChars); i++) {
for (unsigned i = 0; i < strlen(invalidChars); i++) {
char invalidStr[4];
invalidStr[0] = invalidChars[i];
invalidStr[1] = '\0';
Expand Down Expand Up @@ -727,31 +705,6 @@ Pix *mapOutputCLBuffer(KernelEnv rEnv, cl_mem clbuffer, Pix *pixd, Pix *pixs,
return pixd;
}

static cl_mem allocateIntBuffer(KernelEnv rEnv, const l_uint32 *_pValues,
size_t nElements, cl_int *pStatus,
bool sync = false)
{
cl_mem xValues =
clCreateBuffer(rEnv.mpkContext, (cl_mem_flags)(CL_MEM_READ_WRITE),
nElements * sizeof(l_int32), nullptr, pStatus);

if (_pValues != nullptr) {
l_int32 *pValues = (l_int32 *)clEnqueueMapBuffer(
rEnv.mpkCmdQueue, xValues, CL_TRUE, CL_MAP_WRITE, 0,
nElements * sizeof(l_int32), 0, nullptr, nullptr, nullptr);

memcpy(pValues, _pValues, nElements * sizeof(l_int32));

clEnqueueUnmapMemObject(rEnv.mpkCmdQueue, xValues, pValues, 0, nullptr,
nullptr);

if (sync) clFinish(rEnv.mpkCmdQueue);
}

return xValues;
}


void OpenclDevice::releaseMorphCLBuffers()
{
if (pixdCLIntermediate != nullptr) clReleaseMemObject(pixdCLIntermediate);
Expand Down Expand Up @@ -1851,7 +1804,6 @@ int OpenclDevice::HistogramRectOCL(unsigned char *imageData,
static_cast<size_t>(block_size * kHistogramSize * bytes_per_pixel)};

/* map histogramAllChannels as write only */
int numBins = kHistogramSize * bytes_per_pixel * numWorkGroups;

cl_mem histogramBuffer = clCreateBuffer(
histKern.mpkContext, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
Expand Down Expand Up @@ -2046,7 +1998,6 @@ int OpenclDevice::ThresholdRectToPixOCL(unsigned char *imageData,
/* set kernel arguments */
clStatus = clSetKernelArg(rEnv.mpkKernel, 0, sizeof(cl_mem), &imageBuffer);
CHECK_OPENCL(clStatus, "clSetKernelArg imageBuffer");
cl_uint numPixels = width * height;
clStatus = clSetKernelArg(rEnv.mpkKernel, 1, sizeof(int), &height);
CHECK_OPENCL(clStatus, "clSetKernelArg height");
clStatus = clSetKernelArg(rEnv.mpkKernel, 2, sizeof(int), &width);
Expand Down Expand Up @@ -2243,9 +2194,6 @@ static double composeRGBPixelMicroBench(GPUEnv *env, TessScoreEvaluationInputDat
#endif
Pix *pix = pixCreate(input.width, input.height, 32);
l_uint32 *pixData = pixGetData(pix);
int wpl = pixGetWpl(pix);
//l_uint32* output_gpu=pixReadFromTiffKernel(tiffdata,w,h,wpl,line);
//pixSetData(pix, output_gpu);
int i, j;
int idx = 0;
for (i = 0; i < input.height ; i++) {
Expand Down Expand Up @@ -2291,14 +2239,11 @@ static double histogramRectMicroBench( GPUEnv *env, TessScoreEvaluationInputData
timespec time_funct_start, time_funct_end;
#endif

unsigned char pixelHi = (unsigned char)255;

int left = 0;
int top = 0;
int kHistogramSize = 256;
int bytes_per_line = input.width*input.numChannels;
int *histogramAllChannels = new int[kHistogramSize*input.numChannels];
int retVal = 0;
// function call
if (type == DS_DEVICE_OPENCL_DEVICE) {
#if ON_WINDOWS
Expand All @@ -2310,8 +2255,7 @@ static double histogramRectMicroBench( GPUEnv *env, TessScoreEvaluationInputData
#endif

OpenclDevice::gpuEnv = *env;
int wpl = pixGetWpl(input.pix);
retVal = OpenclDevice::HistogramRectOCL(
int retVal = OpenclDevice::HistogramRectOCL(
input.imageData, input.numChannels, bytes_per_line, top, left,
input.width, input.height, kHistogramSize, histogramAllChannels);

Expand Down Expand Up @@ -2400,7 +2344,6 @@ static void ThresholdRectToPix_Native(const unsigned char* imagedata,

static double thresholdRectToPixMicroBench(GPUEnv *env, TessScoreEvaluationInputData input, ds_device_type type) {
double time;
int retVal = 0;
#if ON_WINDOWS
LARGE_INTEGER freq, time_funct_start, time_funct_end;
QueryPerformanceFrequency(&freq);
Expand Down Expand Up @@ -2440,8 +2383,7 @@ static double thresholdRectToPixMicroBench(GPUEnv *env, TessScoreEvaluationInput
#endif

OpenclDevice::gpuEnv = *env;
int wpl = pixGetWpl(input.pix);
retVal = OpenclDevice::ThresholdRectToPixOCL(
int retVal = OpenclDevice::ThresholdRectToPixOCL(
input.imageData, input.numChannels, bytes_per_line, thresholds,
hi_values, &input.pix, input.height, input.width, top, left);

Expand Down Expand Up @@ -2526,7 +2468,6 @@ static double getLineMasksMorphMicroBench(GPUEnv *env, TessScoreEvaluationInputD
#else
clock_gettime( CLOCK_MONOTONIC, &time_funct_start );
#endif
Pix *src_pix = input.pix;
OpenclDevice::gpuEnv = *env;
OpenclDevice::initMorphCLAllocations(wpl, input.height, input.pix);
Pix *pix_vline = nullptr, *pix_hline = nullptr, *pix_closed = nullptr;
Expand Down Expand Up @@ -2723,12 +2664,12 @@ ds_device OpenclDevice::getDeviceSelection( ) {
// select fastest using custom Tesseract selection algorithm
float bestTime = FLT_MAX; // begin search with worst possible time
int bestDeviceIdx = -1;
for (int d = 0; d < profile->numDevices; d++) {
for (unsigned d = 0; d < profile->numDevices; d++) {
ds_device device = profile->devices[d];
TessDeviceScore score = *(TessDeviceScore *)device.score;

float time = score.time;
printf("[DS] Device[%i] %i:%s score is %f\n", d + 1, device.type,
printf("[DS] Device[%u] %i:%s score is %f\n", d + 1, device.type,
device.oclDeviceName, time);
if (time < bestTime) {
bestTime = time;
Expand Down

0 comments on commit 1e5522d

Please sign in to comment.