[Libreoffice-commits] core.git: Branch 'feature/calc-pluggable-opencl' - 9 commits - sc/source

Markus Mohrhard markus.mohrhard at googlemail.com
Wed Sep 18 06:49:06 PDT 2013


 sc/source/core/opencl/openclwrapper.cxx |  218 +++++++++++++++++---------------
 sc/source/core/opencl/openclwrapper.hxx |    7 -
 2 files changed, 123 insertions(+), 102 deletions(-)

New commits:
commit 6c03a649035607c2ffa4a1bf8659ccb910828136
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Wed Sep 18 13:06:16 2013 +0200

    fix memory leak
    
    Change-Id: If1afe59bc5bb40bb3fff4c74b863c6d77e006cc5

diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx
index 0fe2220..d4cf1b7 100644
--- a/sc/source/core/opencl/openclwrapper.cxx
+++ b/sc/source/core/opencl/openclwrapper.cxx
@@ -475,10 +475,10 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
         {
             // something went wrong, fall back to compiling from source
             bBinaryExisted = false;
-            for(size_t i = 0; i < numDevices; ++i)
-            {
-                delete[] pBinary[i];
-            }
+        }
+        for(size_t i = 0; i < numDevices; ++i)
+        {
+            delete[] pBinary[i];
         }
     }
 
commit 3932b6d2c1be477df974e9dc478282f881315c01
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Wed Sep 18 13:05:31 2013 +0200

    fall back to compiling from source if binary failed
    
    Change-Id: I8e8d8fb5b1da18c44e1537f97317d4e5b57c47b6

diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx
index f469aac..0fe2220 100644
--- a/sc/source/core/opencl/openclwrapper.cxx
+++ b/sc/source/core/opencl/openclwrapper.cxx
@@ -473,14 +473,16 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
                                            &binary_status, &clStatus );
         if(clStatus != CL_SUCCESS)
         {
+            // something went wrong, fall back to compiling from source
+            bBinaryExisted = false;
             for(size_t i = 0; i < numDevices; ++i)
             {
                 delete[] pBinary[i];
             }
         }
-        CHECK_OPENCL( clStatus, "clCreateProgramWithBinary" );
     }
