Kaydet (Commit) 598f25c8 authored tarafından Tor Lillqvist's avatar Tor Lillqvist

Clean up confusing OpenCL code a bit

Get rid of the silly OpenCLDevice class that had only static members. We can
as well just use namespacing. Remove functions only used internally in
openclwrapper.cxx from the now public openclwrapper.hxx header.

Change-Id: If7336edd262c772564dc13e64113d72d0b52428c
üst a02a54f5
......@@ -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<boost::shared_ptr<osl::File> > 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<OpenCLPlatformInfo>& fillOpenCLInfo();
/**
......
......@@ -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);
......
......@@ -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;
......
......@@ -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<boost::shared_ptr<osl::File> > OpenCLDevice::binaryGenerated( const char * clFileName, cl_context context )
std::vector<boost::shared_ptr<osl::File> > binaryGenerated( const char * clFileName, cl_context context )
{
size_t numDevices=0;
......@@ -170,7 +169,7 @@ std::vector<boost::shared_ptr<osl::File> > 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,45 +429,6 @@ bool OpenCLDevice::buildProgramFromBinary(const char* buildOption, GPUEnv* gpuIn
return buildProgram(buildOption, gpuInfo, idx);
}
bool OpenCLDevice::initOpenCLRunEnv( int argc )
{
if ( ( argc > MAX_CLFILE_NUM ) || ( argc < 0 ) )
return true;
if ( !bIsInited )
{
if ( !gpuEnv.mnIsUserCreated )
memset( &gpuEnv, 0, sizeof(gpuEnv) );
//initialize devices, context, command_queue
bool status = initOpenCLRunEnv( &gpuEnv );
if ( status )
{
return true;
}
//initialize program, kernelName, kernelCount
if( getenv( "SC_FLOAT" ) )
{
gpuEnv.mnKhrFp64Flag = false;
gpuEnv.mnAmdFp64Flag = false;
}
if( gpuEnv.mnKhrFp64Flag )
{
SAL_INFO("opencl", "Use Khr double");
}
else if( gpuEnv.mnAmdFp64Flag )
{
SAL_INFO("opencl", "Use AMD double type");
}
else
{
SAL_INFO("opencl", "USE float type");
}
bIsInited = true;
}
return false;
}
namespace {
void checkDeviceForDoubleSupport(cl_device_id deviceId, bool& bKhrFp64, bool& bAmdFp64)
......@@ -501,9 +463,7 @@ void checkDeviceForDoubleSupport(cl_device_id deviceId, bool& bKhrFp64, bool& bA
}
}
}
bool OpenCLDevice::initOpenCLRunEnv( GPUEnv *gpuInfo )
bool initOpenCLRunEnv( GPUEnv *gpuInfo )
{
bool bKhrFp64 = false;
bool bAmdFp64 = false;
......@@ -516,7 +476,44 @@ bool OpenCLDevice::initOpenCLRunEnv( GPUEnv *gpuInfo )
return false;
}
namespace {
bool initOpenCLRunEnv( int argc )
{
if ( ( argc > MAX_CLFILE_NUM ) || ( argc < 0 ) )
return true;
if ( !bIsInited )
{
if ( !gpuEnv.mnIsUserCreated )
memset( &gpuEnv, 0, sizeof(gpuEnv) );
//initialize devices, context, command_queue
bool status = initOpenCLRunEnv( &gpuEnv );
if ( status )
{
return true;
}
//initialize program, kernelName, kernelCount
if( getenv( "SC_FLOAT" ) )
{
gpuEnv.mnKhrFp64Flag = false;
gpuEnv.mnAmdFp64Flag = false;
}
if( gpuEnv.mnKhrFp64Flag )
{
SAL_INFO("opencl", "Use Khr double");
}
else if( gpuEnv.mnAmdFp64Flag )
{
SAL_INFO("opencl", "Use AMD double type");
}
else
{
SAL_INFO("opencl", "USE float type");
}
bIsInited = true;
}
return false;
}
// 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);
}
......
......@@ -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<VectorRef>* 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<char> 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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment