View Full Version : Chapter 2, example 2: Testing device extensions

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"


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)

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")


/* 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))

End If

PrintL "Press any key to continue..."

End Function

Function pError( sError As String )

PrintL sError

End Function

22-09-2012, 06:28
I am unable to find the file "OpenCL.DLL" on my computer.
Can my computer use a version of an existing file?
If so, where can I download it?

Here is attachment of my computer's configuration:

Petr Schreiber
22-09-2012, 14:05

your hardware should be strong enough. Can you find atiocl.dll on your PC?
If not, you could consider downloading AMD APP SDK (http://developer.amd.com/tools/hc/AMDAPPSDK/Pages/default.aspx). Here is direct link to 32bit version:

It should contain opencl.dll or atiocl.dll. I remember I used it 3 years ago, now I have Intel and NVIDIA based system, so cannot test.

If I remember correctly ATi deprecated the support for OpenCL on Radeon HD 4xxx series (which you have), but you still should be able to run it on your CPU.

I think I took atiocl.dll, renamed it to opencl.dll and moved to the same directory as thinbasic.exe is located, but I am not sure if this step is still necessary, let me know how it went!


23-09-2012, 00:46
Thanks for the new example and explanations Petr.