-    else
+
+    if(!bBinaryExisted)
     {
         // create a CL program using the kernel source
         fprintf(stderr, "Create kernel from source\n");
commit 3435be23b042983f9dfd889e8730a6829b6a1d9e
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Wed Sep 18 12:45:37 2013 +0200

    void* is dangerous
    
    no idea how that ever worked
    
    Change-Id: Ie14c86a241a2fa7e1963385987245cd0ee965fcf

diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx
index 1a19451..f469aac 100644
--- a/sc/source/core/opencl/openclwrapper.cxx
+++ b/sc/source/core/opencl/openclwrapper.cxx
@@ -437,17 +437,19 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
     if (aGeneratedFiles.size() == numDevices)
     {
         bBinaryExisted = true;
-        boost::scoped_array<char*> pBinary(new char*[numDevices]);
+        boost::scoped_array<unsigned char*> pBinary(new unsigned char*[numDevices]);
         for(size_t i = 0; i < numDevices; ++i)
         {
             sal_uInt64 nSize;
             aGeneratedFiles[i]->getSize(nSize);
-            char* binary = new char[nSize];
+            unsigned char* binary = new unsigned char[nSize];
             sal_uInt64 nBytesRead;
             aGeneratedFiles[i]->read(binary, nSize, nBytesRead);
             if(nSize != nBytesRead)
                 assert(false);
 
+            length = nBytesRead;
+
             pBinary[i] = binary;
         }
 
@@ -467,7 +469,7 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
 
         fprintf(stderr, "Create kernel from binary\n");
         gpuInfo->mpArryPrograms[idx] = clCreateProgramWithBinary( gpuInfo->mpContext,numDevices,
-                                           mpArryDevsID.get(), &length, (const unsigned char**) &pBinary,
+                                           mpArryDevsID.get(), &length, (const unsigned char**) pBinary.get(),
                                            &binary_status, &clStatus );
         if(clStatus != CL_SUCCESS)
         {
commit 801a705d948327c1dbfb1d6cd5dd807d7e4c847a
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Tue Sep 17 19:31:41 2013 +0200

    use $UserInstallation/cache to cache opencl kernels
    
    Change-Id: I2ba1fb6172cfd0c725a45d4506b46e8f04a33093

diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx
index 45dedc2..1a19451 100644
--- a/sc/source/core/opencl/openclwrapper.cxx
+++ b/sc/source/core/opencl/openclwrapper.cxx
@@ -18,6 +18,7 @@
 #include <boost/scoped_array.hpp>
 
 #include "sal/config.h"
+#include <osl/file.hxx>
 #include "oclkernels.hxx"
 
 #include <stdio.h>
commit a18dafee772a5b78005100df9b2e09ce125a4847
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Tue Sep 17 23:04:54 2013 +0200

    read the binary files from the cached folder
    
    Change-Id: Ifc5084a3d8ef661e3b3fca7fbc76e1c0c62b2056

diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx
index 1ca38fd..45dedc2 100644
--- a/sc/source/core/opencl/openclwrapper.cxx
+++ b/sc/source/core/opencl/openclwrapper.cxx
@@ -18,7 +18,6 @@
 #include <boost/scoped_array.hpp>
 
 #include "sal/config.h"
-#include <osl/file.hxx>
 #include "oclkernels.hxx"
 
 #include <stdio.h>
@@ -216,45 +215,42 @@ OString createFileName(cl_device_id deviceId, const char* clFileName)
 
 }
 
-int OpenclDevice::binaryGenerated( const char * clFileName, FILE ** fhandle )
+std::vector<boost::shared_ptr<osl::File> > OpenclDevice::binaryGenerated( const char * clFileName, cl_context context )
 {
-    unsigned int i = 0;
-    cl_int clStatus;
-    int status = 0;
-    FILE *fd = NULL;
     cl_uint numDevices=0;
-    if ( getenv("SC_OPENCLCPU") )
-    {
-        clStatus = clGetDeviceIDs(gpuEnv.mpPlatformID, // platform
-                                  CL_DEVICE_TYPE_CPU,  // device_type for CPU device
-                                  0,                   // num_entries
-                                  NULL,                // devices ID
-                                  &numDevices);
-    }
-    else
-    {
-        clStatus = clGetDeviceIDs(gpuEnv.mpPlatformID, // platform
-                                  CL_DEVICE_TYPE_GPU,  // device_type for GPU device
-                                  0,                   // num_entries
-                                  NULL,                // devices ID
-                                  &numDevices);
-    }
-    CHECK_OPENCL( clStatus, "clGetDeviceIDs" );
 
-    for ( i = 0; i < numDevices; i++ )
+    std::vector<boost::shared_ptr<osl::File> > aGeneratedFiles;
+    cl_int clStatus = clGetContextInfo( context, CL_CONTEXT_NUM_DEVICES,
+            sizeof(numDevices), &numDevices, NULL );
+
+    if(clStatus != CL_SUCCESS)
+        return aGeneratedFiles;
+
+
+    // grab the handles to all of the devices in the context.
+    boost::scoped_array<cl_device_id> mpArryDevsID(new cl_device_id[numDevices]);
+    clStatus = clGetContextInfo( context, CL_CONTEXT_DEVICES,
+            sizeof( cl_device_id ) * numDevices, mpArryDevsID.get(), NULL );
+
+    if(clStatus != CL_SUCCESS)
+        return aGeneratedFiles;
+
+    for ( size_t i = 0; i < numDevices; i++ )
     {
-        if ( gpuEnv.mpArryDevsID[i] != 0 )
+        if ( mpArryDevsID[i] != 0 )
         {
             OString fileName = createFileName(gpuEnv.mpArryDevsID[i], clFileName);
-            fd = fopen( fileName.getStr(), "rb" );
-            status = ( fd != NULL ) ? 1 : 0;
+            osl::File* pNewFile = new osl::File(rtl::OStringToOUString(fileName, RTL_TEXTENCODING_UTF8));
+            if(pNewFile->open(osl_File_OpenFlag_Read) == osl::FileBase::E_None)
+                aGeneratedFiles.push_back(boost::shared_ptr<osl::File>(pNewFile));
+            else
+            {
+                delete pNewFile;
+                break;
+            }
         }
     }
-    if ( fd != NULL )
-    {
-        *fhandle = fd;
-    }
-    return status;
+    return aGeneratedFiles;
 
 }
 
@@ -418,10 +414,8 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
 {
     cl_int clStatus = 0;
     size_t length;
-    char *binary;
-    int b_error, binary_status, binaryExisted, idx;
+    int binary_status, idx;
     cl_uint numDevices;
-    FILE *fd;
     const char* filename = "kernel.cl";
     fprintf(stderr, "compileKernelFile ... \n");
     if ( cachedOfKernerPrg(gpuInfo, filename) == 1 )
@@ -431,49 +425,57 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
 
     idx = gpuInfo->mnFileCount;
 
-    binaryExisted = 0;
-    if ( ( binaryExisted = binaryGenerated( filename, &fd ) ) == 1 )
-    {
-        clStatus = clGetContextInfo( gpuInfo->mpContext, CL_CONTEXT_NUM_DEVICES,
-                       sizeof(numDevices), &numDevices, NULL );
-        CHECK_OPENCL( clStatus, "clGetContextInfo" );
+    clStatus = clGetContextInfo( gpuInfo->mpContext, CL_CONTEXT_NUM_DEVICES,
+            sizeof(numDevices), &numDevices, NULL );
+    CHECK_OPENCL( clStatus, "clGetContextInfo" );
 
-        boost::scoped_array<cl_device_id> mpArryDevsID(new cl_device_id[numDevices]);
+    std::vector<boost::shared_ptr<osl::File> > aGeneratedFiles = binaryGenerated(
+            filename, gpuInfo->mpContext );
 
-        b_error = 0;
-        length = 0;
-        b_error |= fseek( fd, 0, SEEK_END ) < 0;
-        b_error |= ( length = ftell(fd) ) <= 0;
-        b_error |= fseek( fd, 0, SEEK_SET ) < 0;
-        if ( b_error )
-        {
-            return 0;
-        }
-
-        binary = (char*) malloc( length + 2 );
-        if ( !binary )
+    bool bBinaryExisted = false;
+    if (aGeneratedFiles.size() == numDevices)
+    {
+        bBinaryExisted = true;
+        boost::scoped_array<char*> pBinary(new char*[numDevices]);
+        for(size_t i = 0; i < numDevices; ++i)
         {
-            return 0;
+            sal_uInt64 nSize;
+            aGeneratedFiles[i]->getSize(nSize);
+            char* binary = new char[nSize];
+            sal_uInt64 nBytesRead;
+            aGeneratedFiles[i]->read(binary, nSize, nBytesRead);
+            if(nSize != nBytesRead)
+                assert(false);
+
+            pBinary[i] = binary;
         }
 
-        memset( binary, 0, length + 2 );
-        b_error |= fread( binary, 1, length, fd ) != length;
-
-
-        fclose( fd );
-        fd = NULL;
         // grab the handles to all of the devices in the context.
+        boost::scoped_array<cl_device_id> mpArryDevsID(new cl_device_id[numDevices]);
         clStatus = clGetContextInfo( gpuInfo->mpContext, CL_CONTEXT_DEVICES,
                        sizeof( cl_device_id ) * numDevices, mpArryDevsID.get(), NULL );
+
+        if(clStatus != CL_SUCCESS)
+        {
+            for(size_t i = 0; i < numDevices; ++i)
+            {
+                delete[] pBinary[i];
+            }
+        }
         CHECK_OPENCL( clStatus, "clGetContextInfo" );
 
         fprintf(stderr, "Create kernel from binary\n");
         gpuInfo->mpArryPrograms[idx] = clCreateProgramWithBinary( gpuInfo->mpContext,numDevices,
-                                           mpArryDevsID.get(), &length, (const unsigned char**) &binary,
+                                           mpArryDevsID.get(), &length, (const unsigned char**) &pBinary,
                                            &binary_status, &clStatus );
+        if(clStatus != CL_SUCCESS)
+        {
+            for(size_t i = 0; i < numDevices; ++i)
+            {
+                delete[] pBinary[i];
+            }
+        }
         CHECK_OPENCL( clStatus, "clCreateProgramWithBinary" );
-
-        free( binary );
     }
     else
     {
@@ -558,7 +560,7 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
 
     strcpy( gpuEnv.mArryKnelSrcFile[idx], filename );
 
-    if ( binaryExisted == 0 )
+    if ( !bBinaryExisted )
         generatBinFromKernelSource( gpuEnv.mpArryPrograms[idx], filename );
 
     gpuInfo->mnFileCount += 1;
diff --git a/sc/source/core/opencl/openclwrapper.hxx b/sc/source/core/opencl/openclwrapper.hxx
index 7bcf5c4..c81c313 100644
--- a/sc/source/core/opencl/openclwrapper.hxx
+++ b/sc/source/core/opencl/openclwrapper.hxx
@@ -13,6 +13,9 @@
 #include <config_features.h>
 #include <formula/opcode.hxx>
 #include <sal/detail/log.h>
+#include <osl/file.hxx>
+#include <vector>
+#include <boost/shared_ptr.hpp>
 #include <cassert>
 #include "platforminfo.hxx"
 
@@ -174,7 +177,7 @@ public:
     static int cachedOfKernerPrg( const GPUEnv *gpuEnvCached, const char * clFileName );
     static int generatBinFromKernelSource( cl_program program, const char * clFileName );
     static int writeBinaryToFile( const OString& rName, const char* birary, size_t numBytes );
-    static int binaryGenerated( const char * clFileName, FILE ** fhandle );
+    static std::vector<boost::shared_ptr<osl::File> > binaryGenerated( const char * clFileName, cl_context context);
     static int compileKernelFile( const char *filename, GPUEnv *gpuInfo, const char *buildOption );
 
     static int initOpenclAttr( OpenCLEnv * env );
commit 3f81eaa5302c7d214b6dc89067be0679e4dbf8cc
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Tue Sep 17 20:30:53 2013 +0200

    replace one more file write with location in profile directory
    
    Change-Id: I1774f66d018923c6f892d21acf8a81330628cd98

diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx
index 98ef0d2..1ca38fd 100644
--- a/sc/source/core/opencl/openclwrapper.cxx
+++ b/sc/source/core/opencl/openclwrapper.cxx
@@ -419,10 +419,9 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
     cl_int clStatus = 0;
     size_t length;
     char *binary;
-    const char *source;
     int b_error, binary_status, binaryExisted, idx;
     cl_uint numDevices;
-    FILE *fd, *fd1;
+    FILE *fd;
     const char* filename = "kernel.cl";
     fprintf(stderr, "compileKernelFile ... \n");
     if ( cachedOfKernerPrg(gpuInfo, filename) == 1 )
@@ -432,8 +431,6 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
 
     idx = gpuInfo->mnFileCount;
 
-    source = kernel_src;
-
     binaryExisted = 0;
     if ( ( binaryExisted = binaryGenerated( filename, &fd ) ) == 1 )
     {
@@ -483,8 +480,9 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
         // create a CL program using the kernel source
         fprintf(stderr, "Create kernel from source\n");
         size_t source_size[1];
-        source_size[0] = strlen( source );
-        gpuEnv.mpArryPrograms[idx] = clCreateProgramWithSource( gpuEnv.mpContext, 1, &source,
+
+        source_size[0] = strlen( kernel_src );
+        gpuEnv.mpArryPrograms[idx] = clCreateProgramWithSource( gpuEnv.mpContext, 1, &kernel_src,
                                          source_size, &clStatus);
         CHECK_OPENCL( clStatus, "clCreateProgramWithSource" );
     }
@@ -544,12 +542,16 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
             return 0;
         }
 
-        fd1 = fopen( "kernel-build.log", "w+" );
-        if ( fd1 != NULL )
-        {
-            fwrite( buildLog.get(), sizeof(char), length, fd1 );
-            fclose( fd1 );
-        }
+        OString aBuildLogFileURL = maCacheFolder + "kernel-build.log";
+        osl::File aBuildLogFile(rtl::OStringToOUString(aBuildLogFileURL, RTL_TEXTENCODING_UTF8));
+        osl::FileBase::RC status = aBuildLogFile.open(
+                osl_File_OpenFlag_Write | osl_File_OpenFlag_Create );
+
+        if(status != osl::FileBase::E_None)
+            return 0;
+
+        sal_uInt64 nBytesWritten = 0;
+        aBuildLogFile.write( buildLog.get(), length, nBytesWritten );
 
         return 0;
     }
commit b48735215a1520475820eee585ad4a8aefc47e7f
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Tue Sep 17 19:49:20 2013 +0200

    fix another memory leak
    
    Change-Id: I6761bcc137934b02815ce10d43f3bc9bee7a1b90

diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx
index 2ec2046..98ef0d2 100644
--- a/sc/source/core/opencl/openclwrapper.cxx
+++ b/sc/source/core/opencl/openclwrapper.cxx
@@ -418,7 +418,7 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
 {
     cl_int clStatus = 0;
     size_t length;
-    char *buildLog = NULL, *binary;
+    char *binary;
     const char *source;
     int b_error, binary_status, binaryExisted, idx;
     cl_uint numDevices;
@@ -526,20 +526,17 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
             printf("opencl create build log fail\n");
             return 0;
         }
-        buildLog = (char*) malloc( length );
-        if ( buildLog == (char*) NULL )
-        {
-            return 0;
-        }
+
+        boost::scoped_array<char> buildLog(new char[length]);
         if ( !gpuInfo->mnIsUserCreated )
         {
             clStatus = clGetProgramBuildInfo( gpuInfo->mpArryPrograms[idx], gpuInfo->mpArryDevsID[0],
-                           CL_PROGRAM_BUILD_LOG, length, buildLog, &length );
+                           CL_PROGRAM_BUILD_LOG, length, buildLog.get(), &length );
         }
         else
         {
             clStatus = clGetProgramBuildInfo( gpuInfo->mpArryPrograms[idx], gpuInfo->mpDevID,
-                           CL_PROGRAM_BUILD_LOG, length, buildLog, &length );
+                           CL_PROGRAM_BUILD_LOG, length, buildLog.get(), &length );
         }
         if ( clStatus != CL_SUCCESS )
         {
@@ -550,11 +547,10 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
         fd1 = fopen( "kernel-build.log", "w+" );
         if ( fd1 != NULL )
         {
-            fwrite( buildLog, sizeof(char), length, fd1 );
+            fwrite( buildLog.get(), sizeof(char), length, fd1 );
             fclose( fd1 );
         }
 
-        free( buildLog );
         return 0;
     }
 
commit 4f2ad2e76561af53c0e2135f767c75f082f3070e
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Tue Sep 17 19:36:09 2013 +0200

    fix another memory leak
    
    Change-Id: I31359f121fa858dfc1868d74a2f827e5332592c3

diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx
index 3610659..2ec2046 100644
--- a/sc/source/core/opencl/openclwrapper.cxx
+++ b/sc/source/core/opencl/openclwrapper.cxx
@@ -420,10 +420,8 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
     size_t length;
     char *buildLog = NULL, *binary;
     const char *source;
-    size_t source_size[1];
     int b_error, binary_status, binaryExisted, idx;
     cl_uint numDevices;
-    cl_device_id *mpArryDevsID;
     FILE *fd, *fd1;
     const char* filename = "kernel.cl";
     fprintf(stderr, "compileKernelFile ... \n");
@@ -436,7 +434,6 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
 
     source = kernel_src;
 
-    source_size[0] = strlen( source );
     binaryExisted = 0;
     if ( ( binaryExisted = binaryGenerated( filename, &fd ) ) == 1 )
     {
@@ -444,11 +441,7 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
                        sizeof(numDevices), &numDevices, NULL );
         CHECK_OPENCL( clStatus, "clGetContextInfo" );
 
-        mpArryDevsID = (cl_device_id*) malloc( sizeof(cl_device_id) * numDevices );
-        if ( mpArryDevsID == NULL )
-        {
-            return 0;
-        }
+        boost::scoped_array<cl_device_id> mpArryDevsID(new cl_device_id[numDevices]);
 
         b_error = 0;
         length = 0;
@@ -474,23 +467,23 @@ int OpenclDevice::compileKernelFile( GPUEnv *gpuInfo, const char *buildOption )
         fd = NULL;
         // grab the handles to all of the devices in the context.
         clStatus = clGetContextInfo( gpuInfo->mpContext, CL_CONTEXT_DEVICES,
-                       sizeof( cl_device_id ) * numDevices, mpArryDevsID, NULL );
+                       sizeof( cl_device_id ) * numDevices, mpArryDevsID.get(), NULL );
         CHECK_OPENCL( clStatus, "clGetContextInfo" );
 
         fprintf(stderr, "Create kernel from binary\n");
         gpuInfo->mpArryPrograms[idx] = clCreateProgramWithBinary( gpuInfo->mpContext,numDevices,
-                                           mpArryDevsID, &length, (const unsigned char**) &binary,
+                                           mpArryDevsID.get(), &length, (const unsigned char**) &binary,
                                            &binary_status, &clStatus );
         CHECK_OPENCL( clStatus, "clCreateProgramWithBinary" );
 
         free( binary );
-        free( mpArryDevsID );
-        mpArryDevsID = NULL;
     }
     else
     {
         // create a CL program using the kernel source
         fprintf(stderr, "Create kernel from source\n");
+        size_t source_size[1];
+        source_size[0] = strlen( source );
         gpuEnv.mpArryPrograms[idx] = clCreateProgramWithSource( gpuEnv.mpContext, 1, &source,
                                          source_size, &clStatus);
         CHECK_OPENCL( clStatus, "clCreateProgramWithSource" );
commit 7fb7f9661a2a1ec1bff2be2a7a94f03043c2ee46
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Tue Sep 17 19:31:41 2013 +0200

    use $UserInstallation/cache to cache opencl kernels
    
    Change-Id: I2ba1fb6172cfd0c725a45d4506b46e8f04a33093

diff --git a/sc/source/core/opencl/openclwrapper.cxx b/sc/source/core/opencl/openclwrapper.cxx
index 684abe6..3610659 100644
--- a/sc/source/core/opencl/openclwrapper.cxx
+++ b/sc/source/core/opencl/openclwrapper.cxx
@@ -7,14 +7,18 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  */
 
+#include <config_folders.h>
+
 #include "openclwrapper.hxx"
 
 #include <rtl/ustring.hxx>
 #include <rtl/strbuf.hxx>
 #include <rtl/digest.h>
+#include <rtl/bootstrap.hxx>
 #include <boost/scoped_array.hpp>
 
 #include "sal/config.h"
+#include <osl/file.hxx>
 #include "oclkernels.hxx"
 
 #include <stdio.h>
@@ -76,9 +80,20 @@ OString generateHashForSource()
     return aBuffer.makeStringAndClear();
 }
 
