Skip to content

Instantly share code, notes, and snippets.

@dogukancagatay
Last active September 21, 2022 16:03
Show Gist options
  • Save dogukancagatay/8419284 to your computer and use it in GitHub Desktop.
Save dogukancagatay/8419284 to your computer and use it in GitHub Desktop.
List OpenCL devices on the system using OpenCL C++ Wrapper.
On Mac OS X 10.9 Maverics I couldn't get C++ OpenCL code run. I guess OpenCL on Mac OS X doens't have C++ Wrapper, so I manually added the wrapper header to fix the issue. I followed the steps below to make it work.
- Download cl.hpp from http://www.khronos.org/registry/cl/api/1.1/cl.hpp
- Move the downloaded cl.hpp file inside /System/Library/Frameworks/OpenCL.framework/Headers/ directory.
- When including in C++ add #include <OpenCL/cl.hpp> instead of #include <OpenCL/opencl.h>
#include <iostream>
#define __NO_STD_VECTOR // Use cl::vector instead of STL version
#define __CL_ENABLE_EXCEPTIONS
#ifdef __APPLE__
//#include <OpenCL/opencl.h>
#include <OpenCL/cl.hpp> /* read cpp_wrapper_fix.txt */
#else
#include <CL/cl.hpp>
#endif
int main(int, char**) {
cl::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
int platform_id = 0;
int device_id = 0;
std::cout << "Number of Platforms: " << platforms.size() << std::endl;
for(cl::vector<cl::Platform>::iterator it = platforms.begin(); it != platforms.end(); ++it){
cl::Platform platform(*it);
std::cout << "Platform ID: " << platform_id++ << std::endl;
std::cout << "Platform Name: " << platform.getInfo<CL_PLATFORM_NAME>() << std::endl;
std::cout << "Platform Vendor: " << platform.getInfo<CL_PLATFORM_VENDOR>() << std::endl;
cl::vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_CPU, &devices);
for(cl::vector<cl::Device>::iterator it2 = devices.begin(); it2 != devices.end(); ++it2){
cl::Device device(*it2);
std::cout << "\tDevice " << device_id++ << ": " << std::endl;
std::cout << "\t\tDevice Name: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;
std::cout << "\t\tDevice Type: " << device.getInfo<CL_DEVICE_TYPE>();
std::cout << " (GPU: " << CL_DEVICE_TYPE_GPU << ", CPU: " << CL_DEVICE_TYPE_CPU << ")" << std::endl;
std::cout << "\t\tDevice Vendor: " << device.getInfo<CL_DEVICE_VENDOR>() << std::endl;
std::cout << "\t\tDevice Max Compute Units: " << device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>() << std::endl;
std::cout << "\t\tDevice Global Memory: " << device.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>() << std::endl;
std::cout << "\t\tDevice Max Clock Frequency: " << device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>() << std::endl;
std::cout << "\t\tDevice Max Allocateable Memory: " << device.getInfo<CL_DEVICE_MAX_MEM_ALLOC_SIZE>() << std::endl;
std::cout << "\t\tDevice Local Memory: " << device.getInfo<CL_DEVICE_LOCAL_MEM_SIZE>() << std::endl;
std::cout << "\t\tDevice Available: " << device.getInfo< CL_DEVICE_AVAILABLE>() << std::endl;
}
std::cout<< std::endl;
}
}
UNAME_S := $(shell uname -s)
# -std=c++11 -Wall -march=native
ifeq ($(UNAME_S),Linux)
CXX=clang++
CPPFLAGS=-O3
LDFLAGS=-O3
LDLIBS=-lOpenCL
endif
ifeq ($(UNAME_S),Darwin)
CXX=clang++
CPPFLAGS=-O3
LDFLAGS=-O3
LDLIBS=-framework OpenCL
endif
RM=rm -f
SRCS=device_query.cpp
OBJS=device_query.o
EXEC=device_query
all: $(OBJS)
$(CXX) $(LDFLAGS) -o $(EXEC) $(OBJS) $(LDLIBS)
%.o: %.cpp
$(CXX) $(CFLAGS) $(CPPFLAGS) -c $<
clean:
$(RM) $(OBJS) $(EXEC)
dist-clean:
$(RM) $(EXEC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment