项目作者: hurou927

项目描述 :
opencl cheat seat
高级语言: C++
项目地址: git://github.com/hurou927/opencl-wrapper.git
创建时间: 2017-08-08T08:44:42Z
项目社区:https://github.com/hurou927/opencl-wrapper

开源协议:

下载


OpenCL Wrapper like CUDA

Usage

include
  1. #include "header/my_clo.hpp"
instatiation
  1. clLikeCUDA clo;///platform_id = 0, device_id = 0
  2. clLikeCUDA clo(deviceNumber);
  3. clLikeCUDA clo(-1);///stdio selection mode
build OpenCL source
  1. cl_kernel kernel1 = clo.clCreateKernelFromFile(fileName, functionName, options);
malloc device memory

cl_mem_flag -> https://www.khronos.org/registry/OpenCL/sdk/1.0/docs/man/xhtml/clCreateBuffer.html

  1. cl_mem *d_a;
  2. clo.clMalloc(&d_a,sizeof(int)*1024*1024,CL_MEM_READ_ONLY);
set arguments
  1. #if __cplusplus >= 201103L //-std=c++11
  2. clo.clSetKernelArgs(kernel1, d_a, d_b);
  3. #else
  4. clSetKernelArg(kernel1,0,sizeof(cl_mem),d_a);
  5. clSetKernelArg(kernel1,1,sizeof(cl_mem),d_b);
  6. #endif
memory copy
  • clMemcpyHostToDevice device -> host
  • clMemcpyDeviceToHost host -> device
  • clMemcpyDeivceToDevice device -> device
  1. clo.clMemcpy(d_a,h_a,sizeof(int)*1024*1024,clMemcpyHostToDevice);
run kernel
  1. cldim3 blocks(64,64);
  2. cldim3 threads(16,16);
  3. cl_int err = clo.runkernel(kernel1,blocks,threads);

Compile

  1. g++ -o clcheat clcheat.cpp -std=c++11 -lOpenCL
  2. clang++ -o clcheat clcheat.cpp -std=c++11 -framework OpenCL

Run

  1. $./clcheat
  2. [No.] platform = "platformName", device = "deviceName"
  3. ----------------------------------------------------------
  4. [ 0] platform = "NVIDIA CUDA", device = "GeForce GTX 750 Ti"
  5. [ 1] platform = "Intel(R) OpenCL", device = "Intel(R) Core(TM) i5-4440 CPU @ 3.10GHz"
  6. ----------------------------------------------------------
  7. input device number : 0
  8. ----------------------------------------------------------
  9. CL_PLATFORM_NAME,NVIDIA CUDA,CL_DEVICE_NAME,GeForce GTX 750 Ti
  10. --------------------------------------
  11. [ Matrix Multiplication(1024 x 1024) ]
  12. host->device, 3.799,ms
  13. kernel , 12.602,ms
  14. device->host, 0.859,ms