1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef TESSERACT_OPENCL_OPENCLWRAPPER_H_
#define TESSERACT_OPENCL_OPENCLWRAPPER_H_
#include <allheaders.h>
#include <cstdio>
#include "tprintf.h"
// including CL/cl.h doesn't occur until USE_OPENCL defined below
/**************************************************************************
* enable/disable use of OpenCL
**************************************************************************/
#ifdef USE_OPENCL
/* willus mod -- move pix.h inside this section */
#include "pix.h"
# ifdef __APPLE__
# include <OpenCL/cl.h>
# else
# include <CL/cl.h>
# endif
namespace tesseract {
class Image;
struct TessDeviceScore;
// device type
enum ds_device_type { DS_DEVICE_NATIVE_CPU = 0, DS_DEVICE_OPENCL_DEVICE };
struct ds_device {
ds_device_type type;
cl_device_id oclDeviceID;
char *oclDeviceName;
char *oclDriverVersion;
// a pointer to the score data, the content/format is application defined.
TessDeviceScore *score;
};
# ifndef strcasecmp
# define strcasecmp strcmp
# endif
# define MAX_KERNEL_STRING_LEN 64
# define MAX_CLFILE_NUM 50
# define MAX_CLKERNEL_NUM 200
# define MAX_KERNEL_NAME_LEN 64
# define CL_QUEUE_THREAD_HANDLE_AMD 0x403E
# define GROUPSIZE_X 16
# define GROUPSIZE_Y 16
# define GROUPSIZE_HMORX 256
# define GROUPSIZE_HMORY 1
struct KernelEnv {
cl_context mpkContext;
cl_command_queue mpkCmdQueue;
cl_program mpkProgram;
cl_kernel mpkKernel;
char mckKernelName[150];
};
struct OpenCLEnv {
cl_platform_id mpOclPlatformID;
cl_context mpOclContext;
cl_device_id mpOclDevsID;
cl_command_queue mpOclCmdQueue;
};
typedef int (*cl_kernel_function)(void **userdata, KernelEnv *kenv);
# define CHECK_OPENCL(status, name) \
if (status != CL_SUCCESS) { \
tprintf("OpenCL error code is %d at when %s .\n", status, name); \
}
struct GPUEnv {
// share vb in all modules in hb library
cl_platform_id mpPlatformID;
cl_device_type mDevType;
cl_context mpContext;
cl_device_id *mpArryDevsID;
cl_device_id mpDevID;
cl_command_queue mpCmdQueue;
cl_kernel mpArryKernels[MAX_CLFILE_NUM];
cl_program mpArryPrograms[MAX_CLFILE_NUM]; // one program object maps one
// kernel source file
char mArryKnelSrcFile[MAX_CLFILE_NUM][256], // the max len of kernel file name is 256
mArrykernelNames[MAX_CLKERNEL_NUM][MAX_KERNEL_STRING_LEN + 1];
cl_kernel_function mpArryKnelFuncs[MAX_CLKERNEL_NUM];
int mnKernelCount, mnFileCount, // only one kernel file
mnIsUserCreated; // 1: created , 0:no create and needed to create by
// opencl wrapper
int mnKhrFp64Flag;
int mnAmdFp64Flag;
};
class OpenclDevice {
public:
static GPUEnv gpuEnv;
static int isInited;
OpenclDevice();
~OpenclDevice();
static int InitEnv(); // load dll, call InitOpenclRunEnv(0)
static int InitOpenclRunEnv(int argc); // RegistOpenclKernel, double flags, compile kernels
static int InitOpenclRunEnv_DeviceSelection(
int argc); // RegistOpenclKernel, double flags, compile kernels
static int RegistOpenclKernel();
static int ReleaseOpenclRunEnv();
static int ReleaseOpenclEnv(GPUEnv *gpuInfo);
static int CompileKernelFile(GPUEnv *gpuInfo, const char *buildOption);
static int CachedOfKernerPrg(const GPUEnv *gpuEnvCached, const char *clFileName);
static int GeneratBinFromKernelSource(cl_program program, const char *clFileName);
static int WriteBinaryToFile(const char *fileName, const char *birary, size_t numBytes);
static int BinaryGenerated(const char *clFileName, FILE **fhandle);
// static int CompileKernelFile( const char *filename, GPUEnv *gpuInfo, const
// char *buildOption );
static l_uint32 *pixReadFromTiffKernel(l_uint32 *tiffdata, l_int32 w, l_int32 h, l_int32 wpl,
l_uint32 *line);
static int composeRGBPixelCl(int *tiffdata, int *line, int h, int w);
/* OpenCL implementations of Morphological operations*/
// Initialization of OCL buffers used in Morph operations
static int initMorphCLAllocations(l_int32 wpl, l_int32 h, Image pixs);
static void releaseMorphCLBuffers();
static void pixGetLinesCL(Image pixd, Image pixs, Image *pix_vline, Image *pix_hline, Image *pixClosed,
bool getpixClosed, l_int32 close_hsize, l_int32 close_vsize,
l_int32 open_hsize, l_int32 open_vsize, l_int32 line_hsize,
l_int32 line_vsize);
// int InitOpenclAttr( OpenCLEnv * env );
// int ReleaseKernel( KernelEnv * env );
static int SetKernelEnv(KernelEnv *envInfo);
// int CreateKernel( char * kernelname, KernelEnv * env );
// int RunKernel( const char *kernelName, void **userdata );
// int ConvertToString( const char *filename, char **source );
// int CheckKernelName( KernelEnv *envInfo, const char *kernelName );
// int RegisterKernelWrapper( const char *kernelName, cl_kernel_function
// function ); int RunKernelWrapper( cl_kernel_function function, const char *
// kernelName, void **usrdata ); int GetKernelEnvAndFunc( const char
// *kernelName, KernelEnv *env, cl_kernel_function *function );
static int LoadOpencl();
# ifdef WIN32
// static int OpenclInite();
static void FreeOpenclDll();
# endif
inline static int AddKernelConfig(int kCount, const char *kName);
/* for binarization */
static int HistogramRectOCL(void *imagedata, int bytes_per_pixel, int bytes_per_line, int left,
int top, int width, int height, int kHistogramSize,
int *histogramAllChannels);
static int ThresholdRectToPixOCL(unsigned char *imagedata, int bytes_per_pixel,
int bytes_per_line, int *thresholds, int *hi_values, Image *pix,
int rect_height, int rect_width, int rect_top, int rect_left);
static ds_device getDeviceSelection();
static ds_device selectedDevice;
static bool deviceIsSelected;
static bool selectedDeviceIsOpenCL();
};
}
#endif // USE_OPENCL
#endif // TESSERACT_OPENCL_OPENCLWRAPPER_H_
|