[BUG] createGLBuffer and copyToGLBuffer do not point to the same device

Open
#242 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp

Research direction

Start with forge/examples/opencl and cl_helpers.h, especially get_devices and the two createCLGLContext overloads. Reproduce the failure on a system with multiple graphics cards, then trace which OpenCL device each OpenGL buffer operation uses. Done means the examples create and copy GL buffers through the same device without relying on selecting the last device.

Written by the indexing model from the issue text.

Description

If you have more than one display cards, all the examples in forge/examples/opencl will fail.

A temporary workaround is to modify createCLGLContext in cl_helpers.h so that cl::Context always use the last device instead of the first device. But it is not a solution at all.

void get_devices(const forge::Window &wnd,
                 std::vector<cl::Platform> &platforms_interop,
                 std::vector<Device> &devices_interop) {
  std::vector<cl::Platform> platforms;
  Platform::get(&platforms);

  for (auto platform : platforms) {
    std::vector<cl::Device> devices;
    try {
      platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
      for (auto device : devices) {
        if (checkGLInterop(platform, device, wnd)) {
          devices_interop.push_back(device);
          platforms_interop.push_back(platform);
        }
      }

    } catch (const cl::Error &err) {
      if (err.err() != CL_DEVICE_NOT_FOUND) {
        std::cout << "Platform: " << platform.getInfo<CL_PLATFORM_NAME>()
                  << " got error = " << err.err() << std::endl;
        throw std::runtime_error("Fatal Error!");
      }
    }
  }
  if (devices_interop.size() == 0)
    throw std::runtime_error("No CL-GL sharing contexts found");
}

cl::Context createCLGLContext(const forge::Window &wnd, cl::Platform platform,
                              cl::Device device) {
  // std::cout << "Platform: " << platform.getInfo<CL_PLATFORM_NAME>()
  //           << std::endl;
  // std::cout << "Device: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;
#if defined(OS_MAC)
  CGLContextObj cgl_current_ctx = CGLGetCurrentContext();
  CGLShareGroupObj cgl_share_group = CGLGetShareGroup(cgl_current_ctx);

  cl_context_properties cps[] = {CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
                                 (cl_context_properties)cgl_share_group, 0};
#elif defined(OS_LNX)
  cl_context_properties cps[] = {CL_GL_CONTEXT_KHR,
                                 (cl_context_properties)wnd.context(),
                                 CL_GLX_DISPLAY_KHR,
                                 (cl_context_properties)wnd.display(),
                                 CL_CONTEXT_PLATFORM,
                                 (cl_context_properties)platform(),
                                 0};
#else /* OS_WIN */
  cl_context_properties cps[] = {CL_GL_CONTEXT_KHR,
                                 (cl_context_properties)wnd.context(),
                                 CL_WGL_HDC_KHR,
                                 (cl_context_properties)wnd.display(),
                                 CL_CONTEXT_PLATFORM,
                                 (cl_context_properties)platform(),
                                 0};
#endif
  return cl::Context(device, cps);
}

cl::Context createCLGLContext(const forge::Window &wnd) {
  std::vector<cl::Platform> platforms;
  std::vector<Device> devices;
  get_devices(wnd, platforms, devices);
  for (size_t i = devices.size() - 1; i >= 0; i--) {
    // for (size_t i = 0; i < devices.size(); i++) {
    try {
      return createCLGLContext(wnd, platforms[i], devices[i]);

    } catch (const cl::Error &err) {
      std::cout << "[createCLGLContext]Error:" << err.err() << std::endl;
    }
  }
  return cl::Context();
}
Dominant language
C++
Stars
238
Forks
46
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from arrayfire/forge

All issues in arrayfire/forge

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.