mfaieghi
mfaieghi

Reputation: 610

Accessing platforms and devices in AMD OpenCL platform

I'm trying to run my first codes in OpenCL. I wrote the following program to detect the available platforms and devices in my computer. The code works fine in my computer which has Intel CPU and NVIDIA GPU. It detects every platform and device correctly. But, when I run it on a computer with AMD-FX 770K and Radeon R7 240, the output is as the figure shown below. At least, it must show the Radeon R7 240 GPU as a device in this platform, but it doesn't. Any idea why the code is not working properly on AMD platform?

enter image description here

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <CL/cl.h>

int main(){

cl_int err, i, j;
cl_platform_id *platforms;
cl_device_id *devices;
cl_uint num_platforms, num_devices;
size_t plat_name_size, devi_name_size;
char *plat_name_data, *devi_name_data;

err = clGetPlatformIDs(1, NULL, &num_platforms);
if (err < 0){
    perror("No platform is found");
    exit(1);
}
platforms = (cl_platform_id*)malloc(sizeof(cl_platform_id)*num_platforms);
clGetPlatformIDs(num_platforms, platforms, NULL);

printf("Number of found platforms is %d\n ", num_platforms);

for (i = 0; i < num_platforms; i++){

    err = clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 0, NULL, &plat_name_size);
    if (err < 0){
        perror("Couldn't read platform name.");
        exit(1);
    }
    plat_name_data = (char*)malloc(plat_name_size);
    clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, plat_name_size, plat_name_data, NULL);
    printf("Platform No.%d is: %s\n", i, plat_name_data);
    free(plat_name_data);

    err = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 1, NULL, &num_devices);
    if (err < 0){
        perror("No device is found in this platform");
        exit(1);
    }
    devices = (cl_device_id*)malloc(sizeof(cl_device_id)*(num_devices));
    clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
    printf("Number of devices found in this platform is: %d\n", num_devices);
    for (j = 0; j < num_devices; j++){
        err = clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 0, NULL, &devi_name_size);
        if (err < 0){
            perror("Couldn't read the device name.");
            exit(1);
        }
        devi_name_data = (char*)malloc(devi_name_size);
        clGetDeviceInfo(devices[j], CL_DEVICE_NAME, devi_name_size, devi_name_data, NULL);
        printf("Device No.%d name is: %s\n", j, devi_name_data);
        free(devi_name_data);
    }

}
return 0;

}

Upvotes: 0

Views: 454

Answers (1)

ARK
ARK

Reputation: 699

Please set num_entries to "0" in both of below calls:

clGetPlatformIDs(0 /* num_entries */_, NULL, &num_platforms);

clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0 /* num_entries */, NULL, &num_devices);

Upvotes: 2

Related Questions