diff options
author | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2013-09-17 04:24:36 +0200 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2013-09-19 17:03:22 +0200 |
commit | b8a0f786caf49cfd5937e4cd5df5f50f825b2a96 (patch) | |
tree | 7a5f32459caa7591549653fa62ca15ca56d86b3a /sc | |
parent | 1500aae993c0a8b4fa9a5bcec1bc6203f3bcff66 (diff) |
introduce strong versioning of the kernel sources
Change-Id: If42711467b1c8cae4b1044464c7254792ddcd6ad
Diffstat (limited to 'sc')
-rw-r--r-- | sc/source/core/opencl/openclwrapper.cxx | 71 | ||||
-rw-r--r-- | sc/source/core/opencl/openclwrapper.hxx | 5 |
2 files changed, 54 insertions, 22 deletions
diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx index a04447aa7a38..327ba7035064 100644 --- a/sc/source/core/opencl/openclwrapper.cxx +++ b/sc/source/core/opencl/openclwrapper.cxx @@ -10,6 +10,8 @@ #include "openclwrapper.hxx" #include <rtl/ustring.hxx> +#include <rtl/strbuf.hxx> +#include <rtl/digest.h> #include <boost/scoped_array.hpp> #include "sal/config.h" @@ -50,6 +52,31 @@ Kernel::Kernel( const char* pName ) : mpName(pName), mpKernel(NULL) {} GPUEnv OpenclDevice::gpuEnv; int OpenclDevice::isInited =0; +namespace { + +OString generateHashForSource() +{ + size_t nLength = strlen(kernel_src); + sal_uInt8 pBuffer[RTL_DIGEST_LENGTH_MD5]; + rtlDigestError aError = rtl_digest_MD5(kernel_src, nLength, + pBuffer, RTL_DIGEST_LENGTH_MD5); + assert(aError == rtl_Digest_E_None); + + OStringBuffer aBuffer; + const char* pString = "0123456789ABCDEF"; + for(size_t i = 0; i < RTL_DIGEST_LENGTH_MD5; ++i) + { + sal_uInt8 val = pBuffer[i]; + aBuffer.append(pString[val/16]); + aBuffer.append(pString[val%16]); + } + return aBuffer.makeStringAndClear(); +} + +} + +OString OpenclDevice::maSourceHash = generateHashForSource(); + int OpenclDevice::initEnv() { // TODO: This part needs more platform specific handling. On Windows, @@ -190,12 +217,28 @@ int OpenclDevice::convertToString( const char *filename, char **source ) return 0; } +namespace { + +OString createFileName(cl_device_id deviceId, const char* clFileName) +{ + OString fileName(clFileName); + sal_Int32 nIndex = fileName.lastIndexOf(".cl"); + if(nIndex > 0) + fileName = fileName.copy(0, nIndex); + + char deviceName[DEVICE_NAME_LENGTH] = {0}; + clGetDeviceInfo(deviceId, CL_DEVICE_NAME, + sizeof(deviceName), deviceName, NULL); + return fileName + "-" + deviceName + "-" + OpenclDevice::maSourceHash + ".bin"; +} + +} + int OpenclDevice::binaryGenerated( const char * clFileName, FILE ** fhandle ) { unsigned int i = 0; cl_int clStatus; int status = 0; - char *str = NULL; FILE *fd = NULL; cl_uint numDevices=0; if ( getenv("SC_OPENCLCPU") ) @@ -215,19 +258,13 @@ int OpenclDevice::binaryGenerated( const char * clFileName, FILE ** fhandle ) &numDevices); } CHECK_OPENCL( clStatus, "clGetDeviceIDs" ); + for ( i = 0; i < numDevices; i++ ) { - char fileName[256] = { 0 }, cl_name[128] = { 0 }; if ( gpuEnv.mpArryDevsID[i] != 0 ) { - char deviceName[DEVICE_NAME_LENGTH]; - clStatus = clGetDeviceInfo( gpuEnv.mpArryDevsID[i], CL_DEVICE_NAME, sizeof(deviceName), deviceName, NULL ); - CHECK_OPENCL( clStatus, "clGetDeviceInfo" ); - str = (char*) strstr( clFileName, (char*) ".cl" ); - memcpy( cl_name, clFileName, str - clFileName ); - cl_name[str - clFileName] = '\0'; - sprintf( fileName, "./%s-%s.bin", cl_name, deviceName ); - fd = fopen( fileName, "rb" ); + OString fileName = createFileName(gpuEnv.mpArryDevsID[i], clFileName); + fd = fopen( fileName.getStr(), "rb" ); status = ( fd != NULL ) ? 1 : 0; } } @@ -301,17 +338,9 @@ int OpenclDevice::generatBinFromKernelSource( cl_program program, const char * c if ( binarySizes[i] != 0 ) { - OString fileName(clFileName); - sal_Int32 nIndex = fileName.lastIndexOf(".cl"); - if(nIndex > 0) - fileName = fileName.copy(0, nIndex - 1); - - char deviceName[DEVICE_NAME_LENGTH]; - clStatus = clGetDeviceInfo(mpArryDevsID[i], CL_DEVICE_NAME, - sizeof(deviceName), deviceName, NULL); - CHECK_OPENCL( clStatus, "clGetDeviceInfo" ); - - if ( !writeBinaryToFile( fileName + "-" + deviceName, binaries[i], binarySizes[i] ) ) + OString fileName = createFileName(mpArryDevsID[i], clFileName); + if ( !writeBinaryToFile( fileName, + binaries[i], binarySizes[i] ) ) { printf("opencl-wrapper: write binary[%s] failds\n", fileName.getStr()); } diff --git a/sc/source/core/opencl/openclwrapper.hxx b/sc/source/core/opencl/openclwrapper.hxx index d14d24b1bc2f..001dc0ebc45c 100644 --- a/sc/source/core/opencl/openclwrapper.hxx +++ b/sc/source/core/opencl/openclwrapper.hxx @@ -16,6 +16,8 @@ #include <cassert> #include "platforminfo.hxx" +#include <rtl/string.hxx> + #include "clcc/clew.h" // CL_MAP_WRITE_INVALIDATE_REGION is new in OpenCL 1.2. @@ -161,6 +163,7 @@ public: static GPUEnv gpuEnv; static int isInited; static int initEnv(); + static OString maSourceHash; static int registOpenclKernel(); static int releaseOpenclRunEnv(); static int initOpenclRunEnv( GPUEnv *gpu ); @@ -169,7 +172,7 @@ public: static int initOpenclRunEnv( int argc ); 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 writeBinaryToFile( const OString& rName, 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 ); |