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