Last active
April 11, 2018 20:35
-
-
Save fabiovila/14c9aac9da9fc1e150faba45f6eb7900 to your computer and use it in GitHub Desktop.
C OpenCL example wich lists platforms and devices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <CL/cl.h> | |
#define BUFFERSIZE 1024 | |
#define MAX_PLATFORMS 8 | |
#define MAX_DEVICES 8 | |
//gcc -o opencl opencl-list.c -lOpenCL && ./opencl | |
int main(int argc, char** argv) | |
{ | |
char buffer[BUFFERSIZE]; | |
cl_int err; | |
cl_device_id device_id; | |
cl_device_id devices[MAX_DEVICES]; | |
cl_uint num; | |
cl_platform_id platforms[MAX_PLATFORMS]; | |
err = clGetPlatformIDs(MAX_PLATFORMS, platforms, &num); | |
printf ("Num Platforms: %d\n", num); | |
if (err != CL_SUCCESS) | |
{ | |
printf("Error: Failed getting platforms.\n"); | |
return EXIT_FAILURE; | |
} | |
// Search for platforms devices and set a default gpu device | |
for (int i = 0; i < num; i++){ | |
cl_uint n; | |
cl_device_type type; | |
clGetPlatformInfo(platforms[i],CL_PLATFORM_NAME,1024,buffer,NULL); | |
printf ("Platform name: %s\n", buffer ); | |
//clGetPlatformInfo(platforms[i],CL_PLATFORM_EXTENSIONS,1024,buffer,NULL); | |
//printf (" Platform Extensions: %s\n", buffer ); | |
err = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, MAX_DEVICES, devices, &n); | |
if (err != CL_SUCCESS) | |
{ | |
printf("Error: Failed to create a device group!\n"); | |
return EXIT_FAILURE; | |
} | |
for (int d = 0; d < n; d++){ | |
clGetDeviceInfo(devices[i],CL_DEVICE_NAME,1024,buffer,NULL); | |
printf (" Device Name: %s\n", buffer ); | |
clGetDeviceInfo(devices[i],CL_DEVICE_TYPE,1024,&type,NULL); | |
printf (" Device Type: %d\n", type ); | |
// Set device_id with the first GPU device found | |
if (type & CL_DEVICE_TYPE_GPU) { | |
device_id = devices[i]; | |
} | |
} | |
} | |
clGetDeviceInfo(device_id,CL_DEVICE_NAME,1024,buffer,NULL); | |
printf ("\nUsing Device Name: %s\n", buffer ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment