Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,023 questions

51,974 answers

573 users

How to get a list of OpenCL devices in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>

//#define CL_USE_DEPRECATED_OPENCL_2_0_APIS

#include "opencl.h"
//#include "CL/cl.h"


int main() {
    // get all platforms
    cl_uint totalPlatforms;
    cl_platform_id* platforms = NULL;

    clGetPlatformIDs(0, NULL, &totalPlatforms);
    platforms = (cl_platform_id*)malloc(sizeof(cl_platform_id) * totalPlatforms);
    clGetPlatformIDs(totalPlatforms, platforms, NULL);

    char* buf = NULL;
    size_t bufSize;
    cl_uint totalDevices;
    cl_device_id* devices = NULL;;
    cl_uint totalComputeUnits;

    for (unsigned int i = 0; i < totalPlatforms; i++) {
        // get all devices
        clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &totalDevices);
        devices = (cl_device_id*)malloc(sizeof(cl_device_id) * totalDevices);
        clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, totalDevices, devices, NULL);

        for (unsigned int j = 0; j < totalDevices; j++) {
            // print device name
            clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 0, NULL, &bufSize);
            buf = (char*)malloc(bufSize);
            clGetDeviceInfo(devices[j], CL_DEVICE_NAME, bufSize, buf, NULL);
            printf("Device: %s\n", buf);
            free(buf);

            // print device hardware version
            clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, 0, NULL, &bufSize);
            buf = (char*)malloc(bufSize);
            clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, bufSize, buf, NULL);
            printf("Device hardware version: %s\n", buf);
            free(buf);

            // print software driver version
            clGetDeviceInfo(devices[j], CL_DRIVER_VERSION, 0, NULL, &bufSize);
            buf = (char*)malloc(bufSize);
            clGetDeviceInfo(devices[j], CL_DRIVER_VERSION, bufSize, buf, NULL);
            printf("Software driver version: %s\n", buf);
            free(buf);

            // print device c version supported
            clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, 0, NULL, &bufSize);
            buf = (char*)malloc(bufSize);
            clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, bufSize, buf, NULL);
            printf("OpenCL C version supported: %s\n", buf);
            free(buf);

            // print parallel compute units
            clGetDeviceInfo(devices[j], CL_DEVICE_MAX_COMPUTE_UNITS, 
                            sizeof(totalComputeUnits), 
                            &totalComputeUnits, NULL);
            printf("Parallel compute units: %d\n", totalComputeUnits);

        }
        free(devices);
    }
    free(platforms);

    char ch = getchar();

    return 0;
}



/*
run:

Device: NVIDIA GeForce GTX 1050 Ti
Device hardware version: OpenCL 3.0 CUDA
Software driver version: 512.15
OpenCL C version supported: OpenCL C 1.2
Parallel compute units: 6

*/

 



answered Mar 26, 2022 by avibootz
edited Mar 26, 2022 by avibootz
...