Skip to content

Instantly share code, notes, and snippets.

@kimhunter
Last active December 20, 2015 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimhunter/6049958 to your computer and use it in GitHub Desktop.
Save kimhunter/6049958 to your computer and use it in GitHub Desktop.
Test if any OpenCL GPU device is available.
//
// main.cpp
// testSeeEl
//
// Created by Kim on 21/07/2013.
// Copyright (c) 2013 Kim. All rights reserved.
//
//
// clang -framework OpenCL -o testSeeEl testSeeEl.c && ./testSeeEl
//
#include <stdio.h>
#include <OpenCL/OpenCL.h>
int main(int argc, const char * argv[])
{
cl_int errNum;
cl_uint numPlatforms;
cl_platform_id *platformIds;
// First, query the total number of platforms
errNum = clGetPlatformIDs(0, NULL, &numPlatforms);
if (errNum != CL_SUCCESS || numPlatforms <= 0)
{
printf("Failed to find any OpenCL platforms.\n");
return 1;
}
platformIds = (cl_platform_id *)alloca(sizeof(cl_platform_id) * numPlatforms);
errNum = clGetPlatformIDs(numPlatforms, platformIds, NULL);
if (errNum != CL_SUCCESS)
{
printf("Failed to find any OpenCL platforms.\n");
return 1;
}
for (cl_uint i = 0; i < numPlatforms; i++)
{
cl_uint numDevices;
// CL_DEVICE_TYPE_GPU will fail if none, so se if we can get all first
errNum = clGetDeviceIDs(platformIds[i], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);
if (errNum != CL_SUCCESS)
{
printf("Failed to get device info\n");
return 1;
}
// will fail if 0
errNum = clGetDeviceIDs(platformIds[i], CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
if (errNum != CL_SUCCESS || numDevices < 1)
{
printf("No GPU device found for platform\n");
return 0;
}
printf("%d GPU device(s) found\n", numDevices);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment