| 2 | #include "ocl_utils.h" |
| 3 | |
| 4 | OclTask::OclTask(cl_device_type dev_type, const char *kern_name, const char *kern_code) |
| 5 | { |
| 6 | // get platforms |
| 7 | cl_uint plat_num; |
| 8 | OCL_ASSERT(clGetPlatformIDs(0, nullptr, &plat_num)); |
| 9 | cl_platform_id *plat_ids = new cl_platform_id[plat_num]; |
| 10 | OCL_ASSERT(clGetPlatformIDs(plat_num, plat_ids, nullptr)); |
| 11 | // print platform info |
| 12 | for (cl_uint i = 0; i < plat_num; ++i) { |
| 13 | char plat_name[128]; |
| 14 | OCL_ASSERT(clGetPlatformInfo(plat_ids[i], CL_PLATFORM_NAME, sizeof(plat_name), plat_name, nullptr)); |
| 15 | XINFO("found platform %d: %s", i, plat_name); |
| 16 | } |
| 17 | // select platform |
| 18 | plat_ = plat_ids[0]; |
| 19 | delete[] plat_ids; |
| 20 | |
| 21 | // get devices |
| 22 | cl_uint dev_num; |
| 23 | OCL_ASSERT(clGetDeviceIDs(plat_, dev_type, 0, nullptr, &dev_num)); |
| 24 | cl_device_id *dev_ids = new cl_device_id[dev_num]; |
| 25 | OCL_ASSERT(clGetDeviceIDs(plat_, dev_type, dev_num, dev_ids, nullptr)); |
| 26 | // print device info |
| 27 | for (cl_uint i = 0; i < dev_num; ++i) { |
| 28 | char dev_name[128]; |
| 29 | OCL_ASSERT(clGetDeviceInfo(dev_ids[i], CL_DEVICE_NAME, sizeof(dev_name), dev_name, nullptr)); |
| 30 | XINFO("found device %d: %s", i, dev_name); |
| 31 | } |
| 32 | // select device |
| 33 | dev_ = dev_ids[0]; |
| 34 | delete[] dev_ids; |
| 35 | |
| 36 | // create context |
| 37 | cl_int errcode; |
| 38 | ctx_ = clCreateContext(nullptr, 1, &dev_, nullptr, nullptr, &errcode); |
| 39 | OCL_ASSERT(errcode); |
| 40 | |
| 41 | // create command queue |
| 42 | cmdq_ = clCreateCommandQueueWithProperties(ctx_, dev_, nullptr, &errcode); |
| 43 | OCL_ASSERT(errcode); |
| 44 | |
| 45 | // create program |
| 46 | prog_ = clCreateProgramWithSource(ctx_, 1, &kern_code, nullptr, &errcode); |
| 47 | OCL_ASSERT(errcode); |
| 48 | OCL_ASSERT(clBuildProgram(prog_, 1, &dev_, nullptr, nullptr, nullptr)); |
| 49 | |
| 50 | // create kernel |
| 51 | kern_ = clCreateKernel(prog_, kern_name, &errcode); |
| 52 | OCL_ASSERT(errcode); |
| 53 | } |
| 54 | |
| 55 | OclTask::~OclTask() |
| 56 | { |
nothing calls this directly
no outgoing calls
no test coverage detected