Petr Schreiber
21-09-2012, 22:23
The second strange new word from OpenCL world is "device". We already learned (http://www.thinbasic.com/community/showthread.php?11863-Chapter-2-example-1-Testing-platform-extensions) there can be 1 or more OpenCL platforms on your PC.
Each of them can have 1 or more devices.
Device is the actual hardware the OpenCL C code runs on.
If you have 2 GPUs from same vendor on your PC, you have 1 platform but 2 devices.
OpenCL allows you to choose which device(s) you want to use for calculation - you can pick one or all.
The example code below lists device names, types and extensions.
Comparing to the book original, I added going through devices for each platform and added their name and type (GPU/CPU) with OpenCL version support.
So what I learn?
How to enumerate OpenCL devices for 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, with name + types of devices
Uses "Console"
#INCLUDE "%APP_INCLUDEPATH%/cl/cl.tBasicU"
Function TBMain()
/* Host/device data structures */
tcl_platform_id platforms()
tcl_platform_id platform
tcl_uint num_platforms
tcl_device_id devices()
tcl_uint num_devices, addr_data
tcl_int i, ErrCl, p
tcl_device_type deviceType
/* Extension data */
String name_data = Repeat$(48, " ")
String ext_data = Repeat$(4096, " ")
String deviceTypeDescriptor
/* 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
Dim versionCL As Asciiz * 64
For p = 1 To num_platforms
platform = platforms(p)
clGetPlatformInfo(platform,
CL_PLATFORM_NAME, sizeof(szName), ByVal VarPtr(szName), ByVal NULL)
PrintL "List of devices for platform #"+Format$(p) + ": " + szName
/* Determine number of connected devices */
ErrCl = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, ByVal NULL, num_devices)
If (ErrCl < 0) Or num_devices = 0 Then
pError("Couldn't find any devices")
Else
/* Access connected devices */
ReDim devices(num_devices)
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL,
num_devices, devices, ByVal NULL)
/* Obtain data for each connected device */
For i = 1 To num_devices
ErrCl = clGetDeviceInfo(devices(i), CL_DEVICE_NAME,
Len(name_data), ByVal StrPtr(name_data), ByVal NULL)
If (ErrCl < 0) Then
pError("Couldn't read extension data")
APP_SetReturnCode(1) : WaitKey : Exit Function
End If
ErrCl = clGetDeviceInfo(devices(i), CL_DEVICE_VERSION,
SizeOf(versionCL), ByVal StrPtr(versionCL), ByVal NULL)
clGetDeviceInfo(devices(i), CL_DEVICE_ADDRESS_BITS,
SizeOf(ext_data), addr_data, ByVal NULL)
clGetDeviceInfo(devices(i), CL_DEVICE_TYPE,
SizeOf(deviceType), ByVal VarPtr(deviceType), ByVal NULL)
deviceTypeDescriptor = ""
If Bit_Get(deviceType, LogB(2, CL_DEVICE_TYPE_DEFAULT)) Then deviceTypeDescriptor += "DEFAULT "
If Bit_Get(deviceType, LogB(2, CL_DEVICE_TYPE_CPU)) Then deviceTypeDescriptor += "CPU "
If Bit_Get(deviceType, LogB(2, CL_DEVICE_TYPE_GPU)) Then deviceTypeDescriptor += "GPU "
If Bit_Get(deviceType, LogB(2, CL_DEVICE_TYPE_ACCELERATOR)) Then deviceTypeDescriptor += "ACCELERATOR "
clGetDeviceInfo(devices(i), CL_DEVICE_EXTENSIONS,
Len(ext_data), ByVal StrPtr(ext_data), ByVal NULL)
PrintL StrFormat$("NAME: {1}"+$CRLF+"TYPE: {2} ({3})"+$CRLF+"ADDRESS_WIDTH: {4}"+$CRLF+"EXTENSIONS: {5}",
Trim$(name_data), deviceTypeDescriptor, versionCL, Trim$(addr_data), $CRLF+Replace$(TrimFull$(ext_data), " ", $CRLF))
PrintL
Next
End If
PrintL
Next
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
Each of them can have 1 or more devices.
Device is the actual hardware the OpenCL C code runs on.
If you have 2 GPUs from same vendor on your PC, you have 1 platform but 2 devices.
OpenCL allows you to choose which device(s) you want to use for calculation - you can pick one or all.
The example code below lists device names, types and extensions.
Comparing to the book original, I added going through devices for each platform and added their name and type (GPU/CPU) with OpenCL version support.
So what I learn?
How to enumerate OpenCL devices for 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, with name + types of devices
Uses "Console"
#INCLUDE "%APP_INCLUDEPATH%/cl/cl.tBasicU"
Function TBMain()
/* Host/device data structures */
tcl_platform_id platforms()
tcl_platform_id platform
tcl_uint num_platforms
tcl_device_id devices()
tcl_uint num_devices, addr_data
tcl_int i, ErrCl, p
tcl_device_type deviceType
/* Extension data */
String name_data = Repeat$(48, " ")
String ext_data = Repeat$(4096, " ")
String deviceTypeDescriptor
/* 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
Dim versionCL As Asciiz * 64
For p = 1 To num_platforms
platform = platforms(p)
clGetPlatformInfo(platform,
CL_PLATFORM_NAME, sizeof(szName), ByVal VarPtr(szName), ByVal NULL)
PrintL "List of devices for platform #"+Format$(p) + ": " + szName
/* Determine number of connected devices */
ErrCl = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, ByVal NULL, num_devices)
If (ErrCl < 0) Or num_devices = 0 Then
pError("Couldn't find any devices")
Else
/* Access connected devices */
ReDim devices(num_devices)
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL,
num_devices, devices, ByVal NULL)
/* Obtain data for each connected device */
For i = 1 To num_devices
ErrCl = clGetDeviceInfo(devices(i), CL_DEVICE_NAME,
Len(name_data), ByVal StrPtr(name_data), ByVal NULL)
If (ErrCl < 0) Then
pError("Couldn't read extension data")
APP_SetReturnCode(1) : WaitKey : Exit Function
End If
ErrCl = clGetDeviceInfo(devices(i), CL_DEVICE_VERSION,
SizeOf(versionCL), ByVal StrPtr(versionCL), ByVal NULL)
clGetDeviceInfo(devices(i), CL_DEVICE_ADDRESS_BITS,
SizeOf(ext_data), addr_data, ByVal NULL)
clGetDeviceInfo(devices(i), CL_DEVICE_TYPE,
SizeOf(deviceType), ByVal VarPtr(deviceType), ByVal NULL)
deviceTypeDescriptor = ""
If Bit_Get(deviceType, LogB(2, CL_DEVICE_TYPE_DEFAULT)) Then deviceTypeDescriptor += "DEFAULT "
If Bit_Get(deviceType, LogB(2, CL_DEVICE_TYPE_CPU)) Then deviceTypeDescriptor += "CPU "
If Bit_Get(deviceType, LogB(2, CL_DEVICE_TYPE_GPU)) Then deviceTypeDescriptor += "GPU "
If Bit_Get(deviceType, LogB(2, CL_DEVICE_TYPE_ACCELERATOR)) Then deviceTypeDescriptor += "ACCELERATOR "
clGetDeviceInfo(devices(i), CL_DEVICE_EXTENSIONS,
Len(ext_data), ByVal StrPtr(ext_data), ByVal NULL)
PrintL StrFormat$("NAME: {1}"+$CRLF+"TYPE: {2} ({3})"+$CRLF+"ADDRESS_WIDTH: {4}"+$CRLF+"EXTENSIONS: {5}",
Trim$(name_data), deviceTypeDescriptor, versionCL, Trim$(addr_data), $CRLF+Replace$(TrimFull$(ext_data), " ", $CRLF))
PrintL
Next
End If
PrintL
Next
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