diff --git a/include/opencl/openclwrapper.hxx b/include/opencl/openclwrapper.hxx index ba7aada44f14..1a392e53c924 100644 --- a/include/opencl/openclwrapper.hxx +++ b/include/opencl/openclwrapper.hxx @@ -68,25 +68,10 @@ struct GPUEnv bool mnAmdFp64Flag; }; -class OPENCL_DLLPUBLIC OpenCLDevice -{ -public: - static GPUEnv gpuEnv; - static bool bIsInited; - static OString maCacheFolder; - - static bool initOpenCLRunEnv( GPUEnv *gpu ); - static void releaseOpenCLEnv( GPUEnv *gpuInfo ); - static bool initOpenCLRunEnv( int argc ); - static bool generatBinFromKernelSource( cl_program program, const char * clFileName ); - static bool writeBinaryToFile( const OString& rName, const char* birary, size_t numBytes ); - static std::vector > binaryGenerated( const char * clFileName, cl_context context); - static bool buildProgramFromBinary(const char* buildOption, GPUEnv* gpuEnv, const char* filename, int idx); - - static bool initOpenCLAttr( OpenCLEnv * env ); - static void setKernelEnv( KernelEnv *envInfo ); -}; - +extern OPENCL_DLLPUBLIC GPUEnv gpuEnv; +OPENCL_DLLPUBLIC bool generatBinFromKernelSource( cl_program program, const char * clFileName ); +OPENCL_DLLPUBLIC bool buildProgramFromBinary(const char* buildOption, GPUEnv* gpuEnv, const char* filename, int idx); +OPENCL_DLLPUBLIC void setKernelEnv( KernelEnv *envInfo ); OPENCL_DLLPUBLIC const std::vector& fillOpenCLInfo(); /** diff --git a/opencl/inc/opencl_device.hxx b/opencl/inc/opencl_device.hxx index 7435879ecc83..057e2f4ac045 100644 --- a/opencl/inc/opencl_device.hxx +++ b/opencl/inc/opencl_device.hxx @@ -12,7 +12,7 @@ #include "opencl_device_selection.h" -namespace OpenCLDevice { +namespace opencl { ds_device getDeviceSelection(const char* pFileName, bool bForceSelection = false); bool selectedDeviceIsOpenCL(ds_device device); diff --git a/opencl/source/opencl_device.cxx b/opencl/source/opencl_device.cxx index 204ab345b5e0..14eb70358746 100644 --- a/opencl/source/opencl_device.cxx +++ b/opencl/source/opencl_device.cxx @@ -44,9 +44,8 @@ SAL_INFO("opencl.device", "Error code is " << status << " at " name); \ } -namespace OpenCLDevice { +namespace opencl { -bool bIsInited = false; bool bIsDeviceSelected = false; ds_device selectedDevice; diff --git a/opencl/source/openclwrapper.cxx b/opencl/source/openclwrapper.cxx index 86ba6cd3a4e3..b1813754e6c4 100644 --- a/opencl/source/openclwrapper.cxx +++ b/opencl/source/openclwrapper.cxx @@ -48,11 +48,12 @@ using namespace std; namespace opencl { -GPUEnv OpenCLDevice::gpuEnv; -bool OpenCLDevice::bIsInited = false; +GPUEnv gpuEnv; namespace { +bool bIsInited = false; + OString generateMD5(const void* pData, size_t length) { sal_uInt8 pBuffer[RTL_DIGEST_LENGTH_MD5]; @@ -81,11 +82,11 @@ OString getCacheFolder() return rtl::OUStringToOString(url, RTL_TEXTENCODING_UTF8); } +OString maCacheFolder = getCacheFolder(); + } -OString OpenCLDevice::maCacheFolder = getCacheFolder(); - -void OpenCLDevice::setKernelEnv( KernelEnv *envInfo ) +void setKernelEnv( KernelEnv *envInfo ) { envInfo->mpkContext = gpuEnv.mpContext; envInfo->mpkCmdQueue = gpuEnv.mpCmdQueue; @@ -121,13 +122,11 @@ OString createFileName(cl_device_id deviceId, const char* clFileName) OString aString = OString(deviceName) + driverVersion + platformVersion; OString aHash = generateMD5(aString.getStr(), aString.getLength()); - return OpenCLDevice::maCacheFolder + fileName + "-" + + return maCacheFolder + fileName + "-" + aHash + ".bin"; } -} - -std::vector > OpenCLDevice::binaryGenerated( const char * clFileName, cl_context context ) +std::vector > binaryGenerated( const char * clFileName, cl_context context ) { size_t numDevices=0; @@ -170,7 +169,7 @@ std::vector > OpenCLDevice::binaryGenerated( const return aGeneratedFiles; } -bool OpenCLDevice::writeBinaryToFile( const OString& rFileName, const char* binary, size_t numBytes ) +bool writeBinaryToFile( const OString& rFileName, const char* binary, size_t numBytes ) { osl::File file(rtl::OStringToOUString(rFileName, RTL_TEXTENCODING_UTF8)); osl::FileBase::RC status = file.open( @@ -187,7 +186,9 @@ bool OpenCLDevice::writeBinaryToFile( const OString& rFileName, const char* bina return true; } -bool OpenCLDevice::generatBinFromKernelSource( cl_program program, const char * clFileName ) +} + +bool generatBinFromKernelSource( cl_program program, const char * clFileName ) { cl_uint numDevices; @@ -251,7 +252,9 @@ bool OpenCLDevice::generatBinFromKernelSource( cl_program program, const char * return true; } -bool OpenCLDevice::initOpenCLAttr( OpenCLEnv * env ) +namespace { + +bool initOpenCLAttr( OpenCLEnv * env ) { if ( gpuEnv.mnIsUserCreated ) return true; @@ -266,7 +269,7 @@ bool OpenCLDevice::initOpenCLAttr( OpenCLEnv * env ) return false; } -void OpenCLDevice::releaseOpenCLEnv( GPUEnv *gpuInfo ) +void releaseOpenCLEnv( GPUEnv *gpuInfo ) { if ( !bIsInited ) { @@ -290,8 +293,6 @@ void OpenCLDevice::releaseOpenCLEnv( GPUEnv *gpuInfo ) return; } -namespace { - bool buildProgram(const char* buildOption, GPUEnv* gpuInfo, int idx) { cl_int clStatus; @@ -342,7 +343,7 @@ bool buildProgram(const char* buildOption, GPUEnv* gpuInfo, int idx) return false; } - OString aBuildLogFileURL = OpenCLDevice::maCacheFolder + "kernel-build.log"; + 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 ); @@ -361,7 +362,7 @@ bool buildProgram(const char* buildOption, GPUEnv* gpuInfo, int idx) } -bool OpenCLDevice::buildProgramFromBinary(const char* buildOption, GPUEnv* gpuInfo, const char* filename, int idx) +bool buildProgramFromBinary(const char* buildOption, GPUEnv* gpuInfo, const char* filename, int idx) { size_t numDevices; cl_int clStatus = clGetContextInfo( gpuInfo->mpContext, CL_CONTEXT_DEVICES, @@ -428,7 +429,54 @@ bool OpenCLDevice::buildProgramFromBinary(const char* buildOption, GPUEnv* gpuIn return buildProgram(buildOption, gpuInfo, idx); } -bool OpenCLDevice::initOpenCLRunEnv( int argc ) +namespace { + +void checkDeviceForDoubleSupport(cl_device_id deviceId, bool& bKhrFp64, bool& bAmdFp64) +{ + bKhrFp64 = false; + bAmdFp64 = false; + + // Check device extensions for double type + size_t aDevExtInfoSize = 0; + + cl_uint clStatus = clGetDeviceInfo( deviceId, CL_DEVICE_EXTENSIONS, 0, NULL, &aDevExtInfoSize ); + if( clStatus != CL_SUCCESS ) + return; + + boost::scoped_array pExtInfo(new char[aDevExtInfoSize]); + + clStatus = clGetDeviceInfo( deviceId, CL_DEVICE_EXTENSIONS, + sizeof(char) * aDevExtInfoSize, pExtInfo.get(), NULL); + + if( clStatus != CL_SUCCESS ) + return; + + if ( strstr( pExtInfo.get(), "cl_khr_fp64" ) ) + { + bKhrFp64 = true; + } + else + { + // Check if cl_amd_fp64 extension is supported + if ( strstr( pExtInfo.get(), "cl_amd_fp64" ) ) + bAmdFp64 = true; + } +} + +bool initOpenCLRunEnv( GPUEnv *gpuInfo ) +{ + bool bKhrFp64 = false; + bool bAmdFp64 = false; + + checkDeviceForDoubleSupport(gpuInfo->mpArryDevsID[0], bKhrFp64, bAmdFp64); + + gpuInfo->mnKhrFp64Flag = bKhrFp64; + gpuInfo->mnAmdFp64Flag = bAmdFp64; + + return false; +} + +bool initOpenCLRunEnv( int argc ) { if ( ( argc > MAX_CLFILE_NUM ) || ( argc < 0 ) ) return true; @@ -467,57 +515,6 @@ bool OpenCLDevice::initOpenCLRunEnv( int argc ) return false; } -namespace { - -void checkDeviceForDoubleSupport(cl_device_id deviceId, bool& bKhrFp64, bool& bAmdFp64) -{ - bKhrFp64 = false; - bAmdFp64 = false; - - // Check device extensions for double type - size_t aDevExtInfoSize = 0; - - cl_uint clStatus = clGetDeviceInfo( deviceId, CL_DEVICE_EXTENSIONS, 0, NULL, &aDevExtInfoSize ); - if( clStatus != CL_SUCCESS ) - return; - - boost::scoped_array pExtInfo(new char[aDevExtInfoSize]); - - clStatus = clGetDeviceInfo( deviceId, CL_DEVICE_EXTENSIONS, - sizeof(char) * aDevExtInfoSize, pExtInfo.get(), NULL); - - if( clStatus != CL_SUCCESS ) - return; - - if ( strstr( pExtInfo.get(), "cl_khr_fp64" ) ) - { - bKhrFp64 = true; - } - else - { - // Check if cl_amd_fp64 extension is supported - if ( strstr( pExtInfo.get(), "cl_amd_fp64" ) ) - bAmdFp64 = true; - } -} - -} - -bool OpenCLDevice::initOpenCLRunEnv( GPUEnv *gpuInfo ) -{ - bool bKhrFp64 = false; - bool bAmdFp64 = false; - - checkDeviceForDoubleSupport(gpuInfo->mpArryDevsID[0], bKhrFp64, bAmdFp64); - - gpuInfo->mnKhrFp64Flag = bKhrFp64; - gpuInfo->mnAmdFp64Flag = bAmdFp64; - - return false; -} - -namespace { - // based on crashes and hanging during kernel compilation void createDeviceInfo(cl_device_id aDeviceId, OpenCLPlatformInfo& rPlatformInfo) { @@ -725,12 +722,12 @@ bool switchOpenCLDevice(const OUString* pDevice, bool bAutoSelect, bool bForceEv OUString path; osl::FileBase::getSystemPathFromFileURL(url,path); OString dsFileName = rtl::OUStringToOString(path, RTL_TEXTENCODING_UTF8); - ds_device pSelectedDevice = ::OpenCLDevice::getDeviceSelection(dsFileName.getStr(), bForceEvaluation); + ds_device pSelectedDevice = getDeviceSelection(dsFileName.getStr(), bForceEvaluation); pDeviceId = pSelectedDevice.oclDeviceID; } - if(OpenCLDevice::gpuEnv.mpDevID == pDeviceId) + if(gpuEnv.mpDevID == pDeviceId) { // we don't need to change anything // still the same device @@ -769,13 +766,13 @@ bool switchOpenCLDevice(const OUString* pDevice, bool bAutoSelect, bool bForceEv return false; } - OpenCLDevice::releaseOpenCLEnv(&OpenCLDevice::gpuEnv); + releaseOpenCLEnv(&gpuEnv); OpenCLEnv env; env.mpOclPlatformID = platformId; env.mpOclContext = context; env.mpOclDevsID = pDeviceId; env.mpOclCmdQueue = command_queue; - OpenCLDevice::initOpenCLAttr(&env); + initOpenCLAttr(&env); // why do we need this at all? @@ -783,10 +780,10 @@ bool switchOpenCLDevice(const OUString* pDevice, bool bAutoSelect, bool bForceEv // initialisation below.) Because otherwise the code crashes in // initOpenCLRunEnv(). Confused? You should be. - OpenCLDevice::gpuEnv.mpArryDevsID = (cl_device_id*) malloc( sizeof(cl_device_id) ); - OpenCLDevice::gpuEnv.mpArryDevsID[0] = pDeviceId; + gpuEnv.mpArryDevsID = (cl_device_id*) malloc( sizeof(cl_device_id) ); + gpuEnv.mpArryDevsID[0] = pDeviceId; - return !OpenCLDevice::initOpenCLRunEnv(0); + return !initOpenCLRunEnv(0); } void getOpenCLDeviceInfo(size_t& rDeviceId, size_t& rPlatformId) @@ -795,7 +792,7 @@ void getOpenCLDeviceInfo(size_t& rDeviceId, size_t& rPlatformId) if (status < 0) return; - cl_device_id id = OpenCLDevice::gpuEnv.mpDevID; + cl_device_id id = gpuEnv.mpDevID; findDeviceInfoFromDeviceId(id, rDeviceId, rPlatformId); } diff --git a/sc/source/core/opencl/formulagroupcl.cxx b/sc/source/core/opencl/formulagroupcl.cxx index 0615410c0ad3..6ac2a5fb23fc 100644 --- a/sc/source/core/opencl/formulagroupcl.cxx +++ b/sc/source/core/opencl/formulagroupcl.cxx @@ -114,7 +114,7 @@ size_t VectorRef::Marshal( cl_kernel k, int argno, int, cl_program ) } // Obtain cl context KernelEnv kEnv; - ::opencl::OpenCLDevice::setKernelEnv(&kEnv); + ::opencl::setKernelEnv(&kEnv); cl_int err; if (pHostBuffer) { @@ -205,7 +205,7 @@ public: // marshaling // Obtain cl context KernelEnv kEnv; - ::opencl::OpenCLDevice::setKernelEnv(&kEnv); + ::opencl::setKernelEnv(&kEnv); // Pass the scalar result back to the rest of the formula kernel cl_int err = clSetKernelArg(k, argno, sizeof(cl_uint), (void*)&hashCode); if (CL_SUCCESS != err) @@ -389,7 +389,7 @@ size_t DynamicKernelStringArgument::Marshal( cl_kernel k, int argno, int, cl_pro FormulaToken* ref = mFormulaTree->GetFormulaToken(); // Obtain cl context KernelEnv kEnv; - ::opencl::OpenCLDevice::setKernelEnv(&kEnv); + ::opencl::setKernelEnv(&kEnv); cl_int err; formula::VectorRefArray vRef; size_t nStrings = 0; @@ -1132,7 +1132,7 @@ public: assert(Base::mpClmem == NULL); // Obtain cl context KernelEnv kEnv; - ::opencl::OpenCLDevice::setKernelEnv(&kEnv); + ::opencl::setKernelEnv(&kEnv); cl_int err; size_t nInput = mpDVR->GetArrayLength(); size_t nCurWindowSize = mpDVR->GetRefRowSize(); @@ -1913,7 +1913,7 @@ public: { // Obtain cl context KernelEnv kEnv; - ::opencl::OpenCLDevice::setKernelEnv(&kEnv); + ::opencl::setKernelEnv(&kEnv); cl_int err; cl_mem pClmem2; @@ -1971,7 +1971,7 @@ public: { // Obtain cl context KernelEnv kEnv; - ::opencl::OpenCLDevice::setKernelEnv(&kEnv); + ::opencl::setKernelEnv(&kEnv); cl_int err; DynamicKernelArgument* Arg = mvSubArguments[0].get(); DynamicKernelSlidingArgument* slidingArgPtr = @@ -3282,11 +3282,11 @@ public: DynamicKernelSoPArguments>(mpRoot, new OpNop); std::stringstream decl; - if (::opencl::OpenCLDevice::gpuEnv.mnKhrFp64Flag) + if (::opencl::gpuEnv.mnKhrFp64Flag) { decl << "#pragma OPENCL EXTENSION cl_khr_fp64: enable\n"; } - else if (::opencl::OpenCLDevice::gpuEnv.mnAmdFp64Flag) + else if (::opencl::gpuEnv.mnAmdFp64Flag) { decl << "#pragma OPENCL EXTENSION cl_amd_fp64: enable\n"; } @@ -3355,7 +3355,7 @@ public: { // Obtain cl context KernelEnv kEnv; - ::opencl::OpenCLDevice::setKernelEnv(&kEnv); + ::opencl::setKernelEnv(&kEnv); cl_int err; // The results mpResClmem = clCreateBuffer(kEnv.mpkContext, @@ -3417,7 +3417,7 @@ void DynamicKernel::CreateKernel() // Compile kernel here!!! // Obtain cl context KernelEnv kEnv; - ::opencl::OpenCLDevice::setKernelEnv(&kEnv); + ::opencl::setKernelEnv(&kEnv); const char* src = mFullProgramSrc.c_str(); static std::string lastOneKernelHash = ""; static std::string lastSecondKernelHash = ""; @@ -3439,11 +3439,11 @@ void DynamicKernel::CreateKernel() { clReleaseProgram(lastSecondProgram); } - if (::opencl::OpenCLDevice::buildProgramFromBinary("", - &::opencl::OpenCLDevice::gpuEnv, KernelHash.c_str(), 0)) + if (::opencl::buildProgramFromBinary("", + &::opencl::gpuEnv, KernelHash.c_str(), 0)) { - mpProgram = ::opencl::OpenCLDevice::gpuEnv.mpArryPrograms[0]; - ::opencl::OpenCLDevice::gpuEnv.mpArryPrograms[0] = NULL; + mpProgram = ::opencl::gpuEnv.mpArryPrograms[0]; + ::opencl::gpuEnv.mpArryPrograms[0] = NULL; } else { @@ -3452,7 +3452,7 @@ void DynamicKernel::CreateKernel() if (err != CL_SUCCESS) throw OpenCLError(err, __FILE__, __LINE__); err = clBuildProgram(mpProgram, 1, - ::opencl::OpenCLDevice::gpuEnv.mpArryDevsID, "", NULL, NULL); + ::opencl::gpuEnv.mpArryDevsID, "", NULL, NULL); if (err != CL_SUCCESS) { #if OSL_DEBUG_LEVEL > 0 @@ -3460,7 +3460,7 @@ void DynamicKernel::CreateKernel() { cl_build_status stat; cl_int e = clGetProgramBuildInfo( - mpProgram, ::opencl::OpenCLDevice::gpuEnv.mpArryDevsID[0], + mpProgram, ::opencl::gpuEnv.mpArryDevsID[0], CL_PROGRAM_BUILD_STATUS, sizeof(cl_build_status), &stat, 0); SAL_WARN_IF( @@ -3472,7 +3472,7 @@ void DynamicKernel::CreateKernel() { size_t n; e = clGetProgramBuildInfo( - mpProgram, ::opencl::OpenCLDevice::gpuEnv.mpArryDevsID[0], + mpProgram, ::opencl::gpuEnv.mpArryDevsID[0], CL_PROGRAM_BUILD_LOG, 0, 0, &n); SAL_WARN_IF( e != CL_SUCCESS || n == 0, "sc.opencl", @@ -3483,7 +3483,7 @@ void DynamicKernel::CreateKernel() { std::vector log(n); e = clGetProgramBuildInfo( - mpProgram, ::opencl::OpenCLDevice::gpuEnv.mpArryDevsID[0], + mpProgram, ::opencl::gpuEnv.mpArryDevsID[0], CL_PROGRAM_BUILD_LOG, n, &log[0], 0); SAL_WARN_IF( e != CL_SUCCESS || n == 0, "sc.opencl", @@ -3502,7 +3502,7 @@ void DynamicKernel::CreateKernel() throw OpenCLError(err, __FILE__, __LINE__); } // Generate binary out of compiled kernel. - ::opencl::OpenCLDevice::generatBinFromKernelSource(mpProgram, + ::opencl::generatBinFromKernelSource(mpProgram, (mKernelSignature + GetMD5()).c_str()); } lastSecondKernelHash = lastOneKernelHash; @@ -3668,7 +3668,7 @@ bool FormulaGroupInterpreterOpenCL::interpret( ScDocument& rDoc, { // Obtain cl context KernelEnv kEnv; - ::opencl::OpenCLDevice::setKernelEnv(&kEnv); + ::opencl::setKernelEnv(&kEnv); // Run the kernel. pKernel->Launch(xGroup->mnLength); // Map results back