Implemented b3OpenCLUtils::setCachePath
Also made kernel caching work on Mac OSX (and likely Linux, needs testing before enabling)
This commit is contained in:
@@ -409,6 +409,8 @@ void myprintf(const char* msg)
|
|||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
b3OpenCLUtils::setCachePath("/Users/erwincoumans/develop/mycache");
|
||||||
|
|
||||||
b3SetCustomPrintfFunc(myprintf);
|
b3SetCustomPrintfFunc(myprintf);
|
||||||
b3Vector3 test(1,2,3);
|
b3Vector3 test(1,2,3);
|
||||||
test.x = 1;
|
test.x = 1;
|
||||||
|
|||||||
@@ -13,8 +13,9 @@ subject to the following restrictions:
|
|||||||
3. This notice may not be removed or altered from any source distribution.
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//original author: Roman Ponomarev
|
//Original author: Roman Ponomarev
|
||||||
//cleanup by Erwin Coumans
|
//Mostly Reimplemented by Erwin Coumans
|
||||||
|
|
||||||
|
|
||||||
bool gDebugForceLoadingFromSource = false;
|
bool gDebugForceLoadingFromSource = false;
|
||||||
bool gDebugSkipLoadingBinary = false;
|
bool gDebugSkipLoadingBinary = false;
|
||||||
@@ -40,6 +41,12 @@ bool gDebugSkipLoadingBinary = false;
|
|||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#define b3Assert assert
|
#define b3Assert assert
|
||||||
|
#ifdef __APPLE__
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const char* sCachedBinaryPath="cache";
|
||||||
|
|
||||||
|
|
||||||
//Set the preferred platform vendor using the OpenCL SDK
|
//Set the preferred platform vendor using the OpenCL SDK
|
||||||
@@ -150,6 +157,11 @@ const char* b3OpenCLUtils_getSdkVendorName()
|
|||||||
return spPlatformVendor;
|
return spPlatformVendor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void b3OpenCLUtils_setCachePath(const char* path)
|
||||||
|
{
|
||||||
|
sCachedBinaryPath = path;
|
||||||
|
}
|
||||||
|
|
||||||
cl_platform_id b3OpenCLUtils_getPlatform(int platformIndex0, cl_int* pErrNum)
|
cl_platform_id b3OpenCLUtils_getPlatform(int platformIndex0, cl_int* pErrNum)
|
||||||
{
|
{
|
||||||
#ifdef B3_USE_CLEW
|
#ifdef B3_USE_CLEW
|
||||||
@@ -590,41 +602,41 @@ cl_program b3OpenCLUtils_compileCLProgramFromString(cl_context clContext, cl_dev
|
|||||||
cl_program m_cpProgram=0;
|
cl_program m_cpProgram=0;
|
||||||
cl_int status;
|
cl_int status;
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
char binaryFileName[B3_MAX_STRING_LENGTH];
|
char binaryFileName[B3_MAX_STRING_LENGTH];
|
||||||
char* bla=0;
|
|
||||||
|
|
||||||
if (clFileNameForCaching && !(disableBinaryCaching || gDebugSkipLoadingBinary||gDebugForceLoadingFromSource) )
|
|
||||||
{
|
|
||||||
|
|
||||||
char deviceName[256];
|
char deviceName[256];
|
||||||
char driverVersion[256];
|
char driverVersion[256];
|
||||||
const char* strippedName;
|
const char* strippedName;
|
||||||
int fileUpToDate = 0;
|
int fileUpToDate = 0;
|
||||||
int binaryFileValid=0;
|
int binaryFileValid=0;
|
||||||
FILETIME modtimeBinary;
|
|
||||||
|
|
||||||
|
if (clFileNameForCaching)
|
||||||
|
{
|
||||||
clGetDeviceInfo(device, CL_DEVICE_NAME, 256, &deviceName, NULL);
|
clGetDeviceInfo(device, CL_DEVICE_NAME, 256, &deviceName, NULL);
|
||||||
clGetDeviceInfo(device, CL_DRIVER_VERSION, 256, &driverVersion, NULL);
|
clGetDeviceInfo(device, CL_DRIVER_VERSION, 256, &driverVersion, NULL);
|
||||||
|
|
||||||
|
|
||||||
strippedName = strip2(clFileNameForCaching,"\\");
|
strippedName = strip2(clFileNameForCaching,"\\");
|
||||||
strippedName = strip2(strippedName,"/");
|
strippedName = strip2(strippedName,"/");
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
sprintf_s(binaryFileName,B3_MAX_STRING_LENGTH,"%s/%s.%s.%s.bin",sCachedBinaryPath,strippedName, deviceName,driverVersion );
|
||||||
|
#else
|
||||||
|
sprintf(binaryFileName,"%s/%s.%s.%s.bin",sCachedBinaryPath,strippedName, deviceName,driverVersion );
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
if (clFileNameForCaching && !(disableBinaryCaching || gDebugSkipLoadingBinary||gDebugForceLoadingFromSource) )
|
||||||
|
{
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
sprintf_s(binaryFileName,B3_MAX_STRING_LENGTH,"cache/%s.%s.%s.bin",strippedName, deviceName,driverVersion );
|
char* bla=0;
|
||||||
#else
|
|
||||||
sprintf(binaryFileName,"cache/%s.%s.%s.bin",strippedName, deviceName,driverVersion );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
//printf("searching for %s\n", binaryFileName);
|
//printf("searching for %s\n", binaryFileName);
|
||||||
|
|
||||||
|
|
||||||
|
FILETIME modtimeBinary;
|
||||||
|
CreateDirectory(sCachedBinaryPath,0);
|
||||||
CreateDirectory("cache",0);
|
|
||||||
{
|
{
|
||||||
|
|
||||||
HANDLE binaryFileHandle = CreateFile(binaryFileName,GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
|
HANDLE binaryFileHandle = CreateFile(binaryFileName,GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
|
||||||
@@ -735,6 +747,23 @@ cl_program b3OpenCLUtils_compileCLProgramFromString(cl_context clContext, cl_dev
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#else
|
||||||
|
fileUpToDate = true;
|
||||||
|
#ifdef __APPLE__
|
||||||
|
if (mkdir(sCachedBinaryPath,0777) == -1)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
b3Printf("Succesfully created cache directory: %s\n", sCachedBinaryPath);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif //_WIN32
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if( fileUpToDate)
|
if( fileUpToDate)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -774,13 +803,28 @@ cl_program b3OpenCLUtils_compileCLProgramFromString(cl_context clContext, cl_dev
|
|||||||
free (build_log);
|
free (build_log);
|
||||||
b3Assert(0);
|
b3Assert(0);
|
||||||
m_cpProgram = 0;
|
m_cpProgram = 0;
|
||||||
|
|
||||||
|
b3Warning("clBuildProgram reported failure on cached binary: %s\n",binaryFileName);
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
b3Printf("clBuildProgram successfully compiled cached binary: %s\n",binaryFileName);
|
||||||
}
|
}
|
||||||
free (binary);
|
free (binary);
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
b3Warning("Cannot open cached binary: %s\n",binaryFileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
#endif //_WIN32
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (!m_cpProgram)
|
if (!m_cpProgram)
|
||||||
{
|
{
|
||||||
@@ -875,7 +919,6 @@ cl_program b3OpenCLUtils_compileCLProgramFromString(cl_context clContext, cl_dev
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
|
|
||||||
if( clFileNameForCaching )
|
if( clFileNameForCaching )
|
||||||
{ // write to binary
|
{ // write to binary
|
||||||
@@ -918,7 +961,6 @@ cl_program b3OpenCLUtils_compileCLProgramFromString(cl_context clContext, cl_dev
|
|||||||
free (binary);
|
free (binary);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif //_WIN32
|
|
||||||
|
|
||||||
free(compileFlags);
|
free(compileFlags);
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,9 @@ void b3OpenCLUtils_printPlatformInfo(cl_platform_id platform);
|
|||||||
|
|
||||||
const char* b3OpenCLUtils_getSdkVendorName();
|
const char* b3OpenCLUtils_getSdkVendorName();
|
||||||
|
|
||||||
|
///set the path (directory/folder) where the compiled OpenCL kernel are stored
|
||||||
|
void b3OpenCLUtils_setCachePath(const char* path);
|
||||||
|
|
||||||
cl_context b3OpenCLUtils_createContextFromPlatform(cl_platform_id platform, cl_device_type deviceType, cl_int* pErrNum, void* pGLCtx , void* pGLDC ,int preferredDeviceIndex , int preferredPlatformIndex);
|
cl_context b3OpenCLUtils_createContextFromPlatform(cl_platform_id platform, cl_device_type deviceType, cl_int* pErrNum, void* pGLCtx , void* pGLDC ,int preferredDeviceIndex , int preferredPlatformIndex);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@@ -173,6 +176,10 @@ struct b3OpenCLUtils
|
|||||||
{
|
{
|
||||||
return b3OpenCLUtils_createContextFromPlatform(platform, deviceType, pErrNum, pGLCtx,pGLDC,preferredDeviceIndex, preferredPlatformIndex);
|
return b3OpenCLUtils_createContextFromPlatform(platform, deviceType, pErrNum, pGLCtx,pGLDC,preferredDeviceIndex, preferredPlatformIndex);
|
||||||
}
|
}
|
||||||
|
static void setCachePath(const char* path)
|
||||||
|
{
|
||||||
|
b3OpenCLUtils_setCachePath(path);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //__cplusplus
|
#endif //__cplusplus
|
||||||
|
|||||||
Reference in New Issue
Block a user