stan-dev / stan-dev/math

OpenCL - buffer creation fails with Integrated Graphics

Open
#2,789 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

Description

Attempting to run OpenCL code with Intel Integrated graphics gives the error:

[ RUN      ] ProbDistributionsBernoulliCdf.opencl_matches_cpu_small
exception thrown in signature  const std::tuple<std::vector<int, std::allocator<int> >, Eigen::Matrix<stan::math::var_value<double, void>, -1, 1, 0, -1, 1> >&]:
unknown file: Failure
C++ exception with description "initialize_buffer: clCreateBuffer CL_INVALID_HOST_PTR: Unknown error -37" thrown in the test body.

Which has also been seen in this forum post.

I believe this is due to the use of the CL_MEM_USE_HOST_PTR flag when creating matrix_cl objects:

        buffer_cl_
            = cl::Buffer(ctx, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
                         sizeof(T) * size(), A);  // this is always synchronous

Sharing the memory pointer to the GPU appears to cause an issue with integrated graphics that have shared memory for the CPU and GPU, and the CL_MEM_COPY_HOST_PTR flag should be used for these devices instead (and fixes the issue for me locally).

Given that this incurs a copy-cost, we wouldn't want to use it for all cases, only for integrated graphics. I think a simple approach here would be to add a new make/local flag: INTEGRATED_OPENCL, which would request the use of this CL_MEM_COPY_HOST_PTR flag:

#ifdef INTEGRATED_OPENCL
        buffer_cl_
            = cl::Buffer(ctx, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
                         sizeof(T) * size(), A);  // this is always synchronous
#else
        buffer_cl_
            = cl::Buffer(ctx, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
                         sizeof(T) * size(), A);  // this is always synchronous
#endif

@SteveBronder @rok-cesnovar I'm completely new to the OpenCL space so make sure to let me know if there's a better or more performant option here!

Current Version:

v4.4.0

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.

Research direction

Start with stan/math/opencl/matrix_cl.hpp and reproduce the named ProbDistributionsBernoulliCdf.opencl_matches_cpu_small failure on Intel integrated graphics. Trace the buffer creation flags and verify the chosen behavior passes the OpenCL test without breaking other devices; completion should eliminate CL_INVALID_HOST_PTR for the reported setup.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.