An OpenCL.NET Program for Device Information

There was an interesting question on Gamedev.net today asking about whether there is an API in C# for querying information about the GPUs installed. Apparently, GPU-Z uses the CUDA API to get information about an NVIDIA GPU, and GPUPerfAPI for the AMD GPU. However, a good alternative to those APIs is OpenCL, a platform independent framework to gather information about the computing devices in a PC. For C#, OpenCL.NET works quite well.

The following program queries and outputs the device information from OpenCL. It comes in handy when you need to know some basic properties of the computer you are using when you don’t want to use GPU-Z or CPU-Z.

using System;
using System.Diagnostics;
using System.Linq;
using OpenCL.Net.Extensions;
using OpenCL.Net;

namespace Opencl_Device_Query
{
    class Program
    {
        static void Main(string[] args)
        {
            // See https://www.khronos.org/registry/cl/specs/opencl-2.1.pdf for
            // more information on device query.

            ErrorCode e1;
            Platform[] platforms = Cl.GetPlatformIDs(out e1);
            foreach (Platform p in platforms)
            {
                ErrorCode e2;

                InfoBuffer ib2 = Cl.GetPlatformInfo(p, PlatformInfo.Name, out e2);
                char[] platform_name = ib2.CastToArray<char>(ib2.Size);
                System.Console.WriteLine("platform_name:            " + new String(platform_name));

                ib2 = Cl.GetPlatformInfo(p, PlatformInfo.Vendor, out e2);
                char[] platform_vendor = ib2.CastToArray<char>(ib2.Size);
                System.Console.WriteLine("platform_vendor:          " + new String(platform_vendor));

                ib2 = Cl.GetPlatformInfo(p, PlatformInfo.Version, out e2);
                char[] platform_version = ib2.CastToArray<char>(ib2.Size);
                System.Console.WriteLine("platform_version:         " + new String(platform_version));

                ib2 = Cl.GetPlatformInfo(p, PlatformInfo.Extensions, out e2);
                char[] platform_extensions = ib2.CastToArray<char>(ib2.Size);
                System.Console.WriteLine("platform_extensions:      " + new String(platform_extensions));

                DeviceType dt = DeviceType.Default;
                Device[] devices = Cl.GetDeviceIDs(p, dt, out e2);
                foreach (Device d in devices)
                {
                    ErrorCode e3;
                    InfoBuffer ib = Cl.GetDeviceInfo(d, DeviceInfo.Type, out e3);
                    DeviceType device_type = ib.CastTo<DeviceType>();
                    System.Console.WriteLine("device_type:              " + device_type);

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.Vendor, out e3);
                    char[] device_vendor = ib.CastToArray<char>(ib.Size);
                    System.Console.WriteLine("device_vendor:            " + new String(device_vendor));

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.VendorId, out e3);
                    int device_vendor_id = ib.CastTo<int>();
                    System.Console.WriteLine("device_vendor_id:         " + device_vendor_id);

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.Version, out e3);
                    int device_version = ib.CastTo<int>();
                    System.Console.WriteLine("device_version:           " + device_version);

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.Name, out e3);
                    char[] device_name = ib.CastToArray<char>(ib.Size);
                    System.Console.WriteLine("device_name:              " + new String(device_name));

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.DriverVersion, out e3);
                    char[] device_driver_version = ib.CastToArray<char>(ib.Size);
                    System.Console.WriteLine("device_driver_version:    " + new String(device_driver_version));

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.Profile, out e3);
                    char[] device_profile = ib.CastToArray<char>(ib.Size);
                    System.Console.WriteLine("device_profile:           " + new String(device_profile));

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.CompilerAvailable, out e3);
                    bool device_compiler_available = ib.CastTo<bool>();
                    System.Console.WriteLine("device_compiler_available:" + device_compiler_available);

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.AddressBits, out e3);
                    uint address_bits = ib.CastTo<uint>();
                    System.Console.WriteLine("address_bits:             " + address_bits);

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.MaxComputeUnits, out e3);
                    uint max_compute_units = ib.CastTo<uint>();
                    System.Console.WriteLine("max_compute_units:        " + max_compute_units);

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.Extensions, out e3);
                    char[] device_extensions = ib.CastToArray<char>(ib.Size);
                    System.Console.WriteLine("device_extensions:        " + new String(device_extensions));

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.MaxMemAllocSize, out e3);
                    ulong max_mem_alloc_size = ib.CastTo<ulong>();
                    System.Console.WriteLine("max_mem_alloc_size:       " + max_mem_alloc_size);

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.GlobalMemSize, out e3);
                    ulong global_mem_size = ib.CastTo<ulong>();
                    System.Console.WriteLine("global_mem_size:          " + global_mem_size);

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.LocalMemSize, out e3);
                    ulong local_mem_size = ib.CastTo<ulong>();
                    System.Console.WriteLine("local_mem_size:           " + local_mem_size);

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.EndianLittle, out e3);
                    bool endian_little = ib.CastTo<bool>();
                    System.Console.WriteLine("endian_little:            " + endian_little);

                    ib = Cl.GetDeviceInfo(d, DeviceInfo.Available, out e3);
                    bool device_available = ib.CastTo<bool>();
                    System.Console.WriteLine("device_available:         " + device_available);

                    System.Console.WriteLine();
                    System.Console.WriteLine();
                    System.Console.WriteLine();
                }
            }
        }
    }
}


Posted in Tip