Last active
September 29, 2019 05:59
-
-
Save twmht/8e514b9dc7b318b07faab387831ded81 to your computer and use it in GitHub Desktop.
opencl example
This file contains hidden or 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 <iostream> | |
#ifdef __APPLE__ | |
#include <OpenCL/cl.hpp> | |
#else | |
#include <CL/cl.hpp> | |
#endif | |
int main() { | |
// get all platforms (drivers), e.g. NVIDIA | |
std::vector<cl::Platform> all_platforms; | |
cl::Platform::get(&all_platforms); | |
std::cout << all_platforms.size() << std::endl; | |
if (all_platforms.size()==0) { | |
std::cout<<" No platforms found. Check OpenCL installation!\n"; | |
exit(1); | |
} | |
cl::Platform default_platform=all_platforms[0]; | |
// std::cout << "Using platform: "<<default_platform.getInfo<CL_PLATFORM_NAME>()<<"\n"; | |
// get default device (CPUs, GPUs) of the default platform | |
std::vector<cl::Device> all_devices; | |
printf("get devices\n"); | |
default_platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices); | |
printf("get devices done %d\n", all_devices.size()); | |
if(all_devices.size()==0){ | |
std::cout<<" No devices found. Check OpenCL installation!\n"; | |
exit(1); | |
} | |
// use device[1] because that's a GPU; device[0] is the CPU | |
cl::Device default_device=all_devices[0]; | |
// std::cout<< "Using device: "<<default_device.getInfo<CL_DEVICE_NAME>()<<"\n"; | |
cl::Context context({default_device}); | |
cl::CommandQueue queue(context,default_device); | |
// a context is like a "runtime link" to the device and platform; | |
// i.e. communication is possible | |
cl::Buffer buffer_A(context,CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR , sizeof(int)*10); | |
cl_int error = CL_SUCCESS; | |
// zerocopy | |
int* bufferPtr = (int*)queue.enqueueMapBuffer(buffer_A, CL_TRUE, CL_MAP_WRITE, 0, sizeof(int)*10, nullptr, nullptr, &error); | |
if (error != CL_SUCCESS) { | |
printf("Error to map buffer in copy buffer, error=%d\n", error); | |
return 0; | |
} | |
int b[10] = {1,2,3,4,5,6,7,8,9,10}; | |
// copy to the mapped buffer | |
if(bufferPtr != nullptr){ | |
memcpy(bufferPtr, b, 10 * sizeof(int)); | |
} | |
int c[10]; | |
// buffer_A: 1,2,3,4,5,6,7,8,9,10 | |
queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, sizeof(int)*10, c); | |
printf("buffer_A:\n"); | |
for (int i = 0; i < 10; i++) { | |
printf("%d\n", c[i]); | |
} | |
int d[10] = {10,2,3,4,15,6,7,8,9,20}; | |
queue.enqueueWriteBuffer(buffer_A, CL_TRUE, 0, sizeof(int)*10, d); | |
// bufferPtr: 10,2,3,4,15,6,7,8,9,20 | |
printf("map buffer content\n"); | |
for (int i = 0; i < 10; i++) { | |
printf("%d\n", bufferPtr[i]); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment