99#include < strings.h>
1010
1111#include " mythlogging.h"
12- #ifdef MAX
13- #undef MAX
14- #endif
15- #ifdef MIN
16- #undef MIN
17- #endif
18- #include < oclUtils.h>
12+
13+ #include < CL/opencl.h>
1914#include " openclinterface.h"
2015#include " openglsupport.h"
2116
2217void openCLDisplayImageFormats (cl_context context);
2318void openCLPrintDevInfo (cl_device_id device);
19+ cl_int openCLGetPlatformID (cl_platform_id* clSelectedPlatformID);
2420
2521cl_platform_id clSelectedPlatformID = NULL ;
2622
@@ -31,11 +27,11 @@ OpenCLDeviceMap::OpenCLDeviceMap(void)
3127 m_valid = false ;
3228
3329 // Get OpenCL platform ID for NVIDIA if avaiable, otherwise default
34- cl_int ciErrNum = oclGetPlatformID (&clSelectedPlatformID);
30+ cl_int ciErrNum = openCLGetPlatformID (&clSelectedPlatformID);
3531 if (ciErrNum != CL_SUCCESS)
3632 {
3733 LOG (VB_GENERAL, LOG_ERR, QString (" OpenCL Error # %1 (%2)" )
38- .arg (ciErrNum) .arg (oclErrorString (ciErrNum)));
34+ .arg (ciErrNum) .arg (openCLErrorString (ciErrNum)));
3935 return ;
4036 }
4137
@@ -63,10 +59,6 @@ OpenCLDeviceMap::OpenCLDeviceMap(void)
6359
6460 LOG (VB_GENERAL, LOG_INFO, QString (" OpenCL Version: %1" ) .arg (cBuffer));
6561
66- // Log OpenCL SDK Revision #
67- LOG (VB_GENERAL, LOG_INFO, QString (" OpenCL SDK Revision: %1" )
68- .arg (OCL_SDKREVISION));
69-
7062 // Get and log OpenCL device info
7163 cl_uint ciDeviceCount;
7264 cl_device_id *devices;
@@ -152,10 +144,10 @@ void openCLDisplayImageFormats(cl_context context)
152144 {
153145 LOG (VB_GENERAL, LOG_INFO, QString (" %1%2%3" )
154146 .arg (i + 1 , -6 )
155- .arg (oclImageFormatString (ImageFormats[i].
156- image_channel_order), -16 )
157- .arg (oclImageFormatString (ImageFormats[i].
158- image_channel_data_type), -22 ));
147+ .arg (openCLImageFormatString (ImageFormats[i].
148+ image_channel_order), -16 )
149+ .arg (openCLImageFormatString (ImageFormats[i].
150+ image_channel_data_type), -22 ));
159151 }
160152 delete [] ImageFormats;
161153
@@ -180,10 +172,10 @@ void openCLDisplayImageFormats(cl_context context)
180172 {
181173 LOG (VB_GENERAL, LOG_INFO, QString (" %1%2%3" )
182174 .arg (i + 1 , -6 )
183- .arg (oclImageFormatString (ImageFormats[i].
184- image_channel_order), -16 )
185- .arg (oclImageFormatString (ImageFormats[i].
186- image_channel_data_type), -22 ));
175+ .arg (openCLImageFormatString (ImageFormats[i].
176+ image_channel_order), -16 )
177+ .arg (openCLImageFormatString (ImageFormats[i].
178+ image_channel_data_type), -22 ));
187179 }
188180 delete [] ImageFormats;
189181}
@@ -689,6 +681,23 @@ void OpenCLDeviceNvidiaSpecific::Display(void)
689681 .arg (m_integratedMem ? " yes" : " no" ));
690682}
691683
684+ // Adapted from the NVIDIA GPY Computing SDK shrUtils.h
685+ int OpenCLDeviceNvidiaSpecific::ConvertSMVer2Cores (int major, int minor)
686+ {
687+ if (major == 1 && minor >= 0 && minor <= 3 )
688+ return 8 ;
689+
690+ if (major == 2 && minor == 0 )
691+ return 32 ;
692+
693+ if (major == 2 && minor == 1 )
694+ return 48 ;
695+
696+ LOG (VB_GENERAL, LOG_ERR, QString (" Unknown SM version %d.%d!" )
697+ .arg (major) .arg (minor));
698+ return -1 ;
699+ }
700+
692701OpenCLKernel *OpenCLKernel::Create (OpenCLDevice *device, QString entry,
693702 QString filename)
694703{
@@ -742,7 +751,7 @@ OpenCLKernel *OpenCLKernel::Create(OpenCLDevice *device, QString entry,
742751 if (!kernel)
743752 {
744753 LOG (VB_GENERAL, LOG_ERR, QString (" Error creating kernel %1: %2 (%3)" )
745- .arg (entryPt) .arg (ciErrNum) .arg (oclErrorString (ciErrNum)));
754+ .arg (entryPt) .arg (ciErrNum) .arg (openCLErrorString (ciErrNum)));
746755 clReleaseProgram (program);
747756 return NULL ;
748757 }
@@ -774,6 +783,181 @@ OpenCLBuffers::~OpenCLBuffers()
774783 delete [] m_bufs;
775784}
776785
786+ cl_int openCLGetPlatformID (cl_platform_id* clSelectedPlatformID)
787+ {
788+ char chBuffer[1024 ];
789+ cl_uint num_platforms;
790+ cl_platform_id* clPlatformIDs;
791+ cl_int ciErrNum;
792+ *clSelectedPlatformID = NULL ;
793+
794+ // Get OpenCL platform count
795+ ciErrNum = clGetPlatformIDs (0 , NULL , &num_platforms);
796+ if (ciErrNum != CL_SUCCESS)
797+ {
798+ LOG (VB_GENERAL, LOG_ERR, QString (" Error %1 in clGetPlatformIDs" )
799+ .arg (ciErrNum));
800+ return -1000 ;
801+ }
802+
803+ if (num_platforms == 0 )
804+ {
805+ LOG (VB_GENERAL, LOG_ERR, " No OpenCL platform found!" );
806+ return -2000 ;
807+ }
808+
809+ // if there's a platform or more, make space for ID's
810+ clPlatformIDs = new cl_platform_id[num_platforms];
811+ if (!clPlatformIDs)
812+ {
813+ LOG (VB_GENERAL, LOG_ERR,
814+ " Failed to allocate memory for cl_platform IDs!" );
815+ return -3000 ;
816+ }
817+
818+ // get platform info for each platform and trap the NVIDIA platform if found
819+ ciErrNum = clGetPlatformIDs (num_platforms, clPlatformIDs, NULL );
820+ for (cl_uint i = 0 ; i < num_platforms; ++i)
821+ {
822+ ciErrNum = clGetPlatformInfo (clPlatformIDs[i], CL_PLATFORM_NAME, 1024 ,
823+ &chBuffer, NULL );
824+ if (ciErrNum == CL_SUCCESS)
825+ {
826+ if (strstr (chBuffer, " NVIDIA" ) != NULL )
827+ {
828+ *clSelectedPlatformID = clPlatformIDs[i];
829+ break ;
830+ }
831+ }
832+ }
833+
834+ // default to zeroeth platform if NVIDIA not found
835+ if (*clSelectedPlatformID == NULL )
836+ {
837+ LOG (VB_GENERAL, LOG_WARNING, " NVIDIA OpenCL platform not found - "
838+ " defaulting to first platform!" );
839+ *clSelectedPlatformID = clPlatformIDs[0 ];
840+ }
841+
842+ delete [] clPlatformIDs;
843+ return CL_SUCCESS;
844+ }
845+
846+ const char *openCLErrorString (cl_int error)
847+ {
848+ static const char * errorString[] = {
849+ " CL_SUCCESS" ,
850+ " CL_DEVICE_NOT_FOUND" ,
851+ " CL_DEVICE_NOT_AVAILABLE" ,
852+ " CL_COMPILER_NOT_AVAILABLE" ,
853+ " CL_MEM_OBJECT_ALLOCATION_FAILURE" ,
854+ " CL_OUT_OF_RESOURCES" ,
855+ " CL_OUT_OF_HOST_MEMORY" ,
856+ " CL_PROFILING_INFO_NOT_AVAILABLE" ,
857+ " CL_MEM_COPY_OVERLAP" ,
858+ " CL_IMAGE_FORMAT_MISMATCH" ,
859+ " CL_IMAGE_FORMAT_NOT_SUPPORTED" ,
860+ " CL_BUILD_PROGRAM_FAILURE" ,
861+ " CL_MAP_FAILURE" ,
862+ " " ,
863+ " " ,
864+ " " ,
865+ " " ,
866+ " " ,
867+ " " ,
868+ " " ,
869+ " " ,
870+ " " ,
871+ " " ,
872+ " " ,
873+ " " ,
874+ " " ,
875+ " " ,
876+ " " ,
877+ " " ,
878+ " " ,
879+ " CL_INVALID_VALUE" ,
880+ " CL_INVALID_DEVICE_TYPE" ,
881+ " CL_INVALID_PLATFORM" ,
882+ " CL_INVALID_DEVICE" ,
883+ " CL_INVALID_CONTEXT" ,
884+ " CL_INVALID_QUEUE_PROPERTIES" ,
885+ " CL_INVALID_COMMAND_QUEUE" ,
886+ " CL_INVALID_HOST_PTR" ,
887+ " CL_INVALID_MEM_OBJECT" ,
888+ " CL_INVALID_IMAGE_FORMAT_DESCRIPTOR" ,
889+ " CL_INVALID_IMAGE_SIZE" ,
890+ " CL_INVALID_SAMPLER" ,
891+ " CL_INVALID_BINARY" ,
892+ " CL_INVALID_BUILD_OPTIONS" ,
893+ " CL_INVALID_PROGRAM" ,
894+ " CL_INVALID_PROGRAM_EXECUTABLE" ,
895+ " CL_INVALID_KERNEL_NAME" ,
896+ " CL_INVALID_KERNEL_DEFINITION" ,
897+ " CL_INVALID_KERNEL" ,
898+ " CL_INVALID_ARG_INDEX" ,
899+ " CL_INVALID_ARG_VALUE" ,
900+ " CL_INVALID_ARG_SIZE" ,
901+ " CL_INVALID_KERNEL_ARGS" ,
902+ " CL_INVALID_WORK_DIMENSION" ,
903+ " CL_INVALID_WORK_GROUP_SIZE" ,
904+ " CL_INVALID_WORK_ITEM_SIZE" ,
905+ " CL_INVALID_GLOBAL_OFFSET" ,
906+ " CL_INVALID_EVENT_WAIT_LIST" ,
907+ " CL_INVALID_EVENT" ,
908+ " CL_INVALID_OPERATION" ,
909+ " CL_INVALID_GL_OBJECT" ,
910+ " CL_INVALID_BUFFER_SIZE" ,
911+ " CL_INVALID_MIP_LEVEL" ,
912+ " CL_INVALID_GLOBAL_WORK_SIZE" ,
913+ };
914+
915+ const int errorCount = sizeof (errorString) / sizeof (errorString[0 ]);
916+
917+ const int index = -error;
918+
919+ return (index >= 0 && index < errorCount) ? errorString[index] :
920+ " Unspecified Error" ;
921+ }
922+
923+ const char *openCLImageFormatString (cl_uint uiImageFormat)
924+ {
925+ switch (uiImageFormat)
926+ {
927+ // cl_channel_order
928+ case CL_R: return " CL_R" ;
929+ case CL_A: return " CL_A" ;
930+ case CL_RG: return " CL_RG" ;
931+ case CL_RA: return " CL_RA" ;
932+ case CL_RGB: return " CL_RGB" ;
933+ case CL_RGBA: return " CL_RGBA" ;
934+ case CL_BGRA: return " CL_BGRA" ;
935+ case CL_ARGB: return " CL_ARGB" ;
936+ case CL_INTENSITY: return " CL_INTENSITY" ;
937+ case CL_LUMINANCE: return " CL_LUMINANCE" ;
938+
939+ // cl_channel_type
940+ case CL_SNORM_INT8: return " CL_SNORM_INT8" ;
941+ case CL_SNORM_INT16: return " CL_SNORM_INT16" ;
942+ case CL_UNORM_INT8: return " CL_UNORM_INT8" ;
943+ case CL_UNORM_INT16: return " CL_UNORM_INT16" ;
944+ case CL_UNORM_SHORT_565: return " CL_UNORM_SHORT_565" ;
945+ case CL_UNORM_SHORT_555: return " CL_UNORM_SHORT_555" ;
946+ case CL_UNORM_INT_101010: return " CL_UNORM_INT_101010" ;
947+ case CL_SIGNED_INT8: return " CL_SIGNED_INT8" ;
948+ case CL_SIGNED_INT16: return " CL_SIGNED_INT16" ;
949+ case CL_SIGNED_INT32: return " CL_SIGNED_INT32" ;
950+ case CL_UNSIGNED_INT8: return " CL_UNSIGNED_INT8" ;
951+ case CL_UNSIGNED_INT16: return " CL_UNSIGNED_INT16" ;
952+ case CL_UNSIGNED_INT32: return " CL_UNSIGNED_INT32" ;
953+ case CL_HALF_FLOAT: return " CL_HALF_FLOAT" ;
954+ case CL_FLOAT: return " CL_FLOAT" ;
955+
956+ // unknown constant
957+ default : return " Unknown" ;
958+ }
959+ }
960+
777961/*
778962 * vim:ts=4:sw=4:ai:et:si:sts=4
779963 */
0 commit comments