Esempio n. 1
0
File: ocl.cpp Progetto: jcxz/DIP
bool initCLGLContext(boost::compute::context & ctx)
{
  INFOM("Initializing OpenCL/OpenGL shared context");

#if 1
  /* select appropriate device and platform */
  cl_platform_id platform = nullptr;
  cl_device_id device = nullptr;

  if ((!utils::ocl::selectGLDeviceAndPlatform(&device, &platform)) &&
      (!utils::ocl::selectPlatformAndDevice(&device, &platform)))
  {
    ERRORM("Failed to select an appropriate device or platform");
    return false;
  }

  /* setup context */
  cl_context_properties props[] = {
#if defined(FLUIDSIM_OS_MAC)
    CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
    (cl_context_properties) CGLGetShareGroup(CGLGetCurrentContext()),
#elif defined(FLUIDSIM_OS_UNIX)
    CL_GL_CONTEXT_KHR, (cl_context_properties) glXGetCurrentContext(),
    CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(),
    CL_CONTEXT_PLATFORM, (cl_context_properties) platform,
#elif defined(FLUIDSIM_OS_WIN)
    CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(),
    CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(),
    CL_CONTEXT_PLATFORM, (cl_context_properties) platform,
#else
# error "Unsupported OS platform"
#endif
    0
  };

  /* create opencl context */
  cl_int err = CL_SUCCESS;
  cl_context ctx_ = clCreateContext(props, 1, &device, nullptr, nullptr, &err);
  if (err != CL_SUCCESS)
  {
    ERRORM("Failed to create OpenCL context: " << utils::ocl::errorToStr(err));
    return false;
  }

  ctx = boost::compute::context(ctx_, false);
  utils::ocl::printDeviceInfo(ctx.get_devices());
#else
  try
  {
    /* create opencl context */
    ctx = boost::compute::opengl_create_shared_context();
    utils::ocl::printDeviceInfo(ctx.get_devices());
  }
  catch (const boost::compute::opencl_error & e)
  {
    ERRORM(e.what());
    return false;
  }
  catch (const boost::compute::unsupported_extension_error & e)
  {
    ERRORM(e.what());
    return false;
  }
  catch (const std::exception & e)
  {
    ERRORM("An unexpected error occured during opencl context initialization: " << e.what());
    return false;
  }
#endif

  INFOM("Successfully initialized OpenCL context");
  INFOM("Using device   : " << ctx.get_device().name());
  INFOM("Using platform : " << ctx.get_device().platform().name());

  return true;
}
Esempio n. 2
0
boost::compute::program OpenCL::createAndBuildProgram(std::string src, boost::compute::context ctx) {

    LoggerType logger = LoggerType(boost::log::keywords::channel="NLPGraph::Util::OpenCL::createAndBuildProgram");

    boost::compute::program bProgram = boost::compute::program::create_with_source(src, ctx);
    try {
        bProgram.build("-cl-std=CL1.1 -Werror");
    } catch(...) {
        std::string buildLog = bProgram.get_build_info<std::string>(CL_PROGRAM_BUILD_LOG,ctx.get_device());
        LOG(severity_level::critical) << buildLog;
        OpenCLExceptionType except;
        except.msg = buildLog;
        throw except;
    }
    return bProgram;
}