+OString getCacheFolder()
+{
+    OUString url("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}/cache/");
+    rtl::Bootstrap::expandMacros(url);
+
+    osl::Directory::create(url);
+
+    return rtl::OUStringToOString(url, RTL_TEXTENCODING_UTF8);
+}
+
 }
 
 OString OpenclDevice::maSourceHash = generateHashForSource();
+OString OpenclDevice::maCacheFolder = getCacheFolder();
 
 int OpenclDevice::releaseOpenclRunEnv()
 {
@@ -195,7 +210,8 @@ OString createFileName(cl_device_id deviceId, const char* clFileName)
     char deviceName[DEVICE_NAME_LENGTH] = {0};
     clGetDeviceInfo(deviceId, CL_DEVICE_NAME,
             sizeof(deviceName), deviceName, NULL);
-    return fileName + "-" + deviceName + "-" + OpenclDevice::maSourceHash + ".bin";
+    return OpenclDevice::maCacheFolder + fileName + "-" +
+        deviceName + "-" + OpenclDevice::maSourceHash + ".bin";
 }
 
 }
@@ -242,17 +258,19 @@ int OpenclDevice::binaryGenerated( const char * clFileName, FILE ** fhandle )
 
 }
 
-int OpenclDevice::writeBinaryToFile( const OString& rFileName, const char* birary, size_t numBytes )
+int OpenclDevice::writeBinaryToFile( const OString& rFileName, const char* binary, size_t numBytes )
 {
-    FILE *output = NULL;
-    output = fopen( rFileName.getStr(), "wb" );
-    if ( output == NULL )
-    {
+    osl::File file(rtl::OStringToOUString(rFileName, RTL_TEXTENCODING_UTF8));
+    osl::FileBase::RC status = file.open(
+            osl_File_OpenFlag_Write | osl_File_OpenFlag_Create );
+
+    if(status != osl::FileBase::E_None)
         return 0;
-    }
 
-    fwrite( birary, sizeof(char), numBytes, output );
-    fclose( output );
+    sal_uInt64 nBytesWritten = 0;
+    file.write( binary, numBytes, nBytesWritten );
+
+    assert(numBytes == nBytesWritten);
 
     return 1;
 
diff --git a/sc/source/core/opencl/openclwrapper.hxx b/sc/source/core/opencl/openclwrapper.hxx
index 5a89e6b..7bcf5c4 100644
--- a/sc/source/core/opencl/openclwrapper.hxx
+++ b/sc/source/core/opencl/openclwrapper.hxx
@@ -163,6 +163,8 @@ public:
     static GPUEnv gpuEnv;
     static int isInited;
     static OString maSourceHash;
+    static OString maCacheFolder;
+
     static int registOpenclKernel();
     static int releaseOpenclRunEnv();
     static int initOpenclRunEnv( GPUEnv *gpu );


More information about the Libreoffice-commits mailing list