sc/source/core/opencl/openclwrapper.cxx | 183 ++++++++++++-------------------- sc/source/core/opencl/openclwrapper.hxx | 6 - 2 files changed, 73 insertions(+), 116 deletions(-)
New commits: commit 4a95f74ceb02cfa30ba2c6b286aa30d7a01110f1 Author: Markus Mohrhard <markus.mohrh...@googlemail.com> Date: Tue Sep 17 04:25:38 2013 +0200 remove unused method Change-Id: Id33348c76297583e843af2a1ea43f7f755b86033 diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx index 327ba70..7c245b9 100644 --- a/sc/source/core/opencl/openclwrapper.cxx +++ b/sc/source/core/opencl/openclwrapper.cxx @@ -180,43 +180,6 @@ Kernel* OpenclDevice::checkKernelName( const char *kernelName ) return NULL; } -int OpenclDevice::convertToString( const char *filename, char **source ) -{ - int file_size; - size_t result; - FILE *file = NULL; - file_size = 0; - result = 0; - file = fopen( filename, "rb+" ); - printf("open kernel file %s.\n",filename); - - if ( file != NULL ) - { - printf("Open ok!\n"); - fseek( file, 0, SEEK_END ); - - file_size = ftell( file ); - rewind( file ); - *source = (char*) malloc( sizeof(char) * file_size + 1 ); - if ( *source == (char*) NULL ) - { - return 0; - } - result = fread(*source, 1, file_size, file); - if ( result != (size_t) file_size ) - { - free( *source ); - return 0; - } - (*source)[file_size] = '\0'; - fclose( file ); - - return 1; - } - printf("open kernel file failed.\n"); - return 0; -} - namespace { OString createFileName(cl_device_id deviceId, const char* clFileName) diff --git a/sc/source/core/opencl/openclwrapper.hxx b/sc/source/core/opencl/openclwrapper.hxx index 001dc0e..7677f4d 100644 --- a/sc/source/core/opencl/openclwrapper.hxx +++ b/sc/source/core/opencl/openclwrapper.hxx @@ -178,7 +178,6 @@ public: static int initOpenclAttr( OpenCLEnv * env ); static int setKernelEnv( KernelEnv *envInfo ); - static int convertToString( const char *filename, char **source ); static Kernel* checkKernelName( const char *kernelName ); static int getOpenclState(); commit 05e43ea4cb9aa5274fbef2a3a7b721faff9b143e Author: Markus Mohrhard <markus.mohrh...@googlemail.com> Date: Tue Sep 17 04:24:36 2013 +0200 introduce strong versioning of the kernel sources Change-Id: If42711467b1c8cae4b1044464c7254792ddcd6ad diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx index a04447a..327ba70 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 d14d24b..001dc0e 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 ); commit c19e139dbe4e680bcc86ad1b21224c578cb0f19a Author: Markus Mohrhard <markus.mohrh...@googlemail.com> Date: Tue Sep 17 03:48:22 2013 +0200 use OString instead of char* for file name Change-Id: Idd69827c50056febd30f18bd8ade2b4160eafd02 diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx index a9c576d..a04447a 100644 --- a/sc/source/core/opencl/openclwrapper.cxx +++ b/sc/source/core/opencl/openclwrapper.cxx @@ -239,10 +239,10 @@ int OpenclDevice::binaryGenerated( const char * clFileName, FILE ** fhandle ) } -int OpenclDevice::writeBinaryToFile( const char* fileName, const char* birary, size_t numBytes ) +int OpenclDevice::writeBinaryToFile( const OString& rFileName, const char* birary, size_t numBytes ) { FILE *output = NULL; - output = fopen( fileName, "wb" ); + output = fopen( rFileName.getStr(), "wb" ); if ( output == NULL ) { return 0; @@ -298,26 +298,25 @@ int OpenclDevice::generatBinFromKernelSource( cl_program program, const char * c /* dump out each binary into its own separate file. */ for ( size_t i = 0; i < numDevices; i++ ) { - char fileName[256] = { 0 }, cl_name[128] = { 0 }; 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" ); - char* 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 ); - - if ( !writeBinaryToFile( fileName, binaries[i], binarySizes[i] ) ) + if ( !writeBinaryToFile( fileName + "-" + deviceName, binaries[i], binarySizes[i] ) ) { - printf("opencl-wrapper: write binary[%s] failds\n", fileName); + printf("opencl-wrapper: write binary[%s] failds\n", fileName.getStr()); } else - printf("opencl-wrapper: write binary[%s] succesfully\n", fileName); + printf("opencl-wrapper: write binary[%s] succesfully\n", fileName.getStr()); } } commit 78e4ec8b9d66e6d6db99b5f3451734eb4ebaa640 Author: Markus Mohrhard <markus.mohrh...@googlemail.com> Date: Tue Sep 17 03:09:42 2013 +0200 we can still write the next files out if one fails Change-Id: Ibfb604692f8d5d6f01652af6d6e09339c49be6af diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx index 0c7718f..a9c576d 100644 --- a/sc/source/core/opencl/openclwrapper.cxx +++ b/sc/source/core/opencl/openclwrapper.cxx @@ -258,7 +258,6 @@ int OpenclDevice::writeBinaryToFile( const char* fileName, const char* birary, s int OpenclDevice::generatBinFromKernelSource( cl_program program, const char * clFileName ) { cl_uint numDevices; - char *str = NULL; cl_int clStatus = clGetProgramInfo( program, CL_PROGRAM_NUM_DEVICES, sizeof(numDevices), &numDevices, NULL ); @@ -308,7 +307,7 @@ int OpenclDevice::generatBinFromKernelSource( cl_program program, const char * c sizeof(deviceName), deviceName, NULL); CHECK_OPENCL( clStatus, "clGetDeviceInfo" ); - str = (char*) strstr( clFileName, (char*) ".cl" ); + char* 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 ); @@ -316,9 +315,9 @@ int OpenclDevice::generatBinFromKernelSource( cl_program program, const char * c if ( !writeBinaryToFile( fileName, binaries[i], binarySizes[i] ) ) { printf("opencl-wrapper: write binary[%s] failds\n", fileName); - return 0; - } //else - printf("opencl-wrapper: write binary[%s] succesfully\n", fileName); + } + else + printf("opencl-wrapper: write binary[%s] succesfully\n", fileName); } } commit c289a37b935cb2762376dac1fc853c5178568322 Author: Markus Mohrhard <markus.mohrh...@googlemail.com> Date: Tue Sep 17 02:46:16 2013 +0200 share the setting for the device name length Change-Id: I5605d05240bc110916400239db203eeb6b8dde35 diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx index c88dcb83..0c7718f 100644 --- a/sc/source/core/opencl/openclwrapper.cxx +++ b/sc/source/core/opencl/openclwrapper.cxx @@ -39,6 +39,8 @@ #define OPENCL_DLL_NAME "libOpenCL.so" #endif +#define DEVICE_NAME_LENGTH 1024 + using namespace std; namespace sc { namespace opencl { @@ -218,7 +220,7 @@ int OpenclDevice::binaryGenerated( const char * clFileName, FILE ** fhandle ) char fileName[256] = { 0 }, cl_name[128] = { 0 }; if ( gpuEnv.mpArryDevsID[i] != 0 ) { - char deviceName[1024]; + 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" ); @@ -301,7 +303,7 @@ int OpenclDevice::generatBinFromKernelSource( cl_program program, const char * c if ( binarySizes[i] != 0 ) { - char deviceName[1024]; + char deviceName[DEVICE_NAME_LENGTH]; clStatus = clGetDeviceInfo(mpArryDevsID[i], CL_DEVICE_NAME, sizeof(deviceName), deviceName, NULL); CHECK_OPENCL( clStatus, "clGetDeviceInfo" ); @@ -2633,15 +2635,15 @@ void createDeviceInfo(cl_device_id aDeviceId, OpenclPlatformInfo& rPlatformInfo) OpenclDeviceInfo aDeviceInfo; aDeviceInfo.device = aDeviceId; - char pName[64]; - cl_int nState = clGetDeviceInfo(aDeviceId, CL_DEVICE_NAME, 64, pName, NULL); + char pName[DEVICE_NAME_LENGTH]; + cl_int nState = clGetDeviceInfo(aDeviceId, CL_DEVICE_NAME, DEVICE_NAME_LENGTH, pName, NULL); if(nState != CL_SUCCESS) return; aDeviceInfo.maName = OUString::createFromAscii(pName); - char pVendor[64]; - nState = clGetDeviceInfo(aDeviceId, CL_DEVICE_VENDOR, 64, pName, NULL); + char pVendor[DEVICE_NAME_LENGTH]; + nState = clGetDeviceInfo(aDeviceId, CL_DEVICE_VENDOR, DEVICE_NAME_LENGTH, pName, NULL); if(nState != CL_SUCCESS) return; commit 33bb1a58b76d35a6e17c00b506af79314d3cfe3e Author: Markus Mohrhard <markus.mohrh...@googlemail.com> Date: Tue Sep 17 02:42:40 2013 +0200 fix a number of memory leaks Change-Id: I1e81558d0f087c1629006b757b1efb332108d5f1 diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx index 8c4ed8f..c88dcb83 100644 --- a/sc/source/core/opencl/openclwrapper.cxx +++ b/sc/source/core/opencl/openclwrapper.cxx @@ -255,50 +255,34 @@ int OpenclDevice::writeBinaryToFile( const char* fileName, const char* birary, s int OpenclDevice::generatBinFromKernelSource( cl_program program, const char * clFileName ) { - unsigned int i = 0; - cl_int clStatus; - size_t *binarySizes; cl_uint numDevices; - cl_device_id *mpArryDevsID; - char **binaries, *str = NULL; + char *str = NULL; - clStatus = clGetProgramInfo( program, CL_PROGRAM_NUM_DEVICES, + cl_int clStatus = clGetProgramInfo( program, CL_PROGRAM_NUM_DEVICES, sizeof(numDevices), &numDevices, NULL ); CHECK_OPENCL( clStatus, "clGetProgramInfo" ); - mpArryDevsID = (cl_device_id*) malloc( sizeof(cl_device_id) * numDevices ); - if ( mpArryDevsID == NULL ) - { - return 0; - } + std::vector<cl_device_id> mpArryDevsID(numDevices); /* grab the handles to all of the devices in the program. */ clStatus = clGetProgramInfo( program, CL_PROGRAM_DEVICES, - sizeof(cl_device_id) * numDevices, mpArryDevsID, NULL ); + sizeof(cl_device_id) * numDevices, &mpArryDevsID[0], NULL ); CHECK_OPENCL( clStatus, "clGetProgramInfo" ); /* figure out the sizes of each of the binaries. */ - binarySizes = (size_t*) malloc( sizeof(size_t) * numDevices ); + std::vector<size_t> binarySizes(numDevices); clStatus = clGetProgramInfo( program, CL_PROGRAM_BINARY_SIZES, - sizeof(size_t) * numDevices, binarySizes, NULL ); + sizeof(size_t) * numDevices, &binarySizes[0], NULL ); CHECK_OPENCL( clStatus, "clGetProgramInfo" ); /* copy over all of the generated binaries. */ - binaries = (char**) malloc( sizeof(char *) * numDevices ); - if ( binaries == NULL ) - { - return 0; - } + boost::scoped_array<char*> binaries(new char*[numDevices]); - for ( i = 0; i < numDevices; i++ ) + for ( size_t i = 0; i < numDevices; i++ ) { if ( binarySizes[i] != 0 ) { - binaries[i] = (char*) malloc( sizeof(char) * binarySizes[i] ); - if ( binaries[i] == NULL ) - { - return 0; - } + binaries[i] = new char[binarySizes[i]]; } else { @@ -307,11 +291,11 @@ int OpenclDevice::generatBinFromKernelSource( cl_program program, const char * c } clStatus = clGetProgramInfo( program, CL_PROGRAM_BINARIES, - sizeof(char *) * numDevices, binaries, NULL ); + sizeof(char *) * numDevices, binaries.get(), NULL ); CHECK_OPENCL(clStatus,"clGetProgramInfo"); /* dump out each binary into its own separate file. */ - for ( i = 0; i < numDevices; i++ ) + for ( size_t i = 0; i < numDevices; i++ ) { char fileName[256] = { 0 }, cl_name[128] = { 0 }; @@ -337,32 +321,11 @@ int OpenclDevice::generatBinFromKernelSource( cl_program program, const char * c } // Release all resouces and memory - for ( i = 0; i < numDevices; i++ ) - { - if ( binaries[i] != NULL ) - { - free( binaries[i] ); - binaries[i] = NULL; - } - } - - if ( binaries != NULL ) + for ( size_t i = 0; i < numDevices; i++ ) { - free( binaries ); - binaries = NULL; + delete[] binaries[i]; } - if ( binarySizes != NULL ) - { - free( binarySizes ); - binarySizes = NULL; - } - - if ( mpArryDevsID != NULL ) - { - free( mpArryDevsID ); - mpArryDevsID = NULL; - } return 1; } _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits