Petr Schreiber
19-09-2012, 08:42
The first strange new word from OpenCL world is "platform".
Platform is set of OpenCL runtimes provided by each hardware vendor.
For example, on my PC I have installed NVIDIA GeForce card and Intel Core 2 Duo CPU. Both NVIDIA and Intel support OpenCL, so after installing drivers for GPU and SDK for CPU I have two platforms on my PC.
When doing calculation, you are free to choose which platform to use for your computations. The first step is to get to know, what is installed, and that is what the following example gives you info about.
OpenCL has strictly defined functionality, but each hardware vendor can provide extensions to it. The example lists names of extensions as well, what they are good for you don't need to worry as of now, we will look into that later.
Comparing to the book original, I added platform name retrieving routines to the example, to make it more clear "what is what"
So what I learn?
How to enumerate OpenCL platforms on your PC.
So what I need to run it?
You will need the latest ThinBASIC and OpenCL headers (http://www.thinbasic.com/community/showthread.php?10159-OpenCL-Headers-Updated-Sep-15-2011) to run it + of course modern GPU or CPU (http://www.thinbasic.com/community/showthread.php?10161-OpenCL-Supported-hardware).
Code listing (full code in attachement!)
' -- NOTE: Enhanced to list the info for all installed platforms, which have name info added
Uses "Console"
#INCLUDE "%APP_INCLUDEPATH%/cl/cl.tBasicU"
Function TBMain()
/* Host data structures */
tcl_platform_id platforms()
tcl_uint num_platforms
tcl_int i, ErrCL, platform_index = -1
/* Extension data */
String ext_data
tSize ext_size
String icd_ext = "cl_khr_icd"
/* Find number of platforms */
ErrCL = clGetPlatformIDs(1, ByVal NULL, num_platforms)
If (ErrCL < 0) Then
pError("Couldn't find any platforms.")
APP_SetReturnCode(1) : WaitKey : Exit Function
End If
/* Access all installed platforms */
ReDim platforms(num_platforms)
clGetPlatformIDs(num_platforms, platforms, ByVal NULL)
/* Platform name */
Dim szName As Asciiz * 512
/* Find extensions of all platforms */
For i = 1 To num_platforms
clGetPlatformInfo(platforms(i),
CL_PLATFORM_NAME, 512, ByVal VarPtr(szName), ByVal NULL)
PrintL "Platform #"+Format$(i) + ": " + szName
/* Find size of extension data */
ErrCL = clGetPlatformInfo(platforms(i),
CL_PLATFORM_EXTENSIONS, 0, ByVal NULL, ext_size)
If (ErrCL < 0) Then
pError("Couldn't read extension data.")
''APP_SetReturnCode(1) : WaitKey : Exit Function
Else
/* Access extension data */
ext_data = Repeat$(ext_size, $NUL)
clGetPlatformInfo(platforms(i), CL_PLATFORM_EXTENSIONS,
ext_size, ByVal StrPtr(ext_data), ByVal NULL)
PrintL StrFormat$("Supported extensions: {1}", $CRLF+Replace$(ext_data, " ", $CRLF))
ext_data = ""
End If
Next
PrintL
PrintL "Press any key to continue..."
WaitKey
APP_SetReturnCode(0)
End Function
Function pError( sError As String )
Console_SetTextAttribute(%CONSOLE_FOREGROUND_RED | %CONSOLE_FOREGROUND_INTENSITY)
PrintL sError
Console_SetTextAttribute(%CONSOLE_FOREGROUND_RED | %CONSOLE_FOREGROUND_GREEN | %CONSOLE_FOREGROUND_BLUE)
End Function
Platform is set of OpenCL runtimes provided by each hardware vendor.
For example, on my PC I have installed NVIDIA GeForce card and Intel Core 2 Duo CPU. Both NVIDIA and Intel support OpenCL, so after installing drivers for GPU and SDK for CPU I have two platforms on my PC.
When doing calculation, you are free to choose which platform to use for your computations. The first step is to get to know, what is installed, and that is what the following example gives you info about.
OpenCL has strictly defined functionality, but each hardware vendor can provide extensions to it. The example lists names of extensions as well, what they are good for you don't need to worry as of now, we will look into that later.
Comparing to the book original, I added platform name retrieving routines to the example, to make it more clear "what is what"
So what I learn?
How to enumerate OpenCL platforms on your PC.
So what I need to run it?
You will need the latest ThinBASIC and OpenCL headers (http://www.thinbasic.com/community/showthread.php?10159-OpenCL-Headers-Updated-Sep-15-2011) to run it + of course modern GPU or CPU (http://www.thinbasic.com/community/showthread.php?10161-OpenCL-Supported-hardware).
Code listing (full code in attachement!)
' -- NOTE: Enhanced to list the info for all installed platforms, which have name info added
Uses "Console"
#INCLUDE "%APP_INCLUDEPATH%/cl/cl.tBasicU"
Function TBMain()
/* Host data structures */
tcl_platform_id platforms()
tcl_uint num_platforms
tcl_int i, ErrCL, platform_index = -1
/* Extension data */
String ext_data
tSize ext_size
String icd_ext = "cl_khr_icd"
/* Find number of platforms */
ErrCL = clGetPlatformIDs(1, ByVal NULL, num_platforms)
If (ErrCL < 0) Then
pError("Couldn't find any platforms.")
APP_SetReturnCode(1) : WaitKey : Exit Function
End If
/* Access all installed platforms */
ReDim platforms(num_platforms)
clGetPlatformIDs(num_platforms, platforms, ByVal NULL)
/* Platform name */
Dim szName As Asciiz * 512
/* Find extensions of all platforms */
For i = 1 To num_platforms
clGetPlatformInfo(platforms(i),
CL_PLATFORM_NAME, 512, ByVal VarPtr(szName), ByVal NULL)
PrintL "Platform #"+Format$(i) + ": " + szName
/* Find size of extension data */
ErrCL = clGetPlatformInfo(platforms(i),
CL_PLATFORM_EXTENSIONS, 0, ByVal NULL, ext_size)
If (ErrCL < 0) Then
pError("Couldn't read extension data.")
''APP_SetReturnCode(1) : WaitKey : Exit Function
Else
/* Access extension data */
ext_data = Repeat$(ext_size, $NUL)
clGetPlatformInfo(platforms(i), CL_PLATFORM_EXTENSIONS,
ext_size, ByVal StrPtr(ext_data), ByVal NULL)
PrintL StrFormat$("Supported extensions: {1}", $CRLF+Replace$(ext_data, " ", $CRLF))
ext_data = ""
End If
Next
PrintL
PrintL "Press any key to continue..."
WaitKey
APP_SetReturnCode(0)
End Function
Function pError( sError As String )
Console_SetTextAttribute(%CONSOLE_FOREGROUND_RED | %CONSOLE_FOREGROUND_INTENSITY)
PrintL sError
Console_SetTextAttribute(%CONSOLE_FOREGROUND_RED | %CONSOLE_FOREGROUND_GREEN | %CONSOLE_FOREGROUND_BLUE)
End Function