initCL.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef INIT_CL_H
  2. #define INIT_CL_H
  3. void initCL()
  4. {
  5. int preferredDeviceIndex=-1;
  6. int preferredPlatformIndex=-1;
  7. bool allowCpuOpenCL=false;
  8. b3CommandLineArgs args(gArgc,gArgv);
  9. args.GetCmdLineArgument("cl_device", preferredDeviceIndex);
  10. args.GetCmdLineArgument("cl_platform", preferredPlatformIndex);
  11. allowCpuOpenCL = args.CheckCmdLineFlag("allow_opencl_cpu");
  12. void* glCtx=0;
  13. void* glDC = 0;
  14. int ciErrNum = 0;
  15. cl_device_type deviceType = CL_DEVICE_TYPE_GPU;
  16. if (allowCpuOpenCL)
  17. deviceType = CL_DEVICE_TYPE_ALL;
  18. // if (useInterop)
  19. // {
  20. // m_data->m_clContext = b3OpenCLUtils::createContextFromType(deviceType, &ciErrNum, glCtx, glDC);
  21. // } else
  22. {
  23. m_clContext = b3OpenCLUtils::createContextFromType(deviceType, &ciErrNum, 0,0,preferredDeviceIndex, preferredPlatformIndex,&m_platformId);
  24. ASSERT_FALSE(m_clContext==0);
  25. }
  26. b3OpenCLPlatformInfo platformInfo;
  27. b3OpenCLUtils::getPlatformInfo(m_platformId,&platformInfo);
  28. b3Printf("OpenCL Platform Name %s\n", platformInfo.m_platformName);
  29. b3Printf("OpenCL Platform Vendor %s\n", platformInfo.m_platformVendor);
  30. b3Printf("OpenCL Platform Version %s\n", platformInfo.m_platformVersion);
  31. ASSERT_EQ(ciErrNum, CL_SUCCESS);
  32. int numDev = b3OpenCLUtils::getNumDevices(m_clContext);
  33. EXPECT_GT(numDev,0);
  34. if (numDev>0)
  35. {
  36. m_clDevice= b3OpenCLUtils::getDevice(m_clContext,0);
  37. ASSERT_FALSE(m_clDevice==0);
  38. m_clQueue = clCreateCommandQueue(m_clContext, m_clDevice, 0, &ciErrNum);
  39. ASSERT_FALSE(m_clQueue==0);
  40. ASSERT_EQ(ciErrNum, CL_SUCCESS);
  41. b3OpenCLDeviceInfo info;
  42. b3OpenCLUtils::getDeviceInfo(m_clDevice,&info);
  43. b3OpenCLUtils::printDeviceInfo(m_clDevice);
  44. m_clDeviceName = info.m_deviceName;
  45. }
  46. }
  47. void exitCL()
  48. {
  49. clReleaseCommandQueue(m_clQueue);
  50. clReleaseContext(m_clContext);
  51. }
  52. #endif //INIT_CL_H