1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #ifndef SC_OPENCLWRAPPER_HXX
11 #define SC_OPENCLWRAPPER_HXX
13 #include <config_features.h>
14 #include <formula/opcode.hxx>
15 #include <sal/detail/log.h>
16 #include <osl/file.hxx>
18 #include <boost/shared_ptr.hpp>
20 #include "platforminfo.hxx"
22 #include <rtl/string.hxx>
24 #include "clcc/clew.h"
26 // CL_MAP_WRITE_INVALIDATE_REGION is new in OpenCL 1.2.
28 // When compiling against an older OpenCL, use CL_MAP_WRITE.
30 // FIXME: But what if this code has been compiled against OpenCL 1.2
31 // headers but then runs against an OpenCL 1.1 implementation?
32 // Probably the code should check at run-time the version of the
33 // OpenCL implementation and choose which flag to use based on that.
34 #ifdef CL_MAP_WRITE_INVALIDATE_REGION
35 #define OPENCLWRAPPER_CL_MAP_WRITE_FLAG CL_MAP_WRITE_INVALIDATE_REGION
37 #define OPENCLWRAPPER_CL_MAP_WRITE_FLAG CL_MAP_WRITE
40 #define MaxTextExtent 4096
42 #define CL_QUEUE_THREAD_HANDLE_AMD 0x403E
43 #define CL_MAP_WRITE_INVALIDATE_REGION (1 << 2)
45 #define CHECK_OPENCL(status,name) \
46 if( status != CL_SUCCESS ) \
48 printf ("OpenCL error code is %d at " SAL_DETAIL_WHERE " when %s .\n", status, name); \
52 #define CHECK_OPENCL_PTR(status,name) \
53 if( status != CL_SUCCESS ) \
55 printf ("OpenCL error code is %d at " SAL_DETAIL_WHERE " when %s .\n", status, name); \
59 #define CHECK_OPENCL_VOID(status,name) \
60 if( status != CL_SUCCESS ) \
62 printf ("OpenCL error code is %d at " SAL_DETAIL_WHERE " when %s .\n", status, name); \
65 #define CHECK_OPENCL_RELEASE(status,name) \
67 clReleaseMemObject( name ); \
68 if( status != CL_SUCCESS ) \
70 printf ("OpenCL error code is %d at " SAL_DETAIL_WHERE " when clReleaseMemObject( %s ).\n", status, #name); \
73 #define MAX_KERNEL_STRING_LEN 64
74 #define MAX_CLFILE_NUM 50
75 #define MAX_CLKERNEL_NUM 200
76 #define MAX_KERNEL_NAME_LEN 64
80 #define strcasecmp strcmp
87 typedef struct _KernelEnv
89 cl_context mpkContext
;
90 cl_command_queue mpkCmdQueue
;
91 cl_program mpkProgram
;
96 // user defined, this is function wrapper which is used to set the input
97 // parameters, launch kernel and copy data from GPU to CPU or CPU to GPU.
98 typedef int ( *cl_kernel_function
)( void **userdata
, KernelEnv
*kenv
);
102 namespace sc
{ namespace opencl
{
104 typedef unsigned int uint
;
108 cl_platform_id mpOclPlatformID
;
109 cl_context mpOclContext
;
110 cl_device_id mpOclDevsID
;
111 cl_command_queue mpOclCmdQueue
;
116 //share vb in all modules in hb library
117 cl_platform_id mpPlatformID
;
118 cl_device_type mDevType
;
119 cl_context mpContext
;
120 cl_device_id
*mpArryDevsID
;
121 cl_device_id mpDevID
;
122 cl_command_queue mpCmdQueue
;
123 cl_program mpArryPrograms
[MAX_CLFILE_NUM
]; //one program object maps one kernel source file
124 int mnIsUserCreated
; // 1: created , 0:no create and needed to create by opencl wrapper
129 struct SingleVectorFormula
131 const double *mdpInputLeftData
;
132 const double *mdpInputRightData
;
133 size_t mnInputLeftDataSize
;
134 size_t mnInputRightDataSize
;
135 uint mnInputLeftStartPosition
;
136 uint mnInputRightStartPosition
;
137 int mnInputLeftOffset
;
138 int mnInputRightOffset
;
141 struct DoubleVectorFormula
143 const double *mdpInputData
;
144 size_t mnInputDataSize
;
145 uint mnInputStartPosition
;
146 uint mnInputEndPosition
;
147 int mnInputStartOffset
;
148 int mnInputEndOffset
;
154 static GPUEnv gpuEnv
;
156 static OString maCacheFolder
;
158 static int registOpenclKernel();
159 static int releaseOpenclRunEnv();
160 static int initOpenclRunEnv( GPUEnv
*gpu
);
161 static int releaseOpenclEnv( GPUEnv
*gpuInfo
);
162 static int initOpenclRunEnv( int argc
);
163 static int generatBinFromKernelSource( cl_program program
, const char * clFileName
);
164 static int writeBinaryToFile( const OString
& rName
, const char* birary
, size_t numBytes
);
165 static std::vector
<boost::shared_ptr
<osl::File
> > binaryGenerated( const char * clFileName
, cl_context context
);
166 static bool buildProgramFromBinary(const char* buildOption
, GPUEnv
* gpuEnv
, const char* filename
, int idx
);
168 static int initOpenclAttr( OpenCLEnv
* env
);
169 static int setKernelEnv( KernelEnv
*envInfo
);
171 static int getOpenclState();
172 static void setOpenclState( int state
);
175 size_t getOpenCLPlatformCount();
176 const std::vector
<OpenclPlatformInfo
>& fillOpenCLInfo();
179 * Used to set or switch between OpenCL devices.
181 * @param pDeviceId the id of the opencl device of type cl_device_id, NULL means use software calculation
182 * @param bAutoSelect use the algorithm to select the best OpenCL device
184 * @return returns true if there is a valid opencl device that has been set up
186 bool switchOpenclDevice(const OUString
* pDeviceId
, bool bAutoSelect
,
187 bool bForceEvaluation
);
189 void getOpenCLDeviceInfo(size_t& rDeviceId
, size_t& rPlatformId
);
